Raycaster intersects with invisible objects

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

Have a look at this line:

if ( object.visible === false ) return;

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;
};

Author: Fantashit

2 thoughts on “Raycaster intersects with invisible objects

  1. Um, this behavior is somewhat inconsistent. From my point of view, it would be actually better if intersectObject() does not check the visibility at all and delegates this to the app level code.

    If we keep if ( object.visible === false ) return; , we might consider to check the hierarchy, too. However, this will impact performance.

  2. I suggest to remove the test on Object3D.visible and add a test based on Layers instead. This idea was already suggested here #7551 (comment) 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…

Comments are closed.