Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140248

Three.js detect when object is partially and fully occluded

$
0
0

I'm trying to detect when an object in Three.js is partially and fully occluded (hidden behind) another object.

My current simple solution casts a single ray to the the center of the object:

function getScreenPos(object) {
  var pos = object.position.clone();
  camera.updateMatrixWorld();
  pos.project(camera);
  return new THREE.Vector2(pos.x, pos.y);
}

function isOccluded(object) {
  raycaster.setFromCamera(getScreenPos(object), camera);
  var intersects = raycaster.intersectObjects(scene.children);
  if (intersects[0] && intersects[0].object === object) {
    return false;
  } else {
    return true;
  }
}

However it doesn't account for the object's dimensions (width, height, depth).

Not occluded (because center of object is not behind) Object not occluded

Occluded (because center of object is behind) Object occluded

View working demo:

https://jsfiddle.net/kmturley/nb9f5gho/57/

Currently thinking I could calculate the object box size, and cast Rays for each corner of the box. But this might still be a little too simple:

var box = new THREE.Box3().setFromObject(object);
var size = box.getSize();

I would like to find a more robust approach which could give partially occluded and fully occluded booleans values or maybe even percentage occluded?


Viewing all articles
Browse latest Browse all 140248

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>