Object 3D with rayscaster

Hello everyone sorry if this topic already exist but I don’t found solution of my problem.
So I want to move an 3D object in vr scene but when i do this with my raycaster he don’t found my object on my group I think it’s because I add a object3d and not a mesh but I can had mesh because on these object I have 3 mesh and I want to move them at same time.

So this this my code :

group.add(object3d);
var intersects = raycaster.intersectObjects( group.children);

var intersection = intersects[ 0 ];
var object =intersection.object;

intersects[0].object.material.color.set('white');
tempMatrix.getInverse( controller1.matrixWorld );
object.matrix.premultiply( tempMatrix );
object.matrix.decompose( object.position, object.quaternion, object.scale );
controller1.add( object );
controller1.userData.selected = object;

Try it like so:

var intersects = raycaster.intersectObjects( group.children, true );

The second parameter indicates you want to test all descendants of group.children.

1 Like

I just test that but just one mesh of my Object3D move.
No not realy they move but just one by one not all my object move.

So if I understand you correctly, you try to move an entire group of objects and not just individual ones, right?

Yes because if I see my object3d he contain 3 mesh and I want to move them like they are same object

Well, an easy approach is to merge all meshes into one. This can be done with BufferGeometryUtils.mergeBufferGeometries() but only if the geometries have compatible attributes. Besides, it’s best for performance when the meshes share the same material.

Another option is to transform the parent object of an intersection. Instead of doing this:

var object = intersection.object;

try it with

var object = intersection.object.parent;

Keep in mind that you can’t modify the material property in this case since the parent is of type Object3D.

When I want to merge my meshes it say that:
Cannot convert undefined or null to object

And I do that:

var geometry=[]
for (var i = 0; i <object3d.children.length; i++) {
    geometry.push(object3d.children[i]);
}
console.log(geometry);
var mergedGeo = THREE.BufferGeometryUtils.mergeBufferGeometries(geometry);

My console.log show me an array of 3 mesh. I think I m just a noob sorry XD

Use the following example as a code template. It shows how to use BufferGeometryUtils.mergeBufferGeometries().

https://threejs.org/examples/webgl_geometry_minecraft

Besides, please take your time and properly format code snippets in your posts. Don’t just copy/paste them. You can use three ` signs to start and end a code listing.

1 Like

ok sorry thank you

I have encountered this problem recently, and I think it is necessary to modify the official documents.
When you use intersectObject() or intersectObjects() and you pass in groups, you have to set the second parameter to true, otherwise you’re not going to return anything, which seems a little unreasonable and a little misleading.
In addition, in the case of many mesh in the scene, whether setting the second parameter to true will have some impact on the performance.

This behavior is actually noted in the documentation of both methods. Meaning the recursive parameter controls whether to check the descendants of the given object(s) or not.

https://threejs.org/docs/index.html#api/en/core/Raycaster.intersectObject