使用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 }
结束。
标签:currSelectObj,glb,THREE,three,Object3D,objects,animations,js,children From: https://www.cnblogs.com/fanjlqinl/p/18051503