!(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=c;--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 f=[],c=0;switch(i){case e.VertexBuffer.PositionKind:case e.VertexBuffer.NormalKind:for(var h=t.verticesStart;h=t.verticesStart;--h)c=h*u,f.push(e.Vector4.FromArray(n,c));break;case e.VertexBuffer.ColorKind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)c=h*u,4===l.getSize()?f.push(e.Vector4.FromArray(n,c)):f.push(e.Vector3.FromArray(n,c));break;case e.VertexBuffer.UVKind:case e.VertexBuffer.UV2Kind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)c=h*u,f.push(e.Vector2.FromArray(n,c));break;default:e.Tools.Error("Unsupported Vertex Buffer type: "+i)}this.writeVertexAttributeData(f,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),f=[],c=0;switch(i){case e.VertexBuffer.PositionKind:case e.VertexBuffer.NormalKind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)c=h*u,f.push(e.Vector3.FromArray(n,c));break;case e.VertexBuffer.TangentKind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)c=h*u,f.push(e.Vector4.FromArray(n,c));break;case e.VertexBuffer.ColorKind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)c=h*u,f.push(e.Vector4.FromArray(n,c)),4===l.getSize()?f.push(e.Vector4.FromArray(n,c)):f.push(e.Vector3.FromArray(n,c));break;case e.VertexBuffer.UVKind:case e.VertexBuffer.UV2Kind:for(var h=t.verticesStart+t.verticesCount-1;h>=t.verticesStart;--h)c=h*u,f.push(e.Vector2.FromArray(n,c));break;default:e.Tools.Error("Unsupported Vertex Buffer type: "+i)}this.writeVertexAttributeData(f,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 P=this.babylonScene.materials[w].sideOrientation;if(this.setPrimitiveMode(R,c),this.convertToRightHandedSystem&&P===e.Material.ClockWiseSideOrientation){var N=null!=f?this.bufferViews[f].byteOffset:null;null==N&&(N=0);var I=null;if(null!=f&&(I=l.getIndices()),I)this.reorderIndicesBasedOnPrimitiveMode(_,c,I,N,i);else for(var U=0,O=u;Uthis._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._ConvertMaterialsToGLTF=function(r,a,i,n,o,s,l,u){for(var f=0,c=r;fs.width?(n=r?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 null;var n=t?t.getScene():r.getScene();if(!n)return e.Tools.Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!"),null;var o,s,l=this._ResizeTexturesToSameDimensions(t,r,n),u=l.texture1.getSize(),f=u.width,c=u.height,h=l.texture1.readPixels() ;if(!(h instanceof Uint8Array))return e.Tools.Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: "+l.texture1.name),null;if(o=l.texture1.readPixels(),!((h=l.texture2.readPixels())instanceof Uint8Array))return e.Tools.Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: "+l.texture2.name),null;s=l.texture2.readPixels();for(var d=s.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)||(V=!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)||(S=!0)}if(S){var G=this._CreateBase64FromCanvas(m,f,c,i);M.metallicRoughnessTextureBase64=G}if(V){var P=this._CreateBase64FromCanvas(p,f,c,i);M.baseColorTextureBase64=P}return M},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._ConvertMetalRoughFactorsToMetallicRoughness=function(e,r,a,i,n,o,s,l){var u={baseColor:e.albedoColor,metallic:e.metallic,roughness:e.roughness};if(l){if(e.albedoTexture){var f=t._ExportTexture(e.albedoTexture,r,a,i,n,s);f&&(o.baseColorTexture=f)}if(e.metallicTexture){var f=t._ExportTexture(e.metallicTexture,r,a,i,n,s);null!=f&&(o.metallicRoughnessTexture=f)}}return u},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 f={diffuseColor:r.albedoColor||e.Color3.White(),specularColor:r.reflectivityColor||e.Color3.White(),glossiness:r.microSurface||1},c=null,h=this._GetGLTFTextureSampler(r.albedoTexture);if(null!=h.magFilter&&null!=h.minFilter&&null!=h.wrapS&&null!=h.wrapT&&(o.push(h),c=o.length-1),r.reflectivityTexture&&!r.useMicroSurfaceFromReflectivityMapAlpha)return e.Tools.Error("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture currently not supported"),null;var d=this._ConvertSpecularGlossinessTexturesToMetallicRoughness(r.albedoTexture,r.reflectivityTexture,f,a);if(d){if(u){if(d.baseColorTextureBase64){var m=t._GetTextureInfoFromBase64(d.baseColorTextureBase64,"bjsBaseColorTexture_"+n.length+".png",a,i,n,r.albedoTexture.coordinatesIndex,c,l);null!=m&&(s.baseColorTexture=m)}if(d.metallicRoughnessTextureBase64){var p=t._GetTextureInfoFromBase64(d.metallicRoughnessTextureBase64,"bjsMetallicRoughnessTexture_"+n.length+".png",a,i,n,r.reflectivityTexture.coordinatesIndex,c,l);null!=p&&(s.metallicRoughnessTexture=p)}}}else d=this._ConvertSpecularGlossinessToMetallicRoughness(f);return d},t._ConvertPBRMaterial=function(r,a,i,n,o,s,l,u){var f,c={},h={name:r.name};if(f=r.isMetallicWorkflow()?this._ConvertMetalRoughFactorsToMetallicRoughness(r,a,i,n,o,c,l,u):this._ConvertSpecGlossFactorsToMetallicRoughness(r,a,i,n,o,c,l,u)){if(this.FuzzyEquals(f.baseColor,e.Color3.White(),this._epsilon)&&r.alpha>=this._epsilon||(c.baseColorFactor=[f.baseColor.r,f.baseColor.g,f.baseColor.b,r.alpha]),null!=f.metallic&&1!==f.metallic&&(c.metallicFactor=f.metallic),null!=f.roughness&&1!==f.roughness&&(c.roughnessFactor=f.roughness),null==r.backFaceCulling||r.backFaceCulling||(r.twoSidedLighting||e.Tools.Warn(r.name+": Back-face culling enabled and two-sided lighting disabled is not supported in glTF."),h.doubleSided=!0),u){if(r.bumpTexture){var d=t._ExportTexture(r.bumpTexture,a,i,n,o,l);d&&(h.normalTexture=d,1!==r.bumpTexture.level&&(h.normalTexture.scale=r.bumpTexture.level))}if(r.ambientTexture){var d=t._ExportTexture(r.ambientTexture,a,i,n,o,l);if(d){var m={index:d.index};h.occlusionTexture=m,r.ambientTextureStrength&&(m.strength=r.ambientTextureStrength)}}if(r.emissiveTexture){var d=t._ExportTexture(r.emissiveTexture,a,i,n,o,l);null!=d&&(h.emissiveTexture=d)}}if(this.FuzzyEquals(r.emissiveColor,e.Color3.Black(),this._epsilon)||(h.emissiveFactor=r.emissiveColor.asArray()),null!=r.transparencyMode){var p=t._GetAlphaMode(r);p&&"OPAQUE"!==p&&(h.alphaMode=p,"MASK"===p&&(h.alphaCutoff=r.alphaCutOff))}h.pbrMetallicRoughness=c,s.push(h)}},t.GetPixelsFromTexture=function(t){return t.textureType,e.Engine.TEXTURETYPE_UNSIGNED_INT,t.readPixels()},t._ExportTexture=function(r,a,i,n,o,s){for(var l=t._GetGLTFTextureSampler(r),u=null,f=null,c=0;cf[d]&&(f[d]=m),++o}}return{min:u,max:f}},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}));