!(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.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;nthis._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setUint8(this._byteOffset++,e))},e.prototype.getUInt32=function(e){if(ethis._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&&(tthis._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?tthis._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;io.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;aB._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;nl[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