three.js导出场景内模型为glb,gltf

使用three,导出当前场景内指定模型为 glb,或者gltf格式

复制代码
import { GLTFExporter } from 'three/examples/jsm/exporters/GLTFExporter.js'


    //导出模型为GLTF
    public exportGLTFModel() {
        let currSelectObj: THREE.Object3D = SMART.scene.children[2]; // 需要导出的模型。可以自己传入一个
        
        let objects: THREE.Object3D | THREE.Object3D[] = currSelectObj;
        if(currSelectObj.children && currSelectObj.children.length > 0) {
          objects = currSelectObj.children.filter((item) => !this.specialType(item,['GridHelper']));
          if (objects.length === 0) {
            console.log('当前场景中没有任何模型');
            return
          }
        }
        const exporter = new GLTFExporter();
        const animations = this.getAnimations( currSelectObj );
        exporter.parse( objects, ( result )=> {
            console.log('模型转换完毕');
            this.saveString( JSON.stringify( result, null, 2 ), 'smart.gltf' );
        }, function(error) {
            console.log('导出失败:'+ error);
        }, { animations: animations } );
    }
   // 导出模型为glb格式
    public exprotGlbModel(){
        
        let currSelectObj: THREE.Object3D = SMART.scene.children[2];// 需要导出的模型。可以自己传入一个
        
        const exporter = new GLTFExporter();
        const animations = this.getAnimations( currSelectObj );

        let objects: THREE.Object3D | THREE.Object3D[] = currSelectObj;
        if(currSelectObj.children && currSelectObj.children.length > 0) {
            objects = currSelectObj.children.filter((item) => !this.specialType(item,['GridHelper']));
            if (objects.length === 0) {
              console.log('当前场景中没有任何模型');
              return
            }
        }
        exporter.parse( objects, ( result )=> {
            console.log('模型转换完毕');
            this.saveArrayBuffer( result as ArrayBuffer, 'smart.glb' );
        }, function(error) {
            console.log('导出失败:'+ error);
        }, { animations: animations, binary: true } );
        
    }
    //转换为二进制
    public saveArrayBuffer (buffer: BlobPart, filename: string) {
      this.save(new Blob([buffer], { type: 'application/octet-stream' }), filename)
    }

    //转换为字符串
    public saveString (text: BlobPart, filename: string) {
        this.save(new Blob([text], { type: 'text/plain' }), filename)
    }

    //保存文件
    public save(blob: Blob, filename: string) {
      const link = document.createElement('a')
      if (link.href) {
        URL.revokeObjectURL(link.href)
      }
      link.href = URL.createObjectURL(blob)
      link.download = filename || 'data.json'
      link.dispatchEvent(new MouseEvent('click'))
    }
    

    //排除指定的一些类型
    public specialType(obj: THREE.Object3D, otherList: string[] = []) {
        let typeList = [
        'AxesHelper',
        'BoxHelper',
        'TransformControls',
        'VertexNormalsHelper',
        ].concat(otherList)

        return typeList.includes(obj.type)
    }
    //获取动画组
    public getAnimations (scene: THREE.Object3D) {
      const animations: THREE.AnimationClip[] = []
      scene.traverse(function (object) {
        animations.push(...object.animations)
      })
      return animations
    }
复制代码

结束。

posted @   fanjiajia  阅读(538)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示