var BABYLON;!(function(e){var t=(function(){function t(){}return t.GLTF=function(t,r,a){var i=r.replace(/\.[^\/.]+$/,""),s=new e.GLTF2._Exporter(t,a);if(t.isReady)return s._generateGLTF(i);throw new Error("glTF Serializer: Scene is not ready!")},t.GLB=function(t,r,a){var i=r.replace(/\.[^\/.]+$/,""),s=new e.GLTF2._Exporter(t,a);if(t.isReady)return s._generateGLB(i);throw new Error("glTF Serializer: Scene is not ready!")},t})();e.GLTF2Export=t})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){var r=(function(){function r(e,t){this.asset={generator:"BabylonJS",version:"2.0"},this.babylonScene=e,this.bufferViews=new Array,this.accessors=new Array,this.meshes=new Array,this.scenes=new Array,this.nodes=new Array,this.images=new Array,this.materials=new Array,this.textures=new Array,this.imageData={},this.convertToRightHandedSystem=!this.babylonScene.useRightHandedSystem,t&&(this.options=t)}return r.prototype.createBufferView=function(e,t,r,a,i){var s={buffer:e,byteLength:r};return t&&(s.byteOffset=t),i&&(s.name=i),a&&(s.byteStride=a),s},r.prototype.createAccessor=function(e,t,r,a,i,s,n,o){var l={name:t,bufferView:e,componentType:a,count:i,type:r};return n&&(l.min=n),o&&(l.max=o),s&&(l.byteOffset=s),l},r.prototype.calculateMinMaxPositions=function(t,a,i){var s=[1/0,1/0,1/0],n=[-1/0,-1/0,-1/0],o=a+i;if(i)for(var l=a;ln[f]&&(n[f]=d),++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){for(var n=i,o=e.VertexBuffer.DeduceStride(t),l=a.length/o,u=0;u0)if(x||!t._GLTFMaterial._HasTexturesPresent(this.materials[V]))B.material=V;else{var O=t._GLTFMaterial._StripTexturesFromMaterial(this.materials[V]);this.materials.push(O),B.material=this.materials.length-1}}r.primitives.push(B)}}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 d=o.byteLength,g=new Uint8Array(d),p=new Uint8Array(d),m=e.Color3.Black(),v=0,T=0,x=0;xthis._epsilon?_.baseColor.r:1,p[S+1]/=_.baseColor.g>this._epsilon?_.baseColor.g:1,p[S+2]/=_.baseColor.b>this._epsilon?_.baseColor.b:1;var L=e.Color3.FromInts(p[S],p[S+1],p[S+2]),E=L.toGammaSpace();p[S]=255*E.r,p[S+1]=255*E.g,p[S+2]=255*E.b,this.FuzzyEquals(E,e.Color3.White(),this._epsilon)||(F=!0),g[S+1]/=_.roughness>this._epsilon?_.roughness:1,g[S+2]/=_.metallic>this._epsilon?_.metallic:1;var V=e.Color3.FromInts(255,g[S+1],g[S+2]);this.FuzzyEquals(V,e.Color3.White(),this._epsilon)||(R=!0)}if(R){var P=this._CreateBase64FromCanvas(g,h,c,i);_.metallicRoughnessTextureBase64=P}if(F){var G=this._CreateBase64FromCanvas(p,h,c,i);_.baseColorTextureBase64=G}return _},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._ConvertMetalRoughFactorsToMetallicRoughness=function(e,r,a,i,s,n,o){var l={baseColor:e.albedoColor,metallic:e.metallic,roughness:e.roughness};if(o){if(e.albedoTexture){var u=t._ExportTexture(e.albedoTexture,r,a,i,n);u&&(s.baseColorTexture=u)}if(e.metallicTexture){var u=t._ExportTexture(e.metallicTexture,r,a,i,n);null!=u&&(s.metallicRoughnessTexture=u)}}return l},t._ConvertSpecGlossFactorsToMetallicRoughness=function(r,a,i,s,n,o,l){var u={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");var h=this._ConvertSpecularGlossinessTexturesToMetallicRoughness(r.albedoTexture,r.reflectivityTexture,u,a);if(h){if(l){if(h.baseColorTextureBase64){var c=t._GetTextureInfoFromBase64(h.baseColorTextureBase64,"bjsBaseColorTexture_"+s.length+".png",a,i,s,o);null!=c&&(n.baseColorTexture=c)}if(h.metallicRoughnessTextureBase64){var f=t._GetTextureInfoFromBase64(h.metallicRoughnessTextureBase64,"bjsMetallicRoughnessTexture_"+s.length+".png",a,i,s,o);null!=f&&(n.metallicRoughnessTexture=f)}}}else h=this._ConvertSpecularGlossinessToMetallicRoughness(u);return h},t._ConvertPBRMaterial=function(r,a,i,s,n,o,l){var u,h={},c={name:r.name};if(u=r.isMetallicWorkflow()?this._ConvertMetalRoughFactorsToMetallicRoughness(r,a,i,s,h,o,l):this._ConvertSpecGlossFactorsToMetallicRoughness(r,a,i,s,h,o,l),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(r.bumpTexture){var f=t._ExportTexture(r.bumpTexture,a,i,s,o);f&&(c.normalTexture=f)}if(r.ambientTexture){var f=t._ExportTexture(r.ambientTexture,a,i,s,o);if(f){var d={index:f.index};c.occlusionTexture=d,r.ambientTextureStrength&&(d.strength=r.ambientTextureStrength)}}if(r.emissiveTexture){var f=t._ExportTexture(r.emissiveTexture,a,i,s,o);null!=f&&(c.emissiveTexture=f)}}if(this.FuzzyEquals(r.emissiveColor,e.Color3.Black(),this._epsilon)||(c.emissiveFactor=r.emissiveColor.asArray()),null!=r.transparencyMode){var g=t._GetAlphaMode(r);"OPAQUE"!==g&&(c.alphaMode=g,"BLEND"===g&&(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