!(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;s0&&(u?(t._GLTFMaterial._HasTexturesPresent(this.materials[v])||(delete c.attributes.TEXCOORD_0,delete c.attributes.TEXCOORD_1),c.material=v):t._GLTFMaterial._HasTexturesPresent(this.materials[v])&&(m=t._GLTFMaterial._StripTexturesFromMaterial(this.materials[v]),this.materials.push(m),c.material=this.materials.length-1))),r.primitives.push(c)}}}},r.prototype.createScene=function(r,a){if(r.meshes.length){var i,n,o,s=r.meshes,l={nodes:[]};t._GLTFMaterial._ConvertMaterialsToGLTF(r.materials,"image/png",this.images,this.textures,this.materials,this.imageData,!0),this.nodeMap=this.createNodeMapAndAnimations(r,a),this.totalByteLength=a.getByteOffset();var u=this;s.forEach((function(t){if(i=u.nodeMap[t.uniqueId],n=u.nodes[i],t.parent||(u.shouldExportMesh(t)?l.nodes.push(i):e.Tools.Log("Omitting "+t.name+" from scene.")),o=t.getDescendants(!0),!n.children&&o&&o.length){n.children=[];for(var r=0,a=o;rthis._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setUint8(this._byteOffset++,e)},e.prototype.setFloat32=function(e){this._byteOffset+4>this._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setFloat32(this._byteOffset,e,!0),this._byteOffset+=4},e.prototype.setUInt32=function(e){this._byteOffset+4>this._arrayBuffer.byteLength&&this.resizeBuffer(2*this._arrayBuffer.byteLength),this._dataView.setUint32(this._byteOffset,e,!0),this._byteOffset+=4},e})();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){for(var u=0;us.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(),c=u.width,h=u.height,f=l.texture1.readPixels();if(!(f instanceof Uint8Array))return e.Tools.Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: "+l.texture1.name),null;if(o=l.texture1.readPixels(),!((f=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 p=s.byteLength,m=new Uint8Array(p),d=new Uint8Array(p),g=e.Color3.Black(),T=0,y=0,x=0;xthis._epsilon?M.baseColor.r:1,d[S+1]/=M.baseColor.g>this._epsilon?M.baseColor.g:1,d[S+2]/=M.baseColor.b>this._epsilon?M.baseColor.b:1;var L=e.Color3.FromInts(d[S],d[S+1],d[S+2]),w=L.toGammaSpace();d[S]=255*w.r,d[S+1]=255*w.g,d[S+2]=255*w.b,this.FuzzyEquals(w,e.Color3.White(),this._epsilon)||(R=!0),m[S+1]/=M.roughness>this._epsilon?M.roughness:1,m[S+2]/=M.metallic>this._epsilon?M.metallic:1;var P=e.Color3.FromInts(255,m[S+1],m[S+2]);this.FuzzyEquals(P,e.Color3.White(),this._epsilon)||(E=!0)}if(E){var V=this._CreateBase64FromCanvas(m,c,h,i);M.metallicRoughnessTextureBase64=V}if(R){var G=this._CreateBase64FromCanvas(d,c,h,i);M.baseColorTextureBase64=G}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){var l={baseColor:e.albedoColor,metallic:e.metallic,roughness:e.roughness};if(s){if(e.albedoTexture){var u=t._ExportTexture(e.albedoTexture,r,a,i,o);u&&(n.baseColorTexture=u)}if(e.metallicTexture){var u=t._ExportTexture(e.metallicTexture,r,a,i,o);null!=u&&(n.metallicRoughnessTexture=u)}}return l},t._ConvertSpecGlossFactorsToMetallicRoughness=function(r,a,i,n,o,s,l){var u={diffuseColor:r.albedoColor||e.Color3.White(),specularColor:r.reflectivityColor||e.Color3.White(),glossiness:r.microSurface||1};if(r.reflectivityTexture&&!r.useMicroSurfaceFromReflectivityMapAlpha)return e.Tools.Error("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture currently not supported"),null;var c=this._ConvertSpecularGlossinessTexturesToMetallicRoughness(r.albedoTexture,r.reflectivityTexture,u,a);if(c){if(l){if(c.baseColorTextureBase64){var h=t._GetTextureInfoFromBase64(c.baseColorTextureBase64,"bjsBaseColorTexture_"+n.length+".png",a,i,n,s);null!=h&&(o.baseColorTexture=h)}if(c.metallicRoughnessTextureBase64){var f=t._GetTextureInfoFromBase64(c.metallicRoughnessTextureBase64,"bjsMetallicRoughnessTexture_"+n.length+".png",a,i,n,s);null!=f&&(o.metallicRoughnessTexture=f)}}}else c=this._ConvertSpecularGlossinessToMetallicRoughness(u);return c},t._ConvertPBRMaterial=function(r,a,i,n,o,s,l){var u,c={},h={name:r.name};if(u=r.isMetallicWorkflow()?this._ConvertMetalRoughFactorsToMetallicRoughness(r,a,i,n,c,s,l):this._ConvertSpecGlossFactorsToMetallicRoughness(r,a,i,n,c,s,l)){if(this.FuzzyEquals(u.baseColor,e.Color3.White(),this._epsilon)&&r.alpha>=this._epsilon||(c.baseColorFactor=[u.baseColor.r,u.baseColor.g,u.baseColor.b,r.alpha]),null!=u.metallic&&1!==u.metallic&&(c.metallicFactor=u.metallic),null!=u.roughness&&1!==u.roughness&&(c.roughnessFactor=u.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),l){if(r.bumpTexture){var f=t._ExportTexture(r.bumpTexture,a,i,n,s);f&&(h.normalTexture=f)}if(r.ambientTexture){var f=t._ExportTexture(r.ambientTexture,a,i,n,s);if(f){var p={index:f.index};h.occlusionTexture=p,r.ambientTextureStrength&&(p.strength=r.ambientTextureStrength)}}if(r.emissiveTexture){var f=t._ExportTexture(r.emissiveTexture,a,i,n,s);null!=f&&(h.emissiveTexture=f)}}if(this.FuzzyEquals(r.emissiveColor,e.Color3.Black(),this._epsilon)||(h.emissiveFactor=r.emissiveColor.asArray()),null!=r.transparencyMode){var m=t._GetAlphaMode(r);m&&"OPAQUE"!==m&&(h.alphaMode=m,"BLEND"===m&&(h.alphaCutoff=r.alphaCutOff))}h.pbrMetallicRoughness=c,o.push(h)}},t.GetPixelsFromTexture=function(t){return t.textureType,e.Engine.TEXTURETYPE_UNSIGNED_INT,t.readPixels()},t._ExportTexture=function(r,a,i,n,o){var s="texture_"+(n.length-1).toString(),l=r.getInternalTexture();null!=l&&(s=l.url||s),s=e.Tools.GetFilename(s);var u=s.split(".")[0],c="";if("image/jpeg"===a)c=".jpg";else{if("image/png"!==a)return e.Tools.Error("Unsupported mime type "+a),null;c=".png"}s=u+c;var h=t.GetPixelsFromTexture(r),f=r.getSize(),p=this._CreateBase64FromCanvas(h,f.width,f.height,a);return this._GetTextureInfoFromBase64(p,s,a,i,n,o)},t._GetTextureInfoFromBase64=function(e,t,r,a,i,n){for(var o=null,s={source:a.length},l=atob(e.split(",")[1]),u=new ArrayBuffer(l.length),c=new Uint8Array(u),h=0,f=l.length;hc[p]&&(c[p]=m),++o}}return{min:u,max:c}},t.GetRightHandedVector3=function(t){return new e.Vector3(t.x,t.y,-t.z)},t.GetRightHandedVector3FromRef=function(e){e.z*=-1},t.GetRightHandedVector4FromRef=function(e){e.z*=-1,e.w*=-1},t.GetRightHandedQuaternionFromRef=function(e){e.x*=-1,e.y*=-1},t})();t._GLTFUtilities=r})(e.GLTF2||(e.GLTF2={}))})(e||(e={})),e}));