Exporting a three.js mesh as an OBJ or STL

和自甴很熟 提交于 2019-12-20 12:47:06

问题


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.


回答1:


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



回答2:


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.




回答3:


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.




回答4:


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;

}



来源:https://stackoverflow.com/questions/11367822/exporting-a-three-js-mesh-as-an-obj-or-stl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!