Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the 3D boundaries of camera field of view in three.js

I have a three.js scene and would like to know the 3D boundaries of the viewport.

For instance, let's say I have a canvas that is 500px wide and 1000px tall. My camera is looking at position { 0, 0, 0 } and is at position { 0, 0, 100 };

My camera will never move.

Let's say I have an object located at { 0, 0, 0 }. I need to be able to move that object to various points within the viewable area (within the camera field of view). How can I calculate, say, the upper/left hand corner of the camera's field of view? Also, how could I calculate 20% in from the left and 10% down from the top of the camera's field of view?

Here is my camera object:

{
    "object": {
        "uuid": "E26C5F88-CD17-45F8-A492-F3DD704949BF",
        "type": "PerspectiveCamera",
        "matrix": [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 100, 1],
        "zoom": 1,
        "fov": 50,
        "aspect": 0.625,
        "near": 0.1,
        "far": 1000
    }
}
like image 493
swatkins Avatar asked Feb 08 '23 00:02

swatkins


1 Answers

To place an object 20% from the left, and 10% down from the top, you would use something like this:

var left = 0.2;
var top = 0.1;
var depth = 0.7; // from -1 to 1, depends how far into the scene you want the object, -1 being very close, 1 being the furthest away the camera can see
object.position.set( -1 + 2 * left, 1 - 2 * top, depth ).unproject( camera );

This converts a position from NDC (normalized device coordinates), which are all in the range [-1, 1] into world coordinates.

It essentially does the reverse of the following image (source), and converts a point in the right image (NDC), back into the left image (world coordinates):

perspective matrix

For example:

var canvas = document.getElementById('canvas');
var scene = new THREE.Scene();
var renderer = new THREE.WebGLRenderer({canvas: canvas, antialias: true});
var camera = new THREE.PerspectiveCamera(45, canvas.clientWidth / canvas.clientWidth, 1, 1000);
var clock = new THREE.Clock();

var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshBasicMaterial({color: '#f00', wireframe: true});
var box = new THREE.Mesh(geometry, material);

scene.add(box);

var period = 5;
  
var top = 0.3; // 30%
var left = 0.2; // 20%
var depth = 0.7;

render();

function render() {
  requestAnimationFrame(render);
  
  if (canvas.width !== canvas.clientWidth || canvas.height !== canvas.clientHeight) {
    renderer.setSize(canvas.clientWidth, canvas.clientHeight, false);
    camera.aspect = canvas.clientWidth /  canvas.clientHeight;
    camera.updateProjectionMatrix();
  }
  
  box.position.set(-1 + 2 * left, 1 - 2 * top, depth).unproject(camera);
  box.rotation.y += clock.getDelta() * 2 * Math.PI / period;
  
  renderer.render(scene, camera);
}
html, body, canvas {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    display: block;
}
<script src="//cdnjs.cloudflare.com/ajax/libs/three.js/r73/three.min.js"></script>
<canvas id="canvas"></canvas>
like image 193
Brendan Annable Avatar answered Feb 11 '23 01:02

Brendan Annable