如何将所有STL文件合并到一个几何体

时间:2018-01-16 09:29:23

标签: javascript three.js

代码如下。我发现它太麻烦了。有没有更好的方法呢?我这样做是为了整体导出到STL文件,或者做整体切割功能,谢谢!

var material1 = new THREE.MeshPhongMaterial( {
    color:0xffffff,
} );
loader.load( 'fanban1.stl', function ( geometry ) {
    stl_mesh1 = new THREE.Mesh(geometry, material1);
    stl_mesh1.position.set(20, 0, 40);
    stl_mesh1.rotation.set(-Math.PI / 2, 0, 0);
    stl_mesh1.scale.set(0.2, 0.2, 0.2);
    group.add(stl_mesh1);
    arr_mesh.push(stl_mesh1);
});


var material2 = new THREE.MeshPhongMaterial( {
    color:0xFFFF00,
} );
loader.load( 'fanban2.stl', function ( geometry ) {
    stl_mesh2 = new THREE.Mesh(geometry, material2);
    stl_mesh2.position.set(20, 0, -20);
    stl_mesh2.rotation.set(-Math.PI / 2, 0, 0);
    stl_mesh2.scale.set(0.3, 0.3, 0.3);
    group.add(stl_mesh2);
    arr_mesh.push(stl_mesh2);
});
scene.add(group);

然后,我编辑它链接:

    var allGeometry = new THREE.Geometry();
    loader.load( 'fanban.stl', function ( geometry ) {
        stl_mesh = new THREE.Mesh(geometry, material);
        stl_mesh.position.set(20, 0, -60);
        stl_mesh.rotation.set(-Math.PI / 2, 0, 0);
        stl_mesh.scale.set(0.3, 0.3, 0.3);
        group.add(stl_mesh);
        arr_mesh.push(stl_mesh);
        allGeometry.merge(geometry);
    });

但报告错误:THREE.Geometry.merge():geometry不是THREE.Geometry的实例。

1 个答案:

答案 0 :(得分:1)

你的意思是代码审查吗?如果是这样,那么移动您的问题Stack Exchange Code Review会更好。如果是这样,那么当网格加载时它们都做同样的事情,所以这样的事情将是一个良好的开端:

function constructMesh(geo, mat, x, y, z, scaleX, scaleY, scaleZ) {
  var mesh = new THREE.Mesh(geo, mat);
  mesh.position.set(x, y, z);
  mesh.rotation.set(-Math.PI / 2, 0, 0);
  mesh.scale.set(scaleX, scaleY, scaleZ);
  group.add(mesh);
  arr_mesh.push(mesh);
}

实施方式如下:

var materials = [
  new THREE.MeshPhongMaterial({color:0xffffff}),
  new THREE.MeshPhongMaterial({color:0xffff00})
];

loader.load( 'fanban1.stl', function ( geometry ) {
  constructMesh(geo, materials[0], 20, 0, 40, 0.2, 0.2, 0.2);      
});
loader.load( 'fanban2.stl', function ( geometry ) {
  constructMesh(geo, materials[1], 20, 0, -20, 0.3, 0.3, 0.3); 
});
scene.add(group);