javascriptparsing3d.obj

parsing .obj 3D graphics file with JavaScript


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


Solution

  • 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;
        });
    }