var BABYLON;!(function(e){var t=(function(){function t(){}return t.GLTFAsync=function(t,r,a){return t.whenReadyAsync().then((function(){var i=r.replace(/\.[^\/.]+$/,"");return new e.GLTF2._Exporter(t,a)._generateGLTFAsync(i)}))},t.GLBAsync=function(t,r,a){return t.whenReadyAsync().then((function(){var i=r.replace(/\.[^\/.]+$/,"");return new e.GLTF2._Exporter(t,a)._generateGLBAsync(i)}))},t})();e.GLTF2Export=t})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){var r=(function(){function r(e,r){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 a=r||{};this._shouldExportTransformNode=a.shouldExportTransformNode?a.shouldExportTransformNode:function(e){return!0},this._animationSampleRate=a.animationSampleRate?a.animationSampleRate:1/60,this._glTFMaterialExporter=new t._GLTFMaterialExporter(this)}return r.prototype._getLocalEngine=function(){if(!this._localEngine){var t=document.createElement("canvas");t.id="WriteCanvas",t.width=2048,t.height=2048,this._localEngine=new e.Engine(t,!0,{premultipliedAlpha:!1,preserveDrawingBuffer:!0}),this._localEngine.setViewport(new e.Viewport(0,0,1,1))}return this._localEngine},r.prototype.reorderIndicesBasedOnPrimitiveMode=function(t,r,a,i,n){switch(r){case e.Material.TriangleFillMode:i||(i=0);for(var o=t.indexStart,s=t.indexStart+t.indexCount;o=h;--o)n.setUInt32(a[o],i),i+=4;break;case e.Material.TriangleStripDrawMode:t.indexCount>=3&&(n.setUInt32(a[t.indexStart+2],i+4),n.setUInt32(a[t.indexStart+1],i+8))}},r.prototype.reorderVertexAttributeDataBasedOnPrimitiveMode=function(t,r,a,i,n,o,s){if(this._convertToRightHandedSystem&&a===e.Material.ClockWiseSideOrientation)switch(r){case e.Material.TriangleFillMode:this.reorderTriangleFillMode(t,r,a,i,n,o,s);break;case e.Material.TriangleStripDrawMode:this.reorderTriangleStripDrawMode(t,r,a,i,n,o,s);break;case e.Material.TriangleFanDrawMode:this.reorderTriangleFanMode(t,r,a,i,n,o,s)}},r.prototype.reorderTriangleFillMode=function(t,r,a,i,n,o,s){var l=this.getVertexBufferFromMesh(i,t.getMesh());if(l){var u=l.byteStride/e.VertexBuffer.GetTypeByteLength(l.type);if(t.verticesCount%3!=0)e.Tools.Error("The submesh vertices for the triangle fill mode is not divisible by 3!");else{var c=[],h=0;switch(i){case e.VertexBuffer.PositionKind:case e.VertexBuffer.NormalKind:for(var f=t.verticesStart;f=t.verticesStart;--f)h=f*u,c.push(e.Vector4.FromArray(n,h));break;case e.VertexBuffer.ColorKind:for(var f=t.verticesStart+t.verticesCount-1;f>=t.verticesStart;--f)h=f*u,4===l.getSize()?c.push(e.Vector4.FromArray(n,h)):c.push(e.Vector3.FromArray(n,h));break;case e.VertexBuffer.UVKind:case e.VertexBuffer.UV2Kind:for(var f=t.verticesStart+t.verticesCount-1;f>=t.verticesStart;--f)h=f*u,c.push(e.Vector2.FromArray(n,h));break;default:e.Tools.Error("Unsupported Vertex Buffer type: "+i)}this.writeVertexAttributeData(c,o+12,i,n,s)}else e.Tools.Warn("reorderTriangleStripDrawMode: Vertex buffer kind "+i+" not present!")},r.prototype.reorderTriangleFanMode=function(t,r,a,i,n,o,s){var l=this.getVertexBufferFromMesh(i,t.getMesh());if(l){var u=l.byteStride/e.VertexBuffer.GetTypeByteLength(l.type),c=[],h=0;switch(i){case e.VertexBuffer.PositionKind:case e.VertexBuffer.NormalKind:for(var f=t.verticesStart+t.verticesCount-1;f>=t.verticesStart;--f)h=f*u,c.push(e.Vector3.FromArray(n,h));break;case e.VertexBuffer.TangentKind:for(var f=t.verticesStart+t.verticesCount-1;f>=t.verticesStart;--f)h=f*u,c.push(e.Vector4.FromArray(n,h));break;case e.VertexBuffer.ColorKind:for(var f=t.verticesStart+t.verticesCount-1;f>=t.verticesStart;--f)h=f*u,c.push(e.Vector4.FromArray(n,h)),4===l.getSize()?c.push(e.Vector4.FromArray(n,h)):c.push(e.Vector3.FromArray(n,h));break;case e.VertexBuffer.UVKind:case e.VertexBuffer.UV2Kind:for(var f=t.verticesStart+t.verticesCount-1;f>=t.verticesStart;--f)h=f*u,c.push(e.Vector2.FromArray(n,h));break;default:e.Tools.Error("Unsupported Vertex Buffer type: "+i)}this.writeVertexAttributeData(c,o,i,n,s)}else e.Tools.Warn("reorderTriangleFanMode: Vertex buffer kind "+i+" not present!")},r.prototype.writeVertexAttributeData=function(r,a,i,n,o){for(var s=0,l=r;s0){var G=this._babylonScene.materials[R].sideOrientation;if(this._convertToRightHandedSystem&&G===e.Material.ClockWiseSideOrientation){var I=null!=c?this._bufferViews[c].byteOffset:null;null==I&&(I=0);var U=null;if(null!=c&&(U=l.getIndices()),U)this.reorderIndicesBasedOnPrimitiveMode(F,h,U,I,i);else for(var O=0,k=u;Othis._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setUint8(this._byteOffset++,t))},t.prototype.getUInt32=function(t){if(tthis._byteOffset?e.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(t.x=this._dataView.getFloat32(r,!0),t.y=this._dataView.getFloat32(r+4,!0),t.z=this._dataView.getFloat32(r+8,!0))},t.prototype.setVector3Float32FromRef=function(t,r){r+8>this._byteOffset?e.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(this._dataView.setFloat32(r,t.x,!0),this._dataView.setFloat32(r+4,t.y,!0),this._dataView.setFloat32(r+8,t.z,!0))},t.prototype.getVector4Float32FromRef=function(t,r){r+12>this._byteOffset?e.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(t.x=this._dataView.getFloat32(r,!0),t.y=this._dataView.getFloat32(r+4,!0),t.z=this._dataView.getFloat32(r+8,!0),t.w=this._dataView.getFloat32(r+12,!0))},t.prototype.setVector4Float32FromRef=function(t,r){r+12>this._byteOffset?e.Tools.Error("BinaryWriter: byteoffset is greater than the current binary buffer length!"):(this._dataView.setFloat32(r,t.x,!0),this._dataView.setFloat32(r+4,t.y,!0),this._dataView.setFloat32(r+8,t.z,!0),this._dataView.setFloat32(r+12,t.w,!0))},t.prototype.setFloat32=function(t,r){isNaN(t)&&e.Tools.Error("Invalid data being written!"),null!=r&&(rthis._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setFloat32(this._byteOffset,t,!0),this._byteOffset+=4},t.prototype.setUInt32=function(t,r){null!=r?rthis._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setUint32(this._byteOffset,t,!0),this._byteOffset+=4)},t})();t._BinaryWriter=a})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){var t=(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})();e.GLTFData=t})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){var r=(function(){function t(e){this._textureMap={},this._textureMap={},this._exporter=e}return t.FuzzyEquals=function(t,r,a){return e.Scalar.WithinEpsilon(t.r,r.r,a)&&e.Scalar.WithinEpsilon(t.g,r.g,a)&&e.Scalar.WithinEpsilon(t.b,r.b,a)},t.prototype._convertMaterialsToGLTFAsync=function(t,r,a){for(var i=[],n=0,o=t;ns.width?(n=r&&r instanceof e.Texture?e.TextureTools.CreateResizedCopy(r,o.width,o.height,!0):this._createWhiteTexture(o.width,o.height,a),i=t):(i=t,n=r),{texture1:i,texture2:n}},t.prototype._convertSpecularGlossinessTexturesToMetallicRoughnessAsync=function(r,a,i,n){var o=[];if(!r&&!a)return Promise.reject("_ConvertSpecularGlosinessTexturesToMetallicRoughness: diffuse and specular glossiness textures are not defined!");var s=r?r.getScene():a?a.getScene():null;if(s){var l=this._resizeTexturesToSameDimensions(r,a,s),u=l.texture1.getSize(),c=void 0,h=void 0,f=u.width,p=u.height,d=l.texture1.readPixels();if(d instanceof Uint8Array){if(c=l.texture1.readPixels(),(d=l.texture2.readPixels())instanceof Uint8Array){h=l.texture2.readPixels();for(var m=h.byteLength,g=new Uint8Array(m),y=new Uint8Array(m),T=e.Color3.Black(),_=0,v=0,x=0;xt._Epsilon?B.baseColor.r:1,y[w+1]/=B.baseColor.g>t._Epsilon?B.baseColor.g:1,y[w+2]/=B.baseColor.b>t._Epsilon?B.baseColor.b:1;var L=e.Color3.FromInts(y[w],y[w+1],y[w+2]),N=L.toGammaSpace();y[w]=255*N.r,y[w+1]=255*N.g,y[w+2]=255*N.b,t.FuzzyEquals(N,e.Color3.White(),t._Epsilon)||(C=!0),g[w+1]/=B.roughness>t._Epsilon?B.roughness:1,g[w+2]/=B.metallic>t._Epsilon?B.metallic:1;var P=e.Color3.FromInts(255,g[w+1],g[w+2]);t.FuzzyEquals(P,e.Color3.White(),t._Epsilon)||(E=!0)}if(E){var G=this._createBase64FromCanvasAsync(g,f,p,n).then((function(e){B.metallicRoughnessTextureBase64=e}));o.push(G)}if(C){var G=this._createBase64FromCanvasAsync(y,f,p,n).then((function(e){B.baseColorTextureBase64=e}));o.push(G)}return Promise.all(o).then((function(){return B}))}return Promise.reject("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: "+l.texture2.name)}return Promise.reject("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: "+l.texture1.name)}return Promise.reject("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!")},t.prototype._convertSpecularGlossinessToMetallicRoughness=function(r){ var a=this._getPerceivedBrightness(r.diffuseColor),i=this._getPerceivedBrightness(r.specularColor),n=1-this._getMaxComponent(r.specularColor),o=t._SolveMetallic(a,i,n),s=r.diffuseColor.scale(n/(1-t._DielectricSpecular.r)/Math.max(1-o,t._Epsilon)),l=r.specularColor.subtract(t._DielectricSpecular.scale(1-o)).scale(1/Math.max(o,t._Epsilon)),u=e.Color3.Lerp(s,l,o*o);return u=u.clampToRef(0,1,u),{baseColor:u,metallic:o,roughness:1-r.glossiness}},t.prototype._getPerceivedBrightness=function(e){return e?Math.sqrt(.299*e.r*e.r+.587*e.g*e.g+.114*e.b*e.b):0},t.prototype._getMaxComponent=function(e){return e?Math.max(e.r,Math.max(e.g,e.b)):0},t.prototype._gonvertMetalRoughFactorsToMetallicRoughnessAsync=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}))},t.prototype._getGLTFTextureSampler=function(t){var r=this._getGLTFTextureWrapModesSampler(t),a=t instanceof e.Texture?t.samplingMode:null;if(null!=a)switch(a){case e.Texture.LINEAR_LINEAR:r.magFilter=9729,r.minFilter=9729;break;case e.Texture.LINEAR_NEAREST:r.magFilter=9729,r.minFilter=9728;break;case e.Texture.NEAREST_LINEAR:r.magFilter=9728,r.minFilter=9729;break;case e.Texture.NEAREST_LINEAR_MIPLINEAR:r.magFilter=9728,r.minFilter=9987;break;case e.Texture.NEAREST_NEAREST:r.magFilter=9728,r.minFilter=9728;break;case e.Texture.NEAREST_LINEAR_MIPNEAREST:r.magFilter=9728,r.minFilter=9985;break;case e.Texture.LINEAR_NEAREST_MIPNEAREST:r.magFilter=9729,r.minFilter=9984;break;case e.Texture.LINEAR_NEAREST_MIPLINEAR:r.magFilter=9729,r.minFilter=9986;break;case e.Texture.NEAREST_NEAREST_MIPLINEAR:r.magFilter=9728,r.minFilter=9986;break;case e.Texture.LINEAR_LINEAR_MIPLINEAR:r.magFilter=9729,r.minFilter=9987;break;case e.Texture.LINEAR_LINEAR_MIPNEAREST:r.magFilter=9729,r.minFilter=9985;break;case e.Texture.NEAREST_NEAREST_MIPNEAREST:r.magFilter=9728,r.minFilter=9984}return r},t.prototype._getGLTFTextureWrapMode=function(t){switch(t){case e.Texture.WRAP_ADDRESSMODE:return 10497;case e.Texture.CLAMP_ADDRESSMODE:return 33071;case e.Texture.MIRROR_ADDRESSMODE:return 33648;default:return e.Tools.Error("Unsupported Texture Wrap Mode "+t+"!"),10497}},t.prototype._getGLTFTextureWrapModesSampler=function(t){var r=this._getGLTFTextureWrapMode(t instanceof e.Texture?t.wrapU:e.Texture.WRAP_ADDRESSMODE),a=this._getGLTFTextureWrapMode(t instanceof e.Texture?t.wrapV:e.Texture.WRAP_ADDRESSMODE);return 10497===r&&10497===a?{}:{wrapS:r,wrapT:a}},t.prototype._convertSpecGlossFactorsToMetallicRoughness=function(t,r,a,i){var n=this,o=this._exporter._samplers,s=this._exporter._textures,l={diffuseColor:t.albedoColor||e.Color3.White(),specularColor:t.reflectivityColor||e.Color3.White(),glossiness:t.microSurface||1},u=null,c=this._getGLTFTextureSampler(t.albedoTexture);return null!=c.magFilter&&null!=c.minFilter&&null!=c.wrapS&&null!=c.wrapT&&(o.push(c),u=o.length-1),t.reflectivityTexture&&!t.useMicroSurfaceFromReflectivityMapAlpha?Promise.reject("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture are currently not supported"):this._convertSpecularGlossinessTexturesToMetallicRoughnessAsync(t.albedoTexture,t.reflectivityTexture,l,r).then((function(e){if(i){if(e.baseColorTextureBase64){var o=n._getTextureInfoFromBase64(e.baseColorTextureBase64,"bjsBaseColorTexture_"+s.length+".png",r,t.albedoTexture?t.albedoTexture.coordinatesIndex:null,u);o&&(a.baseColorTexture=o)}if(e.metallicRoughnessTextureBase64){var c=n._getTextureInfoFromBase64(e.metallicRoughnessTextureBase64,"bjsMetallicRoughnessTexture_"+s.length+".png",r,t.reflectivityTexture?t.reflectivityTexture.coordinatesIndex:null,u);c&&(a.metallicRoughnessTexture=c)}return e}return n._convertSpecularGlossinessToMetallicRoughness(l)}))},t.prototype._convertPBRMaterialAsync=function(e,t,r){var a=this,i={},n={name:e.name};return e.isMetallicWorkflow()?(e.albedoColor&&(i.baseColorFactor=[e.albedoColor.r,e.albedoColor.g,e.albedoColor.b,e.alpha]),this._gonvertMetalRoughFactorsToMetallicRoughnessAsync(e,t,i,r).then((function(o){return a.setMetallicRoughnessPbrMaterial(o,e,n,i,t,r)}))):this._convertSpecGlossFactorsToMetallicRoughness(e,t,i,r).then((function(o){return a.setMetallicRoughnessPbrMaterial(o,e,n,i,t,r)}))},t.prototype.setMetallicRoughnessPbrMaterial=function(r,a,i,n,o,s){var l=this._exporter._materialMap,u=this._exporter._materials,c=[];if(r){var h=null;if(null!=a.transparencyMode&&(h=this._getAlphaMode(a))&&"OPAQUE"!==h&&(i.alphaMode=h,"MASK"===h&&(i.alphaCutoff=a.alphaCutOff)),t.FuzzyEquals(r.baseColor,e.Color3.White(),t._Epsilon)&&a.alpha>=t._Epsilon||(n.baseColorFactor=[r.baseColor.r,r.baseColor.g,r.baseColor.b,a.alpha]),null!=r.metallic&&1!==r.metallic&&(n.metallicFactor=r.metallic),null!=r.roughness&&1!==r.roughness&&(n.roughnessFactor=r.roughness),null==a.backFaceCulling||a.backFaceCulling||(a.twoSidedLighting||e.Tools.Warn(a.name+": Back-face culling enabled and two-sided lighting disabled is not supported in glTF."),i.doubleSided=!0),s){if(a.bumpTexture){var f=this._exportTextureAsync(a.bumpTexture,o).then((function(e){e&&(i.normalTexture=e,1!==a.bumpTexture.level&&(i.normalTexture.scale=a.bumpTexture.level))}));c.push(f)}if(a.ambientTexture){var f=this._exportTextureAsync(a.ambientTexture,o).then((function(e){if(e){var t={index:e.index};i.occlusionTexture=t,a.ambientTextureStrength&&(t.strength=a.ambientTextureStrength)}}));c.push(f)}if(a.emissiveTexture){var f=this._exportTextureAsync(a.emissiveTexture,o).then((function(e){e&&(i.emissiveTexture=e)}));c.push(f)}}t.FuzzyEquals(a.emissiveColor,e.Color3.Black(),t._Epsilon)||(i.emissiveFactor=a.emissiveColor.asArray()),i.pbrMetallicRoughness=n,u.push(i),l[a.uniqueId]=u.length-1}return Promise.all(c).then((function(e){}))},t.prototype.getPixelsFromTexture=function(t){return t.textureType,e.Engine.TEXTURETYPE_UNSIGNED_INT,t.readPixels()},t.prototype._exportTextureAsync=function(e,t){var r=this;return Promise.resolve().then((function(){var a=e.uid;if(a in r._textureMap)return r._textureMap[a];for(var i=r._exporter._samplers,n=r._getGLTFTextureSampler(e),o=null,s=null,l=0;lc[p]&&(c[p]=d),++o}}return{min:u,max:c}},t.GetRightHandedPositionVector3=function(t){return new e.Vector3(t.x,t.y,-t.z)},t.GetRightHandedPositionVector3FromRef=function(e){e.z*=-1},t.GetRightHandedPositionArray3FromRef=function(e){e[2]*=-1},t.GetRightHandedNormalVector3=function(t){return new e.Vector3(t.x,t.y,-t.z)},t.GetRightHandedNormalVector3FromRef=function(e){e.z*=-1},t.GetRightHandedNormalArray3FromRef=function(e){e[2]*=-1},t.GetRightHandedVector4FromRef=function(e){e.z*=-1,e.w*=-1},t.GetRightHandedArray4FromRef=function(e){e[2]*=-1,e[3]*=-1},t.GetRightHandedQuaternionFromRef=function(e){e.x*=-1,e.y*=-1},t.GetRightHandedQuaternionArrayFromRef=function(e){e[0]*=-1,e[1]*=-1},t})();t._GLTFUtilities=r})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));