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