How to perform a visibility test?

Hi,

I want to detect, seen from camera´s position if 1 pin (an object3D) is in front or behind of a sphere.
Is there any good approach doing so?

Thanks

I am not sure I understand exactly what you mean.

As a start, you can use THREE.Raycaster to cast a ray from the camera towards a single point on the object in question, and see if the ray intersects the sphere, and whether this is before or after it intersects the other object.

1 Like

You can compare dot products of vectors [camera-sphere] and [camera-pin]. If [camera-pin] is greater than [camera-sphere] and the angle between these vectors is less than asin(sphereRadius / [camera-sphere].length()), then the pin is behind the sphere:

var v3CamSphere = new THREE.Vector3();
var v3CamPin = new THREE.Vector3();

// and then in the animation loop
  v3CamSphere.subVectors(sphere.position, camera.position);
  v3CamPin.subVectors(pin.position, camera.position);

  if (
    v3CamPin.dot(v3CamSphere) > v3CamSphere.dot(v3CamSphere) &&
    v3CamSphere.angleTo(v3CamPin) <
      Math.asin(sphere.geometry.parameters.radius / v3CamSphere.length())
  ) {
    pin.material.color.set(0x00ffff);
  } else {
    pin.material.color.set(0xff00ff);
  }

The solution is from the scratch :slight_smile:

5 Likes