Support for event bubbling
Closed this issue · 4 comments
Hi there, been using this lib quite nicely in my game project. Some stumbling blocks related to events however:
Suppose I have this hierarchy:
Object3D
+---+
+Mesh 1
+
Mesh 2
++
+Mesh 3
I want to have a 'select' event bound to Object3D, since it is the parent of al of these Meshes, and I want all of these Meshes to be selectable and hit the same callback.
Since Object3D has no actual mesh, I can't bind anything to it since the event will never trigger. This forces me to either 1. Traverse the tree looking for Mesh 1 and 2, or 2. Bind the same event to all meshes under this tree.
It would be really useful to bind like so:
mesh1.select = function() ....
objControls.add( mesh1 );
objControls.add( obj3d );
or alternatively
objControls.add( mesh1.parent )
Yo! sorry for taking so long to get back to this. I'm going to check it out tonight and think about the best way to do it. If you find a good way, hit me with a pull request and I'll bring it it, because obviously that is a problem...
Nice! I currently have a janky setup where if the object in question is Object3D or Group...
THREE.GetAnyChildMesh = function( obj ){
var mesh = undefined;
obj.traverse( function( o ){
if( o instanceof THREE.Mesh ){
mesh = o;
}
});
return mesh;
}
This is obviously not ideal.
@mflux controling Object3D is now supported! So in your hierarchy above where the obj3d has child mesh1, mesh2, mesh3, do
objectControls = new ObjectControls( camera, { recursive: true } );
...
obj3d.select = function() ...
objControls.add( obj3d );
Setting the recursive param means the events on the obj3d will be triggered when any of its descendant meshes are intersected. Check out the HighlightGroup example.
Hey, that's awesome! Great job on the implementation, this will be very useful.