码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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的值来动态绘制模型。

  • 相关阅读:
    吐血整理,服务端性能测试中间件-项目集成redis实战,一篇打通...
    SpringBoot学习_day7
    用于安装和维护光纤单模和多模的光纤网络测试套件
    自动化测试在 Kubernetes Operator 开发中的应用:以 OpenTelemetry 为例
    User简单描述下微信小程序的相关文件类型?
    Oracle 运维篇+应用容器数据库的install、upgrade、patch、uninstall
    华为机试真题 Java 实现【全量和已占用字符集】
    ORB-SLAM2从理论到代码实现(九):LocalMapping程序详解
    算法每日一题(合并两个有序的数组)
    1.12反向传播误差到更多层
  • 原文地址:https://blog.csdn.net/qq_26540577/article/details/132856809
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号