!(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 e;this&&this.__decorate,this&&this.__extends||(function(){var e=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])}})();!(function(e){var t=(function(){function t(){}return t.OBJ=function(t,r,a,i){var n=[],o=1;r&&(a||(a="mat"),n.push("mtllib "+a+".mtl"));for(var s=0;s=f;--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=[],f=0;switch(i){case e.VertexBuffer.PositionKind:case e.VertexBuffer.NormalKind:for(var h=t.verticesStart;h=t.verticesStart;--h)f=h*u,c.push(e.Vector4.FromArray(n,f));break;case e.VertexBuffer.ColorKind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)f=h*u,4===l.getSize()?c.push(e.Vector4.FromArray(n,f)):c.push(e.Vector3.FromArray(n,f));break;case e.VertexBuffer.UVKind:case e.VertexBuffer.UV2Kind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)f=h*u,c.push(e.Vector2.FromArray(n,f));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=[],f=0;switch(i){case e.VertexBuffer.PositionKind:case e.VertexBuffer.NormalKind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)f=h*u,c.push(e.Vector3.FromArray(n,f));break;case e.VertexBuffer.TangentKind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)f=h*u,c.push(e.Vector4.FromArray(n,f));break;case e.VertexBuffer.ColorKind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)f=h*u,c.push(e.Vector4.FromArray(n,f)),4===l.getSize()?c.push(e.Vector4.FromArray(n,f)):c.push(e.Vector3.FromArray(n,f));break;case e.VertexBuffer.UVKind:case e.VertexBuffer.UV2Kind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)f=h*u,c.push(e.Vector2.FromArray(n,f));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 N=this.babylonScene.materials[S].sideOrientation;if(this.convertToRightHandedSystem&&N===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,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={}))})(e||(e={}));var e;!(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})(e||(e={}));var e;!(function(e){!(function(t){var r=(function(){function t(){}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._ConvertMaterialsToGLTFAsync=function(r,a,i,n,o,s,l,u,c){for(var f=[],h=0,d=r;hs.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._ConvertSpecularGlossinessTexturesToMetallicRoughness=function(t,r,a,i){if(!t&&!r)return e.Tools.Warn("_ConvertSpecularGlosinessTexturesToMetallicRoughness: diffuse and specular glossiness textures are not defined!"),null;var n=t?t.getScene():r?r.getScene():null;if(n){var o=this._ResizeTexturesToSameDimensions(t,r,n),s=o.texture1.getSize(),l=void 0,u=void 0,c=s.width,f=s.height,h=o.texture1.readPixels();if(h instanceof Uint8Array){if(l=o.texture1.readPixels(),(h=o.texture2.readPixels())instanceof Uint8Array){u=o.texture2.readPixels();for(var d=u.byteLength,m=new Uint8Array(d),p=new Uint8Array(d),g=e.Color3.Black(),T=0,y=0,v=0;vthis._epsilon?M.baseColor.r:1,p[B+1]/=M.baseColor.g>this._epsilon?M.baseColor.g:1,p[B+2]/=M.baseColor.b>this._epsilon?M.baseColor.b:1;var E=e.Color3.FromInts(p[B],p[B+1],p[B+2]),w=E.toGammaSpace();p[B]=255*w.r,p[B+1]=255*w.g,p[B+2]=255*w.b,this.FuzzyEquals(w,e.Color3.White(),this._epsilon)||(R=!0),m[B+1]/=M.roughness>this._epsilon?M.roughness:1,m[B+2]/=M.metallic>this._epsilon?M.metallic:1;var L=e.Color3.FromInts(255,m[B+1],m[B+2]);this.FuzzyEquals(L,e.Color3.White(),this._epsilon)||(V=!0)}if(V){var G=this._CreateBase64FromCanvas(m,c,f,i);M.metallicRoughnessTextureBase64=G}if(R){var P=this._CreateBase64FromCanvas(p,c,f,i);M.baseColorTextureBase64=P}return M}e.Tools.Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: "+o.texture2.name)}else e.Tools.Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: "+o.texture1.name)}else e.Tools.Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!");return null},t._ConvertSpecularGlossinessToMetallicRoughness=function(r){var a=t._GetPerceivedBrightness(r.diffuseColor),i=t._GetPerceivedBrightness(r.specularColor),n=1-t._GetMaxComponent(r.specularColor),o=t._SolveMetallic(a,i,n),s=r.diffuseColor.scale(n/(1-this._dielectricSpecular.r)/Math.max(1-o,this._epsilon)),l=r.specularColor.subtract(this._dielectricSpecular.scale(1-o)).scale(1/Math.max(o,this._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._GetPerceivedBrightness=function(e){return e?Math.sqrt(.299*e.r*e.r+.587*e.g*e.g+.114*e.b*e.b):0},t._GetMaxComponent=function(e){return e?Math.max(e.r,Math.max(e.g,e.b)):0},t._ConvertMetalRoughFactorsToMetallicRoughnessAsync=function(e,r,a,i,n,o,s,l){var u=this._GetAlphaMode(e),c="OPAQUE"!==u,f=[],h={baseColor:e.albedoColor,metallic:e.metallic,roughness:e.roughness};if(l){if(e.albedoTexture){var d=t._ExportTextureAsync(e.albedoTexture,r,a,i,n,s,c).then((function(e){e&&(o.baseColorTexture=e)}));f.push(d)}if(e.metallicTexture){var d=t._ExportTextureAsync(e.metallicTexture,r,a,i,n,s,c).then((function(e){e&&(o.metallicRoughnessTexture=e)}));f.push(d)}}return Promise.all(f).then((function(){return h}))},t._GetGLTFTextureSampler=function(r){var a=t._GetGLTFTextureWrapModesSampler(r),i=r instanceof e.Texture?r.samplingMode:null;if(null!=i)switch(i){case e.Texture.LINEAR_LINEAR:a.magFilter=9729,a.minFilter=9729;break;case e.Texture.LINEAR_NEAREST:a.magFilter=9729,a.minFilter=9728;break;case e.Texture.NEAREST_LINEAR:a.magFilter=9728,a.minFilter=9729;break;case e.Texture.NEAREST_LINEAR_MIPLINEAR:a.magFilter=9728,a.minFilter=9987;break;case e.Texture.NEAREST_NEAREST:a.magFilter=9728,a.minFilter=9728;break;case e.Texture.NEAREST_LINEAR_MIPNEAREST:a.magFilter=9728,a.minFilter=9985;break;case e.Texture.LINEAR_NEAREST_MIPNEAREST:a.magFilter=9729,a.minFilter=9984;break;case e.Texture.LINEAR_NEAREST_MIPLINEAR:a.magFilter=9729,a.minFilter=9986;break;case e.Texture.NEAREST_NEAREST_MIPLINEAR:a.magFilter=9728,a.minFilter=9986;break;case e.Texture.LINEAR_LINEAR_MIPLINEAR:a.magFilter=9729,a.minFilter=9987;break;case e.Texture.LINEAR_LINEAR_MIPNEAREST:a.magFilter=9729,a.minFilter=9985;break;case e.Texture.NEAREST_NEAREST_MIPNEAREST:a.magFilter=9728,a.minFilter=9984}return a},t._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._GetGLTFTextureWrapModesSampler=function(r){var a=t._GetGLTFTextureWrapMode(r instanceof e.Texture?r.wrapU:e.Texture.WRAP_ADDRESSMODE),i=t._GetGLTFTextureWrapMode(r instanceof e.Texture?r.wrapV:e.Texture.WRAP_ADDRESSMODE);return 10497===a&&10497===i?{}:{wrapS:a,wrapT:i}},t._ConvertSpecGlossFactorsToMetallicRoughness=function(r,a,i,n,o,s,l,u){var c={diffuseColor:r.albedoColor||e.Color3.White(),specularColor:r.reflectivityColor||e.Color3.White(),glossiness:r.microSurface||1},f=null,h=this._GetGLTFTextureSampler(r.albedoTexture);if(null!=h.magFilter&&null!=h.minFilter&&null!=h.wrapS&&null!=h.wrapT&&(o.push(h),f=o.length-1),r.reflectivityTexture&&!r.useMicroSurfaceFromReflectivityMapAlpha)return e.Tools.Error("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture are currently not supported"),null;var d=this._ConvertSpecularGlossinessTexturesToMetallicRoughness(r.albedoTexture,r.reflectivityTexture,c,a);if(d&&u){if(d.baseColorTextureBase64){var m=t._GetTextureInfoFromBase64(d.baseColorTextureBase64,"bjsBaseColorTexture",a,i,n,r.albedoTexture?r.albedoTexture.coordinatesIndex:null,f,l);null!=m&&(s.baseColorTexture=m)}if(d.metallicRoughnessTextureBase64){var p=t._GetTextureInfoFromBase64(d.metallicRoughnessTextureBase64,"bjsMetallicRoughnessTexture",a,i,n,r.reflectivityTexture?r.reflectivityTexture.coordinatesIndex:null,f,l);null!=p&&(s.metallicRoughnessTexture=p)}return d}return this._ConvertSpecularGlossinessToMetallicRoughness(c)},t._ConvertPBRMaterialAsync=function(e,r,a,i,n,o,s,l,u){var c={},f={name:e.name};if(e.isMetallicWorkflow())return e.albedoColor&&(c.baseColorFactor=[e.albedoColor.r,e.albedoColor.g,e.albedoColor.b,e.alpha]),this._ConvertMetalRoughFactorsToMetallicRoughnessAsync(e,r,a,i,n,c,l,u).then((function(h){return t.SetMetallicRoughnessPbrMaterial(h,e,f,c,r,a,i,n,o,s,l,u)}));var h=this._ConvertSpecGlossFactorsToMetallicRoughness(e,r,a,i,n,c,l,u);return t.SetMetallicRoughnessPbrMaterial(h,e,f,c,r,a,i,n,o,s,l,u)},t.SetMetallicRoughnessPbrMaterial=function(r,a,i,n,o,s,l,u,c,f,h,d){var m=[];if(r){var p=null,g=!1;if(null!=a.transparencyMode&&(p=t._GetAlphaMode(a))&&"OPAQUE"!==p&&(g=!0,i.alphaMode=p,"MASK"===p&&(i.alphaCutoff=a.alphaCutOff)),this.FuzzyEquals(r.baseColor,e.Color3.White(),this._epsilon)&&a.alpha>=this._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),d){if(a.bumpTexture){var T=t._ExportTextureAsync(a.bumpTexture,o,s,l,u,h,g).then((function(e){e&&(i.normalTexture=e,1!==a.bumpTexture.level&&(i.normalTexture.scale=a.bumpTexture.level))}));m.push(T)}if(a.ambientTexture){var T=t._ExportTextureAsync(a.ambientTexture,o,s,l,u,h,g).then((function(e){if(e){var t={index:e.index};i.occlusionTexture=t,a.ambientTextureStrength&&(t.strength=a.ambientTextureStrength)}}));m.push(T)}if(a.emissiveTexture){var T=t._ExportTextureAsync(a.emissiveTexture,o,s,l,u,h,g).then((function(e){e&&(i.emissiveTexture=e)}));m.push(T)}}this.FuzzyEquals(a.emissiveColor,e.Color3.Black(),this._epsilon)||(i.emissiveFactor=a.emissiveColor.asArray()),i.pbrMetallicRoughness=n,c.push(i),f[a.uniqueId]=c.length-1}return Promise.all(m).then((function(e){}))},t.GetPixelsFromTexture=function(t){return t.textureType,e.Engine.TEXTURETYPE_UNSIGNED_INT,t.readPixels()},t._ExportTextureAsync=function(e,r,a,i,n,o,s){for(var l=this,u=t._GetGLTFTextureSampler(e),c=null,f=null,h=0;hc[d]&&(c[d]=m),++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={}))})(e||(e={})),e}));