Ray.IntersectSphere returns a hit for NaN values

Description of the problem

If the sphere in Ray.IntersectSphere has any null or NaN values, the function will return a modified target with its values set to NaN rather than returning null as expected for a non-intersection. When used in raycasting the result is that an object with invalid values is permanently added to the top of the list of current intersections.

The culprit is:

https://github.com/mrdoob/three.js/blob/dev/src/math/Ray.js#L266

As it is using a truthy condition to inditcate a miss. Reversing the comparison to:

if ( !(d2 <= radius2) ) return null;

Would instead return null if either value is invalid.

Three.js version
  • Dev
  • r93
Browser
  • All of them
  • Chrome
  • Firefox
  • Internet Explorer
OS
  • All of them
  • Windows
  • macOS
  • Linux
  • Android
  • iOS

Author: Fantashit

1 thought on “Ray.IntersectSphere returns a hit for NaN values

  1. three.js expects valid data — not null, NaN, or missing; it is up to your application layer to ensure that.

    If three.js accommodated invalid data in this method, it would have to do so in every method.

    I’m extending the raycaster to support instanced geometry

    Instanced geometry is very flexible, and it may be difficult to support instanced-raycasting in general, but a three.js example in which that is done would be awesome, if you are interested. 🙂

Comments are closed.