Issue with Raycasting threejs v109

Hi guys, I am facing an issue with the raycaster:

I am trying to cast a ray from a few points to a plane, but it is unable to intersect with the plane no matter what :confused:

Ray origin from y:-2000, pointing up

image

Its creating the ray correctly but returning no intersections

image

The plane that I am trying to intersect is way above the ray origin, yet not getting any intersections. I have also tried casting the ray from top (origin.y:2000, ray direction pointing down) but no result.
Any thoughts?

Can you please demonstrate the issue with live example?

Notice that intersects.distance is no valid code since intersectObject() always returns an array. And an array has no distance property. You normally code this like so:

if ( intersects.length > 0 ) {

    console.log( intersects[ 0 ].distance ); // log the distance of the first intersection

}

Hi Mugen,

I figured out the issue. I wanted to do a calculation at initiation and in background, so I set

terrain.visible = false

I assumed the Raycaster calculation will not be dependent on mesh visibility, but guess not. Could you briefly explain how it works under the hood? Maybe it should not be dependent on mesh visibility?

I believe you can turn the material’s visible flag to false, and the raycasting would work then.

Personally I feel like it’s logical that the ray doesn’t catch objects that were specifically set to not be visible by the programmer. Much less confusing than if someone were to make objects invisible and the ray still intersected them. But that’s only my opinion :slight_smile:

Will try the material’s visible flag.

but if we look at the raycaster.intersectObject( object_to_be_intersected ) method, I would assume it will check for intersection with the specified object regardless whether it is visible or not. After all, I am specifying the exact very object I am trying to intersect the ray with.

I completely agree with you. I’ve voted to change the behavior in the following issue at github and use THREE.Layers instead in order to control which objects are affected by raycasting.

The current workaround is setting the visibility of the material to false like mentioned by @DolphinIQ.

1 Like

Thanks Mugen! I will look into layers, seems very useful :slight_smile: