Three.js - How to check if an object is visible to the camera

I find it difficult to determine what is the best way to check if a 3D object is visible to the camera’s eyes.

I have a sphere in the middle of the screen. Some cubes are added to the surface randomly. I need to check which cubes are visible (on the front half of the sphere) and which one is invisible (on the back half of the sphere) for the camera’s eyes.

What I have found so far seems to be the right direction, but I have to miss something with the THREE.Raytracer class.

Here is the code script I am using: jsfiddle . I tried to make this as clear as possible.

This part of the violin may contain an error code:

var raycaster = new THREE.Raycaster(); var origin = camera.position, direction, intersects, rayGeometry = new THREE.Geometry(), g; pointGroup.children.forEach(function(pointMesh) { direction = pointMesh.position.clone(); // I THINK THIS CALCULATION MIGHT BE WRONG - BUT DON'T KNOW HOW TO CORRECT IT raycaster.set(origin, direction.sub(origin).normalize()); // if the pointMesh position is on the back half of the globe, the ray should intersect with globe first and the hit the point as second target - because the cube is hidden behind the bigger sphere object intersects = raycaster.intersectObject(pointMesh); // this is always empty - should contain objects that are located on the back of the sphere ... console.log(intersects); }); 

Frustum Culling is not working as described in this question here: post1

Also this post2 and this post3 explain the topic really well, but not quite for this situation.

Thanks for the help!

+7
javascript
source share
2 answers

You want to see Occlusion Culling methods. The Frustum hole works fine and not what you describe. Frustum only rejects checks if the object (or its bounding block) is inside the camera pyramid. You also disable occlusion in addition to Frustum Culling when you want to destroy objects that are closed by other objects inside the truncation. But this is not an easy task.

+4
source share

I just worked, although a similar problem was when I tried to detect when a point in world space passed outside the camera and behind specific objects in the scene. I created jsfiddle (see below) for it. When the red "target" passes behind any of the three "walls", a blue line is drawn from the "target" to the camera. Hope this helps.

+1
source share

All Articles