@2112-lab/central-plant 0.1.4 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/dist/bundle/index.js +33146 -1
  2. package/dist/cjs/_virtual/_rollupPluginBabelHelpers.js +432 -1
  3. package/dist/cjs/node_modules/@2112-lab/pathfinder/dist/index.esm.js +1448 -1
  4. package/dist/cjs/node_modules/three/examples/jsm/controls/OrbitControls.js +1853 -1
  5. package/dist/cjs/node_modules/three/examples/jsm/exporters/GLTFExporter.js +3537 -1
  6. package/dist/cjs/node_modules/three/examples/jsm/exporters/OBJExporter.js +305 -1
  7. package/dist/cjs/node_modules/three/examples/jsm/exporters/PLYExporter.js +542 -1
  8. package/dist/cjs/node_modules/three/examples/jsm/exporters/STLExporter.js +218 -1
  9. package/dist/cjs/node_modules/three/examples/jsm/loaders/DRACOLoader.js +683 -1
  10. package/dist/cjs/node_modules/three/examples/jsm/loaders/GLTFLoader.js +4811 -1
  11. package/dist/cjs/node_modules/three/examples/jsm/loaders/RGBELoader.js +480 -1
  12. package/dist/cjs/node_modules/three/examples/jsm/renderers/CSS2DRenderer.js +309 -1
  13. package/dist/cjs/node_modules/three/examples/jsm/utils/BufferGeometryUtils.js +120 -1
  14. package/dist/cjs/src/analysis/analysis.js +560 -1
  15. package/dist/cjs/src/analysis/testing.js +958 -1
  16. package/dist/cjs/src/core/centralPlant.js +1149 -1
  17. package/dist/cjs/src/core/debugLogger.js +175 -1
  18. package/dist/cjs/src/core/mathUtils.js +574 -1
  19. package/dist/cjs/src/core/nameUtils.js +93 -1
  20. package/dist/cjs/src/data/export.js +716 -1
  21. package/dist/cjs/src/data/import.js +380 -1
  22. package/dist/cjs/src/data/numerics.js +522 -1
  23. package/dist/cjs/src/helpers/sceneHelper.js +572 -1
  24. package/dist/cjs/src/index.js +69 -1
  25. package/dist/cjs/src/managers/components/animationManager.js +123 -1
  26. package/dist/cjs/src/managers/components/componentManager.js +332 -1
  27. package/dist/cjs/src/managers/components/pathfindingManager.js +1441 -1
  28. package/dist/cjs/src/managers/controls/TransformControls.js +1063 -1
  29. package/dist/cjs/src/managers/controls/cameraControlsManager.js +79 -1
  30. package/dist/cjs/src/managers/controls/dragDropManager.js +1026 -1
  31. package/dist/cjs/src/managers/controls/keyboardControlsManager.js +395 -1
  32. package/dist/cjs/src/managers/controls/transformControlsManager.js +1807 -1
  33. package/dist/cjs/src/managers/environment/environmentManager.js +714 -1
  34. package/dist/cjs/src/managers/environment/textureConfig.js +229 -1
  35. package/dist/cjs/src/managers/scene/sceneExportManager.js +264 -1
  36. package/dist/cjs/src/managers/scene/sceneInitializationManager.js +346 -1
  37. package/dist/cjs/src/managers/scene/sceneOperationsManager.js +1509 -1
  38. package/dist/cjs/src/managers/scene/sceneTooltipsManager.js +661 -1
  39. package/dist/cjs/src/managers/system/disposalManager.js +444 -1
  40. package/dist/cjs/src/managers/system/hotReloadManager.js +291 -1
  41. package/dist/cjs/src/managers/system/performanceMonitor.js +863 -1
  42. package/dist/cjs/src/rendering/modelPreloader.js +369 -1
  43. package/dist/cjs/src/rendering/rendering2D.js +631 -1
  44. package/dist/cjs/src/rendering/rendering3D.js +685 -1
  45. package/dist/esm/_virtual/_rollupPluginBabelHelpers.js +396 -1
  46. package/dist/esm/node_modules/@2112-lab/pathfinder/dist/index.esm.js +1444 -1
  47. package/dist/esm/node_modules/three/examples/jsm/controls/OrbitControls.js +1849 -1
  48. package/dist/esm/node_modules/three/examples/jsm/exporters/GLTFExporter.js +3533 -1
  49. package/dist/esm/node_modules/three/examples/jsm/exporters/OBJExporter.js +301 -1
  50. package/dist/esm/node_modules/three/examples/jsm/exporters/PLYExporter.js +538 -1
  51. package/dist/esm/node_modules/three/examples/jsm/exporters/STLExporter.js +214 -1
  52. package/dist/esm/node_modules/three/examples/jsm/loaders/DRACOLoader.js +679 -1
  53. package/dist/esm/node_modules/three/examples/jsm/loaders/GLTFLoader.js +4807 -1
  54. package/dist/esm/node_modules/three/examples/jsm/loaders/RGBELoader.js +476 -1
  55. package/dist/esm/node_modules/three/examples/jsm/renderers/CSS2DRenderer.js +304 -1
  56. package/dist/esm/node_modules/three/examples/jsm/utils/BufferGeometryUtils.js +116 -1
  57. package/dist/esm/src/analysis/analysis.js +536 -1
  58. package/dist/esm/src/analysis/testing.js +954 -1
  59. package/dist/esm/src/core/centralPlant.js +1144 -1
  60. package/dist/esm/src/core/debugLogger.js +167 -1
  61. package/dist/esm/src/core/mathUtils.js +570 -1
  62. package/dist/esm/src/core/nameUtils.js +87 -1
  63. package/dist/esm/src/data/export.js +712 -1
  64. package/dist/esm/src/data/import.js +356 -1
  65. package/dist/esm/src/data/numerics.js +518 -1
  66. package/dist/esm/src/helpers/sceneHelper.js +547 -1
  67. package/dist/esm/src/index.js +35 -1
  68. package/dist/esm/src/managers/components/animationManager.js +119 -1
  69. package/dist/esm/src/managers/components/componentManager.js +328 -1
  70. package/dist/esm/src/managers/components/pathfindingManager.js +1417 -1
  71. package/dist/esm/src/managers/controls/TransformControls.js +1057 -1
  72. package/dist/esm/src/managers/controls/cameraControlsManager.js +75 -1
  73. package/dist/esm/src/managers/controls/dragDropManager.js +1002 -1
  74. package/dist/esm/src/managers/controls/keyboardControlsManager.js +371 -1
  75. package/dist/esm/src/managers/controls/transformControlsManager.js +1782 -1
  76. package/dist/esm/src/managers/environment/environmentManager.js +690 -1
  77. package/dist/esm/src/managers/environment/textureConfig.js +202 -1
  78. package/dist/esm/src/managers/scene/sceneExportManager.js +260 -1
  79. package/dist/esm/src/managers/scene/sceneInitializationManager.js +322 -1
  80. package/dist/esm/src/managers/scene/sceneOperationsManager.js +1485 -1
  81. package/dist/esm/src/managers/scene/sceneTooltipsManager.js +637 -1
  82. package/dist/esm/src/managers/system/disposalManager.js +440 -1
  83. package/dist/esm/src/managers/system/hotReloadManager.js +287 -1
  84. package/dist/esm/src/managers/system/performanceMonitor.js +858 -1
  85. package/dist/esm/src/rendering/modelPreloader.js +364 -1
  86. package/dist/esm/src/rendering/rendering2D.js +627 -1
  87. package/dist/esm/src/rendering/rendering3D.js +661 -1
  88. package/package.json +1 -1
