!(function(e,t){var r=[],n=e.BABYLON||this.BABYLON;"object"==typeof exports&&"object"==typeof module?(n=n||require("babylonjs"),module.exports=t(n)):"function"==typeof define&&define.amd?(r.push("babylonjs"),define("babylonjs-serializers",r,t)):"object"==typeof exports?(n=n||require("babylonjs"),exports["babylonjs-serializers"]=t(n)):e.BABYLON=t(n)})(this,(function(e){e=e||this.BABYLON;var t,b,r,a,n,W,i,o,s,L,l,u,c,M,f,d,h,p,m,g,_,y,x,T,v;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,n){var i=[],a=1;t&&(r||(r="mat"),i.push("mtllib "+r+".mtl"));for(var o=0;o=e.verticesStart;--f)c=f*l,u.push(W.Vector4.FromArray(i,c));break;case W.VertexBuffer.ColorKind:for(f=e.verticesStart+e.verticesCount-1;f>=e.verticesStart;--f)c=f*l,4===s.getSize()?u.push(W.Vector4.FromArray(i,c)):u.push(W.Vector3.FromArray(i,c));break;case W.VertexBuffer.UVKind:case W.VertexBuffer.UV2Kind:for(f=e.verticesStart+e.verticesCount-1;f>=e.verticesStart;--f)c=f*l,u.push(W.Vector2.FromArray(i,c));break;default:W.Tools.Error("Unsupported Vertex Buffer type: "+n)}this.writeVertexAttributeData(u,a+12,n,i,o)}else W.Tools.Warn("reorderTriangleStripDrawMode: Vertex buffer kind "+n+" not present!")},u.prototype.reorderTriangleFanMode=function(e,t,r,n,i,a,o){var s=this.getVertexBufferFromMesh(n,e.getMesh());if(s){var l=s.byteStride/W.VertexBuffer.GetTypeByteLength(s.type),u=[],c=0;switch(n){case W.VertexBuffer.PositionKind:case W.VertexBuffer.NormalKind:for(var f=e.verticesStart+e.verticesCount-1;f>=e.verticesStart;--f)c=f*l,u.push(W.Vector3.FromArray(i,c));break;case W.VertexBuffer.TangentKind:for(f=e.verticesStart+e.verticesCount-1;f>=e.verticesStart;--f)c=f*l,u.push(W.Vector4.FromArray(i,c));break;case W.VertexBuffer.ColorKind:for(f=e.verticesStart+e.verticesCount-1;f>=e.verticesStart;--f)c=f*l,u.push(W.Vector4.FromArray(i,c)),4===s.getSize()?u.push(W.Vector4.FromArray(i,c)):u.push(W.Vector3.FromArray(i,c));break;case W.VertexBuffer.UVKind:case W.VertexBuffer.UV2Kind:for(f=e.verticesStart+e.verticesCount-1;f>=e.verticesStart;--f)c=f*l,u.push(W.Vector2.FromArray(i,c));break;default:W.Tools.Error("Unsupported Vertex Buffer type: "+n)}this.writeVertexAttributeData(u,a,n,i,o)}else W.Tools.Warn("reorderTriangleFanMode: Vertex buffer kind "+n+" not present!")},u.prototype.writeVertexAttributeData=function(e,t,r,n,i){for(var a=0,o=e;athis._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setUint8(this._byteOffset++,e))},e.prototype.getUInt32=function(e){if(ethis._byteOffset?W.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?W.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?W.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?W.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)&&W.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})();z._BinaryWriter=r})((i=W.GLTF2||(W.GLTF2={})).Exporter||(i.Exporter={})),o=e||(e={}),s=(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 n=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([n],i)),r.click()}},e})(),o.GLTFData=s,L=e||(e={}),l=L.GLTF2||(L.GLTF2={}),u=l.Exporter||(l.Exporter={}),c=(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 n=[],i=0,a=e;io.width?(i=t&&t instanceof L.Texture?L.TextureTools.CreateResizedCopy(t,a.width,a.height,!0):this._createWhiteTexture(a.width,a.height,r),n=e):(n=e,i=t),{texture1:n,texture2:i}},B.prototype._convertPixelArrayToFloat32=function(e){if(e instanceof Uint8Array){for(var t=e.length,r=new Float32Array(e.length),n=0;nB._Epsilon?E.baseColor.r:1,g[R+1]/=E.baseColor.g>B._Epsilon?E.baseColor.g:1,g[R+2]/=E.baseColor.b>B._Epsilon?E.baseColor.b:1;var V=L.Color3.FromInts(g[R],g[R+1],g[R+2]).toGammaSpace();g[R]=255*V.r,g[R+1]=255*V.g,g[R+2]=255*V.b,B.FuzzyEquals(V,L.Color3.White(),B._Epsilon)||(S=!0),m[R+1]/=E.roughness>B._Epsilon?E.roughness:1,m[R+2]/=E.metallic>B._Epsilon?E.metallic:1;var C=L.Color3.FromInts(255,m[R+1],m[R+2]);B.FuzzyEquals(C,L.Color3.White(),B._Epsilon)||(M=!0)}if(M){var w=this._createBase64FromCanvasAsync(m,c,f,n).then((function(e){E.metallicRoughnessTextureBase64=e}));i.push(w)}return S&&(w=this._createBase64FromCanvasAsync(g,c,f,n).then((function(e){E.baseColorTextureBase64=e})),i.push(w)),Promise.all(i).then((function(){return E}))}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),n=1-this._getMaxComponent(e.specularColor),i=B._SolveMetallic(t,r,n),a=e.diffuseColor.scale(n/(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(a,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,n){var i=[],a={baseColor:e.albedoColor,metallic:e.metallic,roughness:e.roughness};return n&&(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 a}))},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(a,o,s,l){var u=this;return Promise.resolve().then((function(){var e=u._exporter._samplers,n=u._exporter._textures,t={diffuseColor:a.albedoColor||L.Color3.White(),specularColor:a.reflectivityColor||L.Color3.White(),glossiness:a.microSurface||1},i=null,r=u._getGLTFTextureSampler(a.albedoTexture);return null!=r.magFilter&&null!=r.minFilter&&null!=r.wrapS&&null!=r.wrapT&&(e.push(r),i=e.length-1),a.reflectivityTexture&&!a.useMicroSurfaceFromReflectivityMapAlpha?Promise.reject("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture are currently not supported"):(a.albedoTexture||a.reflectivityTexture)&&l?u._convertSpecularGlossinessTexturesToMetallicRoughnessAsync(a.albedoTexture,a.reflectivityTexture,t,o).then((function(e){if(e.baseColorTextureBase64){var t=u._getTextureInfoFromBase64(e.baseColorTextureBase64,"bjsBaseColorTexture_"+n.length+".png",o,a.albedoTexture?a.albedoTexture.coordinatesIndex:null,i);t&&(s.baseColorTexture=t)}if(e.metallicRoughnessTextureBase64){var r=u._getTextureInfoFromBase64(e.metallicRoughnessTextureBase64,"bjsMetallicRoughnessTexture_"+n.length+".png",o,a.reflectivityTexture?a.reflectivityTexture.coordinatesIndex:null,i);r&&(s.metallicRoughnessTexture=r)}return e})):u._convertSpecularGlossinessToMetallicRoughness(t)}))},B.prototype._convertPBRMaterialAsync=function(t,r,n){var i=this,a={},o={name:t.name};return t.isMetallicWorkflow()?(t.albedoColor&&(a.baseColorFactor=[t.albedoColor.r,t.albedoColor.g,t.albedoColor.b,t.alpha]),this._convertMetalRoughFactorsToMetallicRoughnessAsync(t,r,a,n).then((function(e){return i.setMetallicRoughnessPbrMaterial(e,t,o,a,r,n)}))):this._convertSpecGlossFactorsToMetallicRoughnessAsync(t,r,a,n).then((function(e){return i.setMetallicRoughnessPbrMaterial(e,t,o,a,r,n)}))},B.prototype.setMetallicRoughnessPbrMaterial=function(e,r,n,t,i,a){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"===(n.alphaMode=u)&&(n.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."),n.doubleSided=!0),a){if(r.bumpTexture){var c=this._exportTextureAsync(r.bumpTexture,i).then((function(e){e&&(n.normalTexture=e,1!==r.bumpTexture.level&&(n.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};n.occlusionTexture=t,r.ambientTextureStrength&&(t.strength=r.ambientTextureStrength)}})),l.push(c)),r.emissiveTexture&&(c=this._exportTextureAsync(r.emissiveTexture,i).then((function(e){e&&(n.emissiveTexture=e)})),l.push(c))}B.FuzzyEquals(r.emissiveColor,L.Color3.Black(),B._Epsilon)||(n.emissiveFactor=r.emissiveColor.asArray()),n.pbrMetallicRoughness=t,s.push(n),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(t,r){var n=this,e=this._exporter._extensionsPreExportTextureAsync("exporter",t,r);return e?e.then((function(e){return e?n._exportTextureInfoAsync(e,r):n._exportTextureInfoAsync(t,r)})):this._exportTextureInfoAsync(t,r)},B.prototype._exportTextureInfoAsync=function(u,c){var f=this;return Promise.resolve().then((function(){var r=u.uid;if(r in f._textureMap)return f._textureMap[r];for(var e=f._exporter._samplers,t=f._getGLTFTextureSampler(u),n=null,i=null,a=0;al[f]&&(l[f]=h),++i}}return{min:s,max:l}},p._GetRightHandedPositionVector3=function(e){return new d.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 d.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