three.js 性能优化之模型转化与压缩

模型转换

obj转gltf

  安装插件

1
npm i -g obj2gltf
1
执行转换命令
1
obj2gltf  -i 11-6.obj -o 11-6.gltf -u

 

模型压缩

安装gltf-pipeline

1
npm i -g gltf-pipeline
1
执行压缩命令
1
gltf-pipeline  -i 11-6.gltf -o 11-6_mini.gltf -d

加载压缩后的模型

    import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader'
    import {DRACOLoader} from 'three/examples/jsm/loaders/DRACOLoader'
复制代码
 //加载模型
        let loader = new GLTFLoader();
        let dracoLoader = new DRACOLoader();
        dracoLoader.setDecoderPath("/static/models/draco/");//加载draco算法,这样才能解析压缩后的gltf模型格式,具体的draco算法文件,直接可以three依赖文件中找到,赋值到static目录下面就好了
        loader.setDRACOLoader(dracoLoader);
        loader.load("mini.gltf",//引入压缩的gltf模型
          function(obj) { // 这个obj就是解析后的模型,可添加到scene内。
            obj.scene.position.set(-27.5, 0, -2);//设置物体的位置
            obj.scene.scale.set(0.00065, 0.00065, 0.00065);//设置物体的大小
            that.scene.add(obj.scene);// 在场景中添加物体
          },
          // called while loading is progressing
          function(xhr) {
            console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
          },
          // called when loading has errors
          function(error) {
            console.log("An error happened");
          }
        );
复制代码

 

posted @     阅读(265)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-02-17 WebSocket事件
点击右上角即可分享
微信分享提示