admin管理员组文章数量:1336346
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
}
}
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
}
}
Share
Improve this question
asked Jan 18, 2016 at 21:41
swatkinsswatkins
13.6k4 gold badges50 silver badges78 bronze badges
1 Answer
Reset to default 9To 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):
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./ajax/libs/three.js/r73/three.min.js"></script>
<canvas id="canvas"></canvas>
本文标签: javascriptHow to determine the 3D boundaries of camera field of view in threejsStack Overflow
版权声明:本文标题:javascript - How to determine the 3D boundaries of camera field of view in three.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742356401a2459500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论