Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting a three.js mesh as an OBJ or STL

I would like to create an export as OBJ or STL link for a parametric Three.js mesh object. Just like the export option on http://www.3dtin.com

Any advice would be appreciated.

like image 547
user1507511 Avatar asked Jul 06 '12 18:07

user1507511


1 Answers

To get mesh in .obj format I use this simple function:

THREE.saveGeometryToObj = function (geometry) {
var s = '';
for (i = 0; i < geometry.vertices.length; i++) {
    s+= 'v '+(geometry.vertices[i].x) + ' ' +
    geometry.vertices[i].y + ' '+
    geometry.vertices[i].z + '\n';
}

for (i = 0; i < geometry.faces.length; i++) {

    s+= 'f '+ (geometry.faces[i].a+1) + ' ' +
    (geometry.faces[i].b+1) + ' '+
    (geometry.faces[i].c+1);

    if (geometry.faces[i].d !== undefined) {
        s+= ' '+ (geometry.faces[i].d+1);
    }
    s+= '\n';
}

return s;
}
like image 131
AlexChet Avatar answered Sep 22 '22 12:09

AlexChet