Three.js: InstancedMesh how to use raycast for every instance?

Created on 11 Nov 2019  Â·  7Comments  Â·  Source: mrdoob/three.js

how to use raycast for every instance at InstancedMesh

Most helpful comment

Solved via #17961

All 7 comments

You'll probably need to use GPU picking, rather than the Raycaster, for this.

We should probably update the https://threejs.org/examples/?q=insta#webgl_interactive_instances_gpu example to use InstancedMesh, I think?

Can you elaborate on what you mean a bit more? Do you mean how do you raycast against an InstancedMesh? I don't think there's any reason raycasting can't be supported with instanced geometry with a returned instance id but it's not implemented at the moment. GPU picking could be a more performant solution, though, depending on your use case.

I have implemented this function several months ago, not gpupicking. I want to mention PR, but I am not sure whether three.js needs this function to return the instance ID of instancemesh. I can share my thoughts to you. In mesh.raycast, add the judgment of instancegeometry, multiply the bounding box of instancegeometry by the matrix of each instance and judge the ray intersection, extract the intersecting matrix and the corresponding instance ID, then traverse these matrices, and then use the vertex multiplication matrix to find the point and face intersecting with the ray, and return the corresponding instance id at the same time , I hope I can help you.

I have implemented this function several months ago, not gpupicking. I want to mention PR, but I am not sure whether three.js needs this function to return the instance ID of instancemesh.

If I understand your approach correctly then it's more or less what I was imagining for the function, as well. I think in order to support use cases like clicking and dragging an instance the instanceId would have to be returned in the raycast result, too.

I don't see why InstancedMesh _shouldn't_ have a raycast function so I would support a PR for this if you're willing to contribute it. This is just my opinion, though -- maybe a maintainer of the library can speak on what the plans for InstancedMesh raycast are and whether it would be a welcome addition?

+1 for implementing it, pretty much as @webglzhang describes. That sounds like the right place to start, although I'm a bit skeptical whether it will perform well enough for the quantities of instances that users may throw at it. We can consider optimizing it further later.

OK, I will try to do an instancedmesh raycast demo this weekend, and then share it. If three.js needs this function, I will do this pr。@donmccurdy @gkjohnson Then, there are some things to consider, such as whether the bounding box of instancegeometry should be calculated based on all instances or a single instance. The demo I'm going to do is to calculate the bounding box based on a single instance.We can discuss together to make instancemesh better.

Solved via #17961

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Bandit picture Bandit  Â·  3Comments

zsitro picture zsitro  Â·  3Comments

scrubs picture scrubs  Â·  3Comments

konijn picture konijn  Â·  3Comments

ghost picture ghost  Â·  3Comments