var __decorate=this&&this.__decorate||function(e,t,r,a){var i,s=arguments.length,n=s<3?t:null===a?a=Object.getOwnPropertyDescriptor(t,r):a;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)n=Reflect.decorate(e,t,r,a);else for(var o=e.length-1;o>=0;o--)(i=e[o])&&(n=(s<3?i(n):s>3?i(t,r,n):i(t,r))||n);return s>3&&n&&Object.defineProperty(t,r,n),n},__extends=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])};return function(t,r){function a(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(a.prototype=r.prototype,new a)}})();!(function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("babylonjs")):"function"==typeof define&&define.amd?define("babylonjs-serializers",["babylonjs"],t):"object"==typeof exports?exports["babylonjs-serializers"]=t(require("babylonjs")):e.BABYLON=t(e.BABYLON)})(this,(function(e){var e;!(function(e){var t=(function(){function t(){}return t.OBJ=function(t,r,a,i){var s=[],n=1;r&&(a||(a="mat"),s.push("mtllib "+a+".mtl"));for(var o=0;on[f]&&(n[f]=p),++u}return{min:s,max:n}},r.GetRightHandedVector3=function(t){return new e.Vector3(t.x,t.y,-t.z)},r.GetRightHandedVector4=function(t){return new e.Vector4(t.x,t.y,-t.z,-t.w)},r.GetRightHandedQuaternion=function(t){return new e.Quaternion(-t.x,-t.y,t.z,t.w)},r.prototype.writeAttributeData=function(t,a,i,s,n,o){for(var l=n,u=a.length/i,h=0;h0)if(T||!t._GLTFMaterial._HasTexturesPresent(this.materials[P]))_.material=P;else{var O=t._GLTFMaterial._StripTexturesFromMaterial(this.materials[P]);this.materials.push(O),_.material=this.materials.length-1}}r.primitives.push(_)}}return i},r.prototype.createScene=function(r,a){if(r.meshes.length){var i=r.meshes,s={nodes:new Array};t._GLTFMaterial._ConvertMaterialsToGLTF(r.materials,"image/png",this.images,this.textures,this.materials,this.imageData,!0);var n=this.createNodeMap(r,a);this.nodeMap=n.nodeMap,this.totalByteLength=n.byteOffset,this.binaryBuffer=new ArrayBuffer(this.totalByteLength);for(var o=new DataView(this.binaryBuffer),l=0;lo.width?(s=r?e.TextureTools.CreateResizedCopy(r,n.width,n.height,!0):this._CreateWhiteTexture(n.width,n.height,a),i=t):(i=t,s=r),{texture1:i,texture2:s}},t._ConvertSpecularGlossinessTexturesToMetallicRoughness=function(t,r,a,i){if(!t&&!r)return null;var s=t?t.getScene():r.getScene();if(!s)throw new Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!");var n,o,l=this._ResizeTexturesToSameDimensions(t,r,s),u=l.texture1.getSize(),h=u.width,c=u.height,f=l.texture1.readPixels();if(!(f instanceof Uint8Array))throw new Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: "+l.texture1.name);if(n=l.texture1.readPixels(),!((f=l.texture2.readPixels())instanceof Uint8Array))throw new Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Pixel array buffer type not supported for texture: "+l.texture2.name);o=l.texture2.readPixels();for(var p=o.byteLength,d=new Uint8Array(p),g=new Uint8Array(p),m=e.Color3.Black(),x=0,v=0,T=0;Tthis._epsilon?A.baseColor.r:1,g[S+1]/=A.baseColor.g>this._epsilon?A.baseColor.g:1,g[S+2]/=A.baseColor.b>this._epsilon?A.baseColor.b:1;var E=new e.Color3(g[S],g[S+1],g[S+2]);this.FuzzyEquals(E,e.Color3.White(),this._epsilon)||(R=!0),d[S+1]/=A.roughness>this._epsilon?A.roughness:1,d[S+2]/=A.metallic>this._epsilon?A.metallic:1;var V=new e.Color3(d[S],d[S+1],d[S+2]);this.FuzzyEquals(V,e.Color3.White(),this._epsilon)||(F=!0)}if(F){var L=this._CreateBase64FromCanvas(d,h,c,i);A.metallicRoughnessTextureBase64=L}if(R){var P=this._CreateBase64FromCanvas(g,h,c,i);A.baseColorTextureBase64=P}return A},t._ConvertSpecularGlossinessToMetallicRoughness=function(r){var a=t._GetPerceivedBrightness(r.diffuseColor),i=t._GetPerceivedBrightness(r.specularColor),s=1-t._GetMaxComponent(r.specularColor),n=t._SolveMetallic(a,i,s),o=r.diffuseColor.scale(s/(1-this._dielectricSpecular.r)/Math.max(1-n,this._epsilon)),l=r.specularColor.subtract(this._dielectricSpecular.scale(1-n)).scale(1/Math.max(n,this._epsilon)),u=e.Color3.Lerp(o,l,n*n);return u=u.clampToRef(0,1,u),{baseColor:u,metallic:n,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._ConvertPBRMaterial=function(r,a,i,s,n,o,l){var u,h={},c={name:r.name},f=r.isMetallicWorkflow();if(f)u={baseColor:r.albedoColor,metallic:r.metallic,roughness:r.roughness};else{var p={diffuseColor:r.albedoColor||e.Color3.White(),specularColor:r.reflectivityColor||e.Color3.White(),glossiness:r.microSurface||1};if(r.reflectivityTexture&&!r.useMicroSurfaceFromReflectivityMapAlpha)throw new Error("_ConvertPBRMaterial: Glossiness values not included in the reflectivity texture currently not supported");if(u=this._ConvertSpecularGlossinessTexturesToMetallicRoughness(r.albedoTexture,r.reflectivityTexture,p,a)){if(l){if(u.baseColorTextureBase64){var d=t._GetTextureInfoFromBase64(u.baseColorTextureBase64,"bjsBaseColorTexture_"+s.length+".png",a,i,s,o);null!=d&&(h.baseColorTexture=d)}if(u.metallicRoughnessTextureBase64){var g=t._GetTextureInfoFromBase64(u.metallicRoughnessTextureBase64,"bjsMetallicRoughnessTexture_"+s.length+".png",a,i,s,o);null!=g&&(h.metallicRoughnessTexture=g)}}}else u=this._ConvertSpecularGlossinessToMetallicRoughness(p)}if(this.FuzzyEquals(u.baseColor,e.Color3.White(),this._epsilon)&&r.alpha>=this._epsilon||(h.baseColorFactor=[u.baseColor.r,u.baseColor.g,u.baseColor.b,r.alpha]),null!=u.metallic&&1!==u.metallic&&(h.metallicFactor=u.metallic),null!=u.roughness&&1!==u.roughness&&(h.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."),c.doubleSided=!0),l){if(f&&r.albedoTexture){var m=t._ExportTexture(r.albedoTexture,a,i,s,o);m&&(h.baseColorTexture=m)}if(r.bumpTexture){var m=t._ExportTexture(r.bumpTexture,a,i,s,o);m&&(c.normalTexture=m)}if(r.ambientTexture){var m=t._ExportTexture(r.ambientTexture,a,i,s,o);if(m){var x={index:m.index};c.occlusionTexture=x,r.ambientTextureStrength&&(x.strength=r.ambientTextureStrength)}}if(r.emissiveTexture){var m=t._ExportTexture(r.emissiveTexture,a,i,s,o);null!=m&&(c.emissiveTexture=m)}if(r.metallicTexture){var m=t._ExportTexture(r.metallicTexture,a,i,s,o);null!=m&&(h.metallicRoughnessTexture=m)}}if(this.FuzzyEquals(r.emissiveColor,e.Color3.Black(),this._epsilon)||(c.emissiveFactor=r.emissiveColor.asArray()),null!=r.transparencyMode){var v=t._GetAlphaMode(r);"OPAQUE"!==v&&(c.alphaMode=v,"BLEND"===v&&(c.alphaCutoff=r.alphaCutOff))}c.pbrMetallicRoughness=h,n.push(c)},t._ExportTexture=function(t,r,a,i,s){var n="texture_"+(i.length-1).toString(),o=t.getInternalTexture();null!=o&&(n=o.url||n),n=e.Tools.GetFilename(n);var l=n.split(".")[0],u="";if("image/jpeg"===r)u=".jpg";else{if("image/png"!==r)throw new Error("Unsupported mime type "+r);u=".png"}n=l+u;var h=t.readPixels(),c=t.getSize(),f=this._CreateBase64FromCanvas(h,c.width,c.height,r);return this._GetTextureInfoFromBase64(f,n,r,a,i,s)},t._GetTextureInfoFromBase64=function(e,t,r,a,i,s){for(var n=null,o={source:a.length},l=atob(e.split(",")[1]),u=new Uint8Array(l.length),h=0;h