12 |
- var BABYLON;!(function(e){var t=(function(){function t(){}return t.GLTF=function(t,r,a){var n=r.replace(/\.[^\/.]+$/,""),i=new e.GLTF2._Exporter(t,a);return t.isReady?i._generateGLTF(n):(e.Tools.Error("glTF Serializer: Scene is not ready!"),null)},t.GLB=function(t,r,a){var n=r.replace(/\.[^\/.]+$/,""),i=new e.GLTF2._Exporter(t,a);return t.isReady?i._generateGLB(n):(e.Tools.Error("glTF Serializer: Scene is not ready!"),null)},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=[],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,n){for(var i,s,o=e.VertexBuffer.DeduceStride(r),l=0,u=a.length/o;l<u;++l){if(s=l*o,r===e.VertexBuffer.PositionKind||r===e.VertexBuffer.NormalKind){var c=e.Vector3.FromArray(a,s);this.convertToRightHandedSystem&&t._GLTFUtilities.GetRightHandedVector3FromRef(c),i=c.asArray()}else if(r===e.VertexBuffer.TangentKind||r===e.VertexBuffer.ColorKind){var c=e.Vector4.FromArray(a,s);this.convertToRightHandedSystem&&r!==e.VertexBuffer.ColorKind&&t._GLTFUtilities.GetRightHandedVector4FromRef(c),i=c.asArray()}else r===e.VertexBuffer.UVKind||r===e.VertexBuffer.UV2Kind?(this.convertToRightHandedSystem,i=[a[s],a[s+1]]):(e.Tools.Warn("Unsupported Vertex Buffer Type: "+r),i=[]);i.forEach((function(e){n.setFloat32(e)}))}return 4*a.length},r.prototype.generateJSON=function(e,r,a){var n,i,s,o={byteLength:this.totalByteLength},l=this.totalByteLength,u={asset:this.asset};if(o.byteLength&&(u.buffers=[o]),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&&(i=c.imageData[e.uri],n=e.uri.split(".")[0]+" image",s=t._GLTFUtilities.CreateBufferView(0,l,i.data.length,void 0,n),l+=i.data.buffer.byteLength,c.bufferViews.push(s),e.bufferView=c.bufferViews.length-1,e.name=n,e.mimeType=i.mimeType,e.uri=void 0,u.images||(u.images=[]),u.images.push(e))})),o.byteLength=l}else u.images=this.images;return e||(o.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),n=new Blob([r],{type:"application/octet-stream"}),i=t+".gltf",s=t+".bin",o=new e.GLTFData;if(o.glTFFiles[i]=a,o.glTFFiles[s]=n,this.imageData)for(var l in this.imageData)o.glTFFiles[l]=new Blob([this.imageData[l].data],{type:this.imageData[l].mimeType});return o},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),n=t+".glb",i=a.length,s=0;for(var o in this.imageData)s+=this.imageData[o].data.byteLength;var l=this._getPadding(i),u=this._getPadding(r.byteLength),c=this._getPadding(s),h=28+i+l+r.byteLength+u+s+c,f=new ArrayBuffer(12),m=new DataView(f);m.setUint32(0,1179937895,!0),m.setUint32(4,2,!0),m.setUint32(8,h,!0);var p=new ArrayBuffer(8+i+l),d=new DataView(p);d.setUint32(0,i+l,!0),d.setUint32(4,1313821514,!0);for(var g=new Uint8Array(p,8),T=0;T<i;++T)g[T]=a.charCodeAt(T);for(var y=new Uint8Array(p,8+i),T=0;T<l;++T)y[T]=32;var v=new ArrayBuffer(8),x=new DataView(v);x.setUint32(0,r.byteLength+s+c,!0),x.setUint32(4,5130562,!0);for(var A=new ArrayBuffer(u),b=new Uint8Array(A),T=0;T<u;++T)b[T]=0;for(var C=new ArrayBuffer(c),_=new Uint8Array(C),T=0;T<c;++T)_[T]=0;var B=[f,p,v,r];for(var o in this.imageData)B.push(this.imageData[o].data.buffer);B.push(A),B.push(C);var F=new Blob(B,{type:"application/octet-stream"}),M=new e.GLTFData;return M.glTFFiles[n]=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 n=e.Quaternion.RotationYawPitchRoll(a.rotation.y,a.rotation.x,a.rotation.z);a.rotationQuaternion&&(n=n.multiply(a.rotationQuaternion)),0===n.x&&0===n.y&&0===n.z&&1===n.w||(this.convertToRightHandedSystem&&t._GLTFUtilities.GetRightHandedQuaternionFromRef(n),r.rotation=n.normalize().asArray())},r.prototype.createBufferViewKind=function(r,a,n,i){var s,o,l,u=null;a instanceof e.Mesh?u=a:a instanceof e.InstancedMesh&&(u=a.sourceMesh),u&&(o=u.getVerticesData(r))&&(s=4*o.length,l=t._GLTFUtilities.CreateBufferView(0,n.getByteOffset(),s,i,r+" - "+u.name),this.bufferViews.push(l),this.writeAttributeData(r,o,n))},r.prototype.setPrimitiveAttributes=function(r,a,n){var i,s,o,l,u,c,h,f,m,p,d,g,T,y,v=null,x=null,A=null;a instanceof e.Mesh?v=a:a instanceof e.InstancedMesh&&(v=a.sourceMesh);var b=[{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(v){for(var C=0,_=b;C<_.length;C++){var B=_[C];i=B.kind,v.isVerticesDataPresent(i)&&(this.createBufferViewKind(i,a,n,B.byteStride),B.bufferViewIndex=this.bufferViews.length-1)}if(v.getTotalIndices()&&(s=v.getIndices())){o=4*s.length,l=t._GLTFUtilities.CreateBufferView(0,n.getByteOffset(),o,void 0,"Indices - "+v.name),this.bufferViews.push(l),A=this.bufferViews.length-1;for(var F=0,M=s.length;F<M;++F)n.setUInt32(s[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=b;L<w.length;L++){var B=w[L];if(i=B.kind,(h=v.getVerticesData(i))&&(f=e.VertexBuffer.DeduceStride(i),void 0!=(y=B.bufferViewIndex)))switch(m={min:null,max:null},i==e.VertexBuffer.PositionKind&&(m=t._GLTFUtilities.CalculateMinMaxPositions(h,0,h.length/f,this.convertToRightHandedSystem)),T=t._GLTFUtilities.CreateAccessor(y,i+" - "+a.name,B.accessorType,5126,h.length/f,0,m.min,m.max),this.accessors.push(T),i){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: "+i)}}A&&(T=t._GLTFUtilities.CreateAccessor(A,"indices - "+a.name,"SCALAR",5125,S.indexCount,4*S.indexStart,null,null),this.accessors.push(T),c.indices=this.accessors.length-1),v.material&&(x=null,v.material instanceof e.StandardMaterial||v.material instanceof e.PBRMetallicRoughnessMaterial||v.material instanceof e.PBRMaterial?x=a.getScene().materials.indexOf(v.material):v.material instanceof e.MultiMaterial?(d=v.material,(g=d.subMaterials[S.materialIndex])&&(x=a.getScene().materials.indexOf(g))):e.Tools.Warn("Material type "+v.material.getClassName()+" for material "+v.material.name+" is not yet implemented in glTF serializer."),null!=x&&Object.keys(c.attributes).length>0&&(u?(t._GLTFMaterial._HasTexturesPresent(this.materials[x])||(delete c.attributes.TEXCOORD_0,delete c.attributes.TEXCOORD_1),c.material=x):t._GLTFMaterial._HasTexturesPresent(this.materials[x])&&(p=t._GLTFMaterial._StripTexturesFromMaterial(this.materials[x]),this.materials.push(p),c.material=this.materials.length-1))),r.primitives.push(c)}}}},r.prototype.createScene=function(r,a){if(r.meshes.length){var n,i,s,o=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;o.forEach((function(t){if(n=u.nodeMap[t.uniqueId],i=u.nodes[n],t.parent||(u.shouldExportMesh(t)?l.nodes.push(n):e.Tools.Log("Omitting "+t.name+" from scene.")),s=t.getDescendants(!0),!i.children&&s&&s.length){i.children=[];for(var r=0,a=s;r<a.length;r++){var o=a[r];i.children.push(u.nodeMap[o.uniqueId])}}})),this.scenes.push(l)}},r.prototype.createNodeMapAndAnimations=function(e,r){var a,n,i={},s={name:"runtime animations",channels:[],samplers:[]},o=[],l=this;return e.meshes.forEach((function(u){n=l.createNode(u,r),l.nodes.push(n),a=l.nodes.length-1,i[u.uniqueId]=a,!e.animationGroups.length&&u.animations.length&&t._GLTFAnimation._CreateNodeAnimationFromMeshAnimations(u,s,o,i,l.nodes,r,l.bufferViews,l.accessors,l.convertToRightHandedSystem,l.animationSampleRate)})),s.channels.length&&s.samplers.length&&this.animations.push(s),o.forEach((function(e){e.channels.length&&e.samplers.length&&l.animations.push(e)})),e.animationGroups.length&&t._GLTFAnimation._CreateNodeAnimationFromAnimationGroups(e,l.animations,i,this.nodes,r,this.bufferViews,this.accessors,this.convertToRightHandedSystem,l.animationSampleRate),i},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),n=0,i=a.byteLength;n<i;++n)a[n]=r[n];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={}))})(BABYLON||(BABYLON={}));var BABYLON;!(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],n=void 0;e(t,".glb")?n={type:"model/gltf-binary"}:e(t,".bin")?n={type:"application/octet-stream"}:e(t,".gltf")?n={type:"model/gltf+json"}:e(t,".jpeg")?n={type:"image/jpeg"}:e(t,".png")&&(n={type:"image/png"}),r.href=window.URL.createObjectURL(new Blob([a],n)),r.click()}},e})();e.GLTFData=t})(BABYLON||(BABYLON={}));var BABYLON;!(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,n,i,s,o,l){for(var u=0;u<r.length;++u){var c=r[u];c instanceof e.StandardMaterial?t._ConvertStandardMaterial(c,a,n,i,s,o,l):c instanceof e.PBRMetallicRoughnessMaterial?t._ConvertPBRMetallicRoughnessMaterial(c,a,n,i,s,o,l):c instanceof e.PBRMaterial?t._ConvertPBRMaterial(c,a,n,i,s,o,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,n){return(1-e)*(1-e)*(1-e)*t+3*(1-e)*(1-e)*e*r+3*(1-e)*e*e*a+e*e*e*n}var a=new e.Vector2(0,1),n=new e.Vector2(0,.1),i=new e.Vector2(0,.1),s=new e.Vector2(1300,.1),o=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/s.x,.333333),a.y,n.y,i.y,s.y)})(u);return{baseColorFactor:[o.r,o.g,o.b,l],metallicFactor:0,roughnessFactor:c}},t._SolveMetallic=function(r,a,n){if(a<t._dielectricSpecular.r)return t._dielectricSpecular,0;var i=t._dielectricSpecular.r,s=r*n/(1-t._dielectricSpecular.r)+a-2*t._dielectricSpecular.r,o=t._dielectricSpecular.r-a,l=s*s-4*i*o;return e.Scalar.Clamp((-s+Math.sqrt(l))/(2*i),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 n=t;switch(n.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 "+n.transparencyMode),null}}},t._ConvertStandardMaterial=function(r,a,n,i,s,o,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,n,i,o);null!=h&&(u.baseColorTexture=h)}if(r.bumpTexture){var h=t._ExportTexture(r.bumpTexture,a,n,i,o);h&&(c.normalTexture=h)}if(r.emissiveTexture){var f=t._ExportTexture(r.emissiveTexture,a,n,i,o);f&&(c.emissiveTexture=f),c.emissiveFactor=[1,1,1]}if(r.ambientTexture){var h=t._ExportTexture(r.ambientTexture,a,n,i,o);if(h){var m={index:h.index};c.occlusionTexture=m,m.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,s.push(c)},t._ConvertPBRMetallicRoughnessMaterial=function(r,a,n,i,s,o,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,n,i,o);null!=h&&(u.baseColorTexture=h)}if(r.normalTexture){var h=t._ExportTexture(r.normalTexture,a,n,i,o);h&&(c.normalTexture=h)}if(r.occlusionTexture){var h=t._ExportTexture(r.occlusionTexture,a,n,i,o);h&&(c.occlusionTexture=h,null!=r.occlusionStrength&&(c.occlusionTexture.strength=r.occlusionStrength))}if(r.emissiveTexture){var h=t._ExportTexture(r.emissiveTexture,a,n,i,o);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,s.push(c)},t._CreateBase64FromCanvas=function(e,t,r,a){var n=document.createElement("canvas");n.id="WriteCanvas";var i=n.getContext("2d");n.width=t,n.height=r;var s=i.createImageData(t,r);return s.data.set(e),i.putImageData(s,0,0),n.toDataURL(a)},t._CreateWhiteTexture=function(t,r,a){for(var n=new Uint8Array(t*r*4),i=0;i<n.length;++i)n[i]=255;return e.RawTexture.CreateRGBATexture(n,t,r,a)},t._ResizeTexturesToSameDimensions=function(t,r,a){var n,i,s=t?t.getSize():{width:0,height:0},o=r?r.getSize():{width:0,height:0};return s.width<o.width?(n=t?e.TextureTools.CreateResizedCopy(t,o.width,o.height,!0):this._CreateWhiteTexture(o.width,o.height,a),i=r):s.width>o.width?(i=r?e.TextureTools.CreateResizedCopy(r,s.width,s.height,!0):this._CreateWhiteTexture(s.width,s.height,a),n=t):(n=t,i=r),{texture1:n,texture2:i}},t._ConvertSpecularGlossinessTexturesToMetallicRoughness=function(t,r,a,n){if(!t&&!r)return null;var i=t?t.getScene():r.getScene();if(!i)return e.Tools.Error("_ConvertSpecularGlossinessTexturesToMetallicRoughness: Scene from textures is missing!"),null;var s,o,l=this._ResizeTexturesToSameDimensions(t,r,i),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(s=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;o=l.texture2.readPixels();for(var m=o.byteLength,p=new Uint8Array(m),d=new Uint8Array(m),g=e.Color3.Black(),T=0,y=0,v=0;v<h;++v)for(var x=0;x<c;++x){var A=4*(c*v+x),b=e.Color3.FromInts(s[A],s[A+1],s[A+2]).toLinearSpace().multiply(a.diffuseColor),C=e.Color3.FromInts(o[A],o[A+1],o[A+2]).toLinearSpace().multiply(a.specularColor),_=o[A+3]/255*a.glossiness,B={diffuseColor:b,specularColor:C,glossiness:_},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[A]=255*F.baseColor.r,d[A+1]=255*F.baseColor.g,d[A+2]=255*F.baseColor.b,d[A+3]=l.texture1.hasAlpha?s[A+3]:255,p[A]=0,p[A+1]=255*F.roughness,p[A+2]=255*F.metallic,p[A+3]=255}for(var M={baseColor:g,metallic:T,roughness:y},E=!1,R=!1,v=0;v<h;++v)for(var x=0;x<c;++x){var S=4*(c*v+x);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),p[S+1]/=M.roughness>this._epsilon?M.roughness:1,p[S+2]/=M.metallic>this._epsilon?M.metallic:1;var P=e.Color3.FromInts(255,p[S+1],p[S+2]);this.FuzzyEquals(P,e.Color3.White(),this._epsilon)||(E=!0)}if(E){var V=this._CreateBase64FromCanvas(p,c,h,n);M.metallicRoughnessTextureBase64=V}if(R){var G=this._CreateBase64FromCanvas(d,c,h,n);M.baseColorTextureBase64=G}return M},t._ConvertSpecularGlossinessToMetallicRoughness=function(r){var a=t._GetPerceivedBrightness(r.diffuseColor),n=t._GetPerceivedBrightness(r.specularColor),i=1-t._GetMaxComponent(r.specularColor),s=t._SolveMetallic(a,n,i),o=r.diffuseColor.scale(i/(1-this._dielectricSpecular.r)/Math.max(1-s,this._epsilon)),l=r.specularColor.subtract(this._dielectricSpecular.scale(1-s)).scale(1/Math.max(s,this._epsilon)),u=e.Color3.Lerp(o,l,s*s);return u=u.clampToRef(0,1,u),{baseColor:u,metallic:s,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,n,i,s,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,n,s);u&&(i.baseColorTexture=u)}if(e.metallicTexture){var u=t._ExportTexture(e.metallicTexture,r,a,n,s);null!=u&&(i.metallicRoughnessTexture=u)}}return l},t._ConvertSpecGlossFactorsToMetallicRoughness=function(r,a,n,i,s,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)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_"+i.length+".png",a,n,i,o);null!=h&&(s.baseColorTexture=h)}if(c.metallicRoughnessTextureBase64){var f=t._GetTextureInfoFromBase64(c.metallicRoughnessTextureBase64,"bjsMetallicRoughnessTexture_"+i.length+".png",a,n,i,o);null!=f&&(s.metallicRoughnessTexture=f)}}}else c=this._ConvertSpecularGlossinessToMetallicRoughness(u);return c},t._ConvertPBRMaterial=function(r,a,n,i,s,o,l){var u,c={},h={name:r.name};if(u=r.isMetallicWorkflow()?this._ConvertMetalRoughFactorsToMetallicRoughness(r,a,n,i,c,o,l):this._ConvertSpecGlossFactorsToMetallicRoughness(r,a,n,i,c,o,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,n,i,o);f&&(h.normalTexture=f)}if(r.ambientTexture){var f=t._ExportTexture(r.ambientTexture,a,n,i,o);if(f){var m={index:f.index};h.occlusionTexture=m,r.ambientTextureStrength&&(m.strength=r.ambientTextureStrength)}}if(r.emissiveTexture){var f=t._ExportTexture(r.emissiveTexture,a,n,i,o);null!=f&&(h.emissiveTexture=f)}}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,"BLEND"===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,n,i,s){var o="texture_"+(i.length-1).toString(),l=r.getInternalTexture();null!=l&&(o=l.url||o),o=e.Tools.GetFilename(o);var u=o.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"}o=u+c;var h=t.GetPixelsFromTexture(r),f=r.getSize(),m=this._CreateBase64FromCanvas(h,f.width,f.height,a);return this._GetTextureInfoFromBase64(m,o,a,n,i,s)},t._GetTextureInfoFromBase64=function(e,t,r,a,n,i){for(var s=null,o={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 m={data:c,mimeType:r};if(i[t]=m,"image/jpeg"===r||"image/png"===r){for(var p={uri:t},d=-1,h=0;h<a.length;++h)if(a[h].uri===t){d=h;break}-1===d?(a.push(p),o.source=a.length-1,n.push({source:a.length-1}),s={index:a.length-1}):(o.source=d,s={index:d})}return s},t._dielectricSpecular=new e.Color3(.04,.04,.04),t._maxSpecularPower=1024,t._epsilon=1e-6,t})();t._GLTFMaterial=r})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));var BABYLON;!(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,n,i,s,o){var l=[],u=[],c=r.getKeys(),h=a.calculateMinMaxKeyFrames(c),f=a._DeduceInterpolation(c,n,s),m=h.max-h.min,p=f.interpolationType,d=f.shouldBakeAnimation;if(d?a._CreateBakedAnimation(t,r,n,h.min,h.max,r.framePerSecond,o,l,u,h,i,s):"LINEAR"===p||"STEP"===p?a._CreateLinearOrStepAnimation(t,r,n,m,l,u,i,s):"CUBICSPLINE"===p?a._CreateCubicSplineAnimation(t,r,n,m,l,u,i,s):a._CreateBakedAnimation(t,r,n,h.min,h.max,r.framePerSecond,o,l,u,h,i,s),l.length&&u.length){return{inputs:l,outputs:u,samplerInterpolation:p,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",n=!1,i=t.targetProperty.split(".");switch(i[0]){case"scaling":r="scale";break;case"position":r="translation";break;case"rotation":a="VEC4",r="rotation";break;case"rotationQuaternion":a="VEC4",n=!0,r="rotation";break;default:e.Tools.Error("Unsupported animatable property "+i[0])}return r?{animationChannelTargetPath:r,dataAccessorType:a,useQuaternion:n}:(e.Tools.Error("animation channel target path and data accessor type could be deduced"),null)},a._CreateNodeAnimationFromMeshAnimations=function(e,t,r,n,i,s,o,l,u,c){var h;e.animations&&e.animations.forEach((function(i){var f=a._DeduceAnimationInfo(i);f&&(h={name:i.name,samplers:[],channels:[]},a.AddAnimation(""+i.name,i.hasRunningRuntimeAnimations?t:h,e,i,f.dataAccessorType,f.animationChannelTargetPath,n,s,o,l,u,f.useQuaternion,c),h.samplers.length&&h.channels.length&&r.push(h))}))},a._CreateNodeAnimationFromAnimationGroups=function(t,r,n,i,s,o,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,i=t.animation;if(r instanceof e.Mesh){var f=a._DeduceAnimationInfo(t.animation);if(f){var m=r;a.AddAnimation(""+i.name,h,m,i,f.dataAccessorType,f.animationChannelTargetPath,n,s,o,l,u,f.useQuaternion,c)}}})),h.channels.length&&h.samplers.length&&r.push(h)}))}},a.AddAnimation=function(e,r,n,i,s,o,l,u,c,h,f,m,p){var d,g,T,y,v,x,A,b=a._CreateNodeAnimation(n,i,o,f,m,p);if(b){var C=l[n.uniqueId],_=4*b.inputs.length;d=t._GLTFUtilities.CreateBufferView(0,u.getByteOffset(),_,void 0,e+" keyframe data view"),c.push(d),b.inputs.forEach((function(e){u.setFloat32(e)})),g=t._GLTFUtilities.CreateAccessor(c.length-1,e+" keyframes","SCALAR",5126,b.inputs.length,null,[b.inputsMin],[b.inputsMax]),h.push(g),T=h.length-1,v=b.outputs.length,_="VEC3"===s?12*b.outputs.length:16*b.outputs.length,d=t._GLTFUtilities.CreateBufferView(0,u.getByteOffset(),_,void 0,e+" data view"),c.push(d),b.outputs.forEach((function(e){e.forEach((function(e){u.setFloat32(e)}))})),g=t._GLTFUtilities.CreateAccessor(c.length-1,e+" data",s,5126,v,null,null,null),h.push(g),y=h.length-1,x={interpolation:b.samplerInterpolation,input:T,output:y},r.samplers.push(x),A={sampler:r.samplers.length-1,target:{node:C,path:o}},r.channels.push(A)}},a._CreateBakedAnimation=function(t,r,n,i,s,o,l,u,c,h,f,m){var p,d,g=e.Quaternion.Identity(),T=null,y=null,v=null,x=null,A=null,b=null;h.min=e.Tools.FloatRound(i/o);for(var C=r.getKeys(),_=0,B=C.length;_<B;++_){if(b=null,v=C[_],_+1<B)if(x=C[_+1],v.value.equals(x.value)){if(0!==_)continue;b=v.frame}else b=x.frame;else{if(A=C[_-1],v.value.equals(A.value))continue;b=s}if(b)for(var F=v.frame;F<=b;F+=l)(d=e.Tools.FloatRound(F/o))!==T&&(T=d,y=d,p=r._interpolate(F,0,void 0,r.loopMode),a._SetInterpolatedValue(t,p,d,r,n,g,u,c,f,m))}y&&(h.max=y)},a._ConvertFactorToVector3OrQuaternion=function(t,r,n,i,s,o,l){var u,c,h=null,f=a._GetBasePositionRotationOrScale(r,s,o,l);if(i===e.Animation.ANIMATIONTYPE_FLOAT)switch(u=n.targetProperty.split("."),c=u?u[1]:"",h=l?e.Quaternion.FromArray(f).normalize():e.Vector3.FromArray(f),c){case"x":case"y":h[c]=o&&l?-t:t;break;case"z":h[c]=o&&!l&&"scale"!==s?-t:t;break;case"w":h.w=t;break;default:e.Tools.Error('glTFAnimation: Unsupported component type "'+c+'" for scale animation!')}return h},a._SetInterpolatedValue=function(t,r,a,n,i,s,o,l,u,c){var h,f=n.dataType;o.push(a),"number"==typeof r&&(r=this._ConvertFactorToVector3OrQuaternion(r,t,n,f,i,u,c)),r&&("rotation"===i?(c?s=r:(h=r,e.Quaternion.RotationYawPitchRollToRef(h.y,h.x,h.z,s)),u&&(s.x*=-1,s.y*=-1,l.push(s.asArray()))):(h=r,u&&"scale"!==i&&(h.z*=-1),l.push(h.asArray())))},a._CreateLinearOrStepAnimation=function(e,t,r,n,i,s,o,l){t.getKeys().forEach((function(n){i.push(n.frame/t.framePerSecond),a._AddKeyframeValue(n,t,s,r,e,o,l)}))},a._CreateCubicSplineAnimation=function(e,t,n,i,s,o,l,u){t.getKeys().forEach((function(c){s.push(c.frame/t.framePerSecond),a.AddSplineTangent(r.INTANGENT,o,n,"CUBICSPLINE",c,i,u,l),a._AddKeyframeValue(c,t,o,n,e,l,u),a.AddSplineTangent(r.OUTTANGENT,o,n,"CUBICSPLINE",c,i,u,l)}))},a._GetBasePositionRotationOrScale=function(t,r,a,n){var i;return"rotation"===r?n?t.rotationQuaternion?(i=t.rotationQuaternion.asArray(),a&&(i[0]*=-1,i[1]*=-1)):i=e.Quaternion.Identity().asArray():(i=t.rotation.asArray(),i[2]*=-1):"translation"===r?(i=t.position.asArray(),a&&(i[2]*=-1)):i=t.scaling.asArray(),i},a._AddKeyframeValue=function(t,r,a,n,i,s,o){var l,u,c=r.dataType;c===e.Animation.ANIMATIONTYPE_VECTOR3?(l=t.value.asArray(),s&&"scale"!==n&&(l[2]*=-1),"rotation"===n?a.push(e.Vector3.FromArray(l).toQuaternion().asArray()):a.push(l)):c===e.Animation.ANIMATIONTYPE_FLOAT?(u=this._ConvertFactorToVector3OrQuaternion(t.value,i,r,c,n,s,o))&&("rotation"===n?o?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(),s&&(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 n,i,s=!1;if("rotation"===r&&!a)return{interpolationType:"LINEAR",shouldBakeAnimation:!0};for(var o=0,l=t.length;o<l;++o)if(i=t[o],i.inTangent||i.outTangent)if(n){if("CUBICSPLINE"!==n){n="LINEAR",s=!0;break}}else n="CUBICSPLINE";else if(n){if("CUBICSPLINE"===n||i.interpolation&&i.interpolation===e.AnimationKeyInterpolation.STEP&&"STEP"!==n){n="LINEAR",s=!0;break}}else n=i.interpolation&&i.interpolation===e.AnimationKeyInterpolation.STEP?"STEP":"LINEAR";return n||(n="LINEAR"),{interpolationType:n,shouldBakeAnimation:s}},a.AddSplineTangent=function(e,t,a,n,i,s,o,l){var u,c=e===r.INTANGENT?i.inTangent:i.outTangent;"CUBICSPLINE"===n&&("rotation"===a?c?(u=o?c.scale(s).asArray():c.scale(s).toQuaternion().asArray(),l&&(u[0]*=-1,
- u[1]*=-1)):u=[0,0,0,0]:c?(u=c.scale(s).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={}))})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){var r=(function(){function t(){}return t.CreateBufferView=function(e,t,r,a,n){var i={buffer:e,byteLength:r};return t&&(i.byteOffset=t),n&&(i.name=n),a&&(i.byteStride=a),i},t.CreateAccessor=function(e,t,r,a,n,i,s,o){var l={name:t,bufferView:e,componentType:a,count:n,type:r};return s&&(l.min=s),o&&(l.max=o),i&&(l.byteOffset=i),l},t.CalculateMinMaxPositions=function(r,a,n,i){var s,o,l,u=[1/0,1/0,1/0],c=[-1/0,-1/0,-1/0];if(n)for(var h=a,f=a+n;h<f;++h){s=3*h,o=e.Vector3.FromArray(r,s),i&&t.GetRightHandedVector3FromRef(o),l=o.asArray();for(var m=0;m<3;++m){var p=l[m];p<u[m]&&(u[m]=p),p>c[m]&&(c[m]=p),++s}}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={}))})(BABYLON||(BABYLON={}));
|