Add off file support.

This commit is contained in:
kovacsv 2018-12-19 18:01:44 +01:00
parent 8ae163b245
commit 028182dd60
4 changed files with 15 additions and 10 deletions

View File

@ -7,6 +7,7 @@ Supported file formats:
- 3ds (with textures)
- obj, mtl (with textures)
- stl (ascii and binary)
- off
Website
-------

View File

@ -20,7 +20,7 @@ JSM.ThreeViewer.prototype.AddMesh=function(a){this.scene.add(a);this.DrawIfNeede
JSM.ThreeViewer.prototype.VertexCount=function(){var a=0,b=this;this.scene.traverse(function(e){b.IsRelevantObject(e)&&(a+=e.geometry.vertices.length)});return a};JSM.ThreeViewer.prototype.FaceCount=function(){var a=0;this.scene.traverse(function(b){b instanceof THREE.Mesh&&(a+=b.geometry.faces.length)});return a};JSM.ThreeViewer.prototype.GetMesh=function(a){var b=null,e=0,d;for(d=0;d<this.scene.children.length;d++)if(b=this.scene.children[d],this.IsRelevantObject(b)){if(e==a)return b;e+=1}return null};
JSM.ThreeViewer.prototype.ShowMesh=function(a){a.visible=!0;this.DrawIfNeeded()};JSM.ThreeViewer.prototype.HideMesh=function(a){a.visible=!1;this.DrawIfNeeded()};JSM.ThreeViewer.prototype.RemoveMesh=function(a){a.geometry.dispose();this.scene.remove(a);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.RemoveMeshes=function(){var a,b;for(b=0;b<this.scene.children.length;b++)a=this.scene.children[b],this.IsRelevantObject(a)&&(a.geometry.dispose(),this.scene.remove(a),b--);this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.RemoveLastMesh=function(){var a=null,b=this;this.scene.traverse(function(e){b.IsRelevantObject(e)&&(a=e)});null!==a&&this.scene.remove(a);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.SetCamera=function(a,b,e){this.navigation.SetCamera(a,b,e);this.navigation.SetOrbitCenter(b.Clone());this.DrawIfNeeded()};
JSM.ThreeViewer.prototype.Resize=function(){this.camera.aspect=this.canvas.width/this.canvas.height;this.camera.updateProjectionMatrix();this.renderer.setSize(this.canvas.width,this.canvas.height);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.FitInWindow=function(){if(0!=this.VisibleMeshCount()){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius());this.DrawIfNeeded()}};
JSM.ThreeViewer.prototype.Resize=function(){this.camera.aspect=this.canvas.width/this.canvas.height;this.camera.updateProjectionMatrix();this.renderer.setSize(this.canvas.width,this.canvas.height);this.DrawIfNeeded()};JSM.ThreeViewer.prototype.FitInWindow=function(){if(0!==this.VisibleMeshCount()){var a=this.GetBoundingSphere();this.navigation.FitInWindow(a.GetCenter(),a.GetRadius());this.DrawIfNeeded()}};
JSM.ThreeViewer.prototype.FitMeshesInWindow=function(a){if(0!==a.length){var b=this.GetFilteredBoundingSphere(function(b){return-1!=a.indexOf(b)});this.navigation.FitInWindow(b.GetCenter(),b.GetRadius());this.DrawIfNeeded()}};JSM.ThreeViewer.prototype.AdjustClippingPlanes=function(a){this.GetBoundingSphere().GetRadius()<a?(this.camera.near=0.1,this.camera.far=1E3):(this.camera.near=10,this.camera.far=1E6);this.camera.updateProjectionMatrix();this.Draw()};
JSM.ThreeViewer.prototype.GetCenter=function(){var a=this;return this.GetFilteredCenter(function(b){return a.IsVisibleObject(b)})};JSM.ThreeViewer.prototype.GetBoundingBox=function(){var a=this;return this.GetFilteredBoundingBox(function(b){return a.IsVisibleObject(b)})};JSM.ThreeViewer.prototype.GetBoundingSphere=function(){var a=this;return this.GetFilteredBoundingSphere(function(b){return a.IsVisibleObject(b)})};JSM.ThreeViewer.prototype.GetFilteredCenter=function(a){return this.GetFilteredBoundingBox(a).GetCenter()};
JSM.ThreeViewer.prototype.GetFilteredBoundingBox=function(a){var b=new JSM.Coord(JSM.Inf,JSM.Inf,JSM.Inf),e=new JSM.Coord(-JSM.Inf,-JSM.Inf,-JSM.Inf),d,c;this.scene.traverse(function(g){if(a(g)){d=g.geometry;var f;for(f=0;f<d.vertices.length;f++)c=d.vertices[f].clone(),c.add(g.position),b.x=JSM.Minimum(b.x,c.x),b.y=JSM.Minimum(b.y,c.y),b.z=JSM.Minimum(b.z,c.z),e.x=JSM.Maximum(e.x,c.x),e.y=JSM.Maximum(e.y,c.y),e.z=JSM.Maximum(e.z,c.z)}});return new JSM.Box(b,e)};

View File

@ -368,16 +368,20 @@ JSM.ReadBinaryStlFile=function(a,b){function c(a){var b=[],c;for(c=0;3>c;c++)b[c
JSM.ReadAsciiStlFile=function(a,b){function c(a,c){var d=a[c].trim();if(0===d.length)return c+1;d=d.split(/\s+/);if(0===d.length)return c+1;if("solid"!=d[0]&&"facet"==d[0]&&"normal"==d[1]){if(5>d.length)return-1;var d=[parseFloat(d[2]),parseFloat(d[3]),parseFloat(d[4])],e=[],l,m;for(l=c+1;l<a.length&&3>e.length;l++)if(m=a[l].trim(),0!==m.length&&(m=m.split(/\s+/),0!==m.length&&"vertex"==m[0]))if(4>m.length)break;else m=[parseFloat(m[1]),parseFloat(m[2]),parseFloat(m[3])],e.push(m);l+=1;if(3!=e.length)return-1;
if(void 0!==b.onFace&&null!==b.onFace)b.onFace(e[0],e[1],e[2],d);return l}return c+1}if(void 0===b||null===b)b={};for(var d=0,e=a.split("\n");d<e.length&&-1!=d;)d=c(e,d)};JSM.IsBinaryStlFile=function(a){var b=a.byteLength;if(84>b)return!1;a=new JSM.BinaryReader(a,!0);a.Skip(80);a=a.ReadUnsignedInteger32();return b!=50*a+84?!1:!0};
JSM.ConvertStlToJsonData=function(a,b){var c=new JSM.TriangleModel,d=c.AddBody(new JSM.TriangleBody("Default")),e=c.GetBody(d);null!==a?JSM.ReadBinaryStlFile(a,{onFace:function(a,b,c,d){a=e.AddVertex(a[0],a[1],a[2]);b=e.AddVertex(b[0],b[1],b[2]);c=e.AddVertex(c[0],c[1],c[2]);d=(new JSM.Vector(d[0],d[1],d[2])).Normalize();d=e.AddNormal(d.x,d.y,d.z);e.AddTriangle(a,b,c,d,d,d)}}):null!==b&&JSM.ReadAsciiStlFile(b,{onFace:function(a,b,c,d){a=e.AddVertex(a[0],a[1],a[2]);b=e.AddVertex(b[0],b[1],b[2]);c=
e.AddVertex(c[0],c[1],c[2]);d=(new JSM.Vector(d[0],d[1],d[2])).Normalize();d=e.AddNormal(d.x,d.y,d.z);e.AddTriangle(a,b,c,d,d,d)}});c.Finalize();return JSM.ConvertTriangleModelToJsonData(c)};JSM.ImportFileList=function(){this.isFile=this.descriptors=null};
e.AddVertex(c[0],c[1],c[2]);d=(new JSM.Vector(d[0],d[1],d[2])).Normalize();d=e.AddNormal(d.x,d.y,d.z);e.AddTriangle(a,b,c,d,d,d)}});c.Finalize();return JSM.ConvertTriangleModelToJsonData(c)};
JSM.ReadOffFile=function(a,b){if(void 0===b||null===b)b={};var c={offHeaderFound:!1,infoFound:!1,vertexCount:0,faceCount:0,readVertices:0,readFaces:0},d=a.split("\n"),e,f;for(e=0;e<d.length;e++){f=d[e].trim();var g=c;if(0!==f.length&&"#"!=f[0]&&(f=f.split(/\s+/),!(0===f.length||"#"==f[0][0])))if(g.offHeaderFound)if(g.infoFound)if(g.readVertices<g.vertexCount){if(3==f.length){var h=parseFloat(f[0]),k=parseFloat(f[1]);f=parseFloat(f[2]);if(void 0!==b.onVertex&&null!==b.onVertex)b.onVertex(h,k,f);g.readVertices+=
1}}else{if(g.readFaces<g.faceCount&&(h=parseInt(f[0]),f.length>=h+1)){for(var k=[],l=void 0,m=void 0,l=1;l<h+1;l++)m=parseInt(f[l]),k.push(m);if(void 0!==b.onFace&&null!==b.onFace)b.onFace(k);g.readFaces+=1}}else 3==f.length&&(g.vertexCount=parseInt(f[0]),g.faceCount=parseInt(f[1]),g.infoFound=!0);else 1==f.length&&"OFF"==f[0]&&(g.offHeaderFound=!0)}};
JSM.ConvertOffToJsonData=function(a){var b=new JSM.TriangleModel,c=b.AddBody(new JSM.TriangleBody("Default")),d=b.GetBody(c);JSM.ReadOffFile(a,{onVertex:function(a,b,c){d.AddVertex(a,b,c)},onFace:function(a){var b,c,h,k,l=a.length;for(b=0;b<l-2;b++)c=a[0],h=a[b+1],k=a[b+2],d.AddTriangle(c,h,k)}});b.Finalize();return JSM.ConvertTriangleModelToJsonData(b)};JSM.ImportFileList=function(){this.isFile=this.descriptors=null};
JSM.ImportFileList.prototype.InitFromFiles=function(a){this.descriptors=[];var b,c;for(b=0;b<a.length;b++)c=a[b],c={originalObject:c,originalFileName:c.name,fileName:c.name.toUpperCase(),extension:this.GetFileExtension(c.name).toUpperCase()},this.descriptors.push(c);this.isFile=!0};
JSM.ImportFileList.prototype.InitFromURLs=function(a){this.descriptors=[];var b,c,d;for(b=0;b<a.length;b++)c=a[b],d=this.GetFileName(c),c={originalObject:c,originalFileName:d,fileName:d.toUpperCase(),extension:this.GetFileExtension(d).toUpperCase()},this.descriptors.push(c);this.isFile=!1};
JSM.ImportFileList.prototype.GetInputList=function(){var a=[],b,c,d;for(b=0;b<this.descriptors.length;b++){c=this.descriptors[b];d=c.originalObject;var e=this.isFile;c=".OBJ"==c.extension||".MTL"==c.extension?!1:!0;d={originalObject:d,isFile:e,isArrayBuffer:c};a.push(d)}return a};JSM.ImportFileList.prototype.GetFileName=function(a){var b=a.split("/");1==b.length&&(b=a.split("\\"));return 0===b.length?"":decodeURI(b[b.length-1])};JSM.ImportFileList.prototype.GetFileDescriptor=function(a){return this.descriptors[a]};
JSM.ImportFileList.prototype.GetMainFileIndex=function(){var a,b;for(a=0;a<this.descriptors.length;a++)if(b=this.descriptors[a],this.IsSupportedExtension(b.extension))return a;return-1};JSM.ImportFileList.prototype.GetFileIndexByName=function(a){var b,c,d;for(b=0;b<this.descriptors.length;b++)if(c=this.descriptors[b],d=this.GetFileName(a),c.fileName==d.toUpperCase())return b;return-1};JSM.ImportFileList.prototype.IsSupportedExtension=function(a){return".3DS"==a||".OBJ"==a||".STL"==a?!0:!1};
JSM.ImportFileList.prototype.GetFileExtension=function(a){var b=a.lastIndexOf(".");return-1==b?"":a.substr(b)};
JSM.ImportFileList.prototype.GetInputList=function(){var a=[],b,c,d;for(b=0;b<this.descriptors.length;b++){c=this.descriptors[b];d=c.originalObject;var e=this.isFile;c=".OBJ"==c.extension||".MTL"==c.extension||".OFF"==c.extension?!1:!0;d={originalObject:d,isFile:e,isArrayBuffer:c};a.push(d)}return a};JSM.ImportFileList.prototype.GetFileName=function(a){var b=a.split("/");1==b.length&&(b=a.split("\\"));return 0===b.length?"":decodeURI(b[b.length-1])};
JSM.ImportFileList.prototype.GetFileDescriptor=function(a){return this.descriptors[a]};JSM.ImportFileList.prototype.GetMainFileIndex=function(){var a,b;for(a=0;a<this.descriptors.length;a++)if(b=this.descriptors[a],this.IsSupportedExtension(b.extension))return a;return-1};JSM.ImportFileList.prototype.GetFileIndexByName=function(a){var b,c,d;for(b=0;b<this.descriptors.length;b++)if(c=this.descriptors[b],d=this.GetFileName(a),c.fileName==d.toUpperCase())return b;return-1};
JSM.ImportFileList.prototype.IsSupportedExtension=function(a){return".3DS"==a||".OBJ"==a||".STL"==a||".OFF"==a?!0:!1};JSM.ImportFileList.prototype.GetFileExtension=function(a){var b=a.lastIndexOf(".");return-1==b?"":a.substr(b)};
JSM.ConvertImportFileListToJsonData=function(a,b){function c(){if(void 0!==b.onError&&null!==b.onError)b.onError()}function d(a,c){if(void 0!==b.onReady&&null!==b.onReady)b.onReady(a,c)}function e(a,b,c,d){var e=a.GetFileIndexByName(c);a=a.GetFileName(c);if(-1==e)return d.missing.push(a),null;-1==d.requested.indexOf(a)&&d.requested.push(a);return b[e]}var f=a.GetMainFileIndex();if(-1===f)c();else{var g=a.GetFileDescriptor(f),h={main:g.originalFileName,requested:[],missing:[]},k=a.GetInputList();try{".3DS"==
g.extension?JSM.LoadMultipleBuffers(k,function(b){var g=b[f];null===g?c():(g=JSM.Convert3dsToJsonData(g,{onFileRequested:function(c){return e(a,b,c,h)}}),d(h,g))}):".OBJ"==g.extension?JSM.LoadMultipleBuffers(k,function(b){var g=b[f];null===g?c():(g=JSM.ConvertObjToJsonData(g,{onFileRequested:function(c){return e(a,b,c,h)}}),d(h,g))}):".STL"==g.extension&&JSM.LoadMultipleBuffers(k,function(a){a=a[f];if(null===a)c();else if(JSM.IsBinaryStlFile(a))a=JSM.ConvertStlToJsonData(a,null),d(h,a);else{for(a=
0;a<k.length;a++)k[a].isArrayBuffer=!1;JSM.LoadMultipleBuffers(k,function(a){a=a[f];null===a?c():(a=JSM.ConvertStlToJsonData(null,a),d(h,a))})}})}catch(l){c()}}};JSM.ConvertFileListToJsonData=function(a,b){var c=new JSM.ImportFileList;c.InitFromFiles(a);JSM.ConvertImportFileListToJsonData(c,b)};JSM.ConvertURLListToJsonData=function(a,b){var c=new JSM.ImportFileList;c.InitFromURLs(a);JSM.ConvertImportFileListToJsonData(c,b)};JSM.IsPowerOfTwo=function(a){return a&0===a-1};
JSM.NextPowerOfTwo=function(a){if(JSM.IsPowerOfTwo(a))return a;for(var b=1;b<a;)b*=2;return b};JSM.ResizeImageToPowerOfTwoSides=function(a){if(JSM.IsPowerOfTwo(a.width)&&!JSM.IsPowerOfTwo(a.height))return a;var b=JSM.NextPowerOfTwo(a.width),c=JSM.NextPowerOfTwo(a.height),d=document.createElement("canvas");d.width=b;d.height=c;d=d.getContext("2d");d.drawImage(a,0,0,b,c);return d.getImageData(0,0,b,c)};
g.extension?JSM.LoadMultipleBuffers(k,function(b){var g=b[f];null===g?c():(g=JSM.Convert3dsToJsonData(g,{onFileRequested:function(c){return e(a,b,c,h)}}),d(h,g))}):".OBJ"==g.extension?JSM.LoadMultipleBuffers(k,function(b){var g=b[f];null===g?c():(g=JSM.ConvertObjToJsonData(g,{onFileRequested:function(c){return e(a,b,c,h)}}),d(h,g))}):".STL"==g.extension?JSM.LoadMultipleBuffers(k,function(a){a=a[f];if(null===a)c();else if(JSM.IsBinaryStlFile(a))a=JSM.ConvertStlToJsonData(a,null),d(h,a);else{for(a=
0;a<k.length;a++)k[a].isArrayBuffer=!1;JSM.LoadMultipleBuffers(k,function(a){a=a[f];null===a?c():(a=JSM.ConvertStlToJsonData(null,a),d(h,a))})}}):".OFF"==g.extension&&JSM.LoadMultipleBuffers(k,function(a){a=a[f];null===a?c():(a=JSM.ConvertOffToJsonData(a),d(h,a))})}catch(l){c()}}};JSM.ConvertFileListToJsonData=function(a,b){var c=new JSM.ImportFileList;c.InitFromFiles(a);JSM.ConvertImportFileListToJsonData(c,b)};
JSM.ConvertURLListToJsonData=function(a,b){var c=new JSM.ImportFileList;c.InitFromURLs(a);JSM.ConvertImportFileListToJsonData(c,b)};JSM.IsPowerOfTwo=function(a){return a&0===a-1};JSM.NextPowerOfTwo=function(a){if(JSM.IsPowerOfTwo(a))return a;for(var b=1;b<a;)b*=2;return b};
JSM.ResizeImageToPowerOfTwoSides=function(a){if(JSM.IsPowerOfTwo(a.width)&&!JSM.IsPowerOfTwo(a.height))return a;var b=JSM.NextPowerOfTwo(a.width),c=JSM.NextPowerOfTwo(a.height),d=document.createElement("canvas");d.width=b;d.height=c;d=d.getContext("2d");d.drawImage(a,0,0,b,c);return d.getImageData(0,0,b,c)};
JSM.WebGLInitContext=function(a){if(null===a||void 0===a.getContext)return null;var b=a.getContext("webgl")||a.getContext("experimental-webgl");if(null===b)return null;b.viewport(0,0,a.width,a.height);b.clearColor(1,1,1,1);return b};
JSM.WebGLInitShaderProgram=function(a,b,c,d){function e(a,b,c,d){c=a.createShader(c);a.shaderSource(c,b);a.compileShader(c);return!a.getShaderParameter(c,a.COMPILE_STATUS)?(void 0!==d&&null!==d&&d(a.getShaderInfoLog(c)),null):c}b=function(a,b,c,d){b=e(a,b,a.FRAGMENT_SHADER,d);c=e(a,c,a.VERTEX_SHADER,d);if(null===b||null===c)return null;d=a.createProgram();a.attachShader(d,c);a.attachShader(d,b);a.linkProgram(d);return!a.getProgramParameter(d,a.LINK_STATUS)?null:d}(a,c,b,d);if(null===b)return null;
a.useProgram(b);return b};JSM.WebGLGetFloatTextureBufferSize=function(a){return JSM.NextPowerOfTwo(Math.ceil(Math.sqrt(a.length/4)))};

View File

@ -127,7 +127,7 @@ ImporterApp.prototype.GetWelcomeText = function ()
var welcomeText = [
'<div class="welcometitle">Welcome to Online 3D Viewer!</div>',
'<div class="welcometext">Here you can view your local 3D models online. You have three ways to open a file. Use the open button above to select files, simply drag and drop files to this browser window, or define the url of the files as location hash.</div>',
'<div class="welcometextformats">Supported formats: 3ds, obj, stl.</div>',
'<div class="welcometextformats">Supported formats: 3ds, obj, stl, off.</div>',
'<div class="welcometext">Powered by <a target="_blank" href="https://github.com/mrdoob/three.js/">Three.js</a> and <a target="_blank" href="https://github.com/kovacsv/JSModeler">JSModeler</a>.</div>',
'<div class="welcometext"><a target="_blank" href="https://github.com/kovacsv/Online3DViewer"><img src="images/githublogo.png"/></a></div>',
].join ('');
@ -491,7 +491,7 @@ ImporterApp.prototype.ProcessFiles = function (fileList, isUrl)
processorFunc (userFiles, {
onError : function () {
myThis.GenerateError ('No readable file found. You can open 3ds, obj and stl files.');
myThis.GenerateError ('No readable file found. You can open 3ds, obj, stl, and off files.');
myThis.SetReadyForTest ();
return;
},