Exporting a three.js mesh as an OBJ or STL

2020-05-27 09:03发布

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.

4条回答
等我变得足够好
2楼-- · 2020-05-27 09:27

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;
}
查看更多
Evening l夕情丶
3楼-- · 2020-05-27 09:27

Writing a OBJExporter should be pretty easy. Just use as reference the OBJLoader. In some weeks I'll probably write it myself if noone has done it by then.

查看更多
叛逆
4楼-- · 2020-05-27 09:39

I would first look into the python OBJ -> three.js converter.

Barring that, I don't think you're going to find any libraries pre-built to do this. I would actually ask 3DTin if they used a library or if they developed it in-house.

查看更多
ゆ 、 Hurt°
5楼-- · 2020-05-27 09:48

I tweaked the above code slightly to allow for arrays of objects that have been duplicated and translated around in a scene. I'm presently using document.writeln then manually copying and pasting into a document.

var l = parent.length;
var j = 0;

while (l--) {

 var numVerts = parent[l].children[0].geometry.vertices.length;
 document.writeln(THREE.saveGeometryToObj(parent[l].children[0],j*(numVerts)));     

 j++;
}

THREE.saveGeometryToObj = function (geo,nums) {

geo.updateMatrixWorld();
var num = parseInt(nums);
var s = '';

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

    var vector = new THREE.Vector3( geo.geometry.vertices[i].x, geo.geometry.vertices[i].y, geo.geometry.vertices[i].z );
    geo.matrixWorld.multiplyVector3( vector );


    s+= 'v '+(vector.x) + ' ' +
    vector.y + ' '+
    vector.z + '</br>';
}

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

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

    if (geo.geometry.faces[i].d!==undefined) {
        s+= ' '+ (geo.geometry.faces[i].d+1+num);
    }
    s+= '</br>';
}

return s;

}

查看更多
登录 后发表回答