1.压缩gltf文件

有时我们的gltf文件较大,可以使用gltf-pipeline进行压缩gltf文件

cnpm install gltf-pipeline -g
gltf-pipeline -i model.gltf -j -d // -j:表示为输出为gltf文件 -d:表示压缩
gltf-pipeline -i model.gltf -b -d // -j:表示为输出为glb文件 -d:表示压缩

2.将gltf文件压后,还需要处理加载器

// 第一步
<script src="js/DRACOLoader.js"></script>
// 第二步
const dracoLoader = new THREE.DRACOLoader()
THREE.DRACOLoader.setDecoderPath('three/js/libs/draco/gltf/') // 设置解压库文件路径
GLTFLoader.setDRACOLoader(dracoLoader)

3.three/js/libs/draco/gltf/ 还需要引入对应的js文件

79.png

4.这样就可以使用GLTFLoader加载模型了

wt.loadModel('./model/model-processed.gltf', GLTFLoader).then(res => {
  md = res.scene
  console.log('md', md)
  scene.add(md)
  loading.remove()
})