1 |
- var BABYLON;!(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);if(t.isReady)return n._generateGLTF(i);throw new Error("glTF Serializer: Scene is not ready!")},t.GLB=function(t,r,a){var i=r.replace(/\.[^\/.]+$/,""),n=new e.GLTF2._Exporter(t,a);if(t.isReady)return n._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={},void 0!==t&&(this.options=t);var r=0;r=this.createScene(this.babylonScene,r,null),this.totalByteLength=r}return r.prototype.createBufferView=function(e,t,r,a,i){var n={buffer:e,byteLength:r};return t>0&&(n.byteOffset=t),i&&(n.name=i),a&&(n.byteStride=a),n},r.prototype.createAccessor=function(e,t,r,a,i,n,s,o){var l={name:t,bufferView:e,componentType:a,count:i,type:r};return s&&(l.min=s),o&&(l.max=o),n&&(l.byteOffset=n),l},r.prototype.calculateMinMax=function(e,t,r,a,i){var n=[1/0,1/0,1/0],s=[-1/0,-1/0,-1/0],o=t+r;if(r>0)for(var l=t;l<o;++l)for(var u=a*l,f=1,h=0;h<a;++h){h!==a-1||i||(f=-1);var c=f*e[u];c<n[h]&&(n[h]=c),c>s[h]&&(s[h]=c),++u}return{min:n,max:s}},r.prototype.writeAttributeData=function(t,r,a,i,n,s,o){var l=n,u=r.length/a,f=0;switch(t){case e.VertexBuffer.PositionKind:case e.VertexBuffer.NormalKind:for(var h=0;h<u;++h){var c=h*a;s.setFloat32(l,r[c],!0),l+=4,s.setFloat32(l,r[c+1],!0),l+=4,o?s.setFloat32(l,r[c+2],!0):s.setFloat32(l,-r[c+2],!0),l+=4}f=4*r.length;break;case e.VertexBuffer.TangentKind:for(var h=0;h<u;++h){var c=h*a;s.setFloat32(l,r[c],!0),l+=4,s.setFloat32(l,r[c+1],!0),l+=4,o?s.setFloat32(l,r[c+2],!0):s.setFloat32(l,-r[c+2],!0),l+=4,s.setFloat32(l,r[c+3],!0),l+=4}f=4*r.length;break;case e.VertexBuffer.ColorKind:for(var h=0;h<u;++h){var c=h*a;s.setFloat32(l,r[c],!0),l+=4,s.setFloat32(l,r[c+1],!0),l+=4,s.setFloat32(l,r[c+2],!0),l+=4,s.setFloat32(l,r[c+3],!0),l+=4}f=4*r.length;break;case e.VertexBuffer.UVKind:case e.VertexBuffer.UV2Kind:for(var h=0;h<u;++h){var c=h*a;s.setFloat32(l,r[c],!0),l+=4,s.setFloat32(l,r[c+1],!0),l+=4}f=4*r.length;break;default:throw new Error("Unsupported vertex buffer type: "+t)}return f},r.prototype.generateJSON=function(e,t,r){var a={byteLength:this.totalByteLength},i={asset:this.asset};if(a.byteLength>0&&(i.buffers=[a]),this.nodes&&0!==this.nodes.length&&(i.nodes=this.nodes),this.meshes&&0!==this.meshes.length&&(i.meshes=this.meshes),this.scenes&&0!==this.scenes.length&&(i.scenes=this.scenes,i.scene=0),this.bufferViews&&0!==this.bufferViews.length&&(i.bufferViews=this.bufferViews),this.accessors&&0!==this.accessors.length&&(i.accessors=this.accessors),this.materials&&0!==this.materials.length&&(i.materials=this.materials),this.textures&&0!==this.textures.length&&(i.textures=this.textures),this.images&&0!==this.images.length)if(e){i.images=[];for(var n=this.images.length,s=this.totalByteLength,o=0;o<n;++o){var l=this.images[o];if(void 0!==l.uri){var u=this.imageData[l.uri],f=l.uri.split(".")[0]+" image",h=this.createBufferView(0,s,u.data.length,void 0,f);s+=u.data.buffer.byteLength,this.bufferViews.push(h),l.bufferView=this.bufferViews.length-1,l.name=f,l.mimeType=u.mimeType,l.uri=void 0,i.images.push(l)}}a.byteLength=s}else i.images=this.images;return e||(a.uri=t+".bin"),r?JSON.stringify(i,null,2):JSON.stringify(i)},r.prototype._generateGLTF=function(t){var r=this.generateJSON(!1,t,!0),a=this.generateBinary(),i=new Blob([a],{type:"application/octet-stream"}),n=t+".gltf",s=t+".bin",o=new e._GLTFData;if(o.glTFFiles[n]=r,o.glTFFiles[s]=i,null!==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=0,t=new ArrayBuffer(this.totalByteLength),r=new DataView(t);return e=this.createScene(this.babylonScene,e,r),t},r.prototype._getPadding=function(e){var t=e%4;return 0===t?t:4-t},r.prototype._generateGLB=function(t){var r=this.generateJSON(!0),a=this.generateBinary(),i=t+".glb",n=r.length,s=0;for(var o in this.imageData)s+=this.imageData[o].data.byteLength;var l=this._getPadding(n),u=this._getPadding(a.byteLength),f=this._getPadding(s),h=28+n+l+a.byteLength+u+s+f,c=new ArrayBuffer(12),g=new DataView(c);g.setUint32(0,1179937895,!0),g.setUint32(4,2,!0),g.setUint32(8,h,!0);var d=new ArrayBuffer(8+n+l),p=new DataView(d);p.setUint32(0,n+l,!0),p.setUint32(4,1313821514,!0);for(var m=new Uint8Array(d,8),v=0;v<n;++v)m[v]=r.charCodeAt(v);for(var x=new Uint8Array(d,8+n),v=0;v<l;++v)x[v]=32;var B=new ArrayBuffer(8),T=new DataView(B);T.setUint32(0,a.byteLength+s+f,!0),T.setUint32(4,5130562,!0);for(var y=new ArrayBuffer(u),b=new Uint8Array(y),v=0;v<u;++v)b[v]=0;for(var w=new ArrayBuffer(f),V=new Uint8Array(w),v=0;v<f;++v)V[v]=0;var A=[c,d,B,a];for(var o in this.imageData)A.push(this.imageData[o].data.buffer);A.push(y),A.push(w);var L=new Blob(A,{type:"application/octet-stream"}),F=new e._GLTFData;return F.glTFFiles[i]=L,F},r.prototype.setNodeTransformation=function(t,r,a){0===r.position.x&&0===r.position.y&&0===r.position.z||(t.translation=a?r.position.asArray():[r.position.x,r.position.y,-r.position.z]),1===r.scaling.x&&1===r.scaling.y&&1===r.scaling.z||(t.scale=a?r.scaling.asArray():[r.scaling.x,r.scaling.y,-r.scaling.z]);var i=e.Quaternion.RotationYawPitchRoll(r.rotation.y,r.rotation.x,r.rotation.z);r.rotationQuaternion&&(i=i.multiply(r.rotationQuaternion)),0===i.x&&0===i.y&&0===i.z&&1===i.w||(t.rotation=a?i.asArray():[-i.x,-i.y,i.z,i.w])},r.prototype.createBufferViewKind=function(t,r,a,i,n){var s=null,o=0;if(r instanceof e.Mesh?s=r:r instanceof e.InstancedMesh&&(s=r.sourceMesh),null!==s){var l=null,u=null,f=null,h=null;if(s.getVerticesDataKinds().indexOf(t)>-1)if(l=s.getVertexBuffer(t),u=l.getOffset(),f=l.getData(),h=l.getStrideSize(),n&&f)o=this.writeAttributeData(t,f,h,u,a,n,i),a+=o;else{var c=null;switch(t){case e.VertexBuffer.PositionKind:o=4*f.length,c="Position - "+s.name;break;case e.VertexBuffer.NormalKind:o=4*f.length,c="Normal - "+s.name;break;case e.VertexBuffer.TangentKind:o=4*f.length,c="Tangent - "+s.name;break;case e.VertexBuffer.ColorKind:o=4*f.length,c="Color - "+s.name;break;case e.VertexBuffer.UVKind:o=4*f.length,c="TexCoord 0 - "+s.name;break;case e.VertexBuffer.UV2Kind:o=4*f.length,c="TexCoord 1 - "+s.name;break;default:e.Tools.Warn("Unsupported VertexBuffer kind: "+t)}if(null!==c){var g=this.createBufferView(0,a,o,4*h,c);a+=o,this.bufferViews.push(g)}}}return o},r.prototype.setPrimitiveAttributes=function(t,r,a,i,n){var s=null;r instanceof e.Mesh?s=r:r instanceof e.InstancedMesh&&(s=r.sourceMesh);var o=null,l=null,u=null,f=null,h=null,c=null;if(null!==s&&(s.isVerticesDataPresent(e.VertexBuffer.PositionKind)&&(a+=this.createBufferViewKind(e.VertexBuffer.PositionKind,r,a,i,n),o=this.bufferViews.length-1),s.isVerticesDataPresent(e.VertexBuffer.NormalKind)&&(a+=this.createBufferViewKind(e.VertexBuffer.NormalKind,r,a,i,n),l=this.bufferViews.length-1),s.isVerticesDataPresent(e.VertexBuffer.ColorKind)&&(a+=this.createBufferViewKind(e.VertexBuffer.ColorKind,r,a,i,n),u=this.bufferViews.length-1),s.isVerticesDataPresent(e.VertexBuffer.TangentKind)&&(a+=this.createBufferViewKind(e.VertexBuffer.TangentKind,r,a,i,n),u=this.bufferViews.length-1),s.isVerticesDataPresent(e.VertexBuffer.UVKind)&&(a+=this.createBufferViewKind(e.VertexBuffer.UVKind,r,a,i,n),f=this.bufferViews.length-1),s.isVerticesDataPresent(e.VertexBuffer.UV2Kind)&&(a+=this.createBufferViewKind(e.VertexBuffer.UV2Kind,r,a,i,n),h=this.bufferViews.length-1),s.getTotalIndices()>0)){var g=s.getIndices();if(n){for(var d=g.length,p=a,m=0;m<d;++m)n.setUint32(p,g[m],!0),p+=4;a=p}else{var v=4*g.length,x=this.createBufferView(0,a,v,void 0,"Indices - "+s.name);a+=v,this.bufferViews.push(x),c=this.bufferViews.length-1}}for(var B=0;B<r.subMeshes.length;++B){var T=r.subMeshes[B],y={attributes:{}};if(null!==s){if(!n){if(null!==o){var b=s.getVertexBuffer(e.VertexBuffer.PositionKind),w=b.getData(),V=b.getStrideSize(),A=this.calculateMinMax(w,0,w.length/V,V,i),L=this.createAccessor(o,"Position","VEC3",5126,w.length/V,0,A.min,A.max);this.accessors.push(L),y.attributes.POSITION=this.accessors.length-1}if(null!==l){var F=s.getVertexBuffer(e.VertexBuffer.NormalKind),S=F.getData(),M=F.getStrideSize(),L=this.createAccessor(l,"Normal","VEC3",5126,S.length/M);this.accessors.push(L),y.attributes.NORMAL=this.accessors.length-1}var L;if(null!==u){var C=s.getVertexBuffer(e.VertexBuffer.ColorKind),E=C.getData(),P=C.getStrideSize(),L=this.createAccessor(u,"Color","VEC4",5126,E.length/P);this.accessors.push(L),y.attributes.COLOR_0=this.accessors.length-1}if(null!==f){var D=s.getVertexBuffer(e.VertexBuffer.UVKind),R=D.getData(),O=D.getStrideSize(),L=this.createAccessor(f,"Texture Coords 0","VEC2",5126,R.length/O);this.accessors.push(L),y.attributes.TEXCOORD_0=this.accessors.length-1}if(null!==h){var K=s.getVertexBuffer(e.VertexBuffer.UV2Kind),N=K.getData(),U=K.getStrideSize(),L=this.createAccessor(h,"Texture Coords 1","VEC2",5126,N.length/U);this.accessors.push(L),y.attributes.TEXCOORD_1=this.accessors.length-1}if(c){var L=this.createAccessor(c,"Indices","SCALAR",5125,T.indexCount,4*T.indexStart);this.accessors.push(L),y.indices=this.accessors.length-1}}if(s.material)if(s.material instanceof e.StandardMaterial||s.material instanceof e.PBRMetallicRoughnessMaterial){var G=r.getScene().materials.indexOf(s.material);y.material=G}else if(s.material instanceof e.MultiMaterial){var _=s.material,z=_.subMaterials[T.materialIndex];if(null!==z){var G=r.getScene().materials.indexOf(z);y.material=G}}else e.Tools.Warn("Material type "+s.material.getClassName()+" for material "+s.material.name+" is not yet implemented in glTF serializer.");t.primitives.push(y)}}return a},r.prototype.createScene=function(e,r,a){if(e.meshes.length>0){var i=e.meshes,n={nodes:new Array};null==a&&t._GLTFMaterial.ConvertMaterialsToGLTF(e.materials,"image/jpeg",this.images,this.textures,this.materials,this.imageData,!0);for(var s=0;s<i.length;++s)if(!this.options||void 0===this.options.shouldExportMesh||this.options.shouldExportMesh(i[s])){var o={mesh:-1},l=i[s],u=l.getScene().useRightHandedSystem;this.setNodeTransformation(o,l,u);var f={primitives:new Array};f.primitives=[],r=this.setPrimitiveAttributes(f,l,r,u,a),this.meshes.push(f),o.mesh=this.meshes.length-1,l.name&&(o.name=l.name),this.nodes.push(o),n.nodes.push(this.nodes.length-1)}this.scenes.push(n)}return r},r})();t._Exporter=r})(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],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})(BABYLON||(BABYLON={}));var BABYLON;!(function(e){!(function(t){var r=(function(){function t(){}return t.ConvertMaterialsToGLTF=function(r,a,i,n,s,o,l){for(var u=0;u<r.length;++u){var f=r[u];f instanceof e.StandardMaterial?t.ConvertStandardMaterial(f,a,i,n,s,o,l):f instanceof e.PBRMetallicRoughnessMaterial&&t.ConvertPBRMetallicRoughnessMaterial(f,a,i,n,s,o,l)}},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),s=new e.Vector2(1300,.1),o=t.diffuseColor.toLinearSpace().scale(.5),l=t.alpha,u=e.Scalar.Clamp(t.specularPower,0,this.maxSpecularPower),f=(function(e){return r(Math.pow(e/s.x,.333333),a.y,i.y,n.y,s.y)})(u);return{baseColorFactor:[o.r,o.g,o.b,l],metallicFactor:0,roughnessFactor:f}},t.SolveMetallic=function(r,a,i){if(a<t.dielectricSpecular.r)return t.dielectricSpecular,0;var n=t.dielectricSpecular.r,s=r*i/(1-t.dielectricSpecular.r)+a-2*t.dielectricSpecular.r,o=t.dielectricSpecular.r-a,l=s*s-4*n*o;return e.Scalar.Clamp((-s+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))throw new Error("Unsupported Babylon material type");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:throw new Error("Unsupported alpha mode "+a.transparencyMode)}},t.ConvertStandardMaterial=function(r,a,i,n,s,o,l){e.Tools.Warn(r.name+": Standard Material is currently not fully supported/implemented in glTF serializer");var u=t.ConvertToGLTFPBRMetallicRoughness(r),f={name:r.name};if(r.backFaceCulling&&(r.twoSidedLighting||e.Tools.Warn(r.name+": Back-face culling enabled and two-sided lighting disabled is not supported in glTF."),f.doubleSided=!0),l){if(r.diffuseTexture){var h=t.ExportTexture(r.diffuseTexture,a,i,n,o);null!=h&&(u.baseColorTexture=h)}if(r.bumpTexture){var h=t.ExportTexture(r.bumpTexture,a,i,n,o);h&&(f.normalTexture=h)}if(r.emissiveTexture){var c=t.ExportTexture(r.emissiveTexture,a,i,n,o);c&&(f.emissiveTexture=c),f.emissiveFactor=[1,1,1]}if(r.ambientTexture){var g=t.ExportTexture(r.ambientTexture,a,i,n,o);g&&(f.occlusionTexture=g)}}(r.alpha<1||r.opacityTexture)&&(r.alphaMode===e.Engine.ALPHA_COMBINE?f.alphaMode="BLEND":e.Tools.Warn(r.name+": glTF 2.0 does not support alpha mode: "+r.alphaMode.toString())),f.pbrMetallicRoughness=u,s.push(f)},t.ConvertPBRMetallicRoughnessMaterial=function(e,r,a,i,n,s,o){var l={};e.baseColor&&(l.baseColorFactor=[e.baseColor.r,e.baseColor.g,e.baseColor.b,e.alpha]),null!=e.metallic&&(l.metallicFactor=e.metallic),null!=e.roughness&&(l.roughnessFactor=e.roughness);var u={name:e.name};if(e.doubleSided&&(u.doubleSided=e.doubleSided),o){if(null!=e.baseTexture){var f=t.ExportTexture(e.baseTexture,r,a,i,s);null!=f&&(l.baseColorTexture=f)}if(e.normalTexture){var f=t.ExportTexture(e.normalTexture,r,a,i,s);f&&(u.normalTexture=f)}if(e.occlusionTexture){var f=t.ExportTexture(e.occlusionTexture,r,a,i,s);f&&(u.occlusionTexture=f,null!=e.occlusionStrength&&(u.occlusionTexture.strength=e.occlusionStrength))}if(e.emissiveTexture){var f=t.ExportTexture(e.emissiveTexture,r,a,i,s);null!=f&&(u.emissiveTexture=f)}}if(e.emissiveColor.equalsFloats(0,0,0)&&(u.emissiveFactor=e.emissiveColor.asArray()),null!=e.transparencyMode){var h=t.GetAlphaMode(e);"OPAQUE"!==h&&(u.alphaMode=h,"BLEND"===h&&(u.alphaCutoff=e.alphaCutOff))}u.pbrMetallicRoughness=l,n.push(u)},t.ExportTexture=function(t,r,a,i,n){var s=null,o={source:a.length},l="texture_"+(i.length-1).toString(),u=t.getInternalTexture();null!=u&&(l=u.url),l=e.Tools.GetFilename(l);var f=l.split(".")[0],h="";"image/jpeg"===r?h=".jpg":"image/png"===r?h=".png":e.Tools.Error("Unsupported mime type "+r),l=f+h;var c=t.readPixels(),g=document.createElement("canvas");g.id="ImageCanvas";var d=g.getContext("2d"),p=t.getSize();g.width=p.width,g.height=p.height;var m=d.createImageData(p.width,p.height);m.data.set(c),d.putImageData(m,0,0);for(var v=g.toDataURL(r),x=atob(v.split(",")[1]),B=new Uint8Array(x.length),T=0;T<x.length;++T)B[T]=x.charCodeAt(T);var y={data:B,mimeType:r};if(n[l]=y,"image/jpeg"===r){for(var b={uri:l},w=-1,T=0;T<a.length;++T)if(a[T].uri===l){w=T;break}-1===w?(a.push(b),o.source=a.length-1,i.push({source:a.length-1}),s={index:a.length-1}):(o.source=w,s={index:w})}return s},t.dielectricSpecular=new e.Color3(.04,.04,.04),t.maxSpecularPower=1024,t})();t._GLTFMaterial=r})(e.GLTF2||(e.GLTF2={}))})(BABYLON||(BABYLON={}));
|