stefano
stefano

Reputation: 123

Three.js connect two object as one

I made this function that for each vertices of geometry create a sphere and place it in the same position of vertice.For example if I have a cube,the function place a sphere for each cube vertice.

function makeSphereVertices(){
console.log("makesphere");

spheres = [];

for(var j=0 ; j<geometryContainer.length ; j++) {               
for (var i=0 ; i<geometryContainer[j].geometry.vertices.length ; i++){
var sphereGeometry = new THREE.SphereGeometry(0.04,10,10);//relative to dimension object : ToDo
var sphereMaterial = new THREE.MeshBasicMaterial({transparent: false,color: 0x000000 /*opacity: 0.01*/});
spheres = new THREE.Mesh(sphereGeometry,sphereMaterial);
spheres.position.set(geometryContainer[j].geometry.vertices[i].x,
                  geometryContainer[j].geometry.vertices[i].y,
                  geometryContainer[j].geometry.vertices[i].z);
                  console.log(geometryContainer[j].id);
    spheres.name = "sphere";            
scene.add(spheres);
verticesSphere.push(spheres);

 }
    }


  }

After this,I have created function to move my cube like this Draggable shape. Now the problem is: I can't find a way to move together cube and all spheres. For example if I drag cube all the spheres remains in the old position. Is there a way to chain the spheres to my cube?Thank you.

Upvotes: 1

Views: 2547

Answers (1)

hobs
hobs

Reputation: 55

By placing them all in a new object.

group = new THREE.Object3D();//create an empty container
group.add( mesh );//add a mesh with geometry to it
scene.add( group );//when done, add the group to the scene

three.js - mesh group example? (THREE.Object3D() advanced)

So make sure you place the cube and all the spheres in your newly created group object.

Upvotes: 2

Related Questions