Its creating the ray correctly but returning no intersections
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
}
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
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.