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