babylonjs.serializers.min.js 35 KB

12
  1. !(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<t.length;s++){n.push("g object"+s),n.push("o object_"+s);var l=null;if(i){var u=e.Matrix.Translation(t[s].position.x,t[s].position.y,t[s].position.z);l=e.Matrix.Translation(-t[s].position.x,-t[s].position.y,-t[s].position.z),t[s].bakeTransformIntoVertices(u)}if(r){var c=t[s].material;c&&n.push("usemtl "+c.id)}var h=t[s].geometry;if(h){var f=h.getVerticesData("position"),p=h.getVerticesData("normal"),m=h.getVerticesData("uv"),d=h.getIndices(),g=0;if(f&&p&&m&&d){for(var T=0;T<f.length;T+=3)n.push("v "+f[T]+" "+f[T+1]+" "+f[T+2]),g++;for(T=0;T<p.length;T+=3)n.push("vn "+p[T]+" "+p[T+1]+" "+p[T+2]);for(T=0;T<m.length;T+=2)n.push("vt "+m[T]+" "+m[T+1]);for(T=0;T<d.length;T+=3)n.push("f "+(d[T+2]+o)+"/"+(d[T+2]+o)+"/"+(d[T+2]+o)+" "+(d[T+1]+o)+"/"+(d[T+1]+o)+"/"+(d[T+1]+o)+" "+(d[T]+o)+"/"+(d[T]+o)+"/"+(d[T]+o));i&&l&&t[s].bakeTransformIntoVertices(l),o+=g}}}return n.join("\n")},t.MTL=function(e){var t=[],r=e.material;t.push("newmtl mat1"),t.push(" Ns "+r.specularPower.toFixed(4)),t.push(" Ni 1.5000"),t.push(" d "+r.alpha.toFixed(4)),t.push(" Tr 0.0000"),t.push(" Tf 1.0000 1.0000 1.0000"),t.push(" illum 2"),t.push(" Ka "+r.ambientColor.r.toFixed(4)+" "+r.ambientColor.g.toFixed(4)+" "+r.ambientColor.b.toFixed(4)),t.push(" Kd "+r.diffuseColor.r.toFixed(4)+" "+r.diffuseColor.g.toFixed(4)+" "+r.diffuseColor.b.toFixed(4)),t.push(" Ks "+r.specularColor.r.toFixed(4)+" "+r.specularColor.g.toFixed(4)+" "+r.specularColor.b.toFixed(4)),t.push(" Ke "+r.emissiveColor.r.toFixed(4)+" "+r.emissiveColor.g.toFixed(4)+" "+r.emissiveColor.b.toFixed(4));return r.ambientTexture&&t.push(" map_Ka "+r.ambientTexture.name),r.diffuseTexture&&t.push(" map_Kd "+r.diffuseTexture.name),r.specularTexture&&t.push(" map_Ks "+r.specularTexture.name),r.bumpTexture&&t.push(" map_bump -imfchan z "+r.bumpTexture.name),r.opacityTexture&&t.push(" map_d "+r.opacityTexture.name),t.join("\n")},t})();e.OBJExport=t})(e||(e={}));var e;!(function(e){var t=(function(){function t(){}return t.GLTF=function(t,r,a){var i=r.replace(/\.[^\/.]+$/,""),n=new e.GLTF2._Exporter(t,a);return t.isReady?n._generateGLTF(i):(e.Tools.Error("glTF Serializer: Scene is not ready!"),null)},t.GLB=function(t,r,a){var i=r.replace(/\.[^\/.]+$/,""),n=new e.GLTF2._Exporter(t,a);return t.isReady?n._generateGLB(i):(e.Tools.Error("glTF Serializer: Scene is not ready!"),null)},t})();e.GLTF2Export=t})(e||(e={}));var e;!(function(e){!(function(t){var r=(function(){function r(e,t){this.asset={generator:"BabylonJS",version:"2.0"},this.babylonScene=e,this.bufferViews=[],this.accessors=[],this.meshes=[],this.scenes=[],this.nodes=[],this.images=[],this.materials=[],this.textures=[],this.animations=[],this.imageData={},this.convertToRightHandedSystem=!this.babylonScene.useRightHandedSystem;var r=t||{};this.shouldExportMesh=r.shouldExportMesh?r.shouldExportMesh:function(e){return!0},this.animationSampleRate=r.animationSampleRate?r.animationSampleRate:1/60}return r.prototype.writeAttributeData=function(r,a,i){for(var n,o,s=e.VertexBuffer.DeduceStride(r),l=0,u=a.length/s;l<u;++l){if(o=l*s,r===e.VertexBuffer.PositionKind||r===e.VertexBuffer.NormalKind){var c=e.Vector3.FromArray(a,o);this.convertToRightHandedSystem&&t._GLTFUtilities.GetRightHandedVector3FromRef(c),n=c.asArray()}else if(r===e.VertexBuffer.TangentKind||r===e.VertexBuffer.ColorKind){var c=e.Vector4.FromArray(a,o);this.convertToRightHandedSystem&&r!==e.VertexBuffer.ColorKind&&t._GLTFUtilities.GetRightHandedVector4FromRef(c),n=c.asArray()}else r===e.VertexBuffer.UVKind||r===e.VertexBuffer.UV2Kind?(this.convertToRightHandedSystem,n=[a[o],a[o+1]]):(e.Tools.Warn("Unsupported Vertex Buffer Type: "+r),n=[]);n.forEach((function(e){i.setFloat32(e)}))}return 4*a.length},r.prototype.generateJSON=function(e,r,a){var i,n,o,s={byteLength:this.totalByteLength},l=this.totalByteLength,u={asset:this.asset};if(s.byteLength&&(u.buffers=[s]),this.nodes&&this.nodes.length&&(u.nodes=this.nodes),this.meshes&&this.meshes.length&&(u.meshes=this.meshes),this.scenes&&this.scenes.length&&(u.scenes=this.scenes,u.scene=0),this.bufferViews&&this.bufferViews.length&&(u.bufferViews=this.bufferViews),this.accessors&&this.accessors.length&&(u.accessors=this.accessors),this.animations&&this.animations.length&&(u.animations=this.animations),this.materials&&this.materials.length&&(u.materials=this.materials),this.textures&&this.textures.length&&(u.textures=this.textures),this.images&&this.images.length)if(e){u.images=[];var c=this;this.images.forEach((function(e){e.uri&&(n=c.imageData[e.uri],i=e.uri.split(".")[0]+" image",o=t._GLTFUtilities.CreateBufferView(0,l,n.data.length,void 0,i),l+=n.data.buffer.byteLength,c.bufferViews.push(o),e.bufferView=c.bufferViews.length-1,e.name=i,e.mimeType=n.mimeType,e.uri=void 0,u.images||(u.images=[]),u.images.push(e))})),s.byteLength=l}else u.images=this.images;return e||(s.uri=r+".bin"),a?JSON.stringify(u,null,2):JSON.stringify(u)},r.prototype._generateGLTF=function(t){var r=this.generateBinary(),a=this.generateJSON(!1,t,!0),i=new Blob([r],{type:"application/octet-stream"}),n=t+".gltf",o=t+".bin",s=new e.GLTFData;if(s.glTFFiles[n]=a,s.glTFFiles[o]=i,this.imageData)for(var l in this.imageData)s.glTFFiles[l]=new Blob([this.imageData[l].data],{type:this.imageData[l].mimeType});return s},r.prototype.generateBinary=function(){var e=new a(4);return this.createScene(this.babylonScene,e),e.getArrayBuffer()},r.prototype._getPadding=function(e){var t=e%4;return 0===t?t:4-t},r.prototype._generateGLB=function(t){var r=this.generateBinary(),a=this.generateJSON(!0),i=t+".glb",n=a.length,o=0;for(var s in this.imageData)o+=this.imageData[s].data.byteLength;var l=this._getPadding(n),u=this._getPadding(r.byteLength),c=this._getPadding(o),h=28+n+l+r.byteLength+u+o+c,f=new ArrayBuffer(12),p=new DataView(f);p.setUint32(0,1179937895,!0),p.setUint32(4,2,!0),p.setUint32(8,h,!0);var m=new ArrayBuffer(8+n+l),d=new DataView(m);d.setUint32(0,n+l,!0),d.setUint32(4,1313821514,!0);for(var g=new Uint8Array(m,8),T=0;T<n;++T)g[T]=a.charCodeAt(T);for(var y=new Uint8Array(m,8+n),T=0;T<l;++T)y[T]=32;var x=new ArrayBuffer(8),v=new DataView(x);v.setUint32(0,r.byteLength+o+c,!0),v.setUint32(4,5130562,!0);for(var b=new ArrayBuffer(u),_=new Uint8Array(b),T=0;T<u;++T)_[T]=0;for(var C=new ArrayBuffer(c),A=new Uint8Array(C),T=0;T<c;++T)A[T]=0;var B=[f,m,x,r];for(var s in this.imageData)B.push(this.imageData[s].data.buffer);B.push(b),B.push(C);var F=new Blob(B,{type:"application/octet-stream"}),M=new e.GLTFData;return M.glTFFiles[i]=F,M},r.prototype.setNodeTransformation=function(r,a){a.position.equalsToFloats(0,0,0)||(r.translation=this.convertToRightHandedSystem?t._GLTFUtilities.GetRightHandedVector3(a.position).asArray():a.position.asArray()),a.scaling.equalsToFloats(1,1,1)||(r.scale=a.scaling.asArray());var i=e.Quaternion.RotationYawPitchRoll(a.rotation.y,a.rotation.x,a.rotation.z);a.rotationQuaternion&&(i=i.multiply(a.rotationQuaternion)),0===i.x&&0===i.y&&0===i.z&&1===i.w||(this.convertToRightHandedSystem&&t._GLTFUtilities.GetRightHandedQuaternionFromRef(i),r.rotation=i.normalize().asArray())},r.prototype.createBufferViewKind=function(r,a,i,n){var o,s,l,u=null;a instanceof e.Mesh?u=a:a instanceof e.InstancedMesh&&(u=a.sourceMesh),u&&(s=u.getVerticesData(r))&&(o=4*s.length,l=t._GLTFUtilities.CreateBufferView(0,i.getByteOffset(),o,n,r+" - "+u.name),this.bufferViews.push(l),this.writeAttributeData(r,s,i))},r.prototype.setPrimitiveAttributes=function(r,a,i){var n,o,s,l,u,c,h,f,p,m,d,g,T,y,x=null,v=null,b=null;a instanceof e.Mesh?x=a:a instanceof e.InstancedMesh&&(x=a.sourceMesh);var _=[{kind:e.VertexBuffer.PositionKind,accessorType:"VEC3",byteStride:12},{kind:e.VertexBuffer.NormalKind,accessorType:"VEC3",byteStride:12},{kind:e.VertexBuffer.ColorKind,accessorType:"VEC4",byteStride:16},{kind:e.VertexBuffer.TangentKind,accessorType:"VEC4",byteStride:16},{kind:e.VertexBuffer.UVKind,accessorType:"VEC2",byteStride:8},{kind:e.VertexBuffer.UV2Kind,accessorType:"VEC2",byteStride:8}];if(x){for(var C=0,A=_;C<A.length;C++){var B=A[C];n=B.kind,x.isVerticesDataPresent(n)&&(this.createBufferViewKind(n,a,i,B.byteStride),B.bufferViewIndex=this.bufferViews.length-1)}if(x.getTotalIndices()&&(o=x.getIndices())){s=4*o.length,l=t._GLTFUtilities.CreateBufferView(0,i.getByteOffset(),s,void 0,"Indices - "+x.name),this.bufferViews.push(l),b=this.bufferViews.length-1;for(var F=0,M=o.length;F<M;++F)i.setUInt32(o[F])}if(a.subMeshes){u=!1;for(var E=0,R=a.subMeshes;E<R.length;E++){var S=R[E];c={attributes:{}};for(var L=0,w=_;L<w.length;L++){var B=w[L];if(n=B.kind,(h=x.getVerticesData(n))&&(f=e.VertexBuffer.DeduceStride(n),void 0!=(y=B.bufferViewIndex)))switch(p={min:null,max:null},n==e.VertexBuffer.PositionKind&&(p=t._GLTFUtilities.CalculateMinMaxPositions(h,0,h.length/f,this.convertToRightHandedSystem)),T=t._GLTFUtilities.CreateAccessor(y,n+" - "+a.name,B.accessorType,5126,h.length/f,0,p.min,p.max),this.accessors.push(T),n){case e.VertexBuffer.PositionKind:c.attributes.POSITION=this.accessors.length-1;break;case e.VertexBuffer.NormalKind:c.attributes.NORMAL=this.accessors.length-1;break;case e.VertexBuffer.ColorKind:c.attributes.COLOR_0=this.accessors.length-1;break;case e.VertexBuffer.TangentKind:c.attributes.TANGENT=this.accessors.length-1;break;case e.VertexBuffer.UVKind:c.attributes.TEXCOORD_0=this.accessors.length-1,u=!0;break;case e.VertexBuffer.UV2Kind:c.attributes.TEXCOORD_1=this.accessors.length-1,u=!0;break;default:e.Tools.Warn("Unsupported Vertex Buffer Type: "+n)}}b&&(T=t._GLTFUtilities.CreateAccessor(b,"indices - "+a.name,"SCALAR",5125,S.indexCount,4*S.indexStart,null,null),this.accessors.push(T),c.indices=this.accessors.length-1),x.material&&(v=null,x.material instanceof e.StandardMaterial||x.material instanceof e.PBRMetallicRoughnessMaterial||x.material instanceof e.PBRMaterial?v=a.getScene().materials.indexOf(x.material):x.material instanceof e.MultiMaterial?(d=x.material,(g=d.subMaterials[S.materialIndex])&&(v=a.getScene().materials.indexOf(g))):e.Tools.Warn("Material type "+x.material.getClassName()+" for material "+x.material.name+" is not yet implemented in glTF serializer."),null!=v&&Object.keys(c.attributes).length>0&&(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;r<a.length;r++){var s=a[r];n.children.push(u.nodeMap[s.uniqueId])}}})),this.scenes.push(l)}},r.prototype.createNodeMapAndAnimations=function(e,r){var a,i,n={},o={name:"runtime animations",channels:[],samplers:[]},s=[],l=this;return e.meshes.forEach((function(u){i=l.createNode(u,r),l.nodes.push(i),a=l.nodes.length-1,n[u.uniqueId]=a,!e.animationGroups.length&&u.animations.length&&t._GLTFAnimation._CreateNodeAnimationFromMeshAnimations(u,o,s,n,l.nodes,r,l.bufferViews,l.accessors,l.convertToRightHandedSystem,l.animationSampleRate)})),o.channels.length&&o.samplers.length&&this.animations.push(o),s.forEach((function(e){e.channels.length&&e.samplers.length&&l.animations.push(e)})),e.animationGroups.length&&t._GLTFAnimation._CreateNodeAnimationFromAnimationGroups(e,l.animations,n,this.nodes,r,this.bufferViews,this.accessors,this.convertToRightHandedSystem,l.animationSampleRate),n},r.prototype.createNode=function(e,t){var r={},a={primitives:[]};return e.name&&(r.name=e.name),this.setNodeTransformation(r,e),this.setPrimitiveAttributes(a,e,t),a.primitives.length&&(this.meshes.push(a),r.mesh=this.meshes.length-1),r},r})();t._Exporter=r;var a=(function(){function e(e){this._arrayBuffer=new ArrayBuffer(e),this._dataView=new DataView(this._arrayBuffer),this._byteOffset=0}return e.prototype.resizeBuffer=function(e){for(var t=new ArrayBuffer(e),r=new Uint8Array(this._arrayBuffer),a=new Uint8Array(t),i=0,n=a.byteLength;i<n;++i)a[i]=r[i];this._arrayBuffer=t,this._dataView=new DataView(this._arrayBuffer)},e.prototype.getArrayBuffer=function(){return this.resizeBuffer(this.getByteOffset()),this._arrayBuffer},e.prototype.getByteOffset=function(){return this._byteOffset},e.prototype.setUInt8=function(e){this._byteOffset+1>this._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;u<r.length;++u){var c=r[u];c instanceof e.StandardMaterial?t._ConvertStandardMaterial(c,a,i,n,o,s,l):c instanceof e.PBRMetallicRoughnessMaterial?t._ConvertPBRMetallicRoughnessMaterial(c,a,i,n,o,s,l):c instanceof e.PBRMaterial?t._ConvertPBRMaterial(c,a,i,n,o,s,l):e.Tools.Error("Unsupported material type: "+c.name)}},t._StripTexturesFromMaterial=function(e){var t={};if(e){t.name=e.name,t.doubleSided=e.doubleSided,t.alphaMode=e.alphaMode,t.alphaCutoff=e.alphaCutoff,t.emissiveFactor=e.emissiveFactor;var r=e.pbrMetallicRoughness;r&&(t.pbrMetallicRoughness={},t.pbrMetallicRoughness.baseColorFactor=r.baseColorFactor,t.pbrMetallicRoughness.metallicFactor=r.metallicFactor,t.pbrMetallicRoughness.roughnessFactor=r.roughnessFactor)}return t},t._HasTexturesPresent=function(e){if(e.emissiveTexture||e.normalTexture||e.occlusionTexture)return!0;var t=e.pbrMetallicRoughness;return!(!t||!t.baseColorTexture&&!t.metallicRoughnessTexture)},t._ConvertToGLTFPBRMetallicRoughness=function(t){function r(e,t,r,a,i){return(1-e)*(1-e)*(1-e)*t+3*(1-e)*(1-e)*e*r+3*(1-e)*e*e*a+e*e*e*i}var a=new e.Vector2(0,1),i=new e.Vector2(0,.1),n=new e.Vector2(0,.1),o=new e.Vector2(1300,.1),s=t.diffuseColor.toLinearSpace().scale(.5),l=t.alpha,u=e.Scalar.Clamp(t.specularPower,0,this._maxSpecularPower),c=(function(e){return r(Math.pow(e/o.x,.333333),a.y,i.y,n.y,o.y)})(u);return{baseColorFactor:[s.r,s.g,s.b,l],metallicFactor:0,roughnessFactor:c}},t._SolveMetallic=function(r,a,i){if(a<t._dielectricSpecular.r)return t._dielectricSpecular,0;var n=t._dielectricSpecular.r,o=r*i/(1-t._dielectricSpecular.r)+a-2*t._dielectricSpecular.r,s=t._dielectricSpecular.r-a,l=o*o-4*n*s;return e.Scalar.Clamp((-o+Math.sqrt(l))/(2*n),0,1)},t._GetAlphaMode=function(t){if(t instanceof e.StandardMaterial){var r=t;return 1!=r.alpha||null!=r.diffuseTexture&&r.diffuseTexture.hasAlpha||null!=r.opacityTexture?"BLEND":"OPAQUE"}if(t instanceof e.PBRMetallicRoughnessMaterial){var a=t;switch(a.transparencyMode){case e.PBRMaterial.PBRMATERIAL_OPAQUE:return"OPAQUE";case e.PBRMaterial.PBRMATERIAL_ALPHABLEND:return"BLEND";case e.PBRMaterial.PBRMATERIAL_ALPHATEST:return"MASK";case e.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND:return e.Tools.Warn(t.name+": GLTF Exporter | Alpha test and blend mode not supported in glTF. Alpha blend used instead."),"BLEND";default:return e.Tools.Error("Unsupported alpha mode "+a.transparencyMode),null}}else{if(!(t instanceof e.PBRMaterial))return e.Tools.Error("Unsupported Babylon material type"),null;var i=t;switch(i.transparencyMode){case e.PBRMaterial.PBRMATERIAL_OPAQUE:return"OPAQUE";case e.PBRMaterial.PBRMATERIAL_ALPHABLEND:return"BLEND";case e.PBRMaterial.PBRMATERIAL_ALPHATEST:return"MASK";case e.PBRMaterial.PBRMATERIAL_ALPHATESTANDBLEND:return e.Tools.Warn(t.name+": GLTF Exporter | Alpha test and blend mode not supported in glTF. Alpha blend used instead."),"BLEND";default:return e.Tools.Error("Unsupported alpha mode "+i.transparencyMode),null}}},t._ConvertStandardMaterial=function(r,a,i,n,o,s,l){var u=t._ConvertToGLTFPBRMetallicRoughness(r),c={name:r.name};if(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.diffuseTexture){var h=t._ExportTexture(r.diffuseTexture,a,i,n,s);null!=h&&(u.baseColorTexture=h)}if(r.bumpTexture){var h=t._ExportTexture(r.bumpTexture,a,i,n,s);h&&(c.normalTexture=h)}if(r.emissiveTexture){var f=t._ExportTexture(r.emissiveTexture,a,i,n,s);f&&(c.emissiveTexture=f),c.emissiveFactor=[1,1,1]}if(r.ambientTexture){var h=t._ExportTexture(r.ambientTexture,a,i,n,s);if(h){var p={index:h.index};c.occlusionTexture=p,p.strength=1}}}(r.alpha<1||r.opacityTexture)&&(r.alphaMode===e.Engine.ALPHA_COMBINE?c.alphaMode="BLEND":e.Tools.Warn(r.name+": glTF 2.0 does not support alpha mode: "+r.alphaMode.toString())),r.emissiveColor&&!this.FuzzyEquals(r.emissiveColor,e.Color3.Black(),this._epsilon)&&(c.emissiveFactor=r.emissiveColor.asArray()),c.pbrMetallicRoughness=u,o.push(c)},t._ConvertPBRMetallicRoughnessMaterial=function(r,a,i,n,o,s,l){var u={};r.baseColor&&(u.baseColorFactor=[r.baseColor.r,r.baseColor.g,r.baseColor.b,r.alpha]),null!=r.metallic&&1!==r.metallic&&(u.metallicFactor=r.metallic),null!=r.roughness&&1!==r.roughness&&(u.roughnessFactor=r.roughness);var c={name:r.name};if(r.doubleSided&&(c.doubleSided=r.doubleSided),l){if(null!=r.baseTexture){var h=t._ExportTexture(r.baseTexture,a,i,n,s);null!=h&&(u.baseColorTexture=h)}if(r.normalTexture){var h=t._ExportTexture(r.normalTexture,a,i,n,s);h&&(c.normalTexture=h)}if(r.occlusionTexture){var h=t._ExportTexture(r.occlusionTexture,a,i,n,s);h&&(c.occlusionTexture=h,null!=r.occlusionStrength&&(c.occlusionTexture.strength=r.occlusionStrength))}if(r.emissiveTexture){var h=t._ExportTexture(r.emissiveTexture,a,i,n,s);null!=h&&(c.emissiveTexture=h)}}if(this.FuzzyEquals(r.emissiveColor,e.Color3.Black(),this._epsilon)&&(c.emissiveFactor=r.emissiveColor.asArray()),null!=r.transparencyMode){var f=t._GetAlphaMode(r);f&&"OPAQUE"!==f&&(c.alphaMode=f,"BLEND"===f&&(c.alphaCutoff=r.alphaCutOff))}c.pbrMetallicRoughness=u,o.push(c)},t._CreateBase64FromCanvas=function(e,t,r,a){var i=document.createElement("canvas");i.id="WriteCanvas";var n=i.getContext("2d");i.width=t,i.height=r;var o=n.createImageData(t,r);return o.data.set(e),n.putImageData(o,0,0),i.toDataURL(a)},t._CreateWhiteTexture=function(t,r,a){for(var i=new Uint8Array(t*r*4),n=0;n<i.length;++n)i[n]=255;return e.RawTexture.CreateRGBATexture(i,t,r,a)},t._ResizeTexturesToSameDimensions=function(t,r,a){var i,n,o=t?t.getSize():{width:0,height:0},s=r?r.getSize():{width:0,height:0};return o.width<s.width?(i=t?e.TextureTools.CreateResizedCopy(t,s.width,s.height,!0):this._CreateWhiteTexture(s.width,s.height,a),n=r):o.width>s.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;x<h;++x)for(var v=0;v<c;++v){var b=4*(c*x+v),_=e.Color3.FromInts(o[b],o[b+1],o[b+2]).toLinearSpace().multiply(a.diffuseColor),C=e.Color3.FromInts(s[b],s[b+1],s[b+2]).toLinearSpace().multiply(a.specularColor),A=s[b+3]/255*a.glossiness,B={diffuseColor:_,specularColor:C,glossiness:A},F=this._ConvertSpecularGlossinessToMetallicRoughness(B);g.r=Math.max(g.r,F.baseColor.r),g.g=Math.max(g.g,F.baseColor.g),g.b=Math.max(g.b,F.baseColor.b),T=Math.max(T,F.metallic),y=Math.max(y,F.roughness),d[b]=255*F.baseColor.r,d[b+1]=255*F.baseColor.g,d[b+2]=255*F.baseColor.b,d[b+3]=l.texture1.hasAlpha?o[b+3]:255,m[b]=0,m[b+1]=255*F.roughness,m[b+2]=255*F.metallic,m[b+3]=255}for(var M={baseColor:g,metallic:T,roughness:y},E=!1,R=!1,x=0;x<h;++x)for(var v=0;v<c;++v){var S=4*(c*x+v);d[S]/=M.baseColor.r>this._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;h<f;++h)c[h]=l.charCodeAt(h);var p={data:c,mimeType:r};if(n[t]=p,"image/jpeg"===r||"image/png"===r){for(var m={uri:t},d=-1,h=0;h<a.length;++h)if(a[h].uri===t){d=h;break}-1===d?(a.push(m),s.source=a.length-1,i.push({source:a.length-1}),o={index:a.length-1}):(s.source=d,o={index:d})}return o},t._dielectricSpecular=new e.Color3(.04,.04,.04),t._maxSpecularPower=1024,t._epsilon=1e-6,t})();t._GLTFMaterial=r})(e.GLTF2||(e.GLTF2={}))})(e||(e={}));var e;!(function(e){!(function(t){var r;!(function(e){e[e.INTANGENT=0]="INTANGENT",e[e.OUTTANGENT=1]="OUTTANGENT"})(r||(r={}));var a=(function(){function a(){}return a._CreateNodeAnimation=function(t,r,i,n,o,s){var l=[],u=[],c=r.getKeys(),h=a.calculateMinMaxKeyFrames(c),f=a._DeduceInterpolation(c,i,o),p=h.max-h.min,m=f.interpolationType,d=f.shouldBakeAnimation;if(d?a._CreateBakedAnimation(t,r,i,h.min,h.max,r.framePerSecond,s,l,u,h,n,o):"LINEAR"===m||"STEP"===m?a._CreateLinearOrStepAnimation(t,r,i,p,l,u,n,o):"CUBICSPLINE"===m?a._CreateCubicSplineAnimation(t,r,i,p,l,u,n,o):a._CreateBakedAnimation(t,r,i,h.min,h.max,r.framePerSecond,s,l,u,h,n,o),l.length&&u.length){return{inputs:l,outputs:u,samplerInterpolation:m,inputsMin:d?h.min:e.Tools.FloatRound(h.min/r.framePerSecond),inputsMax:d?h.max:e.Tools.FloatRound(h.max/r.framePerSecond)}}return null},a._DeduceAnimationInfo=function(t){var r=null,a="VEC3",i=!1,n=t.targetProperty.split(".");switch(n[0]){case"scaling":r="scale";break;case"position":r="translation";break;case"rotation":a="VEC4",r="rotation";break;case"rotationQuaternion":a="VEC4",i=!0,r="rotation";break;default:e.Tools.Error("Unsupported animatable property "+n[0])}return r?{animationChannelTargetPath:r,dataAccessorType:a,useQuaternion:i}:(e.Tools.Error("animation channel target path and data accessor type could be deduced"),null)},a._CreateNodeAnimationFromMeshAnimations=function(e,t,r,i,n,o,s,l,u,c){var h;e.animations&&e.animations.forEach((function(n){var f=a._DeduceAnimationInfo(n);f&&(h={name:n.name,samplers:[],channels:[]},a.AddAnimation(""+n.name,n.hasRunningRuntimeAnimations?t:h,e,n,f.dataAccessorType,f.animationChannelTargetPath,i,o,s,l,u,f.useQuaternion,c),h.samplers.length&&h.channels.length&&r.push(h))}))},a._CreateNodeAnimationFromAnimationGroups=function(t,r,i,n,o,s,l,u,c){var h;if(t.animationGroups){t.animationGroups.forEach((function(t){h={name:t.name,channels:[],samplers:[]},t.targetedAnimations.forEach((function(t){var r=t.target,n=t.animation;if(r instanceof e.Mesh){var f=a._DeduceAnimationInfo(t.animation);if(f){var p=r;a.AddAnimation(""+n.name,h,p,n,f.dataAccessorType,f.animationChannelTargetPath,i,o,s,l,u,f.useQuaternion,c)}}})),h.channels.length&&h.samplers.length&&r.push(h)}))}},a.AddAnimation=function(e,r,i,n,o,s,l,u,c,h,f,p,m){var d,g,T,y,x,v,b,_=a._CreateNodeAnimation(i,n,s,f,p,m);if(_){var C=l[i.uniqueId],A=4*_.inputs.length;d=t._GLTFUtilities.CreateBufferView(0,u.getByteOffset(),A,void 0,e+" keyframe data view"),c.push(d),_.inputs.forEach((function(e){u.setFloat32(e)})),g=t._GLTFUtilities.CreateAccessor(c.length-1,e+" keyframes","SCALAR",5126,_.inputs.length,null,[_.inputsMin],[_.inputsMax]),h.push(g),T=h.length-1,x=_.outputs.length,A="VEC3"===o?12*_.outputs.length:16*_.outputs.length,d=t._GLTFUtilities.CreateBufferView(0,u.getByteOffset(),A,void 0,e+" data view"),c.push(d),_.outputs.forEach((function(e){e.forEach((function(e){u.setFloat32(e)}))})),g=t._GLTFUtilities.CreateAccessor(c.length-1,e+" data",o,5126,x,null,null,null),h.push(g),y=h.length-1,v={interpolation:_.samplerInterpolation,input:T,output:y},r.samplers.push(v),b={sampler:r.samplers.length-1,target:{node:C,path:s}},r.channels.push(b)}},a._CreateBakedAnimation=function(t,r,i,n,o,s,l,u,c,h,f,p){var m,d,g=e.Quaternion.Identity(),T=null,y=null,x=null,v=null,b=null,_=null;h.min=e.Tools.FloatRound(n/s);for(var C=r.getKeys(),A=0,B=C.length;A<B;++A){if(_=null,x=C[A],A+1<B)if(v=C[A+1],x.value.equals(v.value)){if(0!==A)continue;_=x.frame}else _=v.frame;else{if(b=C[A-1],x.value.equals(b.value))continue;_=o}if(_)for(var F=x.frame;F<=_;F+=l)(d=e.Tools.FloatRound(F/s))!==T&&(T=d,y=d,m=r._interpolate(F,0,void 0,r.loopMode),a._SetInterpolatedValue(t,m,d,r,i,g,u,c,f,p))}y&&(h.max=y)},a._ConvertFactorToVector3OrQuaternion=function(t,r,i,n,o,s,l){var u,c,h=null,f=a._GetBasePositionRotationOrScale(r,o,s,l);if(n===e.Animation.ANIMATIONTYPE_FLOAT)switch(u=i.targetProperty.split("."),c=u?u[1]:"",h=l?e.Quaternion.FromArray(f).normalize():e.Vector3.FromArray(f),c){case"x":case"y":h[c]=s&&l?-t:t;break;case"z":h[c]=s&&!l&&"scale"!==o?-t:t;break;case"w":h.w=t;break;default:
  2. e.Tools.Error('glTFAnimation: Unsupported component type "'+c+'" for scale animation!')}return h},a._SetInterpolatedValue=function(t,r,a,i,n,o,s,l,u,c){var h,f=i.dataType;s.push(a),"number"==typeof r&&(r=this._ConvertFactorToVector3OrQuaternion(r,t,i,f,n,u,c)),r&&("rotation"===n?(c?o=r:(h=r,e.Quaternion.RotationYawPitchRollToRef(h.y,h.x,h.z,o)),u&&(o.x*=-1,o.y*=-1,l.push(o.asArray()))):(h=r,u&&"scale"!==n&&(h.z*=-1),l.push(h.asArray())))},a._CreateLinearOrStepAnimation=function(e,t,r,i,n,o,s,l){t.getKeys().forEach((function(i){n.push(i.frame/t.framePerSecond),a._AddKeyframeValue(i,t,o,r,e,s,l)}))},a._CreateCubicSplineAnimation=function(e,t,i,n,o,s,l,u){t.getKeys().forEach((function(c){o.push(c.frame/t.framePerSecond),a.AddSplineTangent(r.INTANGENT,s,i,"CUBICSPLINE",c,n,u,l),a._AddKeyframeValue(c,t,s,i,e,l,u),a.AddSplineTangent(r.OUTTANGENT,s,i,"CUBICSPLINE",c,n,u,l)}))},a._GetBasePositionRotationOrScale=function(t,r,a,i){var n;return"rotation"===r?i?t.rotationQuaternion?(n=t.rotationQuaternion.asArray(),a&&(n[0]*=-1,n[1]*=-1)):n=e.Quaternion.Identity().asArray():(n=t.rotation.asArray(),n[2]*=-1):"translation"===r?(n=t.position.asArray(),a&&(n[2]*=-1)):n=t.scaling.asArray(),n},a._AddKeyframeValue=function(t,r,a,i,n,o,s){var l,u,c=r.dataType;c===e.Animation.ANIMATIONTYPE_VECTOR3?(l=t.value.asArray(),o&&"scale"!==i&&(l[2]*=-1),"rotation"===i?a.push(e.Vector3.FromArray(l).toQuaternion().asArray()):a.push(l)):c===e.Animation.ANIMATIONTYPE_FLOAT?(u=this._ConvertFactorToVector3OrQuaternion(t.value,n,r,c,i,o,s))&&("rotation"===i?s?a.push(u.normalize().asArray()):a.push(u.toQuaternion().normalize().asArray()):a.push(u.asArray())):c===e.Animation.ANIMATIONTYPE_QUATERNION?(l=t.value.normalize().asArray(),o&&(l[0]*=-1,l[1]*=-1),a.push(l)):e.Tools.Error("glTFAnimation: Unsupported key frame values for animation!")},a._DeduceInterpolation=function(t,r,a){var i,n,o=!1;if("rotation"===r&&!a)return{interpolationType:"LINEAR",shouldBakeAnimation:!0};for(var s=0,l=t.length;s<l;++s)if(n=t[s],n.inTangent||n.outTangent)if(i){if("CUBICSPLINE"!==i){i="LINEAR",o=!0;break}}else i="CUBICSPLINE";else if(i){if("CUBICSPLINE"===i||n.interpolation&&n.interpolation===e.AnimationKeyInterpolation.STEP&&"STEP"!==i){i="LINEAR",o=!0;break}}else i=n.interpolation&&n.interpolation===e.AnimationKeyInterpolation.STEP?"STEP":"LINEAR";return i||(i="LINEAR"),{interpolationType:i,shouldBakeAnimation:o}},a.AddSplineTangent=function(e,t,a,i,n,o,s,l){var u,c=e===r.INTANGENT?n.inTangent:n.outTangent;"CUBICSPLINE"===i&&("rotation"===a?c?(u=s?c.scale(o).asArray():c.scale(o).toQuaternion().asArray(),l&&(u[0]*=-1,u[1]*=-1)):u=[0,0,0,0]:c?(u=c.scale(o).asArray(),l&&(u[2]*=-1)):u=[0,0,0],t.push(u))},a.calculateMinMaxKeyFrames=function(e){var t=1/0,r=-1/0;return e.forEach((function(e){t=Math.min(t,e.frame),r=Math.max(r,e.frame)})),{min:t,max:r}},a})();t._GLTFAnimation=a})(e.GLTF2||(e.GLTF2={}))})(e||(e={}));var e;return (function(e){!(function(t){var r=(function(){function t(){}return t.CreateBufferView=function(e,t,r,a,i){var n={buffer:e,byteLength:r};return t&&(n.byteOffset=t),i&&(n.name=i),a&&(n.byteStride=a),n},t.CreateAccessor=function(e,t,r,a,i,n,o,s){var l={name:t,bufferView:e,componentType:a,count:i,type:r};return o&&(l.min=o),s&&(l.max=s),n&&(l.byteOffset=n),l},t.CalculateMinMaxPositions=function(r,a,i,n){var o,s,l,u=[1/0,1/0,1/0],c=[-1/0,-1/0,-1/0];if(i)for(var h=a,f=a+i;h<f;++h){o=3*h,s=e.Vector3.FromArray(r,o),n&&t.GetRightHandedVector3FromRef(s),l=s.asArray();for(var p=0;p<3;++p){var m=l[p];m<u[p]&&(u[p]=m),m>c[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}));