• Three.js 绘制动态模型


    1.概述

            在 Three.js 的世界中,几何体(Geometry)由顶点(vertices),线,面组成,被用来定义物体的「形状」和「大小」。

    2.BufferGeometry 对象

            顾名思义,BufferGeometry 对象和「缓冲」相关,具体而言,该对象能够将几何体的相关数据(如顶点,UV,法线等)存入 GPU 的缓冲区(即显存),从而极大的提高 GPU 渲染性能与内存使用效率。

    let cubeGeometry = new THREE.BufferGeometry();

    2.材质Material

            材质是什么就不用多说了。这里创建MeshBasicMaterial。

    1. const cubeMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
    2. //纹理在运行时更新
    3. cubeMaterial.needsUpdate = true

    3.绘制mesh

           将BufferGeometry和材质使用mesh绘制。

    1. // 根据几何体和材质创建物体
    2. mesh = new THREE.Mesh(cubeGeometry, cubeMaterial);
    3. //运行时更新
    4. mesh.needsUpdate = true;
    5. // 将几何体添加到场景中
    6. scene.add(mesh);

    4.添加顶点,UV,索引

            从目前来看肯定是看不到模型的。因为缺少了顶点vertices信息。

    1. if (indices instanceof Uint8Array)
    2. {
    3. cubeGeometry.setIndex(new THREE.Uint8BufferAttribute(indices, 1));
    4. }else if (indices instanceof Uint16Array)
    5. {
    6. cubeGeometry.setIndex(new THREE.Uint16BufferAttribute(indices, 1));
    7. }else if (indices instanceof Uint32Array)
    8. {
    9. cubeGeometry.setIndex(new THREE.Uint32BufferAttribute(indices, 1));
    10. }else
    11. {
    12. throw new Error("Unsupported indices size (only 8, 16 and 32 bits are supported)");
    13. }
    14. let attribute = new THREE.Float32BufferAttribute(vertices,3);
    15. attribute.needsUpdate = true;
    16. cubeGeometry.setAttribute("position",attribute);
    17. attribute = new THREE.Float32BufferAttribute(uvs,2);
    18. attribute.needsUpdate = true;
    19. cubeGeometry.setAttribute("uv",attribute);

    如上图所示,分别添加了vertices,uv信息,还有三角形索引indices。而输入的三个vertices,uvs,indices皆为array数组,如:

    1. const uvs = new Float32Array([
    2. 0, 0,
    3. 0.5, 0,
    4. 0.5, 0.5,
    5. 0, 0.5,
    6. ]);

    通过不断改变vertices,uvs,indices的值来动态绘制模型。

  • 相关阅读:
    优思学院|质量4.0与质量管理的未来
    Python开发自定义Web框架
    ISIS—ISIS邻居建立&影响ISIS邻居建立的因素有哪些—03
    mysql不是内部或外部命令,也不是可运行的程序或批处理文件解决
    密码技术 (5) - 数字签名
    JavaScript DOM中获取元素、事件基础、操作元素、节点操作
    【Axure高保真原型】曲线图组和堆叠曲线图
    Flask框架——Sijax
    系统安装技能测试
    基于下垂控制的孤岛双机并联逆变器环流抑制模型(Simulink仿真实现)
  • 原文地址:https://blog.csdn.net/qq_26540577/article/details/132856809