@@ -1 +1,3533 @@
1
- import{Color as t,REVISION as e,Vector3 as n,CompressedTexture as s,Source as i,NoColorSpace as r,MathUtils as o,RGBAFormat as a,ImageUtils as c,DoubleSide as h,BufferAttribute as l,PropertyBinding as u,InterpolateDiscrete as f,Matrix4 as w,Scene as d,Quaternion as y,InterpolateLinear as m,NearestFilter as p,NearestMipmapNearestFilter as A,NearestMipmapLinearFilter as T,LinearFilter as x,LinearMipmapNearestFilter as g,LinearMipmapLinearFilter as b,ClampToEdgeWrapping as E,RepeatWrapping as M,MirroredRepeatWrapping as _,SRGBColorSpace as v}from"three";const C={POSITION:["byte","byte normalized","unsigned byte","unsigned byte normalized","short","short normalized","unsigned short","unsigned short normalized"],NORMAL:["byte normalized","short normalized"],TANGENT:["byte normalized","short normalized"],TEXCOORD:["byte","byte normalized","unsigned byte","short","short normalized","unsigned short"]};class O{constructor(){this.textureUtils=null,this.pluginCallbacks=[],this.register(function(t){return new at(t)}),this.register(function(t){return new ct(t)}),this.register(function(t){return new ft(t)}),this.register(function(t){return new wt(t)}),this.register(function(t){return new dt(t)}),this.register(function(t){return new yt(t)}),this.register(function(t){return new ht(t)}),this.register(function(t){return new lt(t)}),this.register(function(t){return new ut(t)}),this.register(function(t){return new mt(t)}),this.register(function(t){return new pt(t)}),this.register(function(t){return new At(t)}),this.register(function(t){return new Tt(t)}),this.register(function(t){return new xt(t)})}register(t){return-1===this.pluginCallbacks.indexOf(t)&&this.pluginCallbacks.push(t),this}unregister(t){return-1!==this.pluginCallbacks.indexOf(t)&&this.pluginCallbacks.splice(this.pluginCallbacks.indexOf(t),1),this}setTextureUtils(t){return this.textureUtils=t,this}parse(t,e,n,s){const i=new ot,r=[];for(let t=0,e=this.pluginCallbacks.length;t<e;t++)r.push(this.pluginCallbacks[t](i));i.setPlugins(r),i.setTextureUtils(this.textureUtils),i.writeAsync(t,e,s).catch(n)}parseAsync(t,e){const n=this;return new Promise(function(s,i){n.parse(t,s,i,e)})}}const R=0,I=1,N=2,H=3,L=4,S=5120,F=5121,z=5122,U=5123,B=5124,K=5125,k=5126,D=34962,G=34963,V=9728,P=9729,j=9984,X=9985,J=9986,q=9987,W=33071,Q=33648,Y=10497,$="KHR_mesh_quantization",Z={};Z[p]=V,Z[A]=j,Z[T]=J,Z[x]=P,Z[g]=X,Z[b]=q,Z[E]=W,Z[M]=Y,Z[_]=Q;const tt={scale:"scale",position:"translation",quaternion:"rotation",morphTargetInfluences:"weights"},et=new t;function nt(t,e){return t.length===e.length&&t.every(function(t,n){return t===e[n]})}function st(t){return 4*Math.ceil(t/4)}function it(t,e=0){const n=st(t.byteLength);if(n!==t.byteLength){const s=new Uint8Array(n);if(s.set(new Uint8Array(t)),0!==e)for(let i=t.byteLength;i<n;i++)s[i]=e;return s.buffer}return t}function rt(){return"undefined"==typeof document&&"undefined"!=typeof OffscreenCanvas?new OffscreenCanvas(1,1):document.createElement("canvas")}class ot{constructor(){this.plugins=[],this.options={},this.pending=[],this.buffers=[],this.byteOffset=0,this.buffers=[],this.nodeMap=new Map,this.skins=[],this.extensionsUsed={},this.extensionsRequired={},this.uids=new Map,this.uid=0,this.json={asset:{version:"2.0",generator:"THREE.GLTFExporter r"+e}},this.cache={meshes:new Map,attributes:new Map,attributesNormalized:new Map,materials:new Map,textures:new Map,images:new Map},this.textureUtils=null}setPlugins(t){this.plugins=t}setTextureUtils(t){this.textureUtils=t}async writeAsync(t,e,n={}){this.options=Object.assign({binary:!1,trs:!1,onlyVisible:!0,maxTextureSize:1/0,animations:[],includeCustomExtensions:!1},n),this.options.animations.length>0&&(this.options.trs=!0),await this.processInputAsync(t),await Promise.all(this.pending);const s=this,i=s.buffers,r=s.json;n=s.options;const o=s.extensionsUsed,a=s.extensionsRequired,c=new Blob(i,{type:"application/octet-stream"}),h=Object.keys(o),l=Object.keys(a);if(h.length>0&&(r.extensionsUsed=h),l.length>0&&(r.extensionsRequired=l),r.buffers&&r.buffers.length>0&&(r.buffers[0].byteLength=c.size),!0===n.binary){const t=new FileReader;t.readAsArrayBuffer(c),t.onloadend=function(){const n=it(t.result),s=new DataView(new ArrayBuffer(8));s.setUint32(0,n.byteLength,!0),s.setUint32(4,5130562,!0);const i=it((o=JSON.stringify(r),(new TextEncoder).encode(o).buffer),32);var o;const a=new DataView(new ArrayBuffer(8));a.setUint32(0,i.byteLength,!0),a.setUint32(4,1313821514,!0);const c=new ArrayBuffer(12),h=new DataView(c);h.setUint32(0,1179937895,!0),h.setUint32(4,2,!0);const l=12+a.byteLength+i.byteLength+s.byteLength+n.byteLength;h.setUint32(8,l,!0);const u=new Blob([c,a,i,s,n],{type:"application/octet-stream"}),f=new FileReader;f.readAsArrayBuffer(u),f.onloadend=function(){e(f.result)}}}else if(r.buffers&&r.buffers.length>0){const t=new FileReader;t.readAsDataURL(c),t.onloadend=function(){const n=t.result;r.buffers[0].uri=n,e(r)}}else e(r)}serializeUserData(t,e){if(0===Object.keys(t.userData).length)return;const n=this.options,s=this.extensionsUsed;try{const i=JSON.parse(JSON.stringify(t.userData));if(n.includeCustomExtensions&&i.gltfExtensions){void 0===e.extensions&&(e.extensions={});for(const t in i.gltfExtensions)e.extensions[t]=i.gltfExtensions[t],s[t]=!0;delete i.gltfExtensions}Object.keys(i).length>0&&(e.extras=i)}catch(t){}}getUID(t,e=!1){if(!1===this.uids.has(t)){const e=new Map;e.set(!0,this.uid++),e.set(!1,this.uid++),this.uids.set(t,e)}return this.uids.get(t).get(e)}isNormalizedNormalAttribute(t){if(this.cache.attributesNormalized.has(t))return!1;const e=new n;for(let n=0,s=t.count;n<s;n++)if(Math.abs(e.fromBufferAttribute(t,n).length()-1)>5e-4)return!1;return!0}createNormalizedNormalAttribute(t){const e=this.cache;if(e.attributesNormalized.has(t))return e.attributesNormalized.get(t);const s=t.clone(),i=new n;for(let t=0,e=s.count;t<e;t++)i.fromBufferAttribute(s,t),0===i.x&&0===i.y&&0===i.z?i.setX(1):i.normalize(),s.setXYZ(t,i.x,i.y,i.z);return e.attributesNormalized.set(t,s),s}applyTextureTransform(t,e){let n=!1;const s={};0===e.offset.x&&0===e.offset.y||(s.offset=e.offset.toArray(),n=!0),0!==e.rotation&&(s.rotation=e.rotation,n=!0),1===e.repeat.x&&1===e.repeat.y||(s.scale=e.repeat.toArray(),n=!0),n&&(t.extensions=t.extensions||{},t.extensions.KHR_texture_transform=s,this.extensionsUsed.KHR_texture_transform=!0)}async buildMetalRoughTextureAsync(t,e){if(t===e)return t;function n(t){return t.colorSpace===v?function(t){return t<.04045?.0773993808*t:Math.pow(.9478672986*t+.0521327014,2.4)}:function(t){return t}}t instanceof s&&(t=await this.decompressTextureAsync(t)),e instanceof s&&(e=await this.decompressTextureAsync(e));const o=t?t.image:null,a=e?e.image:null,c=Math.max(o?o.width:0,a?a.width:0),h=Math.max(o?o.height:0,a?a.height:0),l=rt();l.width=c,l.height=h;const u=l.getContext("2d",{willReadFrequently:!0});u.fillStyle="#00ffff",u.fillRect(0,0,c,h);const f=u.getImageData(0,0,c,h);if(o){u.drawImage(o,0,0,c,h);const e=n(t),s=u.getImageData(0,0,c,h).data;for(let t=2;t<s.length;t+=4)f.data[t]=256*e(s[t]/256)}if(a){u.drawImage(a,0,0,c,h);const t=n(e),s=u.getImageData(0,0,c,h).data;for(let e=1;e<s.length;e+=4)f.data[e]=256*t(s[e]/256)}u.putImageData(f,0,0);const w=(t||e).clone();return w.source=new i(l),w.colorSpace=r,w.channel=(t||e).channel,t&&e&&(t.channel,e.channel),w}async decompressTextureAsync(t,e=1/0){if(null===this.textureUtils)throw new Error("THREE.GLTFExporter: setTextureUtils() must be called to process compressed textures.");return await this.textureUtils.decompress(t,e)}processBuffer(t){const e=this.json,n=this.buffers;return e.buffers||(e.buffers=[{byteLength:0}]),n.push(t),0}processBufferView(t,e,n,s,i){const r=this.json;let a;switch(r.bufferViews||(r.bufferViews=[]),e){case S:case F:a=1;break;case z:case U:a=2;break;default:a=4}let c=t.itemSize*a;i===D&&(c=4*Math.ceil(c/4));const h=st(s*c),l=new DataView(new ArrayBuffer(h));let u=0;for(let i=n;i<n+s;i++){for(let n=0;n<t.itemSize;n++){let s;t.itemSize>4?s=t.array[i*t.itemSize+n]:(0===n?s=t.getX(i):1===n?s=t.getY(i):2===n?s=t.getZ(i):3===n&&(s=t.getW(i)),!0===t.normalized&&(s=o.normalize(s,t.array))),e===k?l.setFloat32(u,s,!0):e===B?l.setInt32(u,s,!0):e===K?l.setUint32(u,s,!0):e===z?l.setInt16(u,s,!0):e===U?l.setUint16(u,s,!0):e===S?l.setInt8(u,s):e===F&&l.setUint8(u,s),u+=a}u%c!==0&&(u+=c-u%c)}const f={buffer:this.processBuffer(l.buffer),byteOffset:this.byteOffset,byteLength:h};void 0!==i&&(f.target=i),i===D&&(f.byteStride=c),this.byteOffset+=h,r.bufferViews.push(f);return{id:r.bufferViews.length-1,byteLength:0}}processBufferViewImage(t){const e=this,n=e.json;return n.bufferViews||(n.bufferViews=[]),new Promise(function(s){const i=new FileReader;i.readAsArrayBuffer(t),i.onloadend=function(){const t=it(i.result),r={buffer:e.processBuffer(t),byteOffset:e.byteOffset,byteLength:t.byteLength};e.byteOffset+=t.byteLength,s(n.bufferViews.push(r)-1)}})}processAccessor(t,e,n,s){const i=this.json;let r;if(t.array.constructor===Float32Array)r=k;else if(t.array.constructor===Int32Array)r=B;else if(t.array.constructor===Uint32Array)r=K;else if(t.array.constructor===Int16Array)r=z;else if(t.array.constructor===Uint16Array)r=U;else if(t.array.constructor===Int8Array)r=S;else{if(t.array.constructor!==Uint8Array)throw new Error("THREE.GLTFExporter: Unsupported bufferAttribute component type: "+t.array.constructor.name);r=F}if(void 0===n&&(n=0),void 0!==s&&s!==1/0||(s=t.count),0===s)return null;const a=function(t,e,n){const s={min:new Array(t.itemSize).fill(Number.POSITIVE_INFINITY),max:new Array(t.itemSize).fill(Number.NEGATIVE_INFINITY)};for(let i=e;i<e+n;i++)for(let e=0;e<t.itemSize;e++){let n;t.itemSize>4?n=t.array[i*t.itemSize+e]:(0===e?n=t.getX(i):1===e?n=t.getY(i):2===e?n=t.getZ(i):3===e&&(n=t.getW(i)),!0===t.normalized&&(n=o.normalize(n,t.array))),s.min[e]=Math.min(s.min[e],n),s.max[e]=Math.max(s.max[e],n)}return s}(t,n,s);let c;void 0!==e&&(c=t===e.index?G:D);const h=this.processBufferView(t,r,n,s,c),l={bufferView:h.id,byteOffset:h.byteOffset,componentType:r,count:s,max:a.max,min:a.min,type:{1:"SCALAR",2:"VEC2",3:"VEC3",4:"VEC4",9:"MAT3",16:"MAT4"}[t.itemSize]};return!0===t.normalized&&(l.normalized=!0),i.accessors||(i.accessors=[]),i.accessors.push(l)-1}processImage(t,e,n,s="image/png"){if(null!==t){const e=this,i=e.cache,r=e.json,o=e.options,a=e.pending;i.images.has(t)||i.images.set(t,{});const h=i.images.get(t),l=s+":flipY/"+n.toString();if(void 0!==h[l])return h[l];r.images||(r.images=[]);const u={mimeType:s},f=rt();f.width=Math.min(t.width,o.maxTextureSize),f.height=Math.min(t.height,o.maxTextureSize);const w=f.getContext("2d",{willReadFrequently:!0});if(!0===n&&(w.translate(0,f.height),w.scale(1,-1)),void 0!==t.data){t.width>o.maxTextureSize||(t.height,o.maxTextureSize);const e=new Uint8ClampedArray(t.height*t.width*4);for(let n=0;n<e.length;n+=4)e[n+0]=t.data[n+0],e[n+1]=t.data[n+1],e[n+2]=t.data[n+2],e[n+3]=t.data[n+3];w.putImageData(new ImageData(e,t.width,t.height),0,0)}else{if(!("undefined"!=typeof HTMLImageElement&&t instanceof HTMLImageElement||"undefined"!=typeof HTMLCanvasElement&&t instanceof HTMLCanvasElement||"undefined"!=typeof ImageBitmap&&t instanceof ImageBitmap||"undefined"!=typeof OffscreenCanvas&&t instanceof OffscreenCanvas))throw new Error("THREE.GLTFExporter: Invalid image type. Use HTMLImageElement, HTMLCanvasElement, ImageBitmap or OffscreenCanvas.");w.drawImage(t,0,0,f.width,f.height)}!0===o.binary?a.push(function(t,e){if(void 0!==t.toBlob)return new Promise(n=>t.toBlob(n,e));let n;return"image/jpeg"===e?n=.92:"image/webp"===e&&(n=.8),t.convertToBlob({type:e,quality:n})}(f,s).then(t=>e.processBufferViewImage(t)).then(t=>{u.bufferView=t})):u.uri=c.getDataURL(f,s);const d=r.images.push(u)-1;return h[l]=d,d}throw new Error("THREE.GLTFExporter: No valid image data found. Unable to process texture.")}processSampler(t){const e=this.json;e.samplers||(e.samplers=[]);const n={magFilter:Z[t.magFilter],minFilter:Z[t.minFilter],wrapS:Z[t.wrapS],wrapT:Z[t.wrapT]};return e.samplers.push(n)-1}async processTextureAsync(t){const e=this.options,n=this.cache,i=this.json;if(n.textures.has(t))return n.textures.get(t);i.textures||(i.textures=[]),t instanceof s&&(t=await this.decompressTextureAsync(t,e.maxTextureSize));let r=t.userData.mimeType;"image/webp"===r&&(r="image/png");const o={sampler:this.processSampler(t),source:this.processImage(t.image,t.format,t.flipY,r)};t.name&&(o.name=t.name),await this.t(async function(e){e.writeTexture&&await e.writeTexture(t,o)});const a=i.textures.push(o)-1;return n.textures.set(t,a),a}async processMaterialAsync(t){const e=this.cache,n=this.json;if(e.materials.has(t))return e.materials.get(t);if(t.isShaderMaterial)return null;n.materials||(n.materials=[]);const s={pbrMetallicRoughness:{}};!0!==t.isMeshStandardMaterial&&t.isMeshBasicMaterial;const i=t.color.toArray().concat([t.opacity]);if(nt(i,[1,1,1,1])||(s.pbrMetallicRoughness.baseColorFactor=i),t.isMeshStandardMaterial?(s.pbrMetallicRoughness.metallicFactor=t.metalness,s.pbrMetallicRoughness.roughnessFactor=t.roughness):(s.pbrMetallicRoughness.metallicFactor=0,s.pbrMetallicRoughness.roughnessFactor=1),t.metalnessMap||t.roughnessMap){const e=await this.buildMetalRoughTextureAsync(t.metalnessMap,t.roughnessMap),n={index:await this.processTextureAsync(e),texCoord:e.channel};this.applyTextureTransform(n,e),s.pbrMetallicRoughness.metallicRoughnessTexture=n}if(t.map){const e={index:await this.processTextureAsync(t.map),texCoord:t.map.channel};this.applyTextureTransform(e,t.map),s.pbrMetallicRoughness.baseColorTexture=e}if(t.emissive){const e=t.emissive;if(Math.max(e.r,e.g,e.b)>0&&(s.emissiveFactor=t.emissive.toArray()),t.emissiveMap){const e={index:await this.processTextureAsync(t.emissiveMap),texCoord:t.emissiveMap.channel};this.applyTextureTransform(e,t.emissiveMap),s.emissiveTexture=e}}if(t.normalMap){const e={index:await this.processTextureAsync(t.normalMap),texCoord:t.normalMap.channel};t.normalScale&&1!==t.normalScale.x&&(e.scale=t.normalScale.x),this.applyTextureTransform(e,t.normalMap),s.normalTexture=e}if(t.aoMap){const e={index:await this.processTextureAsync(t.aoMap),texCoord:t.aoMap.channel};1!==t.aoMapIntensity&&(e.strength=t.aoMapIntensity),this.applyTextureTransform(e,t.aoMap),s.occlusionTexture=e}t.transparent?s.alphaMode="BLEND":t.alphaTest>0&&(s.alphaMode="MASK",s.alphaCutoff=t.alphaTest),t.side===h&&(s.doubleSided=!0),""!==t.name&&(s.name=t.name),this.serializeUserData(t,s),await this.t(async function(e){e.writeMaterialAsync&&await e.writeMaterialAsync(t,s)});const r=n.materials.push(s)-1;return e.materials.set(t,r),r}async processMeshAsync(t){const e=this.cache,n=this.json,s=[t.geometry.uuid];if(Array.isArray(t.material))for(let e=0,n=t.material.length;e<n;e++)s.push(t.material[e].uuid);else s.push(t.material.uuid);const i=s.join(":");if(e.meshes.has(i))return e.meshes.get(i);const r=t.geometry;let o;o=t.isLineSegments?I:t.isLineLoop?N:t.isLine?H:t.isPoints?R:t.material.wireframe?I:L;const a={},c={},h=[],u=[],f={uv:"TEXCOORD_0",uv1:"TEXCOORD_1",uv2:"TEXCOORD_2",uv3:"TEXCOORD_3",color:"COLOR_0",skinWeight:"WEIGHTS_0",skinIndex:"JOINTS_0"},w=r.getAttribute("normal");void 0===w||this.isNormalizedNormalAttribute(w)||r.setAttribute("normal",this.createNormalizedNormalAttribute(w));let d=null;for(let t in r.attributes){if("morph"===t.slice(0,5))continue;const n=r.attributes[t];t=f[t]||t.toUpperCase();if(/^(POSITION|NORMAL|TANGENT|TEXCOORD_\d+|COLOR_\d+|JOINTS_\d+|WEIGHTS_\d+)$/.test(t)||(t="_"+t),e.attributes.has(this.getUID(n))){c[t]=e.attributes.get(this.getUID(n));continue}d=null;const s=n.array;"JOINTS_0"!==t||s instanceof Uint16Array||s instanceof Uint8Array?(s instanceof Uint32Array||s instanceof Int32Array)&&!t.startsWith("_")&&(d=O.Utils.toFloat32BufferAttribute(n)):d=new l(new Uint16Array(s),n.itemSize,n.normalized);const i=this.processAccessor(d||n,r);null!==i&&(t.startsWith("_")||this.detectMeshQuantization(t,n),c[t]=i,e.attributes.set(this.getUID(n),i))}if(void 0!==w&&r.setAttribute("normal",w),0===Object.keys(c).length)return null;if(void 0!==t.morphTargetInfluences&&t.morphTargetInfluences.length>0){const n=[],s=[],i={};if(void 0!==t.morphTargetDictionary)for(const e in t.morphTargetDictionary)i[t.morphTargetDictionary[e]]=e;for(let o=0;o<t.morphTargetInfluences.length;++o){const a={};let c=!1;for(const t in r.morphAttributes){if("position"!==t&&"normal"!==t){c||(c=!0);continue}const n=r.morphAttributes[t][o],s=t.toUpperCase(),i=r.attributes[t];if(e.attributes.has(this.getUID(n,!0))){a[s]=e.attributes.get(this.getUID(n,!0));continue}const h=n.clone();if(!r.morphTargetsRelative)for(let t=0,e=n.count;t<e;t++)for(let e=0;e<n.itemSize;e++)0===e&&h.setX(t,n.getX(t)-i.getX(t)),1===e&&h.setY(t,n.getY(t)-i.getY(t)),2===e&&h.setZ(t,n.getZ(t)-i.getZ(t)),3===e&&h.setW(t,n.getW(t)-i.getW(t));a[s]=this.processAccessor(h,r),e.attributes.set(this.getUID(i,!0),a[s])}u.push(a),n.push(t.morphTargetInfluences[o]),void 0!==t.morphTargetDictionary&&s.push(i[o])}a.weights=n,s.length>0&&(a.extras={},a.extras.targetNames=s)}const y=Array.isArray(t.material);if(y&&0===r.groups.length)return null;let m=!1;if(y&&null===r.index){const t=[];for(let e=0,n=r.attributes.position.count;e<n;e++)t[e]=e;r.setIndex(t),m=!0}const p=y?t.material:[t.material],A=y?r.groups:[{materialIndex:0,start:void 0,count:void 0}];for(let t=0,n=A.length;t<n;t++){const n={mode:o,attributes:c};if(this.serializeUserData(r,n),u.length>0&&(n.targets=u),null!==r.index){let s=this.getUID(r.index);void 0===A[t].start&&void 0===A[t].count||(s+=":"+A[t].start+":"+A[t].count),e.attributes.has(s)?n.indices=e.attributes.get(s):(n.indices=this.processAccessor(r.index,r,A[t].start,A[t].count),e.attributes.set(s,n.indices)),null===n.indices&&delete n.indices}const s=await this.processMaterialAsync(p[A[t].materialIndex]);null!==s&&(n.material=s),h.push(n)}!0===m&&r.setIndex(null),a.primitives=h,n.meshes||(n.meshes=[]),await this.t(function(e){e.writeMesh&&e.writeMesh(t,a)});const T=n.meshes.push(a)-1;return e.meshes.set(i,T),T}detectMeshQuantization(t,e){if(this.extensionsUsed[$])return;let n;switch(e.array.constructor){case Int8Array:n="byte";break;case Uint8Array:n="unsigned byte";break;case Int16Array:n="short";break;case Uint16Array:n="unsigned short";break;default:return}e.normalized&&(n+=" normalized");const s=t.split("_",1)[0];C[s]&&C[s].includes(n)&&(this.extensionsUsed[$]=!0,this.extensionsRequired[$]=!0)}processCamera(t){const e=this.json;e.cameras||(e.cameras=[]);const n=t.isOrthographicCamera,s={type:n?"orthographic":"perspective"};return n?s.orthographic={xmag:2*t.right,ymag:2*t.top,zfar:t.far<=0?.001:t.far,znear:t.near<0?0:t.near}:s.perspective={aspectRatio:t.aspect,yfov:o.degToRad(t.fov),zfar:t.far<=0?.001:t.far,znear:t.near<0?0:t.near},""!==t.name&&(s.name=t.type),e.cameras.push(s)-1}processAnimation(t,e){const n=this.json,s=this.nodeMap;n.animations||(n.animations=[]);const i=(t=O.Utils.mergeMorphTargetTracks(t.clone(),e)).tracks,r=[],o=[];for(let t=0;t<i.length;++t){const n=i[t],a=u.parseTrackName(n.name);let c=u.findNode(e,a.nodeName);const h=tt[a.propertyName];if("bones"===a.objectName&&(c=!0===c.isSkinnedMesh?c.skeleton.getBoneByName(a.objectIndex):void 0),!c||!h)continue;const w=1;let d,y=n.values.length/n.times.length;h===tt.morphTargetInfluences&&(y/=c.morphTargetInfluences.length),!0===n.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline?(d="CUBICSPLINE",y/=3):d=n.getInterpolation()===f?"STEP":"LINEAR",o.push({input:this.processAccessor(new l(n.times,w)),output:this.processAccessor(new l(n.values,y)),interpolation:d}),r.push({sampler:o.length-1,target:{node:s.get(c),path:h}})}return n.animations.push({name:t.name||"clip_"+n.animations.length,samplers:o,channels:r}),n.animations.length-1}processSkin(t){const e=this.json,n=this.nodeMap,s=e.nodes[n.get(t)],i=t.skeleton;if(void 0===i)return null;const r=t.skeleton.bones[0];if(void 0===r)return null;const o=[],a=new Float32Array(16*i.bones.length),c=new w;for(let e=0;e<i.bones.length;++e)o.push(n.get(i.bones[e])),c.copy(i.boneInverses[e]),c.multiply(t.bindMatrix).toArray(a,16*e);void 0===e.skins&&(e.skins=[]),e.skins.push({inverseBindMatrices:this.processAccessor(new l(a,16)),joints:o,skeleton:n.get(r)});return s.skin=e.skins.length-1}async processNodeAsync(t){const e=this.json,n=this.options,s=this.nodeMap;e.nodes||(e.nodes=[]);const i={};if(n.trs){const e=t.quaternion.toArray(),n=t.position.toArray(),s=t.scale.toArray();nt(e,[0,0,0,1])||(i.rotation=e),nt(n,[0,0,0])||(i.translation=n),nt(s,[1,1,1])||(i.scale=s)}else t.matrixAutoUpdate&&t.updateMatrix(),!1===nt(t.matrix.elements,[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1])&&(i.matrix=t.matrix.elements);if(""!==t.name&&(i.name=String(t.name)),this.serializeUserData(t,i),t.isMesh||t.isLine||t.isPoints){const e=await this.processMeshAsync(t);null!==e&&(i.mesh=e)}else t.isCamera&&(i.camera=this.processCamera(t));t.isSkinnedMesh&&this.skins.push(t);const r=e.nodes.push(i)-1;if(s.set(t,r),t.children.length>0){const e=[];for(let s=0,i=t.children.length;s<i;s++){const i=t.children[s];if(i.visible||!1===n.onlyVisible){const t=await this.processNodeAsync(i);null!==t&&e.push(t)}}e.length>0&&(i.children=e)}return await this.t(function(e){e.writeNode&&e.writeNode(t,i)}),r}async processSceneAsync(t){const e=this.json,n=this.options;e.scenes||(e.scenes=[],e.scene=0);const s={};""!==t.name&&(s.name=t.name),e.scenes.push(s);const i=[];for(let e=0,s=t.children.length;e<s;e++){const s=t.children[e];if(s.visible||!1===n.onlyVisible){const t=await this.processNodeAsync(s);null!==t&&i.push(t)}}i.length>0&&(s.nodes=i),this.serializeUserData(t,s)}async processObjectsAsync(t){const e=new d;e.name="AuxScene";for(let n=0;n<t.length;n++)e.children.push(t[n]);await this.processSceneAsync(e)}async processInputAsync(t){const e=this.options;t=t instanceof Array?t:[t],await this.t(function(e){e.beforeParse&&e.beforeParse(t)});const n=[];for(let e=0;e<t.length;e++)t[e]instanceof d?await this.processSceneAsync(t[e]):n.push(t[e]);n.length>0&&await this.processObjectsAsync(n);for(let t=0;t<this.skins.length;++t)this.processSkin(this.skins[t]);for(let n=0;n<e.animations.length;++n)this.processAnimation(e.animations[n],t[0]);await this.t(function(e){e.afterParse&&e.afterParse(t)})}async t(t){for(let e=0,n=this.plugins.length;e<n;e++)await t(this.plugins[e])}}class at{constructor(t){this.writer=t,this.name="KHR_lights_punctual"}writeNode(t,e){if(!t.isLight)return;if(!t.isDirectionalLight&&!t.isPointLight&&!t.isSpotLight)return;const n=this.writer,s=n.json,i=n.extensionsUsed,r={};t.name&&(r.name=t.name),r.color=t.color.toArray(),r.intensity=t.intensity,t.isDirectionalLight?r.type="directional":t.isPointLight?(r.type="point",t.distance>0&&(r.range=t.distance)):t.isSpotLight&&(r.type="spot",t.distance>0&&(r.range=t.distance),r.spot={},r.spot.innerConeAngle=(1-t.penumbra)*t.angle,r.spot.outerConeAngle=t.angle),void 0!==t.decay&&t.decay,t.target&&(t.target.parent!==t||0!==t.target.position.x||0!==t.target.position.y||t.target.position.z),i[this.name]||(s.extensions=s.extensions||{},s.extensions[this.name]={lights:[]},i[this.name]=!0);const o=s.extensions[this.name].lights;o.push(r),e.extensions=e.extensions||{},e.extensions[this.name]={light:o.length-1}}}class ct{constructor(t){this.writer=t,this.name="KHR_materials_unlit"}async writeMaterialAsync(t,e){if(!t.isMeshBasicMaterial)return;const n=this.writer.extensionsUsed;e.extensions=e.extensions||{},e.extensions[this.name]={},n[this.name]=!0,e.pbrMetallicRoughness.metallicFactor=0,e.pbrMetallicRoughness.roughnessFactor=.9}}class ht{constructor(t){this.writer=t,this.name="KHR_materials_clearcoat"}async writeMaterialAsync(t,e){if(!t.isMeshPhysicalMaterial||0===t.clearcoat)return;const n=this.writer,s=n.extensionsUsed,i={};if(i.clearcoatFactor=t.clearcoat,t.clearcoatMap){const e={index:await n.processTextureAsync(t.clearcoatMap),texCoord:t.clearcoatMap.channel};n.applyTextureTransform(e,t.clearcoatMap),i.clearcoatTexture=e}if(i.clearcoatRoughnessFactor=t.clearcoatRoughness,t.clearcoatRoughnessMap){const e={index:await n.processTextureAsync(t.clearcoatRoughnessMap),texCoord:t.clearcoatRoughnessMap.channel};n.applyTextureTransform(e,t.clearcoatRoughnessMap),i.clearcoatRoughnessTexture=e}if(t.clearcoatNormalMap){const e={index:await n.processTextureAsync(t.clearcoatNormalMap),texCoord:t.clearcoatNormalMap.channel};1!==t.clearcoatNormalScale.x&&(e.scale=t.clearcoatNormalScale.x),n.applyTextureTransform(e,t.clearcoatNormalMap),i.clearcoatNormalTexture=e}e.extensions=e.extensions||{},e.extensions[this.name]=i,s[this.name]=!0}}class lt{constructor(t){this.writer=t,this.name="KHR_materials_dispersion"}async writeMaterialAsync(t,e){if(!t.isMeshPhysicalMaterial||0===t.dispersion)return;const n=this.writer.extensionsUsed,s={};s.dispersion=t.dispersion,e.extensions=e.extensions||{},e.extensions[this.name]=s,n[this.name]=!0}}class ut{constructor(t){this.writer=t,this.name="KHR_materials_iridescence"}async writeMaterialAsync(t,e){if(!t.isMeshPhysicalMaterial||0===t.iridescence)return;const n=this.writer,s=n.extensionsUsed,i={};if(i.iridescenceFactor=t.iridescence,t.iridescenceMap){const e={index:await n.processTextureAsync(t.iridescenceMap),texCoord:t.iridescenceMap.channel};n.applyTextureTransform(e,t.iridescenceMap),i.iridescenceTexture=e}if(i.iridescenceIor=t.iridescenceIOR,i.iridescenceThicknessMinimum=t.iridescenceThicknessRange[0],i.iridescenceThicknessMaximum=t.iridescenceThicknessRange[1],t.iridescenceThicknessMap){const e={index:await n.processTextureAsync(t.iridescenceThicknessMap),texCoord:t.iridescenceThicknessMap.channel};n.applyTextureTransform(e,t.iridescenceThicknessMap),i.iridescenceThicknessTexture=e}e.extensions=e.extensions||{},e.extensions[this.name]=i,s[this.name]=!0}}class ft{constructor(t){this.writer=t,this.name="KHR_materials_transmission"}async writeMaterialAsync(t,e){if(!t.isMeshPhysicalMaterial||0===t.transmission)return;const n=this.writer,s=n.extensionsUsed,i={};if(i.transmissionFactor=t.transmission,t.transmissionMap){const e={index:await n.processTextureAsync(t.transmissionMap),texCoord:t.transmissionMap.channel};n.applyTextureTransform(e,t.transmissionMap),i.transmissionTexture=e}e.extensions=e.extensions||{},e.extensions[this.name]=i,s[this.name]=!0}}class wt{constructor(t){this.writer=t,this.name="KHR_materials_volume"}async writeMaterialAsync(t,e){if(!t.isMeshPhysicalMaterial||0===t.transmission)return;const n=this.writer,s=n.extensionsUsed,i={};if(i.thicknessFactor=t.thickness,t.thicknessMap){const e={index:await n.processTextureAsync(t.thicknessMap),texCoord:t.thicknessMap.channel};n.applyTextureTransform(e,t.thicknessMap),i.thicknessTexture=e}t.attenuationDistance!==1/0&&(i.attenuationDistance=t.attenuationDistance),i.attenuationColor=t.attenuationColor.toArray(),e.extensions=e.extensions||{},e.extensions[this.name]=i,s[this.name]=!0}}class dt{constructor(t){this.writer=t,this.name="KHR_materials_ior"}async writeMaterialAsync(t,e){if(!t.isMeshPhysicalMaterial||1.5===t.ior)return;const n=this.writer.extensionsUsed,s={};s.ior=t.ior,e.extensions=e.extensions||{},e.extensions[this.name]=s,n[this.name]=!0}}class yt{constructor(t){this.writer=t,this.name="KHR_materials_specular"}async writeMaterialAsync(t,e){if(!t.isMeshPhysicalMaterial||1===t.specularIntensity&&t.specularColor.equals(et)&&!t.specularIntensityMap&&!t.specularColorMap)return;const n=this.writer,s=n.extensionsUsed,i={};if(t.specularIntensityMap){const e={index:await n.processTextureAsync(t.specularIntensityMap),texCoord:t.specularIntensityMap.channel};n.applyTextureTransform(e,t.specularIntensityMap),i.specularTexture=e}if(t.specularColorMap){const e={index:await n.processTextureAsync(t.specularColorMap),texCoord:t.specularColorMap.channel};n.applyTextureTransform(e,t.specularColorMap),i.specularColorTexture=e}i.specularFactor=t.specularIntensity,i.specularColorFactor=t.specularColor.toArray(),e.extensions=e.extensions||{},e.extensions[this.name]=i,s[this.name]=!0}}class mt{constructor(t){this.writer=t,this.name="KHR_materials_sheen"}async writeMaterialAsync(t,e){if(!t.isMeshPhysicalMaterial||0==t.sheen)return;const n=this.writer,s=n.extensionsUsed,i={};if(t.sheenRoughnessMap){const e={index:await n.processTextureAsync(t.sheenRoughnessMap),texCoord:t.sheenRoughnessMap.channel};n.applyTextureTransform(e,t.sheenRoughnessMap),i.sheenRoughnessTexture=e}if(t.sheenColorMap){const e={index:await n.processTextureAsync(t.sheenColorMap),texCoord:t.sheenColorMap.channel};n.applyTextureTransform(e,t.sheenColorMap),i.sheenColorTexture=e}i.sheenRoughnessFactor=t.sheenRoughness,i.sheenColorFactor=t.sheenColor.toArray(),e.extensions=e.extensions||{},e.extensions[this.name]=i,s[this.name]=!0}}class pt{constructor(t){this.writer=t,this.name="KHR_materials_anisotropy"}async writeMaterialAsync(t,e){if(!t.isMeshPhysicalMaterial||0==t.anisotropy)return;const n=this.writer,s=n.extensionsUsed,i={};if(t.anisotropyMap){const e={index:await n.processTextureAsync(t.anisotropyMap)};n.applyTextureTransform(e,t.anisotropyMap),i.anisotropyTexture=e}i.anisotropyStrength=t.anisotropy,i.anisotropyRotation=t.anisotropyRotation,e.extensions=e.extensions||{},e.extensions[this.name]=i,s[this.name]=!0}}class At{constructor(t){this.writer=t,this.name="KHR_materials_emissive_strength"}async writeMaterialAsync(t,e){if(!t.isMeshStandardMaterial||1===t.emissiveIntensity)return;const n=this.writer.extensionsUsed,s={};s.emissiveStrength=t.emissiveIntensity,e.extensions=e.extensions||{},e.extensions[this.name]=s,n[this.name]=!0}}class Tt{constructor(t){this.writer=t,this.name="EXT_materials_bump"}async writeMaterialAsync(t,e){if(!t.isMeshStandardMaterial||1===t.bumpScale&&!t.bumpMap)return;const n=this.writer,s=n.extensionsUsed,i={};if(t.bumpMap){const e={index:await n.processTextureAsync(t.bumpMap),texCoord:t.bumpMap.channel};n.applyTextureTransform(e,t.bumpMap),i.bumpTexture=e}i.bumpFactor=t.bumpScale,e.extensions=e.extensions||{},e.extensions[this.name]=i,s[this.name]=!0}}class xt{constructor(t){this.writer=t,this.name="EXT_mesh_gpu_instancing"}writeNode(t,e){if(!t.isInstancedMesh)return;const s=this.writer,i=t,r=new Float32Array(3*i.count),o=new Float32Array(4*i.count),a=new Float32Array(3*i.count),c=new w,h=new n,u=new y,f=new n;for(let t=0;t<i.count;t++)i.getMatrixAt(t,c),c.decompose(h,u,f),h.toArray(r,3*t),u.toArray(o,4*t),f.toArray(a,3*t);const d={TRANSLATION:s.processAccessor(new l(r,3)),ROTATION:s.processAccessor(new l(o,4)),SCALE:s.processAccessor(new l(a,3))};i.instanceColor&&(d.i=s.processAccessor(i.instanceColor)),e.extensions=e.extensions||{},e.extensions[this.name]={attributes:d},s.extensionsUsed[this.name]=!0,s.extensionsRequired[this.name]=!0}}O.Utils={insertKeyframe:function(t,e){const n=.001,s=t.getValueSize(),i=new t.TimeBufferType(t.times.length+1),r=new t.ValueBufferType(t.values.length+s),o=t.createInterpolant(new t.ValueBufferType(s));let a;if(0===t.times.length){i[0]=e;for(let t=0;t<s;t++)r[t]=0;a=0}else if(e<t.times[0]){if(Math.abs(t.times[0]-e)<n)return 0;i[0]=e,i.set(t.times,1),r.set(o.evaluate(e),0),r.set(t.values,s),a=0}else if(e>t.times[t.times.length-1]){if(Math.abs(t.times[t.times.length-1]-e)<n)return t.times.length-1;i[i.length-1]=e,i.set(t.times,0),r.set(t.values,0),r.set(o.evaluate(e),t.values.length),a=i.length-1}else for(let c=0;c<t.times.length;c++){if(Math.abs(t.times[c]-e)<n)return c;if(t.times[c]<e&&t.times[c+1]>e){i.set(t.times.slice(0,c+1),0),i[c+1]=e,i.set(t.times.slice(c+1),c+2),r.set(t.values.slice(0,(c+1)*s),0),r.set(o.evaluate(e),(c+1)*s),r.set(t.values.slice((c+1)*s),(c+2)*s),a=c+1;break}}return t.times=i,t.values=r,a},mergeMorphTargetTracks:function(t,e){const n=[],s={},i=t.tracks;for(let t=0;t<i.length;++t){let r=i[t];const o=u.parseTrackName(r.name),a=u.findNode(e,o.nodeName);if("morphTargetInfluences"!==o.propertyName||void 0===o.propertyIndex){n.push(r);continue}if(r.createInterpolant!==r.InterpolantFactoryMethodDiscrete&&r.createInterpolant!==r.InterpolantFactoryMethodLinear){if(r.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline)throw new Error("THREE.GLTFExporter: Cannot merge tracks with glTF CUBICSPLINE interpolation.");r=r.clone(),r.setInterpolation(m)}const c=a.morphTargetInfluences.length,h=a.morphTargetDictionary[o.propertyIndex];if(void 0===h)throw new Error("THREE.GLTFExporter: Morph target name not found: "+o.propertyIndex);let l;if(void 0===s[a.uuid]){l=r.clone();const t=new l.ValueBufferType(c*l.times.length);for(let e=0;e<l.times.length;e++)t[e*c+h]=l.values[e];l.name=(o.nodeName||"")+".morphTargetInfluences",l.values=t,s[a.uuid]=l,n.push(l);continue}const f=r.createInterpolant(new r.ValueBufferType(1));l=s[a.uuid];for(let t=0;t<l.times.length;t++)l.values[t*c+h]=f.evaluate(l.times[t]);for(let t=0;t<r.times.length;t++){const e=this.insertKeyframe(l,r.times[t]);l.values[e*c+h]=r.values[t]}}return t.tracks=n,t},toFloat32BufferAttribute:function(t){const e=new l(new Float32Array(t.count*t.itemSize),t.itemSize,!1);if(!t.normalized&&!t.isInterleavedBufferAttribute)return e.array.set(t.array),e;for(let n=0,s=t.count;n<s;n++)for(let s=0;s<t.itemSize;s++)e.setComponent(n,s,t.getComponent(n,s));return e}};export{O as GLTFExporter};
1
+ import { Color, REVISION, Vector3, CompressedTexture, Source, NoColorSpace, MathUtils, RGBAFormat, ImageUtils, DoubleSide, BufferAttribute, PropertyBinding, InterpolateDiscrete, Matrix4, Scene, Quaternion, InterpolateLinear, NearestFilter, NearestMipmapNearestFilter, NearestMipmapLinearFilter, LinearFilter, LinearMipmapNearestFilter, LinearMipmapLinearFilter, ClampToEdgeWrapping, RepeatWrapping, MirroredRepeatWrapping, SRGBColorSpace } from 'three';
2
+
3
+ /**
4
+ * The KHR_mesh_quantization extension allows these extra attribute component types
5
+ *
6
+ * @see https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_mesh_quantization/README.md#extending-mesh-attributes
7
+ */
8
+ const KHR_mesh_quantization_ExtraAttrTypes = {
9
+ POSITION: [
10
+ 'byte',
11
+ 'byte normalized',
12
+ 'unsigned byte',
13
+ 'unsigned byte normalized',
14
+ 'short',
15
+ 'short normalized',
16
+ 'unsigned short',
17
+ 'unsigned short normalized',
18
+ ],
19
+ NORMAL: [
20
+ 'byte normalized',
21
+ 'short normalized',
22
+ ],
23
+ TANGENT: [
24
+ 'byte normalized',
25
+ 'short normalized',
26
+ ],
27
+ TEXCOORD: [
28
+ 'byte',
29
+ 'byte normalized',
30
+ 'unsigned byte',
31
+ 'short',
32
+ 'short normalized',
33
+ 'unsigned short',
34
+ ],
35
+ };
36
+
37
+ /**
38
+ * An exporter for `glTF` 2.0.
39
+ *
40
+ * glTF (GL Transmission Format) is an [open format specification]{@link https://github.com/KhronosGroup/glTF/tree/master/specification/2.0}
41
+ * for efficient delivery and loading of 3D content. Assets may be provided either in JSON (.gltf)
42
+ * or binary (.glb) format. External files store textures (.jpg, .png) and additional binary
43
+ * data (.bin). A glTF asset may deliver one or more scenes, including meshes, materials,
44
+ * textures, skins, skeletons, morph targets, animations, lights, and/or cameras.
45
+ *
46
+ * GLTFExporter supports the [glTF 2.0 extensions]{@link https://github.com/KhronosGroup/glTF/tree/master/extensions/}:
47
+ *
48
+ * - KHR_lights_punctual
49
+ * - KHR_materials_clearcoat
50
+ * - KHR_materials_dispersion
51
+ * - KHR_materials_emissive_strength
52
+ * - KHR_materials_ior
53
+ * - KHR_materials_iridescence
54
+ * - KHR_materials_specular
55
+ * - KHR_materials_sheen
56
+ * - KHR_materials_transmission
57
+ * - KHR_materials_unlit
58
+ * - KHR_materials_volume
59
+ * - KHR_mesh_quantization
60
+ * - KHR_texture_transform
61
+ * - EXT_materials_bump
62
+ * - EXT_mesh_gpu_instancing
63
+ *
64
+ * The following glTF 2.0 extension is supported by an external user plugin:
65
+ *
66
+ * - [KHR_materials_variants]{@link https://github.com/takahirox/three-gltf-extensions}
67
+ *
68
+ * ```js
69
+ * const exporter = new GLTFExporter();
70
+ * const data = await exporter.parseAsync( scene, options );
71
+ * ```
72
+ *
73
+ * @three_import import { GLTFExporter } from 'three/addons/exporters/GLTFExporter.js';
74
+ */
75
+ class GLTFExporter {
76
+
77
+ /**
78
+ * Constructs a new glTF exporter.
79
+ */
80
+ constructor() {
81
+
82
+ /**
83
+ * A reference to a texture utils module.
84
+ *
85
+ * @type {?(WebGLTextureUtils|WebGPUTextureUtils)}
86
+ * @default null
87
+ */
88
+ this.textureUtils = null;
89
+
90
+ this.pluginCallbacks = [];
91
+
92
+ this.register( function ( writer ) {
93
+
94
+ return new GLTFLightExtension( writer );
95
+
96
+ } );
97
+
98
+ this.register( function ( writer ) {
99
+
100
+ return new GLTFMaterialsUnlitExtension( writer );
101
+
102
+ } );
103
+
104
+ this.register( function ( writer ) {
105
+
106
+ return new GLTFMaterialsTransmissionExtension( writer );
107
+
108
+ } );
109
+
110
+ this.register( function ( writer ) {
111
+
112
+ return new GLTFMaterialsVolumeExtension( writer );
113
+
114
+ } );
115
+
116
+ this.register( function ( writer ) {
117
+
118
+ return new GLTFMaterialsIorExtension( writer );
119
+
120
+ } );
121
+
122
+ this.register( function ( writer ) {
123
+
124
+ return new GLTFMaterialsSpecularExtension( writer );
125
+
126
+ } );
127
+
128
+ this.register( function ( writer ) {
129
+
130
+ return new GLTFMaterialsClearcoatExtension( writer );
131
+
132
+ } );
133
+
134
+ this.register( function ( writer ) {
135
+
136
+ return new GLTFMaterialsDispersionExtension( writer );
137
+
138
+ } );
139
+
140
+ this.register( function ( writer ) {
141
+
142
+ return new GLTFMaterialsIridescenceExtension( writer );
143
+
144
+ } );
145
+
146
+ this.register( function ( writer ) {
147
+
148
+ return new GLTFMaterialsSheenExtension( writer );
149
+
150
+ } );
151
+
152
+ this.register( function ( writer ) {
153
+
154
+ return new GLTFMaterialsAnisotropyExtension( writer );
155
+
156
+ } );
157
+
158
+ this.register( function ( writer ) {
159
+
160
+ return new GLTFMaterialsEmissiveStrengthExtension( writer );
161
+
162
+ } );
163
+
164
+ this.register( function ( writer ) {
165
+
166
+ return new GLTFMaterialsBumpExtension( writer );
167
+
168
+ } );
169
+
170
+ this.register( function ( writer ) {
171
+
172
+ return new GLTFMeshGpuInstancing( writer );
173
+
174
+ } );
175
+
176
+ }
177
+
178
+ /**
179
+ * Registers a plugin callback. This API is internally used to implement the various
180
+ * glTF extensions but can also used by third-party code to add additional logic
181
+ * to the exporter.
182
+ *
183
+ * @param {function(writer:GLTFWriter)} callback - The callback function to register.
184
+ * @return {GLTFExporter} A reference to this exporter.
185
+ */
186
+ register( callback ) {
187
+
188
+ if ( this.pluginCallbacks.indexOf( callback ) === - 1 ) {
189
+
190
+ this.pluginCallbacks.push( callback );
191
+
192
+ }
193
+
194
+ return this;
195
+
196
+ }
197
+
198
+ /**
199
+ * Unregisters a plugin callback.
200
+ *
201
+ * @param {Function} callback - The callback function to unregister.
202
+ * @return {GLTFExporter} A reference to this exporter.
203
+ */
204
+ unregister( callback ) {
205
+
206
+ if ( this.pluginCallbacks.indexOf( callback ) !== - 1 ) {
207
+
208
+ this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 );
209
+
210
+ }
211
+
212
+ return this;
213
+
214
+ }
215
+
216
+ /**
217
+ * Sets the texture utils for this exporter. Only relevant when compressed textures have to be exported.
218
+ *
219
+ * Depending on whether you use {@link WebGLRenderer} or {@link WebGPURenderer}, you must inject the
220
+ * corresponding texture utils {@link WebGLTextureUtils} or {@link WebGPUTextureUtils}.
221
+ *
222
+ * @param {WebGLTextureUtils|WebGPUTextureUtils} utils - The texture utils.
223
+ * @return {GLTFExporter} A reference to this exporter.
224
+ */
225
+ setTextureUtils( utils ) {
226
+
227
+ this.textureUtils = utils;
228
+
229
+ return this;
230
+
231
+ }
232
+
233
+ /**
234
+ * Parses the given scenes and generates the glTF output.
235
+ *
236
+ * @param {Scene|Array<Scene>} input - A scene or an array of scenes.
237
+ * @param {GLTFExporter~OnDone} onDone - A callback function that is executed when the export has finished.
238
+ * @param {GLTFExporter~OnError} onError - A callback function that is executed when an error happens.
239
+ * @param {GLTFExporter~Options} options - options
240
+ */
241
+ parse( input, onDone, onError, options ) {
242
+
243
+ const writer = new GLTFWriter();
244
+ const plugins = [];
245
+
246
+ for ( let i = 0, il = this.pluginCallbacks.length; i < il; i ++ ) {
247
+
248
+ plugins.push( this.pluginCallbacks[ i ]( writer ) );
249
+
250
+ }
251
+
252
+ writer.setPlugins( plugins );
253
+ writer.setTextureUtils( this.textureUtils );
254
+ writer.writeAsync( input, onDone, options ).catch( onError );
255
+
256
+ }
257
+
258
+ /**
259
+ * Async version of {@link GLTFExporter#parse}.
260
+ *
261
+ * @param {Scene|Array<Scene>} input - A scene or an array of scenes.
262
+ * @param {GLTFExporter~Options} options - options.
263
+ * @return {Promise<ArrayBuffer|string>} A Promise that resolved with the exported glTF data.
264
+ */
265
+ parseAsync( input, options ) {
266
+
267
+ const scope = this;
268
+
269
+ return new Promise( function ( resolve, reject ) {
270
+
271
+ scope.parse( input, resolve, reject, options );
272
+
273
+ } );
274
+
275
+ }
276
+
277
+ }
278
+
279
+ //------------------------------------------------------------------------------
280
+ // Constants
281
+ //------------------------------------------------------------------------------
282
+
283
+ const WEBGL_CONSTANTS = {
284
+ POINTS: 0x0000,
285
+ LINES: 0x0001,
286
+ LINE_LOOP: 0x0002,
287
+ LINE_STRIP: 0x0003,
288
+ TRIANGLES: 0x0004,
289
+ TRIANGLE_STRIP: 0x0005,
290
+ TRIANGLE_FAN: 0x0006,
291
+
292
+ BYTE: 0x1400,
293
+ UNSIGNED_BYTE: 0x1401,
294
+ SHORT: 0x1402,
295
+ UNSIGNED_SHORT: 0x1403,
296
+ INT: 0x1404,
297
+ UNSIGNED_INT: 0x1405,
298
+ FLOAT: 0x1406,
299
+
300
+ ARRAY_BUFFER: 0x8892,
301
+ ELEMENT_ARRAY_BUFFER: 0x8893,
302
+
303
+ NEAREST: 0x2600,
304
+ LINEAR: 0x2601,
305
+ NEAREST_MIPMAP_NEAREST: 0x2700,
306
+ LINEAR_MIPMAP_NEAREST: 0x2701,
307
+ NEAREST_MIPMAP_LINEAR: 0x2702,
308
+ LINEAR_MIPMAP_LINEAR: 0x2703,
309
+
310
+ CLAMP_TO_EDGE: 33071,
311
+ MIRRORED_REPEAT: 33648,
312
+ REPEAT: 10497
313
+ };
314
+
315
+ const KHR_MESH_QUANTIZATION = 'KHR_mesh_quantization';
316
+
317
+ const THREE_TO_WEBGL = {};
318
+
319
+ THREE_TO_WEBGL[ NearestFilter ] = WEBGL_CONSTANTS.NEAREST;
320
+ THREE_TO_WEBGL[ NearestMipmapNearestFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_NEAREST;
321
+ THREE_TO_WEBGL[ NearestMipmapLinearFilter ] = WEBGL_CONSTANTS.NEAREST_MIPMAP_LINEAR;
322
+ THREE_TO_WEBGL[ LinearFilter ] = WEBGL_CONSTANTS.LINEAR;
323
+ THREE_TO_WEBGL[ LinearMipmapNearestFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_NEAREST;
324
+ THREE_TO_WEBGL[ LinearMipmapLinearFilter ] = WEBGL_CONSTANTS.LINEAR_MIPMAP_LINEAR;
325
+
326
+ THREE_TO_WEBGL[ ClampToEdgeWrapping ] = WEBGL_CONSTANTS.CLAMP_TO_EDGE;
327
+ THREE_TO_WEBGL[ RepeatWrapping ] = WEBGL_CONSTANTS.REPEAT;
328
+ THREE_TO_WEBGL[ MirroredRepeatWrapping ] = WEBGL_CONSTANTS.MIRRORED_REPEAT;
329
+
330
+ const PATH_PROPERTIES = {
331
+ scale: 'scale',
332
+ position: 'translation',
333
+ quaternion: 'rotation',
334
+ morphTargetInfluences: 'weights'
335
+ };
336
+
337
+ const DEFAULT_SPECULAR_COLOR = new Color();
338
+
339
+ // GLB constants
340
+ // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
341
+
342
+ const GLB_HEADER_BYTES = 12;
343
+ const GLB_HEADER_MAGIC = 0x46546C67;
344
+ const GLB_VERSION = 2;
345
+
346
+ const GLB_CHUNK_PREFIX_BYTES = 8;
347
+ const GLB_CHUNK_TYPE_JSON = 0x4E4F534A;
348
+ const GLB_CHUNK_TYPE_BIN = 0x004E4942;
349
+
350
+ //------------------------------------------------------------------------------
351
+ // Utility functions
352
+ //------------------------------------------------------------------------------
353
+
354
+ /**
355
+ * Compare two arrays
356
+ *
357
+ * @private
358
+ * @param {Array} array1 Array 1 to compare
359
+ * @param {Array} array2 Array 2 to compare
360
+ * @return {boolean} Returns true if both arrays are equal
361
+ */
362
+ function equalArray( array1, array2 ) {
363
+
364
+ return ( array1.length === array2.length ) && array1.every( function ( element, index ) {
365
+
366
+ return element === array2[ index ];
367
+
368
+ } );
369
+
370
+ }
371
+
372
+ /**
373
+ * Converts a string to an ArrayBuffer.
374
+ *
375
+ * @private
376
+ * @param {string} text
377
+ * @return {ArrayBuffer}
378
+ */
379
+ function stringToArrayBuffer( text ) {
380
+
381
+ return new TextEncoder().encode( text ).buffer;
382
+
383
+ }
384
+
385
+ /**
386
+ * Is identity matrix
387
+ *
388
+ * @private
389
+ * @param {Matrix4} matrix
390
+ * @returns {boolean} Returns true, if parameter is identity matrix
391
+ */
392
+ function isIdentityMatrix( matrix ) {
393
+
394
+ return equalArray( matrix.elements, [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] );
395
+
396
+ }
397
+
398
+ /**
399
+ * Get the min and max vectors from the given attribute
400
+ *
401
+ * @private
402
+ * @param {BufferAttribute} attribute Attribute to find the min/max in range from start to start + count
403
+ * @param {number} start Start index
404
+ * @param {number} count Range to cover
405
+ * @return {Object} Object containing the `min` and `max` values (As an array of attribute.itemSize components)
406
+ */
407
+ function getMinMax( attribute, start, count ) {
408
+
409
+ const output = {
410
+
411
+ min: new Array( attribute.itemSize ).fill( Number.POSITIVE_INFINITY ),
412
+ max: new Array( attribute.itemSize ).fill( Number.NEGATIVE_INFINITY )
413
+
414
+ };
415
+
416
+ for ( let i = start; i < start + count; i ++ ) {
417
+
418
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
419
+
420
+ let value;
421
+
422
+ if ( attribute.itemSize > 4 ) {
423
+
424
+ // no support for interleaved data for itemSize > 4
425
+
426
+ value = attribute.array[ i * attribute.itemSize + a ];
427
+
428
+ } else {
429
+
430
+ if ( a === 0 ) value = attribute.getX( i );
431
+ else if ( a === 1 ) value = attribute.getY( i );
432
+ else if ( a === 2 ) value = attribute.getZ( i );
433
+ else if ( a === 3 ) value = attribute.getW( i );
434
+
435
+ if ( attribute.normalized === true ) {
436
+
437
+ value = MathUtils.normalize( value, attribute.array );
438
+
439
+ }
440
+
441
+ }
442
+
443
+ output.min[ a ] = Math.min( output.min[ a ], value );
444
+ output.max[ a ] = Math.max( output.max[ a ], value );
445
+
446
+ }
447
+
448
+ }
449
+
450
+ return output;
451
+
452
+ }
453
+
454
+ /**
455
+ * Get the required size + padding for a buffer, rounded to the next 4-byte boundary.
456
+ * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#data-alignment
457
+ *
458
+ * @private
459
+ * @param {number} bufferSize The size the original buffer. Should be an integer.
460
+ * @returns {number} new buffer size with required padding as an integer.
461
+ *
462
+ */
463
+ function getPaddedBufferSize( bufferSize ) {
464
+
465
+ return Math.ceil( bufferSize / 4 ) * 4;
466
+
467
+ }
468
+
469
+ /**
470
+ * Returns a buffer aligned to 4-byte boundary.
471
+ *
472
+ * @private
473
+ * @param {ArrayBuffer} arrayBuffer Buffer to pad
474
+ * @param {number} [paddingByte=0] Should be an integer
475
+ * @returns {ArrayBuffer} The same buffer if it's already aligned to 4-byte boundary or a new buffer
476
+ */
477
+ function getPaddedArrayBuffer( arrayBuffer, paddingByte = 0 ) {
478
+
479
+ const paddedLength = getPaddedBufferSize( arrayBuffer.byteLength );
480
+
481
+ if ( paddedLength !== arrayBuffer.byteLength ) {
482
+
483
+ const array = new Uint8Array( paddedLength );
484
+ array.set( new Uint8Array( arrayBuffer ) );
485
+
486
+ if ( paddingByte !== 0 ) {
487
+
488
+ for ( let i = arrayBuffer.byteLength; i < paddedLength; i ++ ) {
489
+
490
+ array[ i ] = paddingByte;
491
+
492
+ }
493
+
494
+ }
495
+
496
+ return array.buffer;
497
+
498
+ }
499
+
500
+ return arrayBuffer;
501
+
502
+ }
503
+
504
+ function getCanvas() {
505
+
506
+ if ( typeof document === 'undefined' && typeof OffscreenCanvas !== 'undefined' ) {
507
+
508
+ return new OffscreenCanvas( 1, 1 );
509
+
510
+ }
511
+
512
+ return document.createElement( 'canvas' );
513
+
514
+ }
515
+
516
+ function getToBlobPromise( canvas, mimeType ) {
517
+
518
+ if ( canvas.toBlob !== undefined ) {
519
+
520
+ return new Promise( ( resolve ) => canvas.toBlob( resolve, mimeType ) );
521
+
522
+ }
523
+
524
+ let quality;
525
+
526
+ // Blink's implementation of convertToBlob seems to default to a quality level of 100%
527
+ // Use the Blink default quality levels of toBlob instead so that file sizes are comparable.
528
+ if ( mimeType === 'image/jpeg' ) {
529
+
530
+ quality = 0.92;
531
+
532
+ } else if ( mimeType === 'image/webp' ) {
533
+
534
+ quality = 0.8;
535
+
536
+ }
537
+
538
+ return canvas.convertToBlob( {
539
+
540
+ type: mimeType,
541
+ quality: quality
542
+
543
+ } );
544
+
545
+ }
546
+
547
+ /**
548
+ * Writer
549
+ *
550
+ * @private
551
+ */
552
+ class GLTFWriter {
553
+
554
+ constructor() {
555
+
556
+ this.plugins = [];
557
+
558
+ this.options = {};
559
+ this.pending = [];
560
+ this.buffers = [];
561
+
562
+ this.byteOffset = 0;
563
+ this.buffers = [];
564
+ this.nodeMap = new Map();
565
+ this.skins = [];
566
+
567
+ this.extensionsUsed = {};
568
+ this.extensionsRequired = {};
569
+
570
+ this.uids = new Map();
571
+ this.uid = 0;
572
+
573
+ this.json = {
574
+ asset: {
575
+ version: '2.0',
576
+ generator: 'THREE.GLTFExporter r' + REVISION
577
+ }
578
+ };
579
+
580
+ this.cache = {
581
+ meshes: new Map(),
582
+ attributes: new Map(),
583
+ attributesNormalized: new Map(),
584
+ materials: new Map(),
585
+ textures: new Map(),
586
+ images: new Map()
587
+ };
588
+
589
+ this.textureUtils = null;
590
+
591
+ }
592
+
593
+ setPlugins( plugins ) {
594
+
595
+ this.plugins = plugins;
596
+
597
+ }
598
+
599
+ setTextureUtils( utils ) {
600
+
601
+ this.textureUtils = utils;
602
+
603
+ }
604
+
605
+ /**
606
+ * Parse scenes and generate GLTF output
607
+ *
608
+ * @param {Scene|Array<Scene>} input Scene or Array of THREE.Scenes
609
+ * @param {Function} onDone Callback on completed
610
+ * @param {Object} options options
611
+ */
612
+ async writeAsync( input, onDone, options = {} ) {
613
+
614
+ this.options = Object.assign( {
615
+ // default options
616
+ binary: false,
617
+ trs: false,
618
+ onlyVisible: true,
619
+ maxTextureSize: Infinity,
620
+ animations: [],
621
+ includeCustomExtensions: false
622
+ }, options );
623
+
624
+ if ( this.options.animations.length > 0 ) {
625
+
626
+ // Only TRS properties, and not matrices, may be targeted by animation.
627
+ this.options.trs = true;
628
+
629
+ }
630
+
631
+ await this.processInputAsync( input );
632
+
633
+ await Promise.all( this.pending );
634
+
635
+ const writer = this;
636
+ const buffers = writer.buffers;
637
+ const json = writer.json;
638
+ options = writer.options;
639
+
640
+ const extensionsUsed = writer.extensionsUsed;
641
+ const extensionsRequired = writer.extensionsRequired;
642
+
643
+ // Merge buffers.
644
+ const blob = new Blob( buffers, { type: 'application/octet-stream' } );
645
+
646
+ // Declare extensions.
647
+ const extensionsUsedList = Object.keys( extensionsUsed );
648
+ const extensionsRequiredList = Object.keys( extensionsRequired );
649
+
650
+ if ( extensionsUsedList.length > 0 ) json.extensionsUsed = extensionsUsedList;
651
+ if ( extensionsRequiredList.length > 0 ) json.extensionsRequired = extensionsRequiredList;
652
+
653
+ // Update bytelength of the single buffer.
654
+ if ( json.buffers && json.buffers.length > 0 ) json.buffers[ 0 ].byteLength = blob.size;
655
+
656
+ if ( options.binary === true ) {
657
+
658
+ // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#glb-file-format-specification
659
+
660
+ const reader = new FileReader();
661
+ reader.readAsArrayBuffer( blob );
662
+ reader.onloadend = function () {
663
+
664
+ // Binary chunk.
665
+ const binaryChunk = getPaddedArrayBuffer( reader.result );
666
+ const binaryChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
667
+ binaryChunkPrefix.setUint32( 0, binaryChunk.byteLength, true );
668
+ binaryChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_BIN, true );
669
+
670
+ // JSON chunk.
671
+ const jsonChunk = getPaddedArrayBuffer( stringToArrayBuffer( JSON.stringify( json ) ), 0x20 );
672
+ const jsonChunkPrefix = new DataView( new ArrayBuffer( GLB_CHUNK_PREFIX_BYTES ) );
673
+ jsonChunkPrefix.setUint32( 0, jsonChunk.byteLength, true );
674
+ jsonChunkPrefix.setUint32( 4, GLB_CHUNK_TYPE_JSON, true );
675
+
676
+ // GLB header.
677
+ const header = new ArrayBuffer( GLB_HEADER_BYTES );
678
+ const headerView = new DataView( header );
679
+ headerView.setUint32( 0, GLB_HEADER_MAGIC, true );
680
+ headerView.setUint32( 4, GLB_VERSION, true );
681
+ const totalByteLength = GLB_HEADER_BYTES
682
+ + jsonChunkPrefix.byteLength + jsonChunk.byteLength
683
+ + binaryChunkPrefix.byteLength + binaryChunk.byteLength;
684
+ headerView.setUint32( 8, totalByteLength, true );
685
+
686
+ const glbBlob = new Blob( [
687
+ header,
688
+ jsonChunkPrefix,
689
+ jsonChunk,
690
+ binaryChunkPrefix,
691
+ binaryChunk
692
+ ], { type: 'application/octet-stream' } );
693
+
694
+ const glbReader = new FileReader();
695
+ glbReader.readAsArrayBuffer( glbBlob );
696
+ glbReader.onloadend = function () {
697
+
698
+ onDone( glbReader.result );
699
+
700
+ };
701
+
702
+ };
703
+
704
+ } else {
705
+
706
+ if ( json.buffers && json.buffers.length > 0 ) {
707
+
708
+ const reader = new FileReader();
709
+ reader.readAsDataURL( blob );
710
+ reader.onloadend = function () {
711
+
712
+ const base64data = reader.result;
713
+ json.buffers[ 0 ].uri = base64data;
714
+ onDone( json );
715
+
716
+ };
717
+
718
+ } else {
719
+
720
+ onDone( json );
721
+
722
+ }
723
+
724
+ }
725
+
726
+
727
+ }
728
+
729
+ /**
730
+ * Serializes a userData.
731
+ *
732
+ * @param {THREE.Object3D|THREE.Material} object
733
+ * @param {Object} objectDef
734
+ */
735
+ serializeUserData( object, objectDef ) {
736
+
737
+ if ( Object.keys( object.userData ).length === 0 ) return;
738
+
739
+ const options = this.options;
740
+ const extensionsUsed = this.extensionsUsed;
741
+
742
+ try {
743
+
744
+ const json = JSON.parse( JSON.stringify( object.userData ) );
745
+
746
+ if ( options.includeCustomExtensions && json.gltfExtensions ) {
747
+
748
+ if ( objectDef.extensions === undefined ) objectDef.extensions = {};
749
+
750
+ for ( const extensionName in json.gltfExtensions ) {
751
+
752
+ objectDef.extensions[ extensionName ] = json.gltfExtensions[ extensionName ];
753
+ extensionsUsed[ extensionName ] = true;
754
+
755
+ }
756
+
757
+ delete json.gltfExtensions;
758
+
759
+ }
760
+
761
+ if ( Object.keys( json ).length > 0 ) objectDef.extras = json;
762
+
763
+ } catch ( error ) {
764
+
765
+ console.warn( 'THREE.GLTFExporter: userData of \'' + object.name + '\' ' +
766
+ 'won\'t be serialized because of JSON.stringify error - ' + error.message );
767
+
768
+ }
769
+
770
+ }
771
+
772
+ /**
773
+ * Returns ids for buffer attributes.
774
+ *
775
+ * @param {Object} attribute
776
+ * @param {boolean} [isRelativeCopy=false]
777
+ * @return {number} An integer
778
+ */
779
+ getUID( attribute, isRelativeCopy = false ) {
780
+
781
+ if ( this.uids.has( attribute ) === false ) {
782
+
783
+ const uids = new Map();
784
+
785
+ uids.set( true, this.uid ++ );
786
+ uids.set( false, this.uid ++ );
787
+
788
+ this.uids.set( attribute, uids );
789
+
790
+ }
791
+
792
+ const uids = this.uids.get( attribute );
793
+
794
+ return uids.get( isRelativeCopy );
795
+
796
+ }
797
+
798
+ /**
799
+ * Checks if normal attribute values are normalized.
800
+ *
801
+ * @param {BufferAttribute} normal
802
+ * @returns {boolean}
803
+ */
804
+ isNormalizedNormalAttribute( normal ) {
805
+
806
+ const cache = this.cache;
807
+
808
+ if ( cache.attributesNormalized.has( normal ) ) return false;
809
+
810
+ const v = new Vector3();
811
+
812
+ for ( let i = 0, il = normal.count; i < il; i ++ ) {
813
+
814
+ // 0.0005 is from glTF-validator
815
+ if ( Math.abs( v.fromBufferAttribute( normal, i ).length() - 1.0 ) > 0.0005 ) return false;
816
+
817
+ }
818
+
819
+ return true;
820
+
821
+ }
822
+
823
+ /**
824
+ * Creates normalized normal buffer attribute.
825
+ *
826
+ * @param {BufferAttribute} normal
827
+ * @returns {BufferAttribute}
828
+ *
829
+ */
830
+ createNormalizedNormalAttribute( normal ) {
831
+
832
+ const cache = this.cache;
833
+
834
+ if ( cache.attributesNormalized.has( normal ) ) return cache.attributesNormalized.get( normal );
835
+
836
+ const attribute = normal.clone();
837
+ const v = new Vector3();
838
+
839
+ for ( let i = 0, il = attribute.count; i < il; i ++ ) {
840
+
841
+ v.fromBufferAttribute( attribute, i );
842
+
843
+ if ( v.x === 0 && v.y === 0 && v.z === 0 ) {
844
+
845
+ // if values can't be normalized set (1, 0, 0)
846
+ v.setX( 1.0 );
847
+
848
+ } else {
849
+
850
+ v.normalize();
851
+
852
+ }
853
+
854
+ attribute.setXYZ( i, v.x, v.y, v.z );
855
+
856
+ }
857
+
858
+ cache.attributesNormalized.set( normal, attribute );
859
+
860
+ return attribute;
861
+
862
+ }
863
+
864
+ /**
865
+ * Applies a texture transform, if present, to the map definition. Requires
866
+ * the KHR_texture_transform extension.
867
+ *
868
+ * @param {Object} mapDef
869
+ * @param {THREE.Texture} texture
870
+ */
871
+ applyTextureTransform( mapDef, texture ) {
872
+
873
+ let didTransform = false;
874
+ const transformDef = {};
875
+
876
+ if ( texture.offset.x !== 0 || texture.offset.y !== 0 ) {
877
+
878
+ transformDef.offset = texture.offset.toArray();
879
+ didTransform = true;
880
+
881
+ }
882
+
883
+ if ( texture.rotation !== 0 ) {
884
+
885
+ transformDef.rotation = texture.rotation;
886
+ didTransform = true;
887
+
888
+ }
889
+
890
+ if ( texture.repeat.x !== 1 || texture.repeat.y !== 1 ) {
891
+
892
+ transformDef.scale = texture.repeat.toArray();
893
+ didTransform = true;
894
+
895
+ }
896
+
897
+ if ( didTransform ) {
898
+
899
+ mapDef.extensions = mapDef.extensions || {};
900
+ mapDef.extensions[ 'KHR_texture_transform' ] = transformDef;
901
+ this.extensionsUsed[ 'KHR_texture_transform' ] = true;
902
+
903
+ }
904
+
905
+ }
906
+
907
+ async buildMetalRoughTextureAsync( metalnessMap, roughnessMap ) {
908
+
909
+ if ( metalnessMap === roughnessMap ) return metalnessMap;
910
+
911
+ function getEncodingConversion( map ) {
912
+
913
+ if ( map.colorSpace === SRGBColorSpace ) {
914
+
915
+ return function SRGBToLinear( c ) {
916
+
917
+ return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 );
918
+
919
+ };
920
+
921
+ }
922
+
923
+ return function LinearToLinear( c ) {
924
+
925
+ return c;
926
+
927
+ };
928
+
929
+ }
930
+
931
+ if ( metalnessMap instanceof CompressedTexture ) {
932
+
933
+ metalnessMap = await this.decompressTextureAsync( metalnessMap );
934
+
935
+ }
936
+
937
+ if ( roughnessMap instanceof CompressedTexture ) {
938
+
939
+ roughnessMap = await this.decompressTextureAsync( roughnessMap );
940
+
941
+ }
942
+
943
+ const metalness = metalnessMap ? metalnessMap.image : null;
944
+ const roughness = roughnessMap ? roughnessMap.image : null;
945
+
946
+ const width = Math.max( metalness ? metalness.width : 0, roughness ? roughness.width : 0 );
947
+ const height = Math.max( metalness ? metalness.height : 0, roughness ? roughness.height : 0 );
948
+
949
+ const canvas = getCanvas();
950
+ canvas.width = width;
951
+ canvas.height = height;
952
+
953
+ const context = canvas.getContext( '2d', {
954
+ willReadFrequently: true,
955
+ } );
956
+ context.fillStyle = '#00ffff';
957
+ context.fillRect( 0, 0, width, height );
958
+
959
+ const composite = context.getImageData( 0, 0, width, height );
960
+
961
+ if ( metalness ) {
962
+
963
+ context.drawImage( metalness, 0, 0, width, height );
964
+
965
+ const convert = getEncodingConversion( metalnessMap );
966
+ const data = context.getImageData( 0, 0, width, height ).data;
967
+
968
+ for ( let i = 2; i < data.length; i += 4 ) {
969
+
970
+ composite.data[ i ] = convert( data[ i ] / 256 ) * 256;
971
+
972
+ }
973
+
974
+ }
975
+
976
+ if ( roughness ) {
977
+
978
+ context.drawImage( roughness, 0, 0, width, height );
979
+
980
+ const convert = getEncodingConversion( roughnessMap );
981
+ const data = context.getImageData( 0, 0, width, height ).data;
982
+
983
+ for ( let i = 1; i < data.length; i += 4 ) {
984
+
985
+ composite.data[ i ] = convert( data[ i ] / 256 ) * 256;
986
+
987
+ }
988
+
989
+ }
990
+
991
+ context.putImageData( composite, 0, 0 );
992
+
993
+ //
994
+
995
+ const reference = metalnessMap || roughnessMap;
996
+
997
+ const texture = reference.clone();
998
+
999
+ texture.source = new Source( canvas );
1000
+ texture.colorSpace = NoColorSpace;
1001
+ texture.channel = ( metalnessMap || roughnessMap ).channel;
1002
+
1003
+ if ( metalnessMap && roughnessMap && metalnessMap.channel !== roughnessMap.channel ) {
1004
+
1005
+ console.warn( 'THREE.GLTFExporter: UV channels for metalnessMap and roughnessMap textures must match.' );
1006
+
1007
+ }
1008
+
1009
+ console.warn( 'THREE.GLTFExporter: Merged metalnessMap and roughnessMap textures.' );
1010
+
1011
+ return texture;
1012
+
1013
+ }
1014
+
1015
+
1016
+ async decompressTextureAsync( texture, maxTextureSize = Infinity ) {
1017
+
1018
+ if ( this.textureUtils === null ) {
1019
+
1020
+ throw new Error( 'THREE.GLTFExporter: setTextureUtils() must be called to process compressed textures.' );
1021
+
1022
+ }
1023
+
1024
+ return await this.textureUtils.decompress( texture, maxTextureSize );
1025
+
1026
+ }
1027
+
1028
+ /**
1029
+ * Process a buffer to append to the default one.
1030
+ * @param {ArrayBuffer} buffer
1031
+ * @return {0}
1032
+ */
1033
+ processBuffer( buffer ) {
1034
+
1035
+ const json = this.json;
1036
+ const buffers = this.buffers;
1037
+
1038
+ if ( ! json.buffers ) json.buffers = [ { byteLength: 0 } ];
1039
+
1040
+ // All buffers are merged before export.
1041
+ buffers.push( buffer );
1042
+
1043
+ return 0;
1044
+
1045
+ }
1046
+
1047
+ /**
1048
+ * Process and generate a BufferView
1049
+ * @param {BufferAttribute} attribute
1050
+ * @param {number} componentType
1051
+ * @param {number} start
1052
+ * @param {number} count
1053
+ * @param {number} [target] Target usage of the BufferView
1054
+ * @return {Object}
1055
+ */
1056
+ processBufferView( attribute, componentType, start, count, target ) {
1057
+
1058
+ const json = this.json;
1059
+
1060
+ if ( ! json.bufferViews ) json.bufferViews = [];
1061
+
1062
+ // Create a new dataview and dump the attribute's array into it
1063
+
1064
+ let componentSize;
1065
+
1066
+ switch ( componentType ) {
1067
+
1068
+ case WEBGL_CONSTANTS.BYTE:
1069
+ case WEBGL_CONSTANTS.UNSIGNED_BYTE:
1070
+
1071
+ componentSize = 1;
1072
+
1073
+ break;
1074
+
1075
+ case WEBGL_CONSTANTS.SHORT:
1076
+ case WEBGL_CONSTANTS.UNSIGNED_SHORT:
1077
+
1078
+ componentSize = 2;
1079
+
1080
+ break;
1081
+
1082
+ default:
1083
+
1084
+ componentSize = 4;
1085
+
1086
+ }
1087
+
1088
+ let byteStride = attribute.itemSize * componentSize;
1089
+
1090
+ if ( target === WEBGL_CONSTANTS.ARRAY_BUFFER ) {
1091
+
1092
+ // Each element of a vertex attribute MUST be aligned to 4-byte boundaries
1093
+ // inside a bufferView
1094
+ byteStride = Math.ceil( byteStride / 4 ) * 4;
1095
+
1096
+ }
1097
+
1098
+ const byteLength = getPaddedBufferSize( count * byteStride );
1099
+ const dataView = new DataView( new ArrayBuffer( byteLength ) );
1100
+ let offset = 0;
1101
+
1102
+ for ( let i = start; i < start + count; i ++ ) {
1103
+
1104
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
1105
+
1106
+ let value;
1107
+
1108
+ if ( attribute.itemSize > 4 ) {
1109
+
1110
+ // no support for interleaved data for itemSize > 4
1111
+
1112
+ value = attribute.array[ i * attribute.itemSize + a ];
1113
+
1114
+ } else {
1115
+
1116
+ if ( a === 0 ) value = attribute.getX( i );
1117
+ else if ( a === 1 ) value = attribute.getY( i );
1118
+ else if ( a === 2 ) value = attribute.getZ( i );
1119
+ else if ( a === 3 ) value = attribute.getW( i );
1120
+
1121
+ if ( attribute.normalized === true ) {
1122
+
1123
+ value = MathUtils.normalize( value, attribute.array );
1124
+
1125
+ }
1126
+
1127
+ }
1128
+
1129
+ if ( componentType === WEBGL_CONSTANTS.FLOAT ) {
1130
+
1131
+ dataView.setFloat32( offset, value, true );
1132
+
1133
+ } else if ( componentType === WEBGL_CONSTANTS.INT ) {
1134
+
1135
+ dataView.setInt32( offset, value, true );
1136
+
1137
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_INT ) {
1138
+
1139
+ dataView.setUint32( offset, value, true );
1140
+
1141
+ } else if ( componentType === WEBGL_CONSTANTS.SHORT ) {
1142
+
1143
+ dataView.setInt16( offset, value, true );
1144
+
1145
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_SHORT ) {
1146
+
1147
+ dataView.setUint16( offset, value, true );
1148
+
1149
+ } else if ( componentType === WEBGL_CONSTANTS.BYTE ) {
1150
+
1151
+ dataView.setInt8( offset, value );
1152
+
1153
+ } else if ( componentType === WEBGL_CONSTANTS.UNSIGNED_BYTE ) {
1154
+
1155
+ dataView.setUint8( offset, value );
1156
+
1157
+ }
1158
+
1159
+ offset += componentSize;
1160
+
1161
+ }
1162
+
1163
+ if ( ( offset % byteStride ) !== 0 ) {
1164
+
1165
+ offset += byteStride - ( offset % byteStride );
1166
+
1167
+ }
1168
+
1169
+ }
1170
+
1171
+ const bufferViewDef = {
1172
+
1173
+ buffer: this.processBuffer( dataView.buffer ),
1174
+ byteOffset: this.byteOffset,
1175
+ byteLength: byteLength
1176
+
1177
+ };
1178
+
1179
+ if ( target !== undefined ) bufferViewDef.target = target;
1180
+
1181
+ if ( target === WEBGL_CONSTANTS.ARRAY_BUFFER ) {
1182
+
1183
+ // Only define byteStride for vertex attributes.
1184
+ bufferViewDef.byteStride = byteStride;
1185
+
1186
+ }
1187
+
1188
+ this.byteOffset += byteLength;
1189
+
1190
+ json.bufferViews.push( bufferViewDef );
1191
+
1192
+ // @TODO Merge bufferViews where possible.
1193
+ const output = {
1194
+
1195
+ id: json.bufferViews.length - 1,
1196
+ byteLength: 0
1197
+
1198
+ };
1199
+
1200
+ return output;
1201
+
1202
+ }
1203
+
1204
+ /**
1205
+ * Process and generate a BufferView from an image Blob.
1206
+ * @param {Blob} blob
1207
+ * @return {Promise<number>} An integer
1208
+ */
1209
+ processBufferViewImage( blob ) {
1210
+
1211
+ const writer = this;
1212
+ const json = writer.json;
1213
+
1214
+ if ( ! json.bufferViews ) json.bufferViews = [];
1215
+
1216
+ return new Promise( function ( resolve ) {
1217
+
1218
+ const reader = new FileReader();
1219
+ reader.readAsArrayBuffer( blob );
1220
+ reader.onloadend = function () {
1221
+
1222
+ const buffer = getPaddedArrayBuffer( reader.result );
1223
+
1224
+ const bufferViewDef = {
1225
+ buffer: writer.processBuffer( buffer ),
1226
+ byteOffset: writer.byteOffset,
1227
+ byteLength: buffer.byteLength
1228
+ };
1229
+
1230
+ writer.byteOffset += buffer.byteLength;
1231
+ resolve( json.bufferViews.push( bufferViewDef ) - 1 );
1232
+
1233
+ };
1234
+
1235
+ } );
1236
+
1237
+ }
1238
+
1239
+ /**
1240
+ * Process attribute to generate an accessor
1241
+ * @param {BufferAttribute} attribute Attribute to process
1242
+ * @param {?BufferGeometry} [geometry] Geometry used for truncated draw range
1243
+ * @param {number} [start=0]
1244
+ * @param {number} [count=Infinity]
1245
+ * @return {?number} Index of the processed accessor on the "accessors" array
1246
+ */
1247
+ processAccessor( attribute, geometry, start, count ) {
1248
+
1249
+ const json = this.json;
1250
+
1251
+ const types = {
1252
+
1253
+ 1: 'SCALAR',
1254
+ 2: 'VEC2',
1255
+ 3: 'VEC3',
1256
+ 4: 'VEC4',
1257
+ 9: 'MAT3',
1258
+ 16: 'MAT4'
1259
+
1260
+ };
1261
+
1262
+ let componentType;
1263
+
1264
+ // Detect the component type of the attribute array
1265
+ if ( attribute.array.constructor === Float32Array ) {
1266
+
1267
+ componentType = WEBGL_CONSTANTS.FLOAT;
1268
+
1269
+ } else if ( attribute.array.constructor === Int32Array ) {
1270
+
1271
+ componentType = WEBGL_CONSTANTS.INT;
1272
+
1273
+ } else if ( attribute.array.constructor === Uint32Array ) {
1274
+
1275
+ componentType = WEBGL_CONSTANTS.UNSIGNED_INT;
1276
+
1277
+ } else if ( attribute.array.constructor === Int16Array ) {
1278
+
1279
+ componentType = WEBGL_CONSTANTS.SHORT;
1280
+
1281
+ } else if ( attribute.array.constructor === Uint16Array ) {
1282
+
1283
+ componentType = WEBGL_CONSTANTS.UNSIGNED_SHORT;
1284
+
1285
+ } else if ( attribute.array.constructor === Int8Array ) {
1286
+
1287
+ componentType = WEBGL_CONSTANTS.BYTE;
1288
+
1289
+ } else if ( attribute.array.constructor === Uint8Array ) {
1290
+
1291
+ componentType = WEBGL_CONSTANTS.UNSIGNED_BYTE;
1292
+
1293
+ } else {
1294
+
1295
+ throw new Error( 'THREE.GLTFExporter: Unsupported bufferAttribute component type: ' + attribute.array.constructor.name );
1296
+
1297
+ }
1298
+
1299
+ if ( start === undefined ) start = 0;
1300
+ if ( count === undefined || count === Infinity ) count = attribute.count;
1301
+
1302
+ // Skip creating an accessor if the attribute doesn't have data to export
1303
+ if ( count === 0 ) return null;
1304
+
1305
+ const minMax = getMinMax( attribute, start, count );
1306
+ let bufferViewTarget;
1307
+
1308
+ // If geometry isn't provided, don't infer the target usage of the bufferView. For
1309
+ // animation samplers, target must not be set.
1310
+ if ( geometry !== undefined ) {
1311
+
1312
+ bufferViewTarget = attribute === geometry.index ? WEBGL_CONSTANTS.ELEMENT_ARRAY_BUFFER : WEBGL_CONSTANTS.ARRAY_BUFFER;
1313
+
1314
+ }
1315
+
1316
+ const bufferView = this.processBufferView( attribute, componentType, start, count, bufferViewTarget );
1317
+
1318
+ const accessorDef = {
1319
+
1320
+ bufferView: bufferView.id,
1321
+ byteOffset: bufferView.byteOffset,
1322
+ componentType: componentType,
1323
+ count: count,
1324
+ max: minMax.max,
1325
+ min: minMax.min,
1326
+ type: types[ attribute.itemSize ]
1327
+
1328
+ };
1329
+
1330
+ if ( attribute.normalized === true ) accessorDef.normalized = true;
1331
+ if ( ! json.accessors ) json.accessors = [];
1332
+
1333
+ return json.accessors.push( accessorDef ) - 1;
1334
+
1335
+ }
1336
+
1337
+ /**
1338
+ * Process image
1339
+ * @param {Image} image to process
1340
+ * @param {number} format Identifier of the format (RGBAFormat)
1341
+ * @param {boolean} flipY before writing out the image
1342
+ * @param {string} mimeType export format
1343
+ * @return {number} Index of the processed texture in the "images" array
1344
+ */
1345
+ processImage( image, format, flipY, mimeType = 'image/png' ) {
1346
+
1347
+ if ( image !== null ) {
1348
+
1349
+ const writer = this;
1350
+ const cache = writer.cache;
1351
+ const json = writer.json;
1352
+ const options = writer.options;
1353
+ const pending = writer.pending;
1354
+
1355
+ if ( ! cache.images.has( image ) ) cache.images.set( image, {} );
1356
+
1357
+ const cachedImages = cache.images.get( image );
1358
+
1359
+ const key = mimeType + ':flipY/' + flipY.toString();
1360
+
1361
+ if ( cachedImages[ key ] !== undefined ) return cachedImages[ key ];
1362
+
1363
+ if ( ! json.images ) json.images = [];
1364
+
1365
+ const imageDef = { mimeType: mimeType };
1366
+
1367
+ const canvas = getCanvas();
1368
+
1369
+ canvas.width = Math.min( image.width, options.maxTextureSize );
1370
+ canvas.height = Math.min( image.height, options.maxTextureSize );
1371
+
1372
+ const ctx = canvas.getContext( '2d', {
1373
+ willReadFrequently: true,
1374
+ } );
1375
+
1376
+ if ( flipY === true ) {
1377
+
1378
+ ctx.translate( 0, canvas.height );
1379
+ ctx.scale( 1, - 1 );
1380
+
1381
+ }
1382
+
1383
+ if ( image.data !== undefined ) { // THREE.DataTexture
1384
+
1385
+ if ( format !== RGBAFormat ) {
1386
+
1387
+ console.error( 'GLTFExporter: Only RGBAFormat is supported.', format );
1388
+
1389
+ }
1390
+
1391
+ if ( image.width > options.maxTextureSize || image.height > options.maxTextureSize ) {
1392
+
1393
+ console.warn( 'GLTFExporter: Image size is bigger than maxTextureSize', image );
1394
+
1395
+ }
1396
+
1397
+ const data = new Uint8ClampedArray( image.height * image.width * 4 );
1398
+
1399
+ for ( let i = 0; i < data.length; i += 4 ) {
1400
+
1401
+ data[ i + 0 ] = image.data[ i + 0 ];
1402
+ data[ i + 1 ] = image.data[ i + 1 ];
1403
+ data[ i + 2 ] = image.data[ i + 2 ];
1404
+ data[ i + 3 ] = image.data[ i + 3 ];
1405
+
1406
+ }
1407
+
1408
+ ctx.putImageData( new ImageData( data, image.width, image.height ), 0, 0 );
1409
+
1410
+ } else {
1411
+
1412
+ if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
1413
+ ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
1414
+ ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ||
1415
+ ( typeof OffscreenCanvas !== 'undefined' && image instanceof OffscreenCanvas ) ) {
1416
+
1417
+ ctx.drawImage( image, 0, 0, canvas.width, canvas.height );
1418
+
1419
+ } else {
1420
+
1421
+ throw new Error( 'THREE.GLTFExporter: Invalid image type. Use HTMLImageElement, HTMLCanvasElement, ImageBitmap or OffscreenCanvas.' );
1422
+
1423
+ }
1424
+
1425
+ }
1426
+
1427
+ if ( options.binary === true ) {
1428
+
1429
+ pending.push(
1430
+
1431
+ getToBlobPromise( canvas, mimeType )
1432
+ .then( blob => writer.processBufferViewImage( blob ) )
1433
+ .then( bufferViewIndex => {
1434
+
1435
+ imageDef.bufferView = bufferViewIndex;
1436
+
1437
+ } )
1438
+
1439
+ );
1440
+
1441
+ } else {
1442
+
1443
+ imageDef.uri = ImageUtils.getDataURL( canvas, mimeType );
1444
+
1445
+ }
1446
+
1447
+ const index = json.images.push( imageDef ) - 1;
1448
+ cachedImages[ key ] = index;
1449
+ return index;
1450
+
1451
+ } else {
1452
+
1453
+ throw new Error( 'THREE.GLTFExporter: No valid image data found. Unable to process texture.' );
1454
+
1455
+ }
1456
+
1457
+ }
1458
+
1459
+ /**
1460
+ * Process sampler
1461
+ * @param {Texture} map Texture to process
1462
+ * @return {number} Index of the processed texture in the "samplers" array
1463
+ */
1464
+ processSampler( map ) {
1465
+
1466
+ const json = this.json;
1467
+
1468
+ if ( ! json.samplers ) json.samplers = [];
1469
+
1470
+ const samplerDef = {
1471
+ magFilter: THREE_TO_WEBGL[ map.magFilter ],
1472
+ minFilter: THREE_TO_WEBGL[ map.minFilter ],
1473
+ wrapS: THREE_TO_WEBGL[ map.wrapS ],
1474
+ wrapT: THREE_TO_WEBGL[ map.wrapT ]
1475
+ };
1476
+
1477
+ return json.samplers.push( samplerDef ) - 1;
1478
+
1479
+ }
1480
+
1481
+ /**
1482
+ * Process texture
1483
+ * @param {Texture} map Map to process
1484
+ * @return {Promise<number>} Index of the processed texture in the "textures" array
1485
+ */
1486
+ async processTextureAsync( map ) {
1487
+
1488
+ const writer = this;
1489
+ const options = writer.options;
1490
+ const cache = this.cache;
1491
+ const json = this.json;
1492
+
1493
+ if ( cache.textures.has( map ) ) return cache.textures.get( map );
1494
+
1495
+ if ( ! json.textures ) json.textures = [];
1496
+
1497
+ // make non-readable textures (e.g. CompressedTexture) readable by blitting them into a new texture
1498
+ if ( map instanceof CompressedTexture ) {
1499
+
1500
+ map = await this.decompressTextureAsync( map, options.maxTextureSize );
1501
+
1502
+ }
1503
+
1504
+ let mimeType = map.userData.mimeType;
1505
+
1506
+ if ( mimeType === 'image/webp' ) mimeType = 'image/png';
1507
+
1508
+ const textureDef = {
1509
+ sampler: this.processSampler( map ),
1510
+ source: this.processImage( map.image, map.format, map.flipY, mimeType )
1511
+ };
1512
+
1513
+ if ( map.name ) textureDef.name = map.name;
1514
+
1515
+ await this._invokeAllAsync( async function ( ext ) {
1516
+
1517
+ ext.writeTexture && await ext.writeTexture( map, textureDef );
1518
+
1519
+ } );
1520
+
1521
+ const index = json.textures.push( textureDef ) - 1;
1522
+ cache.textures.set( map, index );
1523
+ return index;
1524
+
1525
+ }
1526
+
1527
+ /**
1528
+ * Process material
1529
+ * @param {THREE.Material} material Material to process
1530
+ * @return {Promise<number|null>} Index of the processed material in the "materials" array
1531
+ */
1532
+ async processMaterialAsync( material ) {
1533
+
1534
+ const cache = this.cache;
1535
+ const json = this.json;
1536
+
1537
+ if ( cache.materials.has( material ) ) return cache.materials.get( material );
1538
+
1539
+ if ( material.isShaderMaterial ) {
1540
+
1541
+ console.warn( 'GLTFExporter: THREE.ShaderMaterial not supported.' );
1542
+ return null;
1543
+
1544
+ }
1545
+
1546
+ if ( ! json.materials ) json.materials = [];
1547
+
1548
+ // @QUESTION Should we avoid including any attribute that has the default value?
1549
+ const materialDef = { pbrMetallicRoughness: {} };
1550
+
1551
+ if ( material.isMeshStandardMaterial !== true && material.isMeshBasicMaterial !== true ) {
1552
+
1553
+ console.warn( 'GLTFExporter: Use MeshStandardMaterial or MeshBasicMaterial for best results.' );
1554
+
1555
+ }
1556
+
1557
+ // pbrMetallicRoughness.baseColorFactor
1558
+ const color = material.color.toArray().concat( [ material.opacity ] );
1559
+
1560
+ if ( ! equalArray( color, [ 1, 1, 1, 1 ] ) ) {
1561
+
1562
+ materialDef.pbrMetallicRoughness.baseColorFactor = color;
1563
+
1564
+ }
1565
+
1566
+ if ( material.isMeshStandardMaterial ) {
1567
+
1568
+ materialDef.pbrMetallicRoughness.metallicFactor = material.metalness;
1569
+ materialDef.pbrMetallicRoughness.roughnessFactor = material.roughness;
1570
+
1571
+ } else {
1572
+
1573
+ materialDef.pbrMetallicRoughness.metallicFactor = 0;
1574
+ materialDef.pbrMetallicRoughness.roughnessFactor = 1;
1575
+
1576
+ }
1577
+
1578
+ // pbrMetallicRoughness.metallicRoughnessTexture
1579
+ if ( material.metalnessMap || material.roughnessMap ) {
1580
+
1581
+ const metalRoughTexture = await this.buildMetalRoughTextureAsync( material.metalnessMap, material.roughnessMap );
1582
+
1583
+ const metalRoughMapDef = {
1584
+ index: await this.processTextureAsync( metalRoughTexture ),
1585
+ texCoord: metalRoughTexture.channel
1586
+ };
1587
+ this.applyTextureTransform( metalRoughMapDef, metalRoughTexture );
1588
+ materialDef.pbrMetallicRoughness.metallicRoughnessTexture = metalRoughMapDef;
1589
+
1590
+ }
1591
+
1592
+ // pbrMetallicRoughness.baseColorTexture
1593
+ if ( material.map ) {
1594
+
1595
+ const baseColorMapDef = {
1596
+ index: await this.processTextureAsync( material.map ),
1597
+ texCoord: material.map.channel
1598
+ };
1599
+ this.applyTextureTransform( baseColorMapDef, material.map );
1600
+ materialDef.pbrMetallicRoughness.baseColorTexture = baseColorMapDef;
1601
+
1602
+ }
1603
+
1604
+ if ( material.emissive ) {
1605
+
1606
+ const emissive = material.emissive;
1607
+ const maxEmissiveComponent = Math.max( emissive.r, emissive.g, emissive.b );
1608
+
1609
+ if ( maxEmissiveComponent > 0 ) {
1610
+
1611
+ materialDef.emissiveFactor = material.emissive.toArray();
1612
+
1613
+ }
1614
+
1615
+ // emissiveTexture
1616
+ if ( material.emissiveMap ) {
1617
+
1618
+ const emissiveMapDef = {
1619
+ index: await this.processTextureAsync( material.emissiveMap ),
1620
+ texCoord: material.emissiveMap.channel
1621
+ };
1622
+ this.applyTextureTransform( emissiveMapDef, material.emissiveMap );
1623
+ materialDef.emissiveTexture = emissiveMapDef;
1624
+
1625
+ }
1626
+
1627
+ }
1628
+
1629
+ // normalTexture
1630
+ if ( material.normalMap ) {
1631
+
1632
+ const normalMapDef = {
1633
+ index: await this.processTextureAsync( material.normalMap ),
1634
+ texCoord: material.normalMap.channel
1635
+ };
1636
+
1637
+ if ( material.normalScale && material.normalScale.x !== 1 ) {
1638
+
1639
+ // glTF normal scale is univariate. Ignore `y`, which may be flipped.
1640
+ // Context: https://github.com/mrdoob/three.js/issues/11438#issuecomment-507003995
1641
+ normalMapDef.scale = material.normalScale.x;
1642
+
1643
+ }
1644
+
1645
+ this.applyTextureTransform( normalMapDef, material.normalMap );
1646
+ materialDef.normalTexture = normalMapDef;
1647
+
1648
+ }
1649
+
1650
+ // occlusionTexture
1651
+ if ( material.aoMap ) {
1652
+
1653
+ const occlusionMapDef = {
1654
+ index: await this.processTextureAsync( material.aoMap ),
1655
+ texCoord: material.aoMap.channel
1656
+ };
1657
+
1658
+ if ( material.aoMapIntensity !== 1.0 ) {
1659
+
1660
+ occlusionMapDef.strength = material.aoMapIntensity;
1661
+
1662
+ }
1663
+
1664
+ this.applyTextureTransform( occlusionMapDef, material.aoMap );
1665
+ materialDef.occlusionTexture = occlusionMapDef;
1666
+
1667
+ }
1668
+
1669
+ // alphaMode
1670
+ if ( material.transparent ) {
1671
+
1672
+ materialDef.alphaMode = 'BLEND';
1673
+
1674
+ } else {
1675
+
1676
+ if ( material.alphaTest > 0.0 ) {
1677
+
1678
+ materialDef.alphaMode = 'MASK';
1679
+ materialDef.alphaCutoff = material.alphaTest;
1680
+
1681
+ }
1682
+
1683
+ }
1684
+
1685
+ // doubleSided
1686
+ if ( material.side === DoubleSide ) materialDef.doubleSided = true;
1687
+ if ( material.name !== '' ) materialDef.name = material.name;
1688
+
1689
+ this.serializeUserData( material, materialDef );
1690
+
1691
+ await this._invokeAllAsync( async function ( ext ) {
1692
+
1693
+ ext.writeMaterialAsync && await ext.writeMaterialAsync( material, materialDef );
1694
+
1695
+ } );
1696
+
1697
+ const index = json.materials.push( materialDef ) - 1;
1698
+ cache.materials.set( material, index );
1699
+ return index;
1700
+
1701
+ }
1702
+
1703
+ /**
1704
+ * Process mesh
1705
+ * @param {THREE.Mesh} mesh Mesh to process
1706
+ * @return {Promise<number|null>} Index of the processed mesh in the "meshes" array
1707
+ */
1708
+ async processMeshAsync( mesh ) {
1709
+
1710
+ const cache = this.cache;
1711
+ const json = this.json;
1712
+
1713
+ const meshCacheKeyParts = [ mesh.geometry.uuid ];
1714
+
1715
+ if ( Array.isArray( mesh.material ) ) {
1716
+
1717
+ for ( let i = 0, l = mesh.material.length; i < l; i ++ ) {
1718
+
1719
+ meshCacheKeyParts.push( mesh.material[ i ].uuid );
1720
+
1721
+ }
1722
+
1723
+ } else {
1724
+
1725
+ meshCacheKeyParts.push( mesh.material.uuid );
1726
+
1727
+ }
1728
+
1729
+ const meshCacheKey = meshCacheKeyParts.join( ':' );
1730
+
1731
+ if ( cache.meshes.has( meshCacheKey ) ) return cache.meshes.get( meshCacheKey );
1732
+
1733
+ const geometry = mesh.geometry;
1734
+
1735
+ let mode;
1736
+
1737
+ // Use the correct mode
1738
+ if ( mesh.isLineSegments ) {
1739
+
1740
+ mode = WEBGL_CONSTANTS.LINES;
1741
+
1742
+ } else if ( mesh.isLineLoop ) {
1743
+
1744
+ mode = WEBGL_CONSTANTS.LINE_LOOP;
1745
+
1746
+ } else if ( mesh.isLine ) {
1747
+
1748
+ mode = WEBGL_CONSTANTS.LINE_STRIP;
1749
+
1750
+ } else if ( mesh.isPoints ) {
1751
+
1752
+ mode = WEBGL_CONSTANTS.POINTS;
1753
+
1754
+ } else {
1755
+
1756
+ mode = mesh.material.wireframe ? WEBGL_CONSTANTS.LINES : WEBGL_CONSTANTS.TRIANGLES;
1757
+
1758
+ }
1759
+
1760
+ const meshDef = {};
1761
+ const attributes = {};
1762
+ const primitives = [];
1763
+ const targets = [];
1764
+
1765
+ // Conversion between attributes names in threejs and gltf spec
1766
+ const nameConversion = {
1767
+ uv: 'TEXCOORD_0',
1768
+ uv1: 'TEXCOORD_1',
1769
+ uv2: 'TEXCOORD_2',
1770
+ uv3: 'TEXCOORD_3',
1771
+ color: 'COLOR_0',
1772
+ skinWeight: 'WEIGHTS_0',
1773
+ skinIndex: 'JOINTS_0'
1774
+ };
1775
+
1776
+ const originalNormal = geometry.getAttribute( 'normal' );
1777
+
1778
+ if ( originalNormal !== undefined && ! this.isNormalizedNormalAttribute( originalNormal ) ) {
1779
+
1780
+ console.warn( 'THREE.GLTFExporter: Creating normalized normal attribute from the non-normalized one.' );
1781
+
1782
+ geometry.setAttribute( 'normal', this.createNormalizedNormalAttribute( originalNormal ) );
1783
+
1784
+ }
1785
+
1786
+ // @QUESTION Detect if .vertexColors = true?
1787
+ // For every attribute create an accessor
1788
+ let modifiedAttribute = null;
1789
+
1790
+ for ( let attributeName in geometry.attributes ) {
1791
+
1792
+ // Ignore morph target attributes, which are exported later.
1793
+ if ( attributeName.slice( 0, 5 ) === 'morph' ) continue;
1794
+
1795
+ const attribute = geometry.attributes[ attributeName ];
1796
+ attributeName = nameConversion[ attributeName ] || attributeName.toUpperCase();
1797
+
1798
+ // Prefix all geometry attributes except the ones specifically
1799
+ // listed in the spec; non-spec attributes are considered custom.
1800
+ const validVertexAttributes =
1801
+ /^(POSITION|NORMAL|TANGENT|TEXCOORD_\d+|COLOR_\d+|JOINTS_\d+|WEIGHTS_\d+)$/;
1802
+
1803
+ if ( ! validVertexAttributes.test( attributeName ) ) attributeName = '_' + attributeName;
1804
+
1805
+ if ( cache.attributes.has( this.getUID( attribute ) ) ) {
1806
+
1807
+ attributes[ attributeName ] = cache.attributes.get( this.getUID( attribute ) );
1808
+ continue;
1809
+
1810
+ }
1811
+
1812
+ // Enforce glTF vertex attribute requirements:
1813
+ // - JOINTS_0 must be UNSIGNED_BYTE or UNSIGNED_SHORT
1814
+ // - Only custom attributes may be INT or UNSIGNED_INT
1815
+ modifiedAttribute = null;
1816
+ const array = attribute.array;
1817
+
1818
+ if ( attributeName === 'JOINTS_0' &&
1819
+ ! ( array instanceof Uint16Array ) &&
1820
+ ! ( array instanceof Uint8Array ) ) {
1821
+
1822
+ console.warn( 'GLTFExporter: Attribute "skinIndex" converted to type UNSIGNED_SHORT.' );
1823
+ modifiedAttribute = new BufferAttribute( new Uint16Array( array ), attribute.itemSize, attribute.normalized );
1824
+
1825
+ } else if ( ( array instanceof Uint32Array || array instanceof Int32Array ) && ! attributeName.startsWith( '_' ) ) {
1826
+
1827
+ console.warn( `GLTFExporter: Attribute "${ attributeName }" converted to type FLOAT.` );
1828
+ modifiedAttribute = GLTFExporter.Utils.toFloat32BufferAttribute( attribute );
1829
+
1830
+ }
1831
+
1832
+ const accessor = this.processAccessor( modifiedAttribute || attribute, geometry );
1833
+
1834
+ if ( accessor !== null ) {
1835
+
1836
+ if ( ! attributeName.startsWith( '_' ) ) {
1837
+
1838
+ this.detectMeshQuantization( attributeName, attribute );
1839
+
1840
+ }
1841
+
1842
+ attributes[ attributeName ] = accessor;
1843
+ cache.attributes.set( this.getUID( attribute ), accessor );
1844
+
1845
+ }
1846
+
1847
+ }
1848
+
1849
+ if ( originalNormal !== undefined ) geometry.setAttribute( 'normal', originalNormal );
1850
+
1851
+ // Skip if no exportable attributes found
1852
+ if ( Object.keys( attributes ).length === 0 ) return null;
1853
+
1854
+ // Morph targets
1855
+ if ( mesh.morphTargetInfluences !== undefined && mesh.morphTargetInfluences.length > 0 ) {
1856
+
1857
+ const weights = [];
1858
+ const targetNames = [];
1859
+ const reverseDictionary = {};
1860
+
1861
+ if ( mesh.morphTargetDictionary !== undefined ) {
1862
+
1863
+ for ( const key in mesh.morphTargetDictionary ) {
1864
+
1865
+ reverseDictionary[ mesh.morphTargetDictionary[ key ] ] = key;
1866
+
1867
+ }
1868
+
1869
+ }
1870
+
1871
+ for ( let i = 0; i < mesh.morphTargetInfluences.length; ++ i ) {
1872
+
1873
+ const target = {};
1874
+ let warned = false;
1875
+
1876
+ for ( const attributeName in geometry.morphAttributes ) {
1877
+
1878
+ // glTF 2.0 morph supports only POSITION/NORMAL/TANGENT.
1879
+ // Three.js doesn't support TANGENT yet.
1880
+
1881
+ if ( attributeName !== 'position' && attributeName !== 'normal' ) {
1882
+
1883
+ if ( ! warned ) {
1884
+
1885
+ console.warn( 'GLTFExporter: Only POSITION and NORMAL morph are supported.' );
1886
+ warned = true;
1887
+
1888
+ }
1889
+
1890
+ continue;
1891
+
1892
+ }
1893
+
1894
+ const attribute = geometry.morphAttributes[ attributeName ][ i ];
1895
+ const gltfAttributeName = attributeName.toUpperCase();
1896
+
1897
+ // Three.js morph attribute has absolute values while the one of glTF has relative values.
1898
+ //
1899
+ // glTF 2.0 Specification:
1900
+ // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#morph-targets
1901
+
1902
+ const baseAttribute = geometry.attributes[ attributeName ];
1903
+
1904
+ if ( cache.attributes.has( this.getUID( attribute, true ) ) ) {
1905
+
1906
+ target[ gltfAttributeName ] = cache.attributes.get( this.getUID( attribute, true ) );
1907
+ continue;
1908
+
1909
+ }
1910
+
1911
+ // Clones attribute not to override
1912
+ const relativeAttribute = attribute.clone();
1913
+
1914
+ if ( ! geometry.morphTargetsRelative ) {
1915
+
1916
+ for ( let j = 0, jl = attribute.count; j < jl; j ++ ) {
1917
+
1918
+ for ( let a = 0; a < attribute.itemSize; a ++ ) {
1919
+
1920
+ if ( a === 0 ) relativeAttribute.setX( j, attribute.getX( j ) - baseAttribute.getX( j ) );
1921
+ if ( a === 1 ) relativeAttribute.setY( j, attribute.getY( j ) - baseAttribute.getY( j ) );
1922
+ if ( a === 2 ) relativeAttribute.setZ( j, attribute.getZ( j ) - baseAttribute.getZ( j ) );
1923
+ if ( a === 3 ) relativeAttribute.setW( j, attribute.getW( j ) - baseAttribute.getW( j ) );
1924
+
1925
+ }
1926
+
1927
+ }
1928
+
1929
+ }
1930
+
1931
+ target[ gltfAttributeName ] = this.processAccessor( relativeAttribute, geometry );
1932
+ cache.attributes.set( this.getUID( baseAttribute, true ), target[ gltfAttributeName ] );
1933
+
1934
+ }
1935
+
1936
+ targets.push( target );
1937
+
1938
+ weights.push( mesh.morphTargetInfluences[ i ] );
1939
+
1940
+ if ( mesh.morphTargetDictionary !== undefined ) targetNames.push( reverseDictionary[ i ] );
1941
+
1942
+ }
1943
+
1944
+ meshDef.weights = weights;
1945
+
1946
+ if ( targetNames.length > 0 ) {
1947
+
1948
+ meshDef.extras = {};
1949
+ meshDef.extras.targetNames = targetNames;
1950
+
1951
+ }
1952
+
1953
+ }
1954
+
1955
+ const isMultiMaterial = Array.isArray( mesh.material );
1956
+
1957
+ if ( isMultiMaterial && geometry.groups.length === 0 ) return null;
1958
+
1959
+ let didForceIndices = false;
1960
+
1961
+ if ( isMultiMaterial && geometry.index === null ) {
1962
+
1963
+ const indices = [];
1964
+
1965
+ for ( let i = 0, il = geometry.attributes.position.count; i < il; i ++ ) {
1966
+
1967
+ indices[ i ] = i;
1968
+
1969
+ }
1970
+
1971
+ geometry.setIndex( indices );
1972
+
1973
+ didForceIndices = true;
1974
+
1975
+ }
1976
+
1977
+ const materials = isMultiMaterial ? mesh.material : [ mesh.material ];
1978
+ const groups = isMultiMaterial ? geometry.groups : [ { materialIndex: 0, start: undefined, count: undefined } ];
1979
+
1980
+ for ( let i = 0, il = groups.length; i < il; i ++ ) {
1981
+
1982
+ const primitive = {
1983
+ mode: mode,
1984
+ attributes: attributes,
1985
+ };
1986
+
1987
+ this.serializeUserData( geometry, primitive );
1988
+
1989
+ if ( targets.length > 0 ) primitive.targets = targets;
1990
+
1991
+ if ( geometry.index !== null ) {
1992
+
1993
+ let cacheKey = this.getUID( geometry.index );
1994
+
1995
+ if ( groups[ i ].start !== undefined || groups[ i ].count !== undefined ) {
1996
+
1997
+ cacheKey += ':' + groups[ i ].start + ':' + groups[ i ].count;
1998
+
1999
+ }
2000
+
2001
+ if ( cache.attributes.has( cacheKey ) ) {
2002
+
2003
+ primitive.indices = cache.attributes.get( cacheKey );
2004
+
2005
+ } else {
2006
+
2007
+ primitive.indices = this.processAccessor( geometry.index, geometry, groups[ i ].start, groups[ i ].count );
2008
+ cache.attributes.set( cacheKey, primitive.indices );
2009
+
2010
+ }
2011
+
2012
+ if ( primitive.indices === null ) delete primitive.indices;
2013
+
2014
+ }
2015
+
2016
+ const material = await this.processMaterialAsync( materials[ groups[ i ].materialIndex ] );
2017
+
2018
+ if ( material !== null ) primitive.material = material;
2019
+
2020
+ primitives.push( primitive );
2021
+
2022
+ }
2023
+
2024
+ if ( didForceIndices === true ) {
2025
+
2026
+ geometry.setIndex( null );
2027
+
2028
+ }
2029
+
2030
+ meshDef.primitives = primitives;
2031
+
2032
+ if ( ! json.meshes ) json.meshes = [];
2033
+
2034
+ await this._invokeAllAsync( function ( ext ) {
2035
+
2036
+ ext.writeMesh && ext.writeMesh( mesh, meshDef );
2037
+
2038
+ } );
2039
+
2040
+ const index = json.meshes.push( meshDef ) - 1;
2041
+ cache.meshes.set( meshCacheKey, index );
2042
+ return index;
2043
+
2044
+ }
2045
+
2046
+ /**
2047
+ * If a vertex attribute with a
2048
+ * [non-standard data type](https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#meshes-overview)
2049
+ * is used, it is checked whether it is a valid data type according to the
2050
+ * [KHR_mesh_quantization](https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_mesh_quantization/README.md)
2051
+ * extension.
2052
+ * In this case the extension is automatically added to the list of used extensions.
2053
+ *
2054
+ * @param {string} attributeName
2055
+ * @param {THREE.BufferAttribute} attribute
2056
+ */
2057
+ detectMeshQuantization( attributeName, attribute ) {
2058
+
2059
+ if ( this.extensionsUsed[ KHR_MESH_QUANTIZATION ] ) return;
2060
+
2061
+ let attrType = undefined;
2062
+
2063
+ switch ( attribute.array.constructor ) {
2064
+
2065
+ case Int8Array:
2066
+
2067
+ attrType = 'byte';
2068
+
2069
+ break;
2070
+
2071
+ case Uint8Array:
2072
+
2073
+ attrType = 'unsigned byte';
2074
+
2075
+ break;
2076
+
2077
+ case Int16Array:
2078
+
2079
+ attrType = 'short';
2080
+
2081
+ break;
2082
+
2083
+ case Uint16Array:
2084
+
2085
+ attrType = 'unsigned short';
2086
+
2087
+ break;
2088
+
2089
+ default:
2090
+
2091
+ return;
2092
+
2093
+ }
2094
+
2095
+ if ( attribute.normalized ) attrType += ' normalized';
2096
+
2097
+ const attrNamePrefix = attributeName.split( '_', 1 )[ 0 ];
2098
+
2099
+ if ( KHR_mesh_quantization_ExtraAttrTypes[ attrNamePrefix ] && KHR_mesh_quantization_ExtraAttrTypes[ attrNamePrefix ].includes( attrType ) ) {
2100
+
2101
+ this.extensionsUsed[ KHR_MESH_QUANTIZATION ] = true;
2102
+ this.extensionsRequired[ KHR_MESH_QUANTIZATION ] = true;
2103
+
2104
+ }
2105
+
2106
+ }
2107
+
2108
+ /**
2109
+ * Process camera
2110
+ * @param {THREE.Camera} camera Camera to process
2111
+ * @return {number} Index of the processed mesh in the "camera" array
2112
+ */
2113
+ processCamera( camera ) {
2114
+
2115
+ const json = this.json;
2116
+
2117
+ if ( ! json.cameras ) json.cameras = [];
2118
+
2119
+ const isOrtho = camera.isOrthographicCamera;
2120
+
2121
+ const cameraDef = {
2122
+ type: isOrtho ? 'orthographic' : 'perspective'
2123
+ };
2124
+
2125
+ if ( isOrtho ) {
2126
+
2127
+ cameraDef.orthographic = {
2128
+ xmag: camera.right * 2,
2129
+ ymag: camera.top * 2,
2130
+ zfar: camera.far <= 0 ? 0.001 : camera.far,
2131
+ znear: camera.near < 0 ? 0 : camera.near
2132
+ };
2133
+
2134
+ } else {
2135
+
2136
+ cameraDef.perspective = {
2137
+ aspectRatio: camera.aspect,
2138
+ yfov: MathUtils.degToRad( camera.fov ),
2139
+ zfar: camera.far <= 0 ? 0.001 : camera.far,
2140
+ znear: camera.near < 0 ? 0 : camera.near
2141
+ };
2142
+
2143
+ }
2144
+
2145
+ // Question: Is saving "type" as name intentional?
2146
+ if ( camera.name !== '' ) cameraDef.name = camera.type;
2147
+
2148
+ return json.cameras.push( cameraDef ) - 1;
2149
+
2150
+ }
2151
+
2152
+ /**
2153
+ * Creates glTF animation entry from AnimationClip object.
2154
+ *
2155
+ * Status:
2156
+ * - Only properties listed in PATH_PROPERTIES may be animated.
2157
+ *
2158
+ * @param {THREE.AnimationClip} clip
2159
+ * @param {THREE.Object3D} root
2160
+ * @return {number|null}
2161
+ */
2162
+ processAnimation( clip, root ) {
2163
+
2164
+ const json = this.json;
2165
+ const nodeMap = this.nodeMap;
2166
+
2167
+ if ( ! json.animations ) json.animations = [];
2168
+
2169
+ clip = GLTFExporter.Utils.mergeMorphTargetTracks( clip.clone(), root );
2170
+
2171
+ const tracks = clip.tracks;
2172
+ const channels = [];
2173
+ const samplers = [];
2174
+
2175
+ for ( let i = 0; i < tracks.length; ++ i ) {
2176
+
2177
+ const track = tracks[ i ];
2178
+ const trackBinding = PropertyBinding.parseTrackName( track.name );
2179
+ let trackNode = PropertyBinding.findNode( root, trackBinding.nodeName );
2180
+ const trackProperty = PATH_PROPERTIES[ trackBinding.propertyName ];
2181
+
2182
+ if ( trackBinding.objectName === 'bones' ) {
2183
+
2184
+ if ( trackNode.isSkinnedMesh === true ) {
2185
+
2186
+ trackNode = trackNode.skeleton.getBoneByName( trackBinding.objectIndex );
2187
+
2188
+ } else {
2189
+
2190
+ trackNode = undefined;
2191
+
2192
+ }
2193
+
2194
+ }
2195
+
2196
+ if ( ! trackNode || ! trackProperty ) {
2197
+
2198
+ console.warn( 'THREE.GLTFExporter: Could not export animation track "%s".', track.name );
2199
+ continue;
2200
+
2201
+ }
2202
+
2203
+ const inputItemSize = 1;
2204
+ let outputItemSize = track.values.length / track.times.length;
2205
+
2206
+ if ( trackProperty === PATH_PROPERTIES.morphTargetInfluences ) {
2207
+
2208
+ outputItemSize /= trackNode.morphTargetInfluences.length;
2209
+
2210
+ }
2211
+
2212
+ let interpolation;
2213
+
2214
+ // @TODO export CubicInterpolant(InterpolateSmooth) as CUBICSPLINE
2215
+
2216
+ // Detecting glTF cubic spline interpolant by checking factory method's special property
2217
+ // GLTFCubicSplineInterpolant is a custom interpolant and track doesn't return
2218
+ // valid value from .getInterpolation().
2219
+ if ( track.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline === true ) {
2220
+
2221
+ interpolation = 'CUBICSPLINE';
2222
+
2223
+ // itemSize of CUBICSPLINE keyframe is 9
2224
+ // (VEC3 * 3: inTangent, splineVertex, and outTangent)
2225
+ // but needs to be stored as VEC3 so dividing by 3 here.
2226
+ outputItemSize /= 3;
2227
+
2228
+ } else if ( track.getInterpolation() === InterpolateDiscrete ) {
2229
+
2230
+ interpolation = 'STEP';
2231
+
2232
+ } else {
2233
+
2234
+ interpolation = 'LINEAR';
2235
+
2236
+ }
2237
+
2238
+ samplers.push( {
2239
+ input: this.processAccessor( new BufferAttribute( track.times, inputItemSize ) ),
2240
+ output: this.processAccessor( new BufferAttribute( track.values, outputItemSize ) ),
2241
+ interpolation: interpolation
2242
+ } );
2243
+
2244
+ channels.push( {
2245
+ sampler: samplers.length - 1,
2246
+ target: {
2247
+ node: nodeMap.get( trackNode ),
2248
+ path: trackProperty
2249
+ }
2250
+ } );
2251
+
2252
+ }
2253
+
2254
+ json.animations.push( {
2255
+ name: clip.name || 'clip_' + json.animations.length,
2256
+ samplers: samplers,
2257
+ channels: channels
2258
+ } );
2259
+
2260
+ return json.animations.length - 1;
2261
+
2262
+ }
2263
+
2264
+ /**
2265
+ * @param {THREE.Object3D} object
2266
+ * @return {number|null}
2267
+ */
2268
+ processSkin( object ) {
2269
+
2270
+ const json = this.json;
2271
+ const nodeMap = this.nodeMap;
2272
+
2273
+ const node = json.nodes[ nodeMap.get( object ) ];
2274
+
2275
+ const skeleton = object.skeleton;
2276
+
2277
+ if ( skeleton === undefined ) return null;
2278
+
2279
+ const rootJoint = object.skeleton.bones[ 0 ];
2280
+
2281
+ if ( rootJoint === undefined ) return null;
2282
+
2283
+ const joints = [];
2284
+ const inverseBindMatrices = new Float32Array( skeleton.bones.length * 16 );
2285
+ const temporaryBoneInverse = new Matrix4();
2286
+
2287
+ for ( let i = 0; i < skeleton.bones.length; ++ i ) {
2288
+
2289
+ joints.push( nodeMap.get( skeleton.bones[ i ] ) );
2290
+ temporaryBoneInverse.copy( skeleton.boneInverses[ i ] );
2291
+ temporaryBoneInverse.multiply( object.bindMatrix ).toArray( inverseBindMatrices, i * 16 );
2292
+
2293
+ }
2294
+
2295
+ if ( json.skins === undefined ) json.skins = [];
2296
+
2297
+ json.skins.push( {
2298
+ inverseBindMatrices: this.processAccessor( new BufferAttribute( inverseBindMatrices, 16 ) ),
2299
+ joints: joints,
2300
+ skeleton: nodeMap.get( rootJoint )
2301
+ } );
2302
+
2303
+ const skinIndex = node.skin = json.skins.length - 1;
2304
+
2305
+ return skinIndex;
2306
+
2307
+ }
2308
+
2309
+ /**
2310
+ * Process Object3D node
2311
+ * @param {THREE.Object3D} object Object3D to processNodeAsync
2312
+ * @return {Promise<number>} Index of the node in the nodes list
2313
+ */
2314
+ async processNodeAsync( object ) {
2315
+
2316
+ const json = this.json;
2317
+ const options = this.options;
2318
+ const nodeMap = this.nodeMap;
2319
+
2320
+ if ( ! json.nodes ) json.nodes = [];
2321
+
2322
+ const nodeDef = {};
2323
+
2324
+ if ( options.trs ) {
2325
+
2326
+ const rotation = object.quaternion.toArray();
2327
+ const position = object.position.toArray();
2328
+ const scale = object.scale.toArray();
2329
+
2330
+ if ( ! equalArray( rotation, [ 0, 0, 0, 1 ] ) ) {
2331
+
2332
+ nodeDef.rotation = rotation;
2333
+
2334
+ }
2335
+
2336
+ if ( ! equalArray( position, [ 0, 0, 0 ] ) ) {
2337
+
2338
+ nodeDef.translation = position;
2339
+
2340
+ }
2341
+
2342
+ if ( ! equalArray( scale, [ 1, 1, 1 ] ) ) {
2343
+
2344
+ nodeDef.scale = scale;
2345
+
2346
+ }
2347
+
2348
+ } else {
2349
+
2350
+ if ( object.matrixAutoUpdate ) {
2351
+
2352
+ object.updateMatrix();
2353
+
2354
+ }
2355
+
2356
+ if ( isIdentityMatrix( object.matrix ) === false ) {
2357
+
2358
+ nodeDef.matrix = object.matrix.elements;
2359
+
2360
+ }
2361
+
2362
+ }
2363
+
2364
+ // We don't export empty strings name because it represents no-name in Three.js.
2365
+ if ( object.name !== '' ) nodeDef.name = String( object.name );
2366
+
2367
+ this.serializeUserData( object, nodeDef );
2368
+
2369
+ if ( object.isMesh || object.isLine || object.isPoints ) {
2370
+
2371
+ const meshIndex = await this.processMeshAsync( object );
2372
+
2373
+ if ( meshIndex !== null ) nodeDef.mesh = meshIndex;
2374
+
2375
+ } else if ( object.isCamera ) {
2376
+
2377
+ nodeDef.camera = this.processCamera( object );
2378
+
2379
+ }
2380
+
2381
+ if ( object.isSkinnedMesh ) this.skins.push( object );
2382
+
2383
+ const nodeIndex = json.nodes.push( nodeDef ) - 1;
2384
+ nodeMap.set( object, nodeIndex );
2385
+
2386
+ if ( object.children.length > 0 ) {
2387
+
2388
+ const children = [];
2389
+
2390
+ for ( let i = 0, l = object.children.length; i < l; i ++ ) {
2391
+
2392
+ const child = object.children[ i ];
2393
+
2394
+ if ( child.visible || options.onlyVisible === false ) {
2395
+
2396
+ const childNodeIndex = await this.processNodeAsync( child );
2397
+
2398
+ if ( childNodeIndex !== null ) children.push( childNodeIndex );
2399
+
2400
+ }
2401
+
2402
+ }
2403
+
2404
+ if ( children.length > 0 ) nodeDef.children = children;
2405
+
2406
+ }
2407
+
2408
+ await this._invokeAllAsync( function ( ext ) {
2409
+
2410
+ ext.writeNode && ext.writeNode( object, nodeDef );
2411
+
2412
+ } );
2413
+
2414
+ return nodeIndex;
2415
+
2416
+ }
2417
+
2418
+ /**
2419
+ * Process Scene
2420
+ * @param {Scene} scene Scene to process
2421
+ */
2422
+ async processSceneAsync( scene ) {
2423
+
2424
+ const json = this.json;
2425
+ const options = this.options;
2426
+
2427
+ if ( ! json.scenes ) {
2428
+
2429
+ json.scenes = [];
2430
+ json.scene = 0;
2431
+
2432
+ }
2433
+
2434
+ const sceneDef = {};
2435
+
2436
+ if ( scene.name !== '' ) sceneDef.name = scene.name;
2437
+
2438
+ json.scenes.push( sceneDef );
2439
+
2440
+ const nodes = [];
2441
+
2442
+ for ( let i = 0, l = scene.children.length; i < l; i ++ ) {
2443
+
2444
+ const child = scene.children[ i ];
2445
+
2446
+ if ( child.visible || options.onlyVisible === false ) {
2447
+
2448
+ const nodeIndex = await this.processNodeAsync( child );
2449
+
2450
+ if ( nodeIndex !== null ) nodes.push( nodeIndex );
2451
+
2452
+ }
2453
+
2454
+ }
2455
+
2456
+ if ( nodes.length > 0 ) sceneDef.nodes = nodes;
2457
+
2458
+ this.serializeUserData( scene, sceneDef );
2459
+
2460
+ }
2461
+
2462
+ /**
2463
+ * Creates a Scene to hold a list of objects and parse it
2464
+ * @param {Array<THREE.Object3D>} objects List of objects to process
2465
+ */
2466
+ async processObjectsAsync( objects ) {
2467
+
2468
+ const scene = new Scene();
2469
+ scene.name = 'AuxScene';
2470
+
2471
+ for ( let i = 0; i < objects.length; i ++ ) {
2472
+
2473
+ // We push directly to children instead of calling `add` to prevent
2474
+ // modify the .parent and break its original scene and hierarchy
2475
+ scene.children.push( objects[ i ] );
2476
+
2477
+ }
2478
+
2479
+ await this.processSceneAsync( scene );
2480
+
2481
+ }
2482
+
2483
+ /**
2484
+ * @param {THREE.Object3D|Array<THREE.Object3D>} input
2485
+ */
2486
+ async processInputAsync( input ) {
2487
+
2488
+ const options = this.options;
2489
+
2490
+ input = input instanceof Array ? input : [ input ];
2491
+
2492
+ await this._invokeAllAsync( function ( ext ) {
2493
+
2494
+ ext.beforeParse && ext.beforeParse( input );
2495
+
2496
+ } );
2497
+
2498
+ const objectsWithoutScene = [];
2499
+
2500
+ for ( let i = 0; i < input.length; i ++ ) {
2501
+
2502
+ if ( input[ i ] instanceof Scene ) {
2503
+
2504
+ await this.processSceneAsync( input[ i ] );
2505
+
2506
+ } else {
2507
+
2508
+ objectsWithoutScene.push( input[ i ] );
2509
+
2510
+ }
2511
+
2512
+ }
2513
+
2514
+ if ( objectsWithoutScene.length > 0 ) {
2515
+
2516
+ await this.processObjectsAsync( objectsWithoutScene );
2517
+
2518
+ }
2519
+
2520
+ for ( let i = 0; i < this.skins.length; ++ i ) {
2521
+
2522
+ this.processSkin( this.skins[ i ] );
2523
+
2524
+ }
2525
+
2526
+ for ( let i = 0; i < options.animations.length; ++ i ) {
2527
+
2528
+ this.processAnimation( options.animations[ i ], input[ 0 ] );
2529
+
2530
+ }
2531
+
2532
+ await this._invokeAllAsync( function ( ext ) {
2533
+
2534
+ ext.afterParse && ext.afterParse( input );
2535
+
2536
+ } );
2537
+
2538
+ }
2539
+
2540
+ async _invokeAllAsync( func ) {
2541
+
2542
+ for ( let i = 0, il = this.plugins.length; i < il; i ++ ) {
2543
+
2544
+ await func( this.plugins[ i ] );
2545
+
2546
+ }
2547
+
2548
+ }
2549
+
2550
+ }
2551
+
2552
+ /**
2553
+ * Punctual Lights Extension
2554
+ *
2555
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual
2556
+ *
2557
+ * @private
2558
+ */
2559
+ class GLTFLightExtension {
2560
+
2561
+ constructor( writer ) {
2562
+
2563
+ this.writer = writer;
2564
+ this.name = 'KHR_lights_punctual';
2565
+
2566
+ }
2567
+
2568
+ writeNode( light, nodeDef ) {
2569
+
2570
+ if ( ! light.isLight ) return;
2571
+
2572
+ if ( ! light.isDirectionalLight && ! light.isPointLight && ! light.isSpotLight ) {
2573
+
2574
+ console.warn( 'THREE.GLTFExporter: Only directional, point, and spot lights are supported.', light );
2575
+ return;
2576
+
2577
+ }
2578
+
2579
+ const writer = this.writer;
2580
+ const json = writer.json;
2581
+ const extensionsUsed = writer.extensionsUsed;
2582
+
2583
+ const lightDef = {};
2584
+
2585
+ if ( light.name ) lightDef.name = light.name;
2586
+
2587
+ lightDef.color = light.color.toArray();
2588
+
2589
+ lightDef.intensity = light.intensity;
2590
+
2591
+ if ( light.isDirectionalLight ) {
2592
+
2593
+ lightDef.type = 'directional';
2594
+
2595
+ } else if ( light.isPointLight ) {
2596
+
2597
+ lightDef.type = 'point';
2598
+
2599
+ if ( light.distance > 0 ) lightDef.range = light.distance;
2600
+
2601
+ } else if ( light.isSpotLight ) {
2602
+
2603
+ lightDef.type = 'spot';
2604
+
2605
+ if ( light.distance > 0 ) lightDef.range = light.distance;
2606
+
2607
+ lightDef.spot = {};
2608
+ lightDef.spot.innerConeAngle = ( 1.0 - light.penumbra ) * light.angle;
2609
+ lightDef.spot.outerConeAngle = light.angle;
2610
+
2611
+ }
2612
+
2613
+ if ( light.decay !== undefined && light.decay !== 2 ) {
2614
+
2615
+ console.warn( 'THREE.GLTFExporter: Light decay may be lost. glTF is physically-based, '
2616
+ + 'and expects light.decay=2.' );
2617
+
2618
+ }
2619
+
2620
+ if ( light.target
2621
+ && ( light.target.parent !== light
2622
+ || light.target.position.x !== 0
2623
+ || light.target.position.y !== 0
2624
+ || light.target.position.z !== - 1 ) ) {
2625
+
2626
+ console.warn( 'THREE.GLTFExporter: Light direction may be lost. For best results, '
2627
+ + 'make light.target a child of the light with position 0,0,-1.' );
2628
+
2629
+ }
2630
+
2631
+ if ( ! extensionsUsed[ this.name ] ) {
2632
+
2633
+ json.extensions = json.extensions || {};
2634
+ json.extensions[ this.name ] = { lights: [] };
2635
+ extensionsUsed[ this.name ] = true;
2636
+
2637
+ }
2638
+
2639
+ const lights = json.extensions[ this.name ].lights;
2640
+ lights.push( lightDef );
2641
+
2642
+ nodeDef.extensions = nodeDef.extensions || {};
2643
+ nodeDef.extensions[ this.name ] = { light: lights.length - 1 };
2644
+
2645
+ }
2646
+
2647
+ }
2648
+
2649
+ /**
2650
+ * Unlit Materials Extension
2651
+ *
2652
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit
2653
+ *
2654
+ * @private
2655
+ */
2656
+ class GLTFMaterialsUnlitExtension {
2657
+
2658
+ constructor( writer ) {
2659
+
2660
+ this.writer = writer;
2661
+ this.name = 'KHR_materials_unlit';
2662
+
2663
+ }
2664
+
2665
+ async writeMaterialAsync( material, materialDef ) {
2666
+
2667
+ if ( ! material.isMeshBasicMaterial ) return;
2668
+
2669
+ const writer = this.writer;
2670
+ const extensionsUsed = writer.extensionsUsed;
2671
+
2672
+ materialDef.extensions = materialDef.extensions || {};
2673
+ materialDef.extensions[ this.name ] = {};
2674
+
2675
+ extensionsUsed[ this.name ] = true;
2676
+
2677
+ materialDef.pbrMetallicRoughness.metallicFactor = 0.0;
2678
+ materialDef.pbrMetallicRoughness.roughnessFactor = 0.9;
2679
+
2680
+ }
2681
+
2682
+ }
2683
+
2684
+ /**
2685
+ * Clearcoat Materials Extension
2686
+ *
2687
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_clearcoat
2688
+ *
2689
+ * @private
2690
+ */
2691
+ class GLTFMaterialsClearcoatExtension {
2692
+
2693
+ constructor( writer ) {
2694
+
2695
+ this.writer = writer;
2696
+ this.name = 'KHR_materials_clearcoat';
2697
+
2698
+ }
2699
+
2700
+ async writeMaterialAsync( material, materialDef ) {
2701
+
2702
+ if ( ! material.isMeshPhysicalMaterial || material.clearcoat === 0 ) return;
2703
+
2704
+ const writer = this.writer;
2705
+ const extensionsUsed = writer.extensionsUsed;
2706
+
2707
+ const extensionDef = {};
2708
+
2709
+ extensionDef.clearcoatFactor = material.clearcoat;
2710
+
2711
+ if ( material.clearcoatMap ) {
2712
+
2713
+ const clearcoatMapDef = {
2714
+ index: await writer.processTextureAsync( material.clearcoatMap ),
2715
+ texCoord: material.clearcoatMap.channel
2716
+ };
2717
+ writer.applyTextureTransform( clearcoatMapDef, material.clearcoatMap );
2718
+ extensionDef.clearcoatTexture = clearcoatMapDef;
2719
+
2720
+ }
2721
+
2722
+ extensionDef.clearcoatRoughnessFactor = material.clearcoatRoughness;
2723
+
2724
+ if ( material.clearcoatRoughnessMap ) {
2725
+
2726
+ const clearcoatRoughnessMapDef = {
2727
+ index: await writer.processTextureAsync( material.clearcoatRoughnessMap ),
2728
+ texCoord: material.clearcoatRoughnessMap.channel
2729
+ };
2730
+ writer.applyTextureTransform( clearcoatRoughnessMapDef, material.clearcoatRoughnessMap );
2731
+ extensionDef.clearcoatRoughnessTexture = clearcoatRoughnessMapDef;
2732
+
2733
+ }
2734
+
2735
+ if ( material.clearcoatNormalMap ) {
2736
+
2737
+ const clearcoatNormalMapDef = {
2738
+ index: await writer.processTextureAsync( material.clearcoatNormalMap ),
2739
+ texCoord: material.clearcoatNormalMap.channel
2740
+ };
2741
+
2742
+ if ( material.clearcoatNormalScale.x !== 1 ) clearcoatNormalMapDef.scale = material.clearcoatNormalScale.x;
2743
+
2744
+ writer.applyTextureTransform( clearcoatNormalMapDef, material.clearcoatNormalMap );
2745
+ extensionDef.clearcoatNormalTexture = clearcoatNormalMapDef;
2746
+
2747
+ }
2748
+
2749
+ materialDef.extensions = materialDef.extensions || {};
2750
+ materialDef.extensions[ this.name ] = extensionDef;
2751
+
2752
+ extensionsUsed[ this.name ] = true;
2753
+
2754
+
2755
+ }
2756
+
2757
+ }
2758
+
2759
+ /**
2760
+ * Materials dispersion Extension
2761
+ *
2762
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_dispersion
2763
+ *
2764
+ * @private
2765
+ */
2766
+ class GLTFMaterialsDispersionExtension {
2767
+
2768
+ constructor( writer ) {
2769
+
2770
+ this.writer = writer;
2771
+ this.name = 'KHR_materials_dispersion';
2772
+
2773
+ }
2774
+
2775
+ async writeMaterialAsync( material, materialDef ) {
2776
+
2777
+ if ( ! material.isMeshPhysicalMaterial || material.dispersion === 0 ) return;
2778
+
2779
+ const writer = this.writer;
2780
+ const extensionsUsed = writer.extensionsUsed;
2781
+
2782
+ const extensionDef = {};
2783
+
2784
+ extensionDef.dispersion = material.dispersion;
2785
+
2786
+ materialDef.extensions = materialDef.extensions || {};
2787
+ materialDef.extensions[ this.name ] = extensionDef;
2788
+
2789
+ extensionsUsed[ this.name ] = true;
2790
+
2791
+ }
2792
+
2793
+ }
2794
+
2795
+ /**
2796
+ * Iridescence Materials Extension
2797
+ *
2798
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_iridescence
2799
+ *
2800
+ * @private
2801
+ */
2802
+ class GLTFMaterialsIridescenceExtension {
2803
+
2804
+ constructor( writer ) {
2805
+
2806
+ this.writer = writer;
2807
+ this.name = 'KHR_materials_iridescence';
2808
+
2809
+ }
2810
+
2811
+ async writeMaterialAsync( material, materialDef ) {
2812
+
2813
+ if ( ! material.isMeshPhysicalMaterial || material.iridescence === 0 ) return;
2814
+
2815
+ const writer = this.writer;
2816
+ const extensionsUsed = writer.extensionsUsed;
2817
+
2818
+ const extensionDef = {};
2819
+
2820
+ extensionDef.iridescenceFactor = material.iridescence;
2821
+
2822
+ if ( material.iridescenceMap ) {
2823
+
2824
+ const iridescenceMapDef = {
2825
+ index: await writer.processTextureAsync( material.iridescenceMap ),
2826
+ texCoord: material.iridescenceMap.channel
2827
+ };
2828
+ writer.applyTextureTransform( iridescenceMapDef, material.iridescenceMap );
2829
+ extensionDef.iridescenceTexture = iridescenceMapDef;
2830
+
2831
+ }
2832
+
2833
+ extensionDef.iridescenceIor = material.iridescenceIOR;
2834
+ extensionDef.iridescenceThicknessMinimum = material.iridescenceThicknessRange[ 0 ];
2835
+ extensionDef.iridescenceThicknessMaximum = material.iridescenceThicknessRange[ 1 ];
2836
+
2837
+ if ( material.iridescenceThicknessMap ) {
2838
+
2839
+ const iridescenceThicknessMapDef = {
2840
+ index: await writer.processTextureAsync( material.iridescenceThicknessMap ),
2841
+ texCoord: material.iridescenceThicknessMap.channel
2842
+ };
2843
+ writer.applyTextureTransform( iridescenceThicknessMapDef, material.iridescenceThicknessMap );
2844
+ extensionDef.iridescenceThicknessTexture = iridescenceThicknessMapDef;
2845
+
2846
+ }
2847
+
2848
+ materialDef.extensions = materialDef.extensions || {};
2849
+ materialDef.extensions[ this.name ] = extensionDef;
2850
+
2851
+ extensionsUsed[ this.name ] = true;
2852
+
2853
+ }
2854
+
2855
+ }
2856
+
2857
+ /**
2858
+ * Transmission Materials Extension
2859
+ *
2860
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_transmission
2861
+ *
2862
+ * @private
2863
+ */
2864
+ class GLTFMaterialsTransmissionExtension {
2865
+
2866
+ constructor( writer ) {
2867
+
2868
+ this.writer = writer;
2869
+ this.name = 'KHR_materials_transmission';
2870
+
2871
+ }
2872
+
2873
+ async writeMaterialAsync( material, materialDef ) {
2874
+
2875
+ if ( ! material.isMeshPhysicalMaterial || material.transmission === 0 ) return;
2876
+
2877
+ const writer = this.writer;
2878
+ const extensionsUsed = writer.extensionsUsed;
2879
+
2880
+ const extensionDef = {};
2881
+
2882
+ extensionDef.transmissionFactor = material.transmission;
2883
+
2884
+ if ( material.transmissionMap ) {
2885
+
2886
+ const transmissionMapDef = {
2887
+ index: await writer.processTextureAsync( material.transmissionMap ),
2888
+ texCoord: material.transmissionMap.channel
2889
+ };
2890
+ writer.applyTextureTransform( transmissionMapDef, material.transmissionMap );
2891
+ extensionDef.transmissionTexture = transmissionMapDef;
2892
+
2893
+ }
2894
+
2895
+ materialDef.extensions = materialDef.extensions || {};
2896
+ materialDef.extensions[ this.name ] = extensionDef;
2897
+
2898
+ extensionsUsed[ this.name ] = true;
2899
+
2900
+ }
2901
+
2902
+ }
2903
+
2904
+ /**
2905
+ * Materials Volume Extension
2906
+ *
2907
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_volume
2908
+ *
2909
+ * @private
2910
+ */
2911
+ class GLTFMaterialsVolumeExtension {
2912
+
2913
+ constructor( writer ) {
2914
+
2915
+ this.writer = writer;
2916
+ this.name = 'KHR_materials_volume';
2917
+
2918
+ }
2919
+
2920
+ async writeMaterialAsync( material, materialDef ) {
2921
+
2922
+ if ( ! material.isMeshPhysicalMaterial || material.transmission === 0 ) return;
2923
+
2924
+ const writer = this.writer;
2925
+ const extensionsUsed = writer.extensionsUsed;
2926
+
2927
+ const extensionDef = {};
2928
+
2929
+ extensionDef.thicknessFactor = material.thickness;
2930
+
2931
+ if ( material.thicknessMap ) {
2932
+
2933
+ const thicknessMapDef = {
2934
+ index: await writer.processTextureAsync( material.thicknessMap ),
2935
+ texCoord: material.thicknessMap.channel
2936
+ };
2937
+ writer.applyTextureTransform( thicknessMapDef, material.thicknessMap );
2938
+ extensionDef.thicknessTexture = thicknessMapDef;
2939
+
2940
+ }
2941
+
2942
+ if ( material.attenuationDistance !== Infinity ) {
2943
+
2944
+ extensionDef.attenuationDistance = material.attenuationDistance;
2945
+
2946
+ }
2947
+
2948
+ extensionDef.attenuationColor = material.attenuationColor.toArray();
2949
+
2950
+ materialDef.extensions = materialDef.extensions || {};
2951
+ materialDef.extensions[ this.name ] = extensionDef;
2952
+
2953
+ extensionsUsed[ this.name ] = true;
2954
+
2955
+ }
2956
+
2957
+ }
2958
+
2959
+ /**
2960
+ * Materials ior Extension
2961
+ *
2962
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_ior
2963
+ *
2964
+ * @private
2965
+ */
2966
+ class GLTFMaterialsIorExtension {
2967
+
2968
+ constructor( writer ) {
2969
+
2970
+ this.writer = writer;
2971
+ this.name = 'KHR_materials_ior';
2972
+
2973
+ }
2974
+
2975
+ async writeMaterialAsync( material, materialDef ) {
2976
+
2977
+ if ( ! material.isMeshPhysicalMaterial || material.ior === 1.5 ) return;
2978
+
2979
+ const writer = this.writer;
2980
+ const extensionsUsed = writer.extensionsUsed;
2981
+
2982
+ const extensionDef = {};
2983
+
2984
+ extensionDef.ior = material.ior;
2985
+
2986
+ materialDef.extensions = materialDef.extensions || {};
2987
+ materialDef.extensions[ this.name ] = extensionDef;
2988
+
2989
+ extensionsUsed[ this.name ] = true;
2990
+
2991
+ }
2992
+
2993
+ }
2994
+
2995
+ /**
2996
+ * Materials specular Extension
2997
+ *
2998
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_specular
2999
+ *
3000
+ * @private
3001
+ */
3002
+ class GLTFMaterialsSpecularExtension {
3003
+
3004
+ constructor( writer ) {
3005
+
3006
+ this.writer = writer;
3007
+ this.name = 'KHR_materials_specular';
3008
+
3009
+ }
3010
+
3011
+ async writeMaterialAsync( material, materialDef ) {
3012
+
3013
+ if ( ! material.isMeshPhysicalMaterial || ( material.specularIntensity === 1.0 &&
3014
+ material.specularColor.equals( DEFAULT_SPECULAR_COLOR ) &&
3015
+ ! material.specularIntensityMap && ! material.specularColorMap ) ) return;
3016
+
3017
+ const writer = this.writer;
3018
+ const extensionsUsed = writer.extensionsUsed;
3019
+
3020
+ const extensionDef = {};
3021
+
3022
+ if ( material.specularIntensityMap ) {
3023
+
3024
+ const specularIntensityMapDef = {
3025
+ index: await writer.processTextureAsync( material.specularIntensityMap ),
3026
+ texCoord: material.specularIntensityMap.channel
3027
+ };
3028
+ writer.applyTextureTransform( specularIntensityMapDef, material.specularIntensityMap );
3029
+ extensionDef.specularTexture = specularIntensityMapDef;
3030
+
3031
+ }
3032
+
3033
+ if ( material.specularColorMap ) {
3034
+
3035
+ const specularColorMapDef = {
3036
+ index: await writer.processTextureAsync( material.specularColorMap ),
3037
+ texCoord: material.specularColorMap.channel
3038
+ };
3039
+ writer.applyTextureTransform( specularColorMapDef, material.specularColorMap );
3040
+ extensionDef.specularColorTexture = specularColorMapDef;
3041
+
3042
+ }
3043
+
3044
+ extensionDef.specularFactor = material.specularIntensity;
3045
+ extensionDef.specularColorFactor = material.specularColor.toArray();
3046
+
3047
+ materialDef.extensions = materialDef.extensions || {};
3048
+ materialDef.extensions[ this.name ] = extensionDef;
3049
+
3050
+ extensionsUsed[ this.name ] = true;
3051
+
3052
+ }
3053
+
3054
+ }
3055
+
3056
+ /**
3057
+ * Sheen Materials Extension
3058
+ *
3059
+ * Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_sheen
3060
+ *
3061
+ * @private
3062
+ */
3063
+ class GLTFMaterialsSheenExtension {
3064
+
3065
+ constructor( writer ) {
3066
+
3067
+ this.writer = writer;
3068
+ this.name = 'KHR_materials_sheen';
3069
+
3070
+ }
3071
+
3072
+ async writeMaterialAsync( material, materialDef ) {
3073
+
3074
+ if ( ! material.isMeshPhysicalMaterial || material.sheen == 0.0 ) return;
3075
+
3076
+ const writer = this.writer;
3077
+ const extensionsUsed = writer.extensionsUsed;
3078
+
3079
+ const extensionDef = {};
3080
+
3081
+ if ( material.sheenRoughnessMap ) {
3082
+
3083
+ const sheenRoughnessMapDef = {
3084
+ index: await writer.processTextureAsync( material.sheenRoughnessMap ),
3085
+ texCoord: material.sheenRoughnessMap.channel
3086
+ };
3087
+ writer.applyTextureTransform( sheenRoughnessMapDef, material.sheenRoughnessMap );
3088
+ extensionDef.sheenRoughnessTexture = sheenRoughnessMapDef;
3089
+
3090
+ }
3091
+
3092
+ if ( material.sheenColorMap ) {
3093
+
3094
+ const sheenColorMapDef = {
3095
+ index: await writer.processTextureAsync( material.sheenColorMap ),
3096
+ texCoord: material.sheenColorMap.channel
3097
+ };
3098
+ writer.applyTextureTransform( sheenColorMapDef, material.sheenColorMap );
3099
+ extensionDef.sheenColorTexture = sheenColorMapDef;
3100
+
3101
+ }
3102
+
3103
+ extensionDef.sheenRoughnessFactor = material.sheenRoughness;
3104
+ extensionDef.sheenColorFactor = material.sheenColor.toArray();
3105
+
3106
+ materialDef.extensions = materialDef.extensions || {};
3107
+ materialDef.extensions[ this.name ] = extensionDef;
3108
+
3109
+ extensionsUsed[ this.name ] = true;
3110
+
3111
+ }
3112
+
3113
+ }
3114
+
3115
+ /**
3116
+ * Anisotropy Materials Extension
3117
+ *
3118
+ * Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_anisotropy
3119
+ *
3120
+ * @private
3121
+ */
3122
+ class GLTFMaterialsAnisotropyExtension {
3123
+
3124
+ constructor( writer ) {
3125
+
3126
+ this.writer = writer;
3127
+ this.name = 'KHR_materials_anisotropy';
3128
+
3129
+ }
3130
+
3131
+ async writeMaterialAsync( material, materialDef ) {
3132
+
3133
+ if ( ! material.isMeshPhysicalMaterial || material.anisotropy == 0.0 ) return;
3134
+
3135
+ const writer = this.writer;
3136
+ const extensionsUsed = writer.extensionsUsed;
3137
+
3138
+ const extensionDef = {};
3139
+
3140
+ if ( material.anisotropyMap ) {
3141
+
3142
+ const anisotropyMapDef = { index: await writer.processTextureAsync( material.anisotropyMap ) };
3143
+ writer.applyTextureTransform( anisotropyMapDef, material.anisotropyMap );
3144
+ extensionDef.anisotropyTexture = anisotropyMapDef;
3145
+
3146
+ }
3147
+
3148
+ extensionDef.anisotropyStrength = material.anisotropy;
3149
+ extensionDef.anisotropyRotation = material.anisotropyRotation;
3150
+
3151
+ materialDef.extensions = materialDef.extensions || {};
3152
+ materialDef.extensions[ this.name ] = extensionDef;
3153
+
3154
+ extensionsUsed[ this.name ] = true;
3155
+
3156
+ }
3157
+
3158
+ }
3159
+
3160
+ /**
3161
+ * Materials Emissive Strength Extension
3162
+ *
3163
+ * Specification: https://github.com/KhronosGroup/glTF/blob/5768b3ce0ef32bc39cdf1bef10b948586635ead3/extensions/2.0/Khronos/KHR_materials_emissive_strength/README.md
3164
+ *
3165
+ * @private
3166
+ */
3167
+ class GLTFMaterialsEmissiveStrengthExtension {
3168
+
3169
+ constructor( writer ) {
3170
+
3171
+ this.writer = writer;
3172
+ this.name = 'KHR_materials_emissive_strength';
3173
+
3174
+ }
3175
+
3176
+ async writeMaterialAsync( material, materialDef ) {
3177
+
3178
+ if ( ! material.isMeshStandardMaterial || material.emissiveIntensity === 1.0 ) return;
3179
+
3180
+ const writer = this.writer;
3181
+ const extensionsUsed = writer.extensionsUsed;
3182
+
3183
+ const extensionDef = {};
3184
+
3185
+ extensionDef.emissiveStrength = material.emissiveIntensity;
3186
+
3187
+ materialDef.extensions = materialDef.extensions || {};
3188
+ materialDef.extensions[ this.name ] = extensionDef;
3189
+
3190
+ extensionsUsed[ this.name ] = true;
3191
+
3192
+ }
3193
+
3194
+ }
3195
+
3196
+
3197
+ /**
3198
+ * Materials bump Extension
3199
+ *
3200
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/EXT_materials_bump
3201
+ *
3202
+ * @private
3203
+ */
3204
+ class GLTFMaterialsBumpExtension {
3205
+
3206
+ constructor( writer ) {
3207
+
3208
+ this.writer = writer;
3209
+ this.name = 'EXT_materials_bump';
3210
+
3211
+ }
3212
+
3213
+ async writeMaterialAsync( material, materialDef ) {
3214
+
3215
+ if ( ! material.isMeshStandardMaterial || (
3216
+ material.bumpScale === 1 &&
3217
+ ! material.bumpMap ) ) return;
3218
+
3219
+ const writer = this.writer;
3220
+ const extensionsUsed = writer.extensionsUsed;
3221
+
3222
+ const extensionDef = {};
3223
+
3224
+ if ( material.bumpMap ) {
3225
+
3226
+ const bumpMapDef = {
3227
+ index: await writer.processTextureAsync( material.bumpMap ),
3228
+ texCoord: material.bumpMap.channel
3229
+ };
3230
+ writer.applyTextureTransform( bumpMapDef, material.bumpMap );
3231
+ extensionDef.bumpTexture = bumpMapDef;
3232
+
3233
+ }
3234
+
3235
+ extensionDef.bumpFactor = material.bumpScale;
3236
+
3237
+ materialDef.extensions = materialDef.extensions || {};
3238
+ materialDef.extensions[ this.name ] = extensionDef;
3239
+
3240
+ extensionsUsed[ this.name ] = true;
3241
+
3242
+ }
3243
+
3244
+ }
3245
+
3246
+ /**
3247
+ * GPU Instancing Extension
3248
+ *
3249
+ * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_mesh_gpu_instancing
3250
+ *
3251
+ * @private
3252
+ */
3253
+ class GLTFMeshGpuInstancing {
3254
+
3255
+ constructor( writer ) {
3256
+
3257
+ this.writer = writer;
3258
+ this.name = 'EXT_mesh_gpu_instancing';
3259
+
3260
+ }
3261
+
3262
+ writeNode( object, nodeDef ) {
3263
+
3264
+ if ( ! object.isInstancedMesh ) return;
3265
+
3266
+ const writer = this.writer;
3267
+
3268
+ const mesh = object;
3269
+
3270
+ const translationAttr = new Float32Array( mesh.count * 3 );
3271
+ const rotationAttr = new Float32Array( mesh.count * 4 );
3272
+ const scaleAttr = new Float32Array( mesh.count * 3 );
3273
+
3274
+ const matrix = new Matrix4();
3275
+ const position = new Vector3();
3276
+ const quaternion = new Quaternion();
3277
+ const scale = new Vector3();
3278
+
3279
+ for ( let i = 0; i < mesh.count; i ++ ) {
3280
+
3281
+ mesh.getMatrixAt( i, matrix );
3282
+ matrix.decompose( position, quaternion, scale );
3283
+
3284
+ position.toArray( translationAttr, i * 3 );
3285
+ quaternion.toArray( rotationAttr, i * 4 );
3286
+ scale.toArray( scaleAttr, i * 3 );
3287
+
3288
+ }
3289
+
3290
+ const attributes = {
3291
+ TRANSLATION: writer.processAccessor( new BufferAttribute( translationAttr, 3 ) ),
3292
+ ROTATION: writer.processAccessor( new BufferAttribute( rotationAttr, 4 ) ),
3293
+ SCALE: writer.processAccessor( new BufferAttribute( scaleAttr, 3 ) ),
3294
+ };
3295
+
3296
+ if ( mesh.instanceColor )
3297
+ attributes._COLOR_0 = writer.processAccessor( mesh.instanceColor );
3298
+
3299
+ nodeDef.extensions = nodeDef.extensions || {};
3300
+ nodeDef.extensions[ this.name ] = { attributes };
3301
+
3302
+ writer.extensionsUsed[ this.name ] = true;
3303
+ writer.extensionsRequired[ this.name ] = true;
3304
+
3305
+ }
3306
+
3307
+ }
3308
+
3309
+ /**
3310
+ * Static utility functions
3311
+ *
3312
+ * @private
3313
+ */
3314
+ GLTFExporter.Utils = {
3315
+
3316
+ insertKeyframe: function ( track, time ) {
3317
+
3318
+ const tolerance = 0.001; // 1ms
3319
+ const valueSize = track.getValueSize();
3320
+
3321
+ const times = new track.TimeBufferType( track.times.length + 1 );
3322
+ const values = new track.ValueBufferType( track.values.length + valueSize );
3323
+ const interpolant = track.createInterpolant( new track.ValueBufferType( valueSize ) );
3324
+
3325
+ let index;
3326
+
3327
+ if ( track.times.length === 0 ) {
3328
+
3329
+ times[ 0 ] = time;
3330
+
3331
+ for ( let i = 0; i < valueSize; i ++ ) {
3332
+
3333
+ values[ i ] = 0;
3334
+
3335
+ }
3336
+
3337
+ index = 0;
3338
+
3339
+ } else if ( time < track.times[ 0 ] ) {
3340
+
3341
+ if ( Math.abs( track.times[ 0 ] - time ) < tolerance ) return 0;
3342
+
3343
+ times[ 0 ] = time;
3344
+ times.set( track.times, 1 );
3345
+
3346
+ values.set( interpolant.evaluate( time ), 0 );
3347
+ values.set( track.values, valueSize );
3348
+
3349
+ index = 0;
3350
+
3351
+ } else if ( time > track.times[ track.times.length - 1 ] ) {
3352
+
3353
+ if ( Math.abs( track.times[ track.times.length - 1 ] - time ) < tolerance ) {
3354
+
3355
+ return track.times.length - 1;
3356
+
3357
+ }
3358
+
3359
+ times[ times.length - 1 ] = time;
3360
+ times.set( track.times, 0 );
3361
+
3362
+ values.set( track.values, 0 );
3363
+ values.set( interpolant.evaluate( time ), track.values.length );
3364
+
3365
+ index = times.length - 1;
3366
+
3367
+ } else {
3368
+
3369
+ for ( let i = 0; i < track.times.length; i ++ ) {
3370
+
3371
+ if ( Math.abs( track.times[ i ] - time ) < tolerance ) return i;
3372
+
3373
+ if ( track.times[ i ] < time && track.times[ i + 1 ] > time ) {
3374
+
3375
+ times.set( track.times.slice( 0, i + 1 ), 0 );
3376
+ times[ i + 1 ] = time;
3377
+ times.set( track.times.slice( i + 1 ), i + 2 );
3378
+
3379
+ values.set( track.values.slice( 0, ( i + 1 ) * valueSize ), 0 );
3380
+ values.set( interpolant.evaluate( time ), ( i + 1 ) * valueSize );
3381
+ values.set( track.values.slice( ( i + 1 ) * valueSize ), ( i + 2 ) * valueSize );
3382
+
3383
+ index = i + 1;
3384
+
3385
+ break;
3386
+
3387
+ }
3388
+
3389
+ }
3390
+
3391
+ }
3392
+
3393
+ track.times = times;
3394
+ track.values = values;
3395
+
3396
+ return index;
3397
+
3398
+ },
3399
+
3400
+ mergeMorphTargetTracks: function ( clip, root ) {
3401
+
3402
+ const tracks = [];
3403
+ const mergedTracks = {};
3404
+ const sourceTracks = clip.tracks;
3405
+
3406
+ for ( let i = 0; i < sourceTracks.length; ++ i ) {
3407
+
3408
+ let sourceTrack = sourceTracks[ i ];
3409
+ const sourceTrackBinding = PropertyBinding.parseTrackName( sourceTrack.name );
3410
+ const sourceTrackNode = PropertyBinding.findNode( root, sourceTrackBinding.nodeName );
3411
+
3412
+ if ( sourceTrackBinding.propertyName !== 'morphTargetInfluences' || sourceTrackBinding.propertyIndex === undefined ) {
3413
+
3414
+ // Tracks that don't affect morph targets, or that affect all morph targets together, can be left as-is.
3415
+ tracks.push( sourceTrack );
3416
+ continue;
3417
+
3418
+ }
3419
+
3420
+ if ( sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodDiscrete
3421
+ && sourceTrack.createInterpolant !== sourceTrack.InterpolantFactoryMethodLinear ) {
3422
+
3423
+ if ( sourceTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) {
3424
+
3425
+ // This should never happen, because glTF morph target animations
3426
+ // affect all targets already.
3427
+ throw new Error( 'THREE.GLTFExporter: Cannot merge tracks with glTF CUBICSPLINE interpolation.' );
3428
+
3429
+ }
3430
+
3431
+ console.warn( 'THREE.GLTFExporter: Morph target interpolation mode not yet supported. Using LINEAR instead.' );
3432
+
3433
+ sourceTrack = sourceTrack.clone();
3434
+ sourceTrack.setInterpolation( InterpolateLinear );
3435
+
3436
+ }
3437
+
3438
+ const targetCount = sourceTrackNode.morphTargetInfluences.length;
3439
+ const targetIndex = sourceTrackNode.morphTargetDictionary[ sourceTrackBinding.propertyIndex ];
3440
+
3441
+ if ( targetIndex === undefined ) {
3442
+
3443
+ throw new Error( 'THREE.GLTFExporter: Morph target name not found: ' + sourceTrackBinding.propertyIndex );
3444
+
3445
+ }
3446
+
3447
+ let mergedTrack;
3448
+
3449
+ // If this is the first time we've seen this object, create a new
3450
+ // track to store merged keyframe data for each morph target.
3451
+ if ( mergedTracks[ sourceTrackNode.uuid ] === undefined ) {
3452
+
3453
+ mergedTrack = sourceTrack.clone();
3454
+
3455
+ const values = new mergedTrack.ValueBufferType( targetCount * mergedTrack.times.length );
3456
+
3457
+ for ( let j = 0; j < mergedTrack.times.length; j ++ ) {
3458
+
3459
+ values[ j * targetCount + targetIndex ] = mergedTrack.values[ j ];
3460
+
3461
+ }
3462
+
3463
+ // We need to take into consideration the intended target node
3464
+ // of our original un-merged morphTarget animation.
3465
+ mergedTrack.name = ( sourceTrackBinding.nodeName || '' ) + '.morphTargetInfluences';
3466
+ mergedTrack.values = values;
3467
+
3468
+ mergedTracks[ sourceTrackNode.uuid ] = mergedTrack;
3469
+ tracks.push( mergedTrack );
3470
+
3471
+ continue;
3472
+
3473
+ }
3474
+
3475
+ const sourceInterpolant = sourceTrack.createInterpolant( new sourceTrack.ValueBufferType( 1 ) );
3476
+
3477
+ mergedTrack = mergedTracks[ sourceTrackNode.uuid ];
3478
+
3479
+ // For every existing keyframe of the merged track, write a (possibly
3480
+ // interpolated) value from the source track.
3481
+ for ( let j = 0; j < mergedTrack.times.length; j ++ ) {
3482
+
3483
+ mergedTrack.values[ j * targetCount + targetIndex ] = sourceInterpolant.evaluate( mergedTrack.times[ j ] );
3484
+
3485
+ }
3486
+
3487
+ // For every existing keyframe of the source track, write a (possibly
3488
+ // new) keyframe to the merged track. Values from the previous loop may
3489
+ // be written again, but keyframes are de-duplicated.
3490
+ for ( let j = 0; j < sourceTrack.times.length; j ++ ) {
3491
+
3492
+ const keyframeIndex = this.insertKeyframe( mergedTrack, sourceTrack.times[ j ] );
3493
+ mergedTrack.values[ keyframeIndex * targetCount + targetIndex ] = sourceTrack.values[ j ];
3494
+
3495
+ }
3496
+
3497
+ }
3498
+
3499
+ clip.tracks = tracks;
3500
+
3501
+ return clip;
3502
+
3503
+ },
3504
+
3505
+ toFloat32BufferAttribute: function ( srcAttribute ) {
3506
+
3507
+ const dstAttribute = new BufferAttribute( new Float32Array( srcAttribute.count * srcAttribute.itemSize ), srcAttribute.itemSize, false );
3508
+
3509
+ if ( ! srcAttribute.normalized && ! srcAttribute.isInterleavedBufferAttribute ) {
3510
+
3511
+ dstAttribute.array.set( srcAttribute.array );
3512
+
3513
+ return dstAttribute;
3514
+
3515
+ }
3516
+
3517
+ for ( let i = 0, il = srcAttribute.count; i < il; i ++ ) {
3518
+
3519
+ for ( let j = 0; j < srcAttribute.itemSize; j ++ ) {
3520
+
3521
+ dstAttribute.setComponent( i, j, srcAttribute.getComponent( i, j ) );
3522
+
3523
+ }
3524
+
3525
+ }
3526
+
3527
+ return dstAttribute;
3528
+
3529
+ }
3530
+
3531
+ };
3532
+
3533
+ export { GLTFExporter };