When trying to get intersections with a Sprite using a Raycaster like so:
const raycaster = new Raycaster(source, target, 0, Infinity);
const intersection = [];
leaf.object.raycast(raycaster, intersection);
This gets thrown:
TypeError: Cannot read property 'matrixWorld' of undefined
the issue is that a _camera object is somehow expected where there is none. Looks like this was introduced originally in here:
https://github.com/mrdoob/three.js/commit/2fda472d63732ca77cc658c897d894822009b354#diff-cf59543b9c35550d3d4f19faa7f3b4a5
Are you instantiating a new Raycaster each time you raycast?
Are you raycasting against a sprite from a point not equal to the camera position?
Are you instantiating a new
Raycastereach time you raycast?
Yeah, it's inside editor, the code is used for picking, so performance never mattered there.
Are you raycasting against a sprite from a point not equal to the camera position?
Technically, it is from the screen point using the camera transform. I just don't use Raycaster to calculate the Ray. The reason I do this is mostly because there is a custom spatial index in-between, and that requires a ray. Also there are historical reasons.
The main issue as I see it is that Raycaster does not require a camera in its constructor, so a valid Raycaster is used, but it throws an exception.
To support pixel-perfect raycasting, when raycasting against _Sprites_, users must call raycaster.setFromCamera(). See the discussion in #16423 and follow the links.
Feel free to propose another solution if you can think of one.
@WestLangley Is the need to call setFromCamera to raycast against sprites anywhere in the docs? I still kind of feel that Raycaster._camera should be called out in the docs (and possibly made public) along with a note that it's needed for sprites. It feels a little too much like magic to me at the moment.
By all means, propose whatever you feel will make things better. :-)
class CameraRaycaster extends Raycaster ?
I completely understand the argument and the necessity to have projection matrix and not just ray, but as much as it's "least hacky" - it is still hacky currently :)
I remember we discussed different possible solutions in order to fix raycasting against sprites. All of them were hacky but the current one was the most appropriate from our point of view. Happy to see better alternatives :)
Is the need to call setFromCamera to raycast against sprites anywhere in the docs?
I think it's good to mentioned this point in https://threejs.org/docs/index.html#api/en/objects/Sprite.raycast.
Closing for now. With #17118 merged, the updated documentation and a new runtime warning should make things now more transparent. If users still have issues with raycasting in context of sprites, we can still consider different solutions.
Most helpful comment
By all means, propose whatever you feel will make things better. :-)