babylon.stlFileLoader.min.js 2.7 KB

1
  1. var BABYLON;!(function(e){var t=(function(){function t(){this.solidPattern=/solid (\S*)([\S\s]*)endsolid[ ]*(\S*)/g,this.facetsPattern=/facet([\s\S]*?)endfacet/g,this.normalPattern=/normal[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g,this.vertexPattern=/vertex[\s]+([\-+]?[0-9]+\.?[0-9]*([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+[\s]+([\-+]?[0-9]*\.?[0-9]+([eE][\-+]?[0-9]+)?)+/g,this.name="stl",this.extensions={".stl":{isBinary:!0}}}return t.prototype.importMesh=function(t,r,n,a,i,s,o){var l;if(this.isBinary(n)){var u=new e.Mesh("stlmesh",r);return this.parseBinary(u,n),i&&i.push(u),!0}for(var f=new Uint8Array(n),h="",c=0;c<n.byteLength;c++)h+=String.fromCharCode(f[c]);for(n=h;l=this.solidPattern.exec(n);){var d=l[1];if(d!=l[3])return e.Tools.Error("Error in STL, solid name != endsolid name"),!1;if(t&&d)if(t instanceof Array){if(!t.indexOf(d))continue}else if(d!==t)continue;d=d||"stlmesh";var u=new e.Mesh(d,r);this.parseASCII(u,l[2]),i&&i.push(u)}return!0},t.prototype.load=function(e,t,r){var n=this.importMesh(null,e,t,r,null,null,null);return n&&e.createDefaultCameraOrLight(),n},t.prototype.loadAssetContainer=function(t,r,n,a){var i=new e.AssetContainer(t);return this.importMesh(null,t,r,n,i.meshes,null,null),i.removeAllFromScene(),i},t.prototype.isBinary=function(e){var t,r;if(r=new DataView(e),t=50,84+r.getUint32(80,!0)*t===r.byteLength)return!0;for(var n=r.byteLength,a=0;a<n;a++)if(r.getUint8(a)>127)return!0;return!1},t.prototype.parseBinary=function(t,r){for(var n=new DataView(r),a=n.getUint32(80,!0),i=0,s=new Float32Array(3*a*3),o=new Float32Array(3*a*3),l=new Uint32Array(3*a),u=0,f=0;f<a;f++){for(var h=84+50*f,c=n.getFloat32(h,!0),d=n.getFloat32(h+4,!0),m=n.getFloat32(h+8,!0),p=1;p<=3;p++){var v=h+12*p;s[i]=n.getFloat32(v,!0),s[i+2]=n.getFloat32(v+4,!0),s[i+1]=n.getFloat32(v+8,!0),o[i]=c,o[i+2]=d,o[i+1]=m,i+=3}l[u]=u++,l[u]=u++,l[u]=u++}t.setVerticesData(e.VertexBuffer.PositionKind,s),t.setVerticesData(e.VertexBuffer.NormalKind,o),t.setIndices(l),t.computeWorldMatrix(!0)},t.prototype.parseASCII=function(t,r){for(var n,a=[],i=[],s=[],o=0;n=this.facetsPattern.exec(r);){var l=n[1],u=this.normalPattern.exec(l);if(this.normalPattern.lastIndex=0,u){for(var f,h=[Number(u[1]),Number(u[5]),Number(u[3])];f=this.vertexPattern.exec(l);)a.push(Number(f[1]),Number(f[5]),Number(f[3])),i.push(h[0],h[1],h[2]);s.push(o++,o++,o++),this.vertexPattern.lastIndex=0}}this.facetsPattern.lastIndex=0,t.setVerticesData(e.VertexBuffer.PositionKind,a),t.setVerticesData(e.VertexBuffer.NormalKind,i),t.setIndices(s),t.computeWorldMatrix(!0)},t})();e.STLFileLoader=t,e.SceneLoader&&e.SceneLoader.RegisterPlugin(new t)})(BABYLON||(BABYLON={}));