question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Raycaster intersects with invisible objects

See original GitHub issue

There is one old problem for me, but still I can’t find same issue in task tracker.

Have a look at this line: https://github.com/mrdoob/three.js/blob/a1daef37e5a6b80d0173af54edd000202b95fca5/src/core/Raycaster.js#L46 Why Raycaster checks visible property only on intersectable’s object? I mean, if I set visible = false in parent, children become invisible for renderer, but still intersectable. Should Raycaster check nearest parents until find invisible? I have to check result of intersection for invisible parents in every single project. Is it any reason for not checking parents here?

My code:

var bool;
THREE.Object3D.prototype.isVisible = function() {
    bool = true;
    this.traverseAncestors((parent) => {
        if (!parent.visible) bool = false;
    });
    return bool;
};

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:26 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
Mugen87commented, Jun 24, 2019

I suggest to remove the test on Object3D.visible and add a test based on Layers instead. This idea was already suggested here https://github.com/mrdoob/three.js/issues/7551#issuecomment-154400499 and it would be similar to how raycasting works in Unity. In this way, we decouple the raycast from the visibility of the object but still allow to filter out unwanted objects. Something like this in intersectObject() might work:

function intersectObject( object, raycaster, intersects, recursive ) {

        if ( object.layers.test( raycaster.layers ) ) { 

            object.raycast( raycaster, intersects );

        }

	if ( recursive === true ) {

		var children = object.children;

		for ( var i = 0, l = children.length; i < l; i ++ ) {

			intersectObject( children[ i ], raycaster, intersects, true );

		}

	}

}

Notice that children would still be tested even if the layer test evaluates to false for the current object. In this way, layers in Raycaster are treated similar like in WebGLRenderer. Unfortunately, this change might break user code so it’s problematic to get it into the library…

3reactions
themightyatomcommented, Mar 22, 2021

Alternative strategy! (for anyone having similar issues)

It’s worth remembering that you send an array of objects to Raycaster in the first place, and in this array you can easily weed out the objects you don’t want in the Raycast operation. That’s always been my strategy, and never had any problems.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Raycaster hits invisible object? - Questions - three.js forum
This is where I just gave up - the raycaster seems to be detecting some invisible object above the actual object. - Imagine...
Read more >
How to raycasting hidden object or not add to scene in Threejs
One way to achieve what you require would be to not add your Box objects to your scene which would ensure that they...
Read more >
Raycaster - Three.js Tutorials - sbcode.net
Raycasting allows you to create a vector from a 3D point in the scene, and detect which object(s) the vector intersects. The raycasting...
Read more >
Interacting with objects – Visualising Data on the Web
We do this by setting up a 'raycaster' object. Think of our 2D cursor as being on the glass ... setFromCamera( mouse, camera...
Read more >
Picking by Raycaster - CGwith3JS - Google Sites
Picking in Three.js is done via the Raycaster. It is a complete CPU manipulation (unlike the ... if (intersects[0].object.name === "cube1") alert("cube1");
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found