panda
panda

Reputation: 63

How to merge all STL files to one geometry

The code is as follows. I find it too troublesome. Is there a better way to do it? I do this for the overall export to a STL file, or to do the overall cutting function, thank you!

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

Then,I edit it link this:

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

But report errors:THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.

Upvotes: 0

Views: 876

Answers (1)

Katana24
Katana24

Reputation: 8959

Do you mean in terms of a code review? If so then it would be better to move your question Stack Exchange Code Review. If so, then they both do the same thing when the mesh is loaded so something like this would be a good start:

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

Implemented like:

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

Upvotes: 1

Related Questions