4

I have a question. I know that its not possible to parse .obj 3D graphics file using JavaScript and we have to convert it into some other format (preferably JSON). But I want to know why? Why we can't parse .obj file using JavaScript?

I would really appreciate your comments and answers.

Thanks Vik

Vik
  • 488
  • 2
  • 10
  • 19
  • The only real obstacle I see is presenting the data in 3D (which may not be something you need), and getting the file to the JavaScript client. But that can easily be done through a webservice etc or with the new HTML 5 JS File API. – Skurmedel May 05 '11 at 22:21
  • You seay "I know its not possible ... but I want to know why?" ... it does not make sense. How can you say something is not possible, when you don't know why? – Ivan Kuckir Sep 05 '15 at 23:00

3 Answers3

6

Sure you can... why not? It's a text file, just go ahead and parse it.

Here, I'll even get you started:

var objText = getObjFile();
var obj = {};
var vertexMatches = objText.match(/^v( -?\d+(\.\d+)?){3}$/gm);
if (vertexMatches)
{
    obj.vertices = vertexMatches.map(function(vertex)
    {
        var vertices = vertex.split(" ");
        vertices.shift();
        return vertices;
    });
}
gilly3
  • 87,962
  • 25
  • 144
  • 176
3

Of course you can. I have even written my own library for parsing 3D formats - K3D.js. It also supports MD2, 3DS and Collada. OBJ was the easiest to code :)

Ivan Kuckir
  • 2,327
  • 3
  • 27
  • 46
0

There are now a few Javascript libraries to read Wavefront OBJ files. This one works well, though only reads OBJ files and does not output them https://www.npmjs.com/package/obj-file-parser. You can also do this with three.js: https://threejs.org/docs/#examples/en/loaders/OBJLoader.

snibbe
  • 2,715
  • 1
  • 27
  • 34