1 |
- !(function(e,t){var r=[],a=e.BABYLON||this.BABYLON;"object"==typeof exports&&"object"==typeof module?(a=a||require("babylonjs"),module.exports=t(a)):"function"==typeof define&&define.amd?(r.push("babylonjs"),define("babylonjs-serializers",r,t)):"object"==typeof exports?(a=a||require("babylonjs"),exports["babylonjs-serializers"]=t(a)):e.BABYLON=t(a)})(this,(function(e){e=e||this.BABYLON;var t,b,r,i,a,z,n,o,L,s,l,R,m,u,c;this&&this.__decorate,this&&this.__extends||(t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])});return b=e||(e={}),r=(function(){function e(){}return e.OBJ=function(e,t,r,a){var i=[],n=1;t&&(r||(r="mat"),i.push("mtllib "+r+".mtl"));for(var o=0;o<e.length;o++){i.push("g object"+o),i.push("o object_"+o);var s=null;if(a){var l=b.Matrix.Translation(e[o].position.x,e[o].position.y,e[o].position.z);s=b.Matrix.Translation(-e[o].position.x,-e[o].position.y,-e[o].position.z),e[o].bakeTransformIntoVertices(l)}if(t){var u=e[o].material;u&&i.push("usemtl "+u.id)}var c=e[o].geometry;if(c){var h=c.getVerticesData("position"),f=c.getVerticesData("normal"),p=c.getVerticesData("uv"),m=c.getIndices(),d=0;if(h&&m){for(var g=0;g<h.length;g+=3)i.push("v "+h[g]+" "+h[g+1]+" "+h[g+2]),d++;if(null!=f)for(g=0;g<f.length;g+=3)i.push("vn "+f[g]+" "+f[g+1]+" "+f[g+2]);if(null!=p)for(g=0;g<p.length;g+=2)i.push("vt "+p[g]+" "+p[g+1]);for(g=0;g<m.length;g+=3){var _=[String(m[g+2]+n),String(m[g+1]+n),String(m[g]+n)],y=["","",""],T=_,x=null!=p?_:y,v=null!=f?_:y;i.push("f "+T[0]+"/"+x[0]+"/"+v[0]+" "+T[1]+"/"+x[1]+"/"+v[1]+" "+T[2]+"/"+x[2]+"/"+v[2])}a&&s&&e[o].bakeTransformIntoVertices(s),n+=d}else b.Tools.Warn("There are no position vertices or indices on the mesh!")}else b.Tools.Warn("No geometry is present on the mesh")}return i.join("\n")},e.MTL=function(e){var t=[],r=e.material;t.push("newmtl mat1"),t.push(" Ns "+r.specularPower.toFixed(4)),t.push(" Ni 1.5000"),t.push(" d "+r.alpha.toFixed(4)),t.push(" Tr 0.0000"),t.push(" Tf 1.0000 1.0000 1.0000"),t.push(" illum 2"),t.push(" Ka "+r.ambientColor.r.toFixed(4)+" "+r.ambientColor.g.toFixed(4)+" "+r.ambientColor.b.toFixed(4)),t.push(" Kd "+r.diffuseColor.r.toFixed(4)+" "+r.diffuseColor.g.toFixed(4)+" "+r.diffuseColor.b.toFixed(4)),t.push(" Ks "+r.specularColor.r.toFixed(4)+" "+r.specularColor.g.toFixed(4)+" "+r.specularColor.b.toFixed(4)),t.push(" Ke "+r.emissiveColor.r.toFixed(4)+" "+r.emissiveColor.g.toFixed(4)+" "+r.emissiveColor.b.toFixed(4));return r.ambientTexture&&t.push(" map_Ka "+r.ambientTexture.name),r.diffuseTexture&&t.push(" map_Kd "+r.diffuseTexture.name),r.specularTexture&&t.push(" map_Ks "+r.specularTexture.name),r.bumpTexture&&t.push(" map_bump -imfchan z "+r.bumpTexture.name),r.opacityTexture&&t.push(" map_d "+r.opacityTexture.name),t.join("\n")},e})(),b.OBJExport=r,i=e||(e={}),a=(function(){function e(){}return e.GLTFAsync=function(t,r,a){return t.whenReadyAsync().then((function(){var e=r.replace(/\.[^/.]+$/,"");return new i.GLTF2._Exporter(t,a)._generateGLTFAsync(e)}))},e.GLBAsync=function(t,r,a){return t.whenReadyAsync().then((function(){var e=r.replace(/\.[^/.]+$/,"");return new i.GLTF2._Exporter(t,a)._generateGLBAsync(e)}))},e})(),i.GLTF2Export=a,(function(K){var e=(function(){function e(e,t){this._asset={generator:"BabylonJS",version:"2.0"},this._babylonScene=e,this._bufferViews=[],this._accessors=[],this._meshes=[],this._scenes=[],this._nodes=[],this._images=[],this._materials=[],this._materialMap=[],this._textures=[],this._samplers=[],this._animations=[],this._imageData={},this._convertToRightHandedSystem=!this._babylonScene.useRightHandedSystem;var r=t||{};this._shouldExportTransformNode=r.shouldExportTransformNode?r.shouldExportTransformNode:function(e){return!0},this._animationSampleRate=r.animationSampleRate?r.animationSampleRate:1/60,this._glTFMaterialExporter=new K._GLTFMaterialExporter(this)}return e.prototype._getLocalEngine=function(){if(!this._localEngine){var e=document.createElement("canvas");e.id="WriteCanvas",e.width=2048,e.height=2048,this._localEngine=new z.Engine(e,!0,{premultipliedAlpha:!1,preserveDrawingBuffer:!0}),this._localEngine.setViewport(new z.Viewport(0,0,1,1))}return this._localEngine},e.prototype.reorderIndicesBasedOnPrimitiveMode=function(e,t,r,a,i){switch(t){case z.Material.TriangleFillMode:a||(a=0);for(var n=e.indexStart,o=e.indexStart+e.indexCount;n<o;n+=3){var s=a+4*n,l=i.getUInt32(s+4),u=i.getUInt32(s+8);i.setUInt32(u,s+4),i.setUInt32(l,s+8)}break;case z.Material.TriangleFanDrawMode:n=e.indexStart+e.indexCount-1;for(var c=e.indexStart;c<=n;--n)i.setUInt32(r[n],a),a+=4;break;case z.Material.TriangleStripDrawMode:3<=e.indexCount&&(i.setUInt32(r[e.indexStart+2],a+4),i.setUInt32(r[e.indexStart+1],a+8))}},e.prototype.reorderVertexAttributeDataBasedOnPrimitiveMode=function(e,t,r,a,i,n,o){if(this._convertToRightHandedSystem&&r===z.Material.ClockWiseSideOrientation)switch(t){case z.Material.TriangleFillMode:this.reorderTriangleFillMode(e,t,r,a,i,n,o);break;case z.Material.TriangleStripDrawMode:this.reorderTriangleStripDrawMode(e,t,r,a,i,n,o);break;case z.Material.TriangleFanDrawMode:this.reorderTriangleFanMode(e,t,r,a,i,n,o)}},e.prototype.reorderTriangleFillMode=function(e,t,r,a,i,n,o){var s=this.getVertexBufferFromMesh(a,e.getMesh());if(s){var l=s.byteStride/z.VertexBuffer.GetTypeByteLength(s.type);if(e.verticesCount%3!=0)z.Tools.Error("The submesh vertices for the triangle fill mode is not divisible by 3!");else{var u=[],c=0;switch(a){case z.VertexBuffer.PositionKind:case z.VertexBuffer.NormalKind:for(var h=e.verticesStart;h<e.verticesStart+e.verticesCount;h+=3)c=h*l,u.push(z.Vector3.FromArray(i,c)),u.push(z.Vector3.FromArray(i,c+2*l)),u.push(z.Vector3.FromArray(i,c+l));break;case z.VertexBuffer.TangentKind:for(h=e.verticesStart;h<e.verticesStart+e.verticesCount;h+=3)c=h*l,u.push(z.Vector4.FromArray(i,c)),u.push(z.Vector4.FromArray(i,c+2*l)),u.push(z.Vector4.FromArray(i,c+l));break;case z.VertexBuffer.ColorKind:var f=s.getSize();for(h=e.verticesStart;h<e.verticesStart+e.verticesCount;h+=f)c=h*l,4===f?(u.push(z.Vector4.FromArray(i,c)),u.push(z.Vector4.FromArray(i,c+2*l)),u.push(z.Vector4.FromArray(i,c+l))):(u.push(z.Vector3.FromArray(i,c)),u.push(z.Vector3.FromArray(i,c+2*l)),u.push(z.Vector3.FromArray(i,c+l)));break;case z.VertexBuffer.UVKind:case z.VertexBuffer.UV2Kind:for(h=e.verticesStart;h<e.verticesStart+e.verticesCount;h+=3)c=h*l,u.push(z.Vector2.FromArray(i,c)),u.push(z.Vector2.FromArray(i,c+2*l)),u.push(z.Vector2.FromArray(i,c+l));break;default:z.Tools.Error("Unsupported Vertex Buffer type: "+a)}this.writeVertexAttributeData(u,n,a,i,o)}}else z.Tools.Warn("reorderTriangleFillMode: Vertex Buffer Kind "+a+" not present!")},e.prototype.reorderTriangleStripDrawMode=function(e,t,r,a,i,n,o){var s=this.getVertexBufferFromMesh(a,e.getMesh());if(s){var l=s.byteStride/z.VertexBuffer.GetTypeByteLength(s.type),u=[],c=0;switch(a){case z.VertexBuffer.PositionKind:case z.VertexBuffer.NormalKind:c=e.verticesStart,u.push(z.Vector3.FromArray(i,c+2*l)),u.push(z.Vector3.FromArray(i,c+l));break;case z.VertexBuffer.TangentKind:for(var h=e.verticesStart+e.verticesCount-1;h>=e.verticesStart;--h)c=h*l,u.push(z.Vector4.FromArray(i,c));break;case z.VertexBuffer.ColorKind:for(h=e.verticesStart+e.verticesCount-1;h>=e.verticesStart;--h)c=h*l,4===s.getSize()?u.push(z.Vector4.FromArray(i,c)):u.push(z.Vector3.FromArray(i,c));break;case z.VertexBuffer.UVKind:case z.VertexBuffer.UV2Kind:for(h=e.verticesStart+e.verticesCount-1;h>=e.verticesStart;--h)c=h*l,u.push(z.Vector2.FromArray(i,c));break;default:z.Tools.Error("Unsupported Vertex Buffer type: "+a)}this.writeVertexAttributeData(u,n+12,a,i,o)}else z.Tools.Warn("reorderTriangleStripDrawMode: Vertex buffer kind "+a+" not present!")},e.prototype.reorderTriangleFanMode=function(e,t,r,a,i,n,o){var s=this.getVertexBufferFromMesh(a,e.getMesh());if(s){var l=s.byteStride/z.VertexBuffer.GetTypeByteLength(s.type),u=[],c=0;switch(a){case z.VertexBuffer.PositionKind:case z.VertexBuffer.NormalKind:for(var h=e.verticesStart+e.verticesCount-1;h>=e.verticesStart;--h)c=h*l,u.push(z.Vector3.FromArray(i,c));break;case z.VertexBuffer.TangentKind:for(h=e.verticesStart+e.verticesCount-1;h>=e.verticesStart;--h)c=h*l,u.push(z.Vector4.FromArray(i,c));break;case z.VertexBuffer.ColorKind:for(h=e.verticesStart+e.verticesCount-1;h>=e.verticesStart;--h)c=h*l,u.push(z.Vector4.FromArray(i,c)),4===s.getSize()?u.push(z.Vector4.FromArray(i,c)):u.push(z.Vector3.FromArray(i,c));break;case z.VertexBuffer.UVKind:case z.VertexBuffer.UV2Kind:for(h=e.verticesStart+e.verticesCount-1;h>=e.verticesStart;--h)c=h*l,u.push(z.Vector2.FromArray(i,c));break;default:z.Tools.Error("Unsupported Vertex Buffer type: "+a)}this.writeVertexAttributeData(u,n,a,i,o)}else z.Tools.Warn("reorderTriangleFanMode: Vertex buffer kind "+a+" not present!")},e.prototype.writeVertexAttributeData=function(e,t,r,a,i){for(var n=0,o=e;n<o.length;n++){var s=o[n];!this._convertToRightHandedSystem||r===z.VertexBuffer.ColorKind||s instanceof z.Vector2||(s instanceof z.Vector3?r===z.VertexBuffer.NormalKind?K._GLTFUtilities._GetRightHandedNormalVector3FromRef(s):r===z.VertexBuffer.PositionKind?K._GLTFUtilities._GetRightHandedPositionVector3FromRef(s):z.Tools.Error("Unsupported vertex attribute kind!"):K._GLTFUtilities._GetRightHandedVector4FromRef(s)),r===z.VertexBuffer.NormalKind?s.normalize():r===z.VertexBuffer.TangentKind&&s instanceof z.Vector4&&K._GLTFUtilities._NormalizeTangentFromRef(s);for(var l=0,u=s.asArray();l<u.length;l++){var c=u[l];i.setFloat32(c,t),t+=4}}},e.prototype.writeAttributeData=function(e,t,r,a){var i,n=r/4,o=[];switch(e){case z.VertexBuffer.PositionKind:for(var s=0,l=t.length/n;s<l;++s){i=s*n;var u=z.Vector3.FromArray(t,i);this._convertToRightHandedSystem&&K._GLTFUtilities._GetRightHandedPositionVector3FromRef(u),o.push(u.asArray())}break;case z.VertexBuffer.NormalKind:s=0;for(var c=t.length/n;s<c;++s)i=s*n,u=z.Vector3.FromArray(t,i),this._convertToRightHandedSystem&&K._GLTFUtilities._GetRightHandedNormalVector3FromRef(u),u.normalize(),o.push(u.asArray());break;case z.VertexBuffer.TangentKind:s=0;for(var h=t.length/n;s<h;++s)i=s*n,u=z.Vector4.FromArray(t,i),this._convertToRightHandedSystem&&K._GLTFUtilities._GetRightHandedVector4FromRef(u),K._GLTFUtilities._NormalizeTangentFromRef(u),o.push(u.asArray());break;case z.VertexBuffer.ColorKind:s=0;for(var f=t.length/n;s<f;++s)i=s*n,u=3===n?z.Vector3.FromArray(t,i):z.Vector4.FromArray(t,i),o.push(u.asArray());break;case z.VertexBuffer.UVKind:case z.VertexBuffer.UV2Kind:s=0;for(var p=t.length/n;s<p;++s)i=s*n,o.push((this._convertToRightHandedSystem,[t[i],t[i+1]]));break;default:z.Tools.Warn("Unsupported Vertex Buffer Type: "+e),o=[]}for(var m=0,d=o;m<d.length;m++)for(var g=0,_=d[m];g<_.length;g++){var y=_[g];a.setFloat32(y)}},e.prototype.generateJSON=function(e,t,r){var a,i,n,o=this,s={byteLength:this._totalByteLength},l=this._totalByteLength,u={asset:this._asset};return s.byteLength&&(u.buffers=[s]),this._nodes&&this._nodes.length&&(u.nodes=this._nodes),this._meshes&&this._meshes.length&&(u.meshes=this._meshes),this._scenes&&this._scenes.length&&(u.scenes=this._scenes,u.scene=0),this._bufferViews&&this._bufferViews.length&&(u.bufferViews=this._bufferViews),this._accessors&&this._accessors.length&&(u.accessors=this._accessors),this._animations&&this._animations.length&&(u.animations=this._animations),this._materials&&this._materials.length&&(u.materials=this._materials),this._textures&&this._textures.length&&(u.textures=this._textures),this._samplers&&this._samplers.length&&(u.samplers=this._samplers),this._images&&this._images.length&&(e?(u.images=[],this._images.forEach((function(e){e.uri&&(i=o._imageData[e.uri],a=e.uri.split(".")[0]+" image",n=K._GLTFUtilities._CreateBufferView(0,l,i.data.length,void 0,a),l+=i.data.buffer.byteLength,o._bufferViews.push(n),e.bufferView=o._bufferViews.length-1,e.name=a,e.mimeType=i.mimeType,e.uri=void 0,u.images||(u.images=[]),u.images.push(e))})),s.byteLength=l):u.images=this._images),e||(s.uri=t+".bin"),r?JSON.stringify(u,null,2):JSON.stringify(u)},e.prototype._generateGLTFAsync=function(s){var l=this;return this._generateBinaryAsync().then((function(e){var t=l.generateJSON(!1,s,!0),r=new Blob([e],{type:"application/octet-stream"}),a=s+".gltf",i=s+".bin",n=new z.GLTFData;if(n.glTFFiles[a]=t,n.glTFFiles[i]=r,l._imageData)for(var o in l._imageData)n.glTFFiles[o]=new Blob([l._imageData[o].data],{type:l._imageData[o].mimeType});return n}))},e.prototype._generateBinaryAsync=function(){var e=this,t=new r(4);return this.createSceneAsync(this._babylonScene,t).then((function(){return e._localEngine&&e._localEngine.dispose(),t.getArrayBuffer()}))},e.prototype._getPadding=function(e){var t=e%4;return 0===t?t:4-t},e.prototype._generateGLBAsync=function(R){var V=this;return this._generateBinaryAsync().then((function(e){var t=V.generateJSON(!0),r=R+".glb",a=t.length,i=0;for(var n in V._imageData)i+=V._imageData[n].data.byteLength;var o=V._getPadding(a),s=V._getPadding(e.byteLength),l=V._getPadding(i),u=28+a+o+e.byteLength+s+i+l,c=new ArrayBuffer(12),h=new DataView(c);h.setUint32(0,1179937895,!0),h.setUint32(4,2,!0),h.setUint32(8,u,!0);var f=new ArrayBuffer(8+a+o),p=new DataView(f);p.setUint32(0,a+o,!0),p.setUint32(4,1313821514,!0);for(var m=new Uint8Array(f,8),d=0;d<a;++d)m[d]=t.charCodeAt(d);var g=new Uint8Array(f,8+a);for(d=0;d<o;++d)g[d]=32;var _=new ArrayBuffer(8),y=new DataView(_);y.setUint32(0,e.byteLength+i+l,!0),y.setUint32(4,5130562,!0);var T=new ArrayBuffer(s),x=new Uint8Array(T);for(d=0;d<s;++d)x[d]=0;var v=new ArrayBuffer(l),b=new Uint8Array(v);for(d=0;d<l;++d)b[d]=0;var F=[c,f,_,e];for(var n in V._imageData)F.push(V._imageData[n].data.buffer);F.push(T),F.push(v);var A=new Blob(F,{type:"application/octet-stream"}),M=new z.GLTFData;return M.glTFFiles[r]=A,null!=V._localEngine&&V._localEngine.dispose(),M}))},e.prototype.setNodeTransformation=function(e,t){t.getPivotPoint().equalsToFloats(0,0,0)||z.Tools.Warn("Pivot points are not supported in the glTF serializer"),t.position.equalsToFloats(0,0,0)||(e.translation=this._convertToRightHandedSystem?K._GLTFUtilities._GetRightHandedPositionVector3(t.position).asArray():t.position.asArray()),t.scaling.equalsToFloats(1,1,1)||(e.scale=t.scaling.asArray());var r=z.Quaternion.RotationYawPitchRoll(t.rotation.y,t.rotation.x,t.rotation.z);t.rotationQuaternion&&r.multiplyInPlace(t.rotationQuaternion),0===r.x&&0===r.y&&0===r.z&&1===r.w||(this._convertToRightHandedSystem&&K._GLTFUtilities._GetRightHandedQuaternionFromRef(r),e.rotation=r.normalize().asArray())},e.prototype.getVertexBufferFromMesh=function(e,t){if(t.isVerticesDataPresent(e)){var r=t.getVertexBuffer(e);if(r)return r}return null},e.prototype.createBufferViewKind=function(e,t,r,a){var i=t instanceof z.Mesh?t:t instanceof z.InstancedMesh?t.sourceMesh:null;if(i){var n=i.getVerticesData(e);if(n){var o=4*n.length,s=K._GLTFUtilities._CreateBufferView(0,r.getByteOffset(),o,a,e+" - "+i.name);this._bufferViews.push(s),this.writeAttributeData(e,n,a,r)}}},e.prototype.getMeshPrimitiveMode=function(e){return e instanceof z.LinesMesh?z.Material.LineListDrawMode:e.material?e.material.fillMode:z.Material.TriangleFillMode},e.prototype.setPrimitiveMode=function(e,t){switch(t){case z.Material.TriangleFillMode:break;case z.Material.TriangleStripDrawMode:e.mode=5;break;case z.Material.TriangleFanDrawMode:e.mode=6;break;case z.Material.PointListDrawMode:e.mode=0;case z.Material.PointFillMode:e.mode=0;break;case z.Material.LineLoopDrawMode:e.mode=2;break;case z.Material.LineListDrawMode:e.mode=1;break;case z.Material.LineStripDrawMode:e.mode=3}},e.prototype.setAttributeKind=function(e,t){switch(t){case z.VertexBuffer.PositionKind:e.attributes.POSITION=this._accessors.length-1;break;case z.VertexBuffer.NormalKind:e.attributes.NORMAL=this._accessors.length-1;break;case z.VertexBuffer.ColorKind:e.attributes.COLOR_0=this._accessors.length-1;break;case z.VertexBuffer.TangentKind:e.attributes.TANGENT=this._accessors.length-1;break;case z.VertexBuffer.UVKind:e.attributes.TEXCOORD_0=this._accessors.length-1;break;case z.VertexBuffer.UV2Kind:e.attributes.TEXCOORD_1=this._accessors.length-1;break;default:z.Tools.Warn("Unsupported Vertex Buffer Type: "+t)}},e.prototype.setPrimitiveAttributes=function(e,t,r){var a,i,n,o=null;t instanceof z.Mesh?o=t:t instanceof z.InstancedMesh&&(o=t.sourceMesh);var s=[{kind:z.VertexBuffer.PositionKind,accessorType:"VEC3",byteStride:12},{kind:z.VertexBuffer.NormalKind,accessorType:"VEC3",byteStride:12},{kind:z.VertexBuffer.ColorKind,accessorType:"VEC4",byteStride:16},{kind:z.VertexBuffer.TangentKind,accessorType:"VEC4",byteStride:16},{kind:z.VertexBuffer.UVKind,accessorType:"VEC2",byteStride:8},{kind:z.VertexBuffer.UV2Kind,accessorType:"VEC2",byteStride:8}];if(o){for(var l=null,u=this.getMeshPrimitiveMode(o),c={},h=0,f=s;h<f.length;h++){var p=(O=f[h]).kind;if(o.isVerticesDataPresent(p)){var m=this.getVertexBufferFromMesh(p,o);O.byteStride=m?4*m.getSize():4*z.VertexBuffer.DeduceStride(p),12===O.byteStride&&(O.accessorType="VEC3"),this.createBufferViewKind(p,t,r,O.byteStride),O.bufferViewIndex=this._bufferViews.length-1,c[p]=O.bufferViewIndex}}if(o.getTotalIndices()){var d=o.getIndices();if(d){var g=4*d.length;a=K._GLTFUtilities._CreateBufferView(0,r.getByteOffset(),g,void 0,"Indices - "+o.name),this._bufferViews.push(a),l=this._bufferViews.length-1;for(var _=0,y=d.length;_<y;++_)r.setUInt32(d[_])}}if(o.subMeshes)for(var T=0,x=o.subMeshes;T<x.length;T++){var v=x[T];i=!1;var b=v.getMaterial()||o.getScene().defaultMaterial,F=null;if(b)if(o instanceof z.LinesMesh){var A={name:o.name+" material"};(!o.color.equals(z.Color3.White())||o.alpha<1)&&(A.pbrMetallicRoughness={baseColorFactor:o.color.asArray().concat([o.alpha])}),this._materials.push(A),F=this._materials.length-1}else if(b instanceof z.MultiMaterial){var M=b.subMaterials[v.materialIndex];M&&(b=M,F=this._materialMap[b.uniqueId])}else F=this._materialMap[b.uniqueId];var R=null!=F?this._materials[F]:null,V={attributes:{}};this.setPrimitiveMode(V,u);for(var S=0,E=s;S<E.length;S++)if(((p=(O=E[S]).kind)!==z.VertexBuffer.UVKind&&p!==z.VertexBuffer.UV2Kind||!R||this._glTFMaterialExporter._hasTexturesPresent(R))&&(U=o.getVerticesData(p))&&(m=this.getVertexBufferFromMesh(p,o))){var C=m.getSize(),w=O.bufferViewIndex;if(null!=w){n={min:null,max:null},p==z.VertexBuffer.PositionKind&&(n=K._GLTFUtilities._CalculateMinMaxPositions(U,0,U.length/C,this._convertToRightHandedSystem));var B=K._GLTFUtilities._CreateAccessor(w,p+" - "+t.name,O.accessorType,5126,U.length/C,0,n.min,n.max);this._accessors.push(B),this.setAttributeKind(V,p),null==V.attributes.TEXCOORD_0&&null==V.attributes.TEXCOORD_1||(i=!0)}}if(l&&(B=K._GLTFUtilities._CreateAccessor(l,"indices - "+t.name,"SCALAR",5125,v.indexCount,4*v.indexStart,null,null),this._accessors.push(B),V.indices=this._accessors.length-1),null!=F&&0<Object.keys(V.attributes).length){var L=b.sideOrientation;if(this._convertToRightHandedSystem&&L===z.Material.ClockWiseSideOrientation){var N=null!=l?this._bufferViews[l].byteOffset:null;null==N&&(N=0);var P=null;if(null!=l&&(P=o.getIndices()),P)this.reorderIndicesBasedOnPrimitiveMode(v,u,P,N,r);else for(var G=0,I=s;G<I.length;G++){var U,O=I[G];if(U=o.getVerticesData(O.kind)){var D=this._bufferViews[c[O.kind]].byteOffset;D||(D=0),this.reorderVertexAttributeDataBasedOnPrimitiveMode(v,u,L,O.kind,U,D,r)}}}if(!i&&this._glTFMaterialExporter._hasTexturesPresent(this._materials[F])){var k=this._glTFMaterialExporter._stripTexturesFromMaterial(this._materials[F]);this._materials.push(k),F=this._materials.length-1}V.material=F}e.primitives.push(V)}}},e.prototype.createSceneAsync=function(s,l){var u,c,h,f=this,p={nodes:[]},m=s.transformNodes.concat(s.meshes);return this._glTFMaterialExporter._convertMaterialsToGLTFAsync(s.materials,"image/png",!0).then((function(){f._nodeMap=f.createNodeMapAndAnimations(s,m,f._shouldExportTransformNode,l),f._totalByteLength=l.getByteOffset();for(var e=0,t=m;e<t.length;e++){var r=t[e];if(null!=(u=f._nodeMap[r.uniqueId])&&(c=f._nodes[u],r.parent||(f._shouldExportTransformNode(r)?(f._convertToRightHandedSystem&&(c.translation&&(c.translation[2]*=-1,c.translation[0]*=-1),c.rotation=c.rotation?z.Quaternion.FromArray([0,1,0,0]).multiply(z.Quaternion.FromArray(c.rotation)).asArray():z.Quaternion.FromArray([0,1,0,0]).asArray()),p.nodes.push(u)):z.Tools.Log("Omitting "+r.name+" from scene.")),h=r.getDescendants(!0),!c.children&&h&&h.length)){for(var a=[],i=0,n=h;i<n.length;i++){var o=n[i];null!=f._nodeMap[o.uniqueId]&&a.push(f._nodeMap[o.uniqueId])}a.length&&(c.children=a)}}p.nodes.length&&f._scenes.push(p)}))},e.prototype.createNodeMapAndAnimations=function(e,t,r,a){for(var i,n,o=this,s={},l={name:"runtime animations",channels:[],samplers:[]},u=[],c=0,h=t;c<h.length;c++){var f=h[c];r(f)?(n=this.createNode(f,a),(f.getDescendants(!0,(function(e){return e instanceof z.TransformNode})).length||null!=n.mesh)&&(this._nodes.push(n),i=this._nodes.length-1,s[f.uniqueId]=i),!e.animationGroups.length&&f.animations.length&&K._GLTFAnimation._CreateNodeAnimationFromTransformNodeAnimations(f,l,u,s,this._nodes,a,this._bufferViews,this._accessors,this._convertToRightHandedSystem,this._animationSampleRate)):f.name}return l.channels.length&&l.samplers.length&&this._animations.push(l),u.forEach((function(e){e.channels.length&&e.samplers.length&&o._animations.push(e)})),e.animationGroups.length&&K._GLTFAnimation._CreateNodeAnimationFromAnimationGroups(e,this._animations,s,this._nodes,a,this._bufferViews,this._accessors,this._convertToRightHandedSystem,this._animationSampleRate),s},e.prototype.createNode=function(e,t){var r={},a={primitives:[]};return e.name&&(r.name=e.name),this.setNodeTransformation(r,e),this.setPrimitiveAttributes(a,e,t),a.primitives.length&&(this._meshes.push(a),r.mesh=this._meshes.length-1),r},e})();K._Exporter=e;var r=(function(){function e(e){this._arrayBuffer=new ArrayBuffer(e),this._dataView=new DataView(this._arrayBuffer),this._byteOffset=0}return e.prototype.resizeBuffer=function(e){for(var t=new ArrayBuffer(e),r=new Uint8Array(this._arrayBuffer),a=new Uint8Array(t),i=0,n=a.byteLength;i<n;++i)a[i]=r[i];this._arrayBuffer=t,this._dataView=new DataView(this._arrayBuffer)},e.prototype.getArrayBuffer=function(){return this.resizeBuffer(this.getByteOffset()),this._arrayBuffer},e.prototype.getByteOffset=function(){return this._byteOffset},e.prototype.setUInt8=function(e,t){null!=t?t<this._byteOffset?this._dataView.setUint8(t,e):z.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(this._byteOffset+1>this._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setUint8(this._byteOffset++,e))},e.prototype.getUInt32=function(e){if(e<this._byteOffset)return this._dataView.getUint32(e,!0);throw z.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"),new Error("BinaryWriter: byteoffset is greater than the current binary buffer length!")},e.prototype.getVector3Float32FromRef=function(e,t){t+8>this._byteOffset?z.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(e.x=this._dataView.getFloat32(t,!0),e.y=this._dataView.getFloat32(t+4,!0),e.z=this._dataView.getFloat32(t+8,!0))},e.prototype.setVector3Float32FromRef=function(e,t){t+8>this._byteOffset?z.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(this._dataView.setFloat32(t,e.x,!0),this._dataView.setFloat32(t+4,e.y,!0),this._dataView.setFloat32(t+8,e.z,!0))},e.prototype.getVector4Float32FromRef=function(e,t){t+12>this._byteOffset?z.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(e.x=this._dataView.getFloat32(t,!0),e.y=this._dataView.getFloat32(t+4,!0),e.z=this._dataView.getFloat32(t+8,!0),e.w=this._dataView.getFloat32(t+12,!0))},e.prototype.setVector4Float32FromRef=function(e,t){t+12>this._byteOffset?z.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(this._dataView.setFloat32(t,e.x,!0),this._dataView.setFloat32(t+4,e.y,!0),this._dataView.setFloat32(t+8,e.z,!0),this._dataView.setFloat32(t+12,e.w,!0))},e.prototype.setFloat32=function(e,t){isNaN(e)&&z.Tools.Error("Invalid data being written!"),null!=t&&(t<this._byteOffset?this._dataView.setFloat32(t,e,!0):z.Tools.Error("BinaryWriter: byteoffset is greater than the current binary length!")),this._byteOffset+4>this._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setFloat32(this._byteOffset,e,!0),this._byteOffset+=4},e.prototype.setUInt32=function(e,t){null!=t?t<this._byteOffset?this._dataView.setUint32(t,e,!0):z.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(this._byteOffset+4>this._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setUint32(this._byteOffset,e,!0),this._byteOffset+=4)},e})();K._BinaryWriter=r})((z=e||(e={})).GLTF2||(z.GLTF2={})),n=e||(e={}),o=(function(){function e(){this.glTFFiles={}}return e.prototype.downloadFiles=function(){function e(e,t){return-1!==e.indexOf(t,e.length-t.length)}for(var t in this.glTFFiles){var r=document.createElement("a");document.body.appendChild(r),r.setAttribute("type","hidden"),r.download=t;var a=this.glTFFiles[t],i=void 0;e(t,".glb")?i={type:"model/gltf-binary"}:e(t,".bin")?i={type:"application/octet-stream"}:e(t,".gltf")?i={type:"model/gltf+json"}:e(t,".jpeg")?i={type:"image/jpeg"}:e(t,".png")&&(i={type:"image/png"}),r.href=window.URL.createObjectURL(new Blob([a],i)),r.click()}},e})(),n.GLTFData=o,L=e||(e={}),s=L.GLTF2||(L.GLTF2={}),l=(function(){function B(e){this._textureMap={},this._textureMap={},this._exporter=e}return B.FuzzyEquals=function(e,t,r){return L.Scalar.WithinEpsilon(e.r,t.r,r)&&L.Scalar.WithinEpsilon(e.g,t.g,r)&&L.Scalar.WithinEpsilon(e.b,t.b,r)},B.prototype._convertMaterialsToGLTFAsync=function(e,t,r){for(var a=[],i=0,n=e;i<n.length;i++){var o=n[i];o instanceof L.StandardMaterial?a.push(this._convertStandardMaterialAsync(o,t,r)):o instanceof L.PBRMetallicRoughnessMaterial?a.push(this._convertPBRMetallicRoughnessMaterialAsync(o,t,r)):o instanceof L.PBRMaterial?a.push(this._convertPBRMaterialAsync(o,t,r)):L.Tools.Warn("Unsupported material type: "+o.name)}return Promise.all(a).then((function(){}))},B.prototype._stripTexturesFromMaterial=function(e){var t={};if(e){t.name=e.name,t.doubleSided=e.doubleSided,t.alphaMode=e.alphaMode,t.alphaCutoff=e.alphaCutoff,t.emissiveFactor=e.emissiveFactor;var r=e.pbrMetallicRoughness;r&&(t.pbrMetallicRoughness={},t.pbrMetallicRoughness.baseColorFactor=r.baseColorFactor,t.pbrMetallicRoughness.metallicFactor=r.metallicFactor,t.pbrMetallicRoughness.roughnessFactor=r.roughnessFactor)}return t},B.prototype._hasTexturesPresent=function(e){if(e.emissiveTexture||e.normalTexture||e.occlusionTexture)return!0;var t=e.pbrMetallicRoughness;return!(!t||!t.baseColorTexture&&!t.metallicRoughnessTexture)},B.prototype._convertToGLTFPBRMetallicRoughness=function(e){var t,r,a,i,n,o,s,l=new L.Vector2(0,1),u=new L.Vector2(0,.1),c=new L.Vector2(0,.1),h=new L.Vector2(1300,.1),f=e.diffuseColor.toLinearSpace().scale(.5),p=e.alpha,m=L.Scalar.Clamp(e.specularPower,0,B._MaxSpecularPower),d=(t=m,s=Math.pow(t/h.x,.333333),r=s,a=l.y,i=u.y,n=c.y,o=h.y,(1-r)*(1-r)*(1-r)*a+3*(1-r)*(1-r)*r*i+3*(1-r)*r*r*n+r*r*r*o);return{baseColorFactor:[f.r,f.g,f.b,p],metallicFactor:0,roughnessFactor:d}},B._SolveMetallic=function(e,t,r){if(t<this._DielectricSpecular.r)return this._DielectricSpecular,0;var a=this._DielectricSpecular.r,i=e*r/(1-this._DielectricSpecular.r)+t-2*this._DielectricSpecular.r,n=i*i-4*a*(this._DielectricSpecular.r-t);return L.Scalar.Clamp((-i+Math.sqrt(n))/(2*a),0,1)},B.prototype._getAlphaMode=function(e){return e.needAlphaBlending()?"BLEND":e.needAlphaTesting()?"MASK":"OPAQUE"},B.prototype._convertStandardMaterialAsync=function(t,e,r){var a=this._exporter._materialMap,i=this._exporter._materials,n=this._getAlphaMode(t),o=[],s=this._convertToGLTFPBRMetallicRoughness(t),l={name:t.name};if(null==t.backFaceCulling||t.backFaceCulling||(t.twoSidedLighting||L.Tools.Warn(t.name+": Back-face culling enabled and two-sided lighting disabled is not supported in glTF."),l.doubleSided=!0),r&&(t.diffuseTexture&&o.push(this._exportTextureAsync(t.diffuseTexture,e).then((function(e){e&&(s.baseColorTexture=e)}))),t.bumpTexture&&o.push(this._exportTextureAsync(t.bumpTexture,e).then((function(e){e&&(l.normalTexture=e,null!=t.bumpTexture&&1!==t.bumpTexture.level&&(l.normalTexture.scale=t.bumpTexture.level))}))),t.emissiveTexture&&(l.emissiveFactor=[1,1,1],o.push(this._exportTextureAsync(t.emissiveTexture,e).then((function(e){e&&(l.emissiveTexture=e)})))),t.ambientTexture&&o.push(this._exportTextureAsync(t.ambientTexture,e).then((function(e){if(e){var t={index:e.index};(l.occlusionTexture=t).strength=1}})))),(t.alpha<1||t.opacityTexture)&&(t.alphaMode===L.Engine.ALPHA_COMBINE?l.alphaMode="BLEND":L.Tools.Warn(t.name+": glTF 2.0 does not support alpha mode: "+t.alphaMode.toString())),t.emissiveColor&&!B.FuzzyEquals(t.emissiveColor,L.Color3.Black(),B._Epsilon)&&(l.emissiveFactor=t.emissiveColor.asArray()),l.pbrMetallicRoughness=s,"OPAQUE"!==n)switch(n){case"BLEND":l.alphaMode="BLEND";break;case"MASK":l.alphaMode="MASK",l.alphaCutoff=t.alphaCutOff;break;default:L.Tools.Warn("Unsupported alpha mode "+n)}return i.push(l),a[t.uniqueId]=i.length-1,Promise.all(o).then((function(){}))},B.prototype._convertPBRMetallicRoughnessMaterialAsync=function(t,e,r){var a=this._exporter._materialMap,i=this._exporter._materials,n=[],o={};t.baseColor&&(o.baseColorFactor=[t.baseColor.r,t.baseColor.g,t.baseColor.b,t.alpha]),null!=t.metallic&&1!==t.metallic&&(o.metallicFactor=t.metallic),null!=t.roughness&&1!==t.roughness&&(o.roughnessFactor=t.roughness);var s={name:t.name};t.doubleSided&&(s.doubleSided=t.doubleSided);var l=null;return null!=t.transparencyMode&&(l=this._getAlphaMode(t))&&"OPAQUE"!==l&&"MASK"===(s.alphaMode=l)&&(s.alphaCutoff=t.alphaCutOff),r&&(null!=t.baseTexture&&n.push(this._exportTextureAsync(t.baseTexture,e).then((function(e){e&&(o.baseColorTexture=e)}))),t.normalTexture&&n.push(this._exportTextureAsync(t.normalTexture,e).then((function(e){e&&(s.normalTexture=e,1!==t.normalTexture.level&&(s.normalTexture.scale=t.normalTexture.level))}))),t.occlusionTexture&&n.push(this._exportTextureAsync(t.occlusionTexture,e).then((function(e){e&&(s.occlusionTexture=e,null!=t.occlusionStrength&&(s.occlusionTexture.strength=t.occlusionStrength))}))),t.emissiveTexture&&n.push(this._exportTextureAsync(t.emissiveTexture,e).then((function(e){e&&(s.emissiveTexture=e)})))),B.FuzzyEquals(t.emissiveColor,L.Color3.Black(),B._Epsilon)&&(s.emissiveFactor=t.emissiveColor.asArray()),s.pbrMetallicRoughness=o,i.push(s),a[t.uniqueId]=i.length-1,Promise.all(n).then((function(){}))},B.prototype._createBase64FromCanvasAsync=function(s,l,u,e){var c=this;return new Promise(function(r,a){var i,e=L.Engine.TEXTURETYPE_UNSIGNED_INT,t=c._exporter._getLocalEngine();i=new L.Scene(t);var n=t.createRawTexture(s,l,u,L.Engine.TEXTUREFORMAT_RGBA,!1,!0,L.Texture.NEAREST_SAMPLINGMODE,null,e),o=new L.PostProcess("pass","pass",null,null,1,null,L.Texture.NEAREST_SAMPLINGMODE,t,!1,void 0,L.Engine.TEXTURETYPE_UNSIGNED_INT,void 0,null,!1);o.getEffect().executeWhenCompiled((function(){o.onApply=function(e){e._bindTexture("textureSampler",n)},t.setSize(l,u),i.postProcessManager.directRender([o],null),o.dispose(),n.dispose();var e=t.getRenderingCanvas();e?L.Tools.ToBlob(e,(function(e){if(e){var t=new FileReader;t.onload=function(e){var t=e.target.result;i.dispose(),r(t)},t.readAsDataURL(e)}else a("Failed to get blob from image canvas!")})):a("Engine is missing a canvas!")}))})},B.prototype._createWhiteTexture=function(e,t,r){for(var a=new Uint8Array(e*t*4),i=0;i<a.length;i+=4)a[i]=a[i+1]=a[i+2]=a[i+3]=255;return L.RawTexture.CreateRGBATexture(a,e,t,r)},B.prototype._resizeTexturesToSameDimensions=function(e,t,r){var a,i,n=e?e.getSize():{width:0,height:0},o=t?t.getSize():{width:0,height:0};return n.width<o.width?(a=e&&e instanceof L.Texture?L.TextureTools.CreateResizedCopy(e,o.width,o.height,!0):this._createWhiteTexture(o.width,o.height,r),i=t):n.width>o.width?(i=t&&t instanceof L.Texture?L.TextureTools.CreateResizedCopy(t,n.width,n.height,!0):this._createWhiteTexture(n.width,n.height,r),a=e):(a=e,i=t),{texture1:a,texture2:i}},B.prototype._convertPixelArrayToFloat32=function(e){if(e instanceof Uint8Array){for(var t=e.length,r=new Float32Array(e.length),a=0;a<t;++a)r[a]=e[a]/255;return r}if(e instanceof Float32Array)return e;throw new Error("Unsupported pixel format!")},B.prototype._convertSpecularGlossinessTexturesToMetallicRoughnessAsync=function(e,t,r,a){var i=[];if(!e&&!t)return Promise.reject("_ConvertSpecularGlosinessTexturesToMetallicRoughness: diffuse and specular glossiness textures are not defined!");var n=e?e.getScene():t?t.getScene():null;if(n){var o=this._resizeTexturesToSameDimensions(e,t,n),s=o.texture1.getSize(),l=void 0,u=void 0,c=s.width,h=s.height,f=o.texture1.readPixels(),p=o.texture2.readPixels();if(!f)return Promise.reject("Failed to retrieve pixels from diffuse texture!");if(l=this._convertPixelArrayToFloat32(f),!p)return Promise.reject("Failed to retrieve pixels from specular glossiness texture!");for(var m=(u=this._convertPixelArrayToFloat32(p)).byteLength,d=new Uint8Array(m),g=new Uint8Array(m),_=L.Color3.Black(),y=0,T=0,x=0;x<h;++x)for(var v=0;v<c;++v){var b=4*(c*x+v),F={diffuseColor:new L.Color3(l[b],l[b+1],l[b+2]).toLinearSpace().multiply(r.diffuseColor),specularColor:new L.Color3(u[b],u[b+1],u[b+2]).toLinearSpace().multiply(r.specularColor),glossiness:u[b+3]*r.glossiness},A=this._convertSpecularGlossinessToMetallicRoughness(F);_.r=Math.max(_.r,A.baseColor.r),_.g=Math.max(_.g,A.baseColor.g),_.b=Math.max(_.b,A.baseColor.b),y=Math.max(y,A.metallic),T=Math.max(T,A.roughness),g[b]=255*A.baseColor.r,g[b+1]=255*A.baseColor.g,g[b+2]=255*A.baseColor.b,g[b+3]=o.texture1.hasAlpha?255*l[b+3]:255,d[b]=0,d[b+1]=255*A.roughness,d[b+2]=255*A.metallic,d[b+3]=255}var M={baseColor:_,metallic:y,roughness:T},R=!1,V=!1;for(x=0;x<h;++x)for(v=0;v<c;++v){var S=4*(c*x+v);g[S]/=M.baseColor.r>B._Epsilon?M.baseColor.r:1,g[S+1]/=M.baseColor.g>B._Epsilon?M.baseColor.g:1,g[S+2]/=M.baseColor.b>B._Epsilon?M.baseColor.b:1;var E=L.Color3.FromInts(g[S],g[S+1],g[S+2]).toGammaSpace();g[S]=255*E.r,g[S+1]=255*E.g,g[S+2]=255*E.b,B.FuzzyEquals(E,L.Color3.White(),B._Epsilon)||(V=!0),d[S+1]/=M.roughness>B._Epsilon?M.roughness:1,d[S+2]/=M.metallic>B._Epsilon?M.metallic:1;var C=L.Color3.FromInts(255,d[S+1],d[S+2]);B.FuzzyEquals(C,L.Color3.White(),B._Epsilon)||(R=!0)}if(R){var w=this._createBase64FromCanvasAsync(d,c,h,a).then((function(e){M.metallicRoughnessTextureBase64=e}));i.push(w)}return V&&(w=this._createBase64FromCanvasAsync(g,c,h,a).then((function(e){M.baseColorTextureBase64=e})),i.push(w)),Promise.all(i).then((function(){return M}))}return Promise.reject("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!")},B.prototype._convertSpecularGlossinessToMetallicRoughness=function(e){var t=this._getPerceivedBrightness(e.diffuseColor),r=this._getPerceivedBrightness(e.specularColor),a=1-this._getMaxComponent(e.specularColor),i=B._SolveMetallic(t,r,a),n=e.diffuseColor.scale(a/(1-B._DielectricSpecular.r)/Math.max(1-i,B._Epsilon)),o=e.specularColor.subtract(B._DielectricSpecular.scale(1-i)).scale(1/Math.max(i,B._Epsilon)),s=L.Color3.Lerp(n,o,i*i);return{baseColor:s=s.clampToRef(0,1,s),metallic:i,roughness:1-e.glossiness}},B.prototype._getPerceivedBrightness=function(e){return e?Math.sqrt(.299*e.r*e.r+.587*e.g*e.g+.114*e.b*e.b):0},B.prototype._getMaxComponent=function(e){return e?Math.max(e.r,Math.max(e.g,e.b)):0},B.prototype._convertMetalRoughFactorsToMetallicRoughnessAsync=function(e,t,r,a){var i=[],n={baseColor:e.albedoColor,metallic:e.metallic,roughness:e.roughness};return a&&(e.albedoTexture&&i.push(this._exportTextureAsync(e.albedoTexture,t).then((function(e){e&&(r.baseColorTexture=e)}))),e.metallicTexture&&i.push(this._exportTextureAsync(e.metallicTexture,t).then((function(e){e&&(r.metallicRoughnessTexture=e)})))),Promise.all(i).then((function(){return n}))},B.prototype._getGLTFTextureSampler=function(e){var t=this._getGLTFTextureWrapModesSampler(e),r=e instanceof L.Texture?e.samplingMode:null;if(null!=r)switch(r){case L.Texture.LINEAR_LINEAR:t.magFilter=9729,t.minFilter=9729;break;case L.Texture.LINEAR_NEAREST:t.magFilter=9729,t.minFilter=9728;break;case L.Texture.NEAREST_LINEAR:t.magFilter=9728,t.minFilter=9729;break;case L.Texture.NEAREST_LINEAR_MIPLINEAR:t.magFilter=9728,t.minFilter=9987;break;case L.Texture.NEAREST_NEAREST:t.magFilter=9728,t.minFilter=9728;break;case L.Texture.NEAREST_LINEAR_MIPNEAREST:t.magFilter=9728,t.minFilter=9985;break;case L.Texture.LINEAR_NEAREST_MIPNEAREST:t.magFilter=9729,t.minFilter=9984;break;case L.Texture.LINEAR_NEAREST_MIPLINEAR:t.magFilter=9729,t.minFilter=9986;break;case L.Texture.NEAREST_NEAREST_MIPLINEAR:t.magFilter=9728,t.minFilter=9986;break;case L.Texture.LINEAR_LINEAR_MIPLINEAR:t.magFilter=9729,t.minFilter=9987;break;case L.Texture.LINEAR_LINEAR_MIPNEAREST:t.magFilter=9729,t.minFilter=9985;break;case L.Texture.NEAREST_NEAREST_MIPNEAREST:t.magFilter=9728,t.minFilter=9984}return t},B.prototype._getGLTFTextureWrapMode=function(e){switch(e){case L.Texture.WRAP_ADDRESSMODE:return 10497;case L.Texture.CLAMP_ADDRESSMODE:return 33071;case L.Texture.MIRROR_ADDRESSMODE:return 33648;default:return L.Tools.Error("Unsupported Texture Wrap Mode "+e+"!"),10497}},B.prototype._getGLTFTextureWrapModesSampler=function(e){var t=this._getGLTFTextureWrapMode(e instanceof L.Texture?e.wrapU:L.Texture.WRAP_ADDRESSMODE),r=this._getGLTFTextureWrapMode(e instanceof L.Texture?e.wrapV:L.Texture.WRAP_ADDRESSMODE);return 10497===t&&10497===r?{}:{wrapS:t,wrapT:r}},B.prototype._convertSpecGlossFactorsToMetallicRoughnessAsync=function(n,o,s,l){var u=this;return Promise.resolve().then((function(){var e=u._exporter._samplers,a=u._exporter._textures,t={diffuseColor:n.albedoColor||L.Color3.White(),specularColor:n.reflectivityColor||L.Color3.White(),glossiness:n.microSurface||1},i=null,r=u._getGLTFTextureSampler(n.albedoTexture);return null!=r.magFilter&&null!=r.minFilter&&null!=r.wrapS&&null!=r.wrapT&&(e.push(r),i=e.length-1),n.reflectivityTexture&&!n.useMicroSurfaceFromReflectivityMapAlpha?Promise.reject("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture are currently not supported"):(n.albedoTexture||n.reflectivityTexture)&&l?u._convertSpecularGlossinessTexturesToMetallicRoughnessAsync(n.albedoTexture,n.reflectivityTexture,t,o).then((function(e){if(e.baseColorTextureBase64){var t=u._getTextureInfoFromBase64(e.baseColorTextureBase64,"bjsBaseColorTexture_"+a.length+".png",o,n.albedoTexture?n.albedoTexture.coordinatesIndex:null,i);t&&(s.baseColorTexture=t)}if(e.metallicRoughnessTextureBase64){var r=u._getTextureInfoFromBase64(e.metallicRoughnessTextureBase64,"bjsMetallicRoughnessTexture_"+a.length+".png",o,n.reflectivityTexture?n.reflectivityTexture.coordinatesIndex:null,i);r&&(s.metallicRoughnessTexture=r)}return e})):u._convertSpecularGlossinessToMetallicRoughness(t)}))},B.prototype._convertPBRMaterialAsync=function(t,r,a){var i=this,n={},o={name:t.name};return t.isMetallicWorkflow()?(t.albedoColor&&(n.baseColorFactor=[t.albedoColor.r,t.albedoColor.g,t.albedoColor.b,t.alpha]),this._convertMetalRoughFactorsToMetallicRoughnessAsync(t,r,n,a).then((function(e){return i.setMetallicRoughnessPbrMaterial(e,t,o,n,r,a)}))):this._convertSpecGlossFactorsToMetallicRoughnessAsync(t,r,n,a).then((function(e){return i.setMetallicRoughnessPbrMaterial(e,t,o,n,r,a)}))},B.prototype.setMetallicRoughnessPbrMaterial=function(e,r,a,t,i,n){var o=this._exporter._materialMap,s=this._exporter._materials,l=[];if(e){var u=null;if(null!=r.transparencyMode&&(u=this._getAlphaMode(r))&&"OPAQUE"!==u&&"MASK"===(a.alphaMode=u)&&(a.alphaCutoff=r.alphaCutOff),B.FuzzyEquals(e.baseColor,L.Color3.White(),B._Epsilon)&&r.alpha>=B._Epsilon||(t.baseColorFactor=[e.baseColor.r,e.baseColor.g,e.baseColor.b,r.alpha]),null!=e.metallic&&1!==e.metallic&&(t.metallicFactor=e.metallic),null!=e.roughness&&1!==e.roughness&&(t.roughnessFactor=e.roughness),null==r.backFaceCulling||r.backFaceCulling||(r.twoSidedLighting||L.Tools.Warn(r.name+": Back-face culling enabled and two-sided lighting disabled is not supported in glTF."),a.doubleSided=!0),n){if(r.bumpTexture){var c=this._exportTextureAsync(r.bumpTexture,i).then((function(e){e&&(a.normalTexture=e,1!==r.bumpTexture.level&&(a.normalTexture.scale=r.bumpTexture.level))}));l.push(c)}r.ambientTexture&&(c=this._exportTextureAsync(r.ambientTexture,i).then((function(e){if(e){var t={index:e.index};a.occlusionTexture=t,r.ambientTextureStrength&&(t.strength=r.ambientTextureStrength)}})),l.push(c)),r.emissiveTexture&&(c=this._exportTextureAsync(r.emissiveTexture,i).then((function(e){e&&(a.emissiveTexture=e)})),l.push(c))}B.FuzzyEquals(r.emissiveColor,L.Color3.Black(),B._Epsilon)||(a.emissiveFactor=r.emissiveColor.asArray()),a.pbrMetallicRoughness=t,s.push(a),o[r.uniqueId]=s.length-1}return Promise.all(l).then((function(e){}))},B.prototype.getPixelsFromTexture=function(e){return e.textureType,L.Engine.TEXTURETYPE_UNSIGNED_INT,e.readPixels()},B.prototype._exportTextureAsync=function(u,c){var h=this;return Promise.resolve().then((function(){var r=u.uid;if(r in h._textureMap)return h._textureMap[r];for(var e=h._exporter._samplers,t=h._getGLTFTextureSampler(u),a=null,i=null,n=0;n<e.length;++n){var o=e[n];if(o.minFilter===t.minFilter&&o.magFilter===t.magFilter&&o.wrapS===t.wrapS&&o.wrapT===t.wrapT){i=n;break}}null==i?(e.push(t),a=e.length-1):a=i;var s=h.getPixelsFromTexture(u),l=u.getSize();return h._createBase64FromCanvasAsync(s,l.width,l.height,c).then((function(e){var t=h._getTextureInfoFromBase64(e,u.name.replace(/\.\/|\/|\.\\|\\/g,"_"),c,u.coordinatesIndex,a);return t&&(h._textureMap[r]=t),t}))}))},B.prototype._getTextureInfoFromBase64=function(e,t,r,a,i){var n=this._exporter._textures,o=this._exporter._images,s=this._exporter._imageData,l=null,u={source:o.length,name:t};null!=i&&(u.sampler=i);for(var c=atob(e.split(",")[1]),h=new ArrayBuffer(c.length),f=new Uint8Array(h),p=0,m=c.length;p<m;++p)f[p]=c.charCodeAt(p);var d={data:f,mimeType:r},g="image/jpeg"===r?".jpeg":".png",_=t+g;if(_ in s&&(_=t+"_"+L.Tools.RandomId()+g),s[_]=d,"image/jpeg"===r||"image/png"===r){var y={name:t,uri:_},T=null;for(p=0;p<o.length;++p)if(o[p].uri===_){T=p;break}null==T?(o.push(y),u.source=o.length-1):u.source=T,n.push(u),l={index:n.length-1},null!=a&&(l.texCoord=a)}else L.Tools.Error("Unsupported texture mime type "+r);return l},B._DielectricSpecular=new L.Color3(.04,.04,.04),B._MaxSpecularPower=1024,B._Epsilon=1e-6,B})(),s._GLTFMaterialExporter=l,(function(F){var f,e;(e=f||(f={}))[e.INTANGENT=0]="INTANGENT",e[e.OUTTANGENT=1]="OUTTANGENT";var t=(function(){function M(){}return M._CreateNodeAnimation=function(e,t,r,a,i,n){var o=[],s=[],l=t.getKeys(),u=M.calculateMinMaxKeyFrames(l),c=M._DeduceInterpolation(l,r,i),h=u.max-u.min,f=c.interpolationType,p=c.shouldBakeAnimation;return p?M._CreateBakedAnimation(e,t,r,u.min,u.max,t.framePerSecond,n,o,s,u,a,i):"LINEAR"===f||"STEP"===f?M._CreateLinearOrStepAnimation(e,t,r,h,o,s,a,i):"CUBICSPLINE"===f?M._CreateCubicSplineAnimation(e,t,r,h,o,s,a,i):M._CreateBakedAnimation(e,t,r,u.min,u.max,t.framePerSecond,n,o,s,u,a,i),o.length&&s.length?{inputs:o,outputs:s,samplerInterpolation:f,inputsMin:p?u.min:R.Tools.FloatRound(u.min/t.framePerSecond),inputsMax:p?u.max:R.Tools.FloatRound(u.max/t.framePerSecond)}:null},M._DeduceAnimationInfo=function(e){var t=null,r="VEC3",a=!1,i=e.targetProperty.split(".");switch(i[0]){case"scaling":t="scale";break;case"position":t="translation";break;case"rotation":r="VEC4",t="rotation";break;case"rotationQuaternion":r="VEC4",a=!0,t="rotation";break;default:R.Tools.Error("Unsupported animatable property "+i[0])}return t?{animationChannelTargetPath:t,dataAccessorType:r,useQuaternion:a}:(R.Tools.Error("animation channel target path and data accessor type could be deduced"),null)},M._CreateNodeAnimationFromTransformNodeAnimations=function(e,t,r,a,i,n,o,s,l,u){var c;if(e.animations)for(var h=0,f=e.animations;h<f.length;h++){var p=f[h],m=M._DeduceAnimationInfo(p);m&&(c={name:p.name,samplers:[],channels:[]},M.AddAnimation(""+p.name,p.hasRunningRuntimeAnimations?t:c,e,p,m.dataAccessorType,m.animationChannelTargetPath,a,n,o,s,l,m.useQuaternion,u),c.samplers.length&&c.channels.length&&r.push(c))}},M._CreateNodeAnimationFromAnimationGroups=function(e,t,r,a,i,n,o,s,l){var u;if(e.animationGroups)for(var c=0,h=e.animationGroups;c<h.length;c++){var f=h[c];u={name:f.name,channels:[],samplers:[]};for(var p=0,m=f.targetedAnimations;p<m.length;p++){var d=m[p],g=d.target,_=d.animation;if(g instanceof R.Mesh||1===g.length&&g[0]instanceof R.Mesh){var y=M._DeduceAnimationInfo(d.animation);if(y){var T=g instanceof R.Mesh?g:g[0];M.AddAnimation(""+_.name,u,T,_,y.dataAccessorType,y.animationChannelTargetPath,r,i,n,o,s,y.useQuaternion,l)}}}u.channels.length&&u.samplers.length&&t.push(u)}},M.AddAnimation=function(e,t,r,a,i,n,o,s,l,u,c,h,f){var p,m,d,g,_,y,T,x=M._CreateNodeAnimation(r,a,n,c,h,f);if(x){var v=o[r.uniqueId],b=4*x.inputs.length;p=F._GLTFUtilities._CreateBufferView(0,s.getByteOffset(),b,void 0,e+" keyframe data view"),l.push(p),x.inputs.forEach((function(e){s.setFloat32(e)})),m=F._GLTFUtilities._CreateAccessor(l.length-1,e+" keyframes","SCALAR",5126,x.inputs.length,null,[x.inputsMin],[x.inputsMax]),u.push(m),d=u.length-1,_=x.outputs.length,b="VEC3"===i?12*x.outputs.length:16*x.outputs.length,p=F._GLTFUtilities._CreateBufferView(0,s.getByteOffset(),b,void 0,e+" data view"),l.push(p),x.outputs.forEach((function(e){e.forEach((function(e){s.setFloat32(e)}))})),m=F._GLTFUtilities._CreateAccessor(l.length-1,e+" data",i,5126,_,null,null,null),u.push(m),g=u.length-1,y={interpolation:x.samplerInterpolation,input:d,output:g},t.samplers.push(y),T={sampler:t.samplers.length-1,target:{node:v,path:n}},t.channels.push(T)}},M._CreateBakedAnimation=function(e,t,r,a,i,n,o,s,l,u,c,h){var f,p,m=R.Quaternion.Identity(),d=null,g=null,_=null,y=null,T=null,x=null;u.min=R.Tools.FloatRound(a/n);for(var v=t.getKeys(),b=0,F=v.length;b<F;++b){if(x=null,_=v[b],b+1<F)if(y=v[b+1],_.value.equals(y.value)){if(0!==b)continue;x=_.frame}else x=y.frame;else{if(T=v[b-1],_.value.equals(T.value))continue;x=i}if(x)for(var A=_.frame;A<=x;A+=o)(p=R.Tools.FloatRound(A/n))!==d&&(g=d=p,f=t._interpolate(A,0,void 0,t.loopMode),M._SetInterpolatedValue(e,f,p,t,r,m,s,l,c,h))}g&&(u.max=g)},M._ConvertFactorToVector3OrQuaternion=function(e,t,r,a,i,n,o){var s,l,u=null,c=M._GetBasePositionRotationOrScale(t,i,n,o);if(a===R.Animation.ANIMATIONTYPE_FLOAT)switch(l=(s=r.targetProperty.split("."))?s[1]:"",u=o?R.Quaternion.FromArray(c).normalize():R.Vector3.FromArray(c),l){case"x":case"y":u[l]=n&&o&&"scale"!==i?-e:e;break;case"z":u[l]=n&&!o&&"scale"!==i?-e:e;break;case"w":u.w=e;break;default:R.Tools.Error('glTFAnimation: Unsupported component type "'+l+'" for scale animation!')}return u},M._SetInterpolatedValue=function(e,t,r,a,i,n,o,s,l,u){var c,h=a.dataType;o.push(r),"number"==typeof t&&(t=this._ConvertFactorToVector3OrQuaternion(t,e,a,h,i,l,u)),t&&("rotation"===i?(u?n=t:(c=t,R.Quaternion.RotationYawPitchRollToRef(c.y,c.x,c.z,n)),l&&(F._GLTFUtilities._GetRightHandedQuaternionFromRef(n),e.parent||(n=R.Quaternion.FromArray([0,1,0,0]).multiply(n))),s.push(n.asArray())):(c=t,l&&"scale"!==i&&(F._GLTFUtilities._GetRightHandedPositionVector3FromRef(c),e.parent||(c.x*=-1,c.z*=-1)),s.push(c.asArray())))},M._CreateLinearOrStepAnimation=function(e,t,r,a,i,n,o,s){for(var l=0,u=t.getKeys();l<u.length;l++){var c=u[l];i.push(c.frame/t.framePerSecond),M._AddKeyframeValue(c,t,n,r,e,o,s)}},M._CreateCubicSplineAnimation=function(t,r,a,i,n,o,s,l){r.getKeys().forEach((function(e){n.push(e.frame/r.framePerSecond),M.AddSplineTangent(t,f.INTANGENT,o,a,"CUBICSPLINE",e,i,l,s),M._AddKeyframeValue(e,r,o,a,t,s,l),M.AddSplineTangent(t,f.OUTTANGENT,o,a,"CUBICSPLINE",e,i,l,s)}))},M._GetBasePositionRotationOrScale=function(e,t,r,a){var i;return"rotation"===t?a?e.rotationQuaternion?(i=e.rotationQuaternion.asArray(),r&&(F._GLTFUtilities._GetRightHandedQuaternionArrayFromRef(i),e.parent||(i=R.Quaternion.FromArray([0,1,0,0]).multiply(R.Quaternion.FromArray(i)).asArray()))):i=R.Quaternion.Identity().asArray():(i=e.rotation.asArray(),F._GLTFUtilities._GetRightHandedNormalArray3FromRef(i)):"translation"===t?(i=e.position.asArray(),r&&F._GLTFUtilities._GetRightHandedPositionArray3FromRef(i)):i=e.scaling.asArray(),i},M._AddKeyframeValue=function(e,t,r,a,i,n,o){var s,l,u=t.dataType;if(u===R.Animation.ANIMATIONTYPE_VECTOR3){if(s=e.value.asArray(),"rotation"===a){var c=R.Vector3.FromArray(s),h=R.Quaternion.RotationYawPitchRoll(c.y,c.x,c.z);n&&(F._GLTFUtilities._GetRightHandedQuaternionFromRef(h),i.parent||(h=R.Quaternion.FromArray([0,1,0,0]).multiply(h))),s=h.asArray()}else"translation"===a&&n&&(F._GLTFUtilities._GetRightHandedNormalArray3FromRef(s),i.parent||(s[0]*=-1,s[2]*=-1));r.push(s)}else if(u===R.Animation.ANIMATIONTYPE_FLOAT){if(l=this._ConvertFactorToVector3OrQuaternion(e.value,i,t,u,a,n,o)){if("rotation"===a){var f=o?l:R.Quaternion.RotationYawPitchRoll(l.y,l.x,l.z).normalize();n&&(F._GLTFUtilities._GetRightHandedQuaternionFromRef(f),i.parent||(f=R.Quaternion.FromArray([0,1,0,0]).multiply(f))),r.push(f.asArray())}else"translation"===a&&n&&(F._GLTFUtilities._GetRightHandedNormalVector3FromRef(l),i.parent||(l.x*=-1,l.z*=-1));r.push(l.asArray())}}else u===R.Animation.ANIMATIONTYPE_QUATERNION?(s=e.value.normalize().asArray(),n&&(F._GLTFUtilities._GetRightHandedQuaternionArrayFromRef(s),i.parent||(s=R.Quaternion.FromArray([0,1,0,0]).multiply(R.Quaternion.FromArray(s)).asArray())),r.push(s)):R.Tools.Error("glTFAnimation: Unsupported key frame values for animation!")},M._DeduceInterpolation=function(e,t,r){var a,i,n=!1;if("rotation"===t&&!r)return{interpolationType:"LINEAR",shouldBakeAnimation:!0};for(var o=0,s=e.length;o<s;++o)if((i=e[o]).inTangent||i.outTangent)if(a){if("CUBICSPLINE"!==a){a="LINEAR",n=!0;break}}else a="CUBICSPLINE";else if(a){if("CUBICSPLINE"===a||i.interpolation&&i.interpolation===R.AnimationKeyInterpolation.STEP&&"STEP"!==a){a="LINEAR",n=!0;break}}else a=i.interpolation&&i.interpolation===R.AnimationKeyInterpolation.STEP?"STEP":"LINEAR";return a||(a="LINEAR"),{interpolationType:a,shouldBakeAnimation:n}},M.AddSplineTangent=function(e,t,r,a,i,n,o,s,l){var u,c=t===f.INTANGENT?n.inTangent:n.outTangent;if("CUBICSPLINE"===i){if("rotation"===a)if(c){if(s)u=c.scale(o).asArray();else{var h=c.scale(o);u=R.Quaternion.RotationYawPitchRoll(h.y,h.x,h.z).asArray()}l&&(F._GLTFUtilities._GetRightHandedQuaternionArrayFromRef(u),e.parent||(u=R.Quaternion.FromArray([0,1,0,0]).multiply(R.Quaternion.FromArray(u)).asArray()))}else u=[0,0,0,0];else c?(u=c.scale(o).asArray(),l&&"translation"===a&&(F._GLTFUtilities._GetRightHandedPositionArray3FromRef(u),e.parent||(u[0]*=-1,u[2]*=-1))):u=[0,0,0];r.push(u)}},M.calculateMinMaxKeyFrames=function(e){var t=1/0,r=-1/0;return e.forEach((function(e){t=Math.min(t,e.frame),r=Math.max(r,e.frame)})),{min:t,max:r}},M})();F._GLTFAnimation=t})((R=e||(e={})).GLTF2||(R.GLTF2={})),m=e||(e={}),u=m.GLTF2||(m.GLTF2={}),c=(function(){function p(){}return p._CreateBufferView=function(e,t,r,a,i){var n={buffer:e,byteLength:r};return t&&(n.byteOffset=t),i&&(n.name=i),a&&(n.byteStride=a),n},p._CreateAccessor=function(e,t,r,a,i,n,o,s){var l={name:t,bufferView:e,componentType:a,count:i,type:r};return null!=o&&(l.min=o),null!=s&&(l.max=s),null!=n&&(l.byteOffset=n),l},p._CalculateMinMaxPositions=function(e,t,r,a){var i,n,o,s=[1/0,1/0,1/0],l=[-1/0,-1/0,-1/0];if(r)for(var u=t,c=t+r;u<c;++u){i=3*u,n=m.Vector3.FromArray(e,i),a&&p._GetRightHandedPositionVector3FromRef(n),o=n.asArray();for(var h=0;h<3;++h){var f=o[h];f<s[h]&&(s[h]=f),f>l[h]&&(l[h]=f),++i}}return{min:s,max:l}},p._GetRightHandedPositionVector3=function(e){return new m.Vector3(e.x,e.y,-e.z)},p._GetRightHandedPositionVector3FromRef=function(e){e.z*=-1},p._GetRightHandedPositionArray3FromRef=function(e){e[2]*=-1},p._GetRightHandedNormalVector3=function(e){return new m.Vector3(e.x,e.y,-e.z)},p._GetRightHandedNormalVector3FromRef=function(e){e.z*=-1},p._GetRightHandedNormalArray3FromRef=function(e){e[2]*=-1},p._GetRightHandedVector4FromRef=function(e){e.z*=-1,e.w*=-1},p._GetRightHandedArray4FromRef=function(e){e[2]*=-1,e[3]*=-1},p._GetRightHandedQuaternionFromRef=function(e){e.x*=-1,e.y*=-1},p._GetRightHandedQuaternionArrayFromRef=function(e){e[0]*=-1,e[1]*=-1},p._NormalizeTangentFromRef=function(e){var t=Math.sqrt(e.x*e.x+e.y*e.y+e.z*e.z);0<t&&(e.x/=t,e.y/=t,e.z/=t)},p})(),u._GLTFUtilities=c,e}));
|