body {
font-family: monospace;
font-size: 11px;
overflow: hidden;
}
JavaScript
// this is the core of the solution,
// it builds the Frustum object by given camera and mouse coordinates
function updateFrustrum(camera, mousePos0, mousePos1, frustum) {
let pos0 = new THREE.Vector3(Math.min(mousePos0.x, mousePos1.x), Math.min(mousePos0.y, mousePos1.y));
let pos1 = new THREE.Vector3(Math.max(mousePos0.x, mousePos1.x), Math.max(mousePos0.y, mousePos1.y));
// build near and far planes first
{
// camera direction IS normal vector for near frustum plane
// say - plane is looking "away" from you
let cameraDir = new THREE.Vector3();
camera.getWorldDirection(cameraDir);
// INVERTED! camera direction becomes a normal vector for far frustum plane
// say - plane is "facing you"
let cameraDirInv = cameraDir.clone().negate();
// calc the point that is in the middle of the view, and lies on the near plane
let cameraNear = camera.position.clone().add(cameraDir.clone().multiplyScalar(camera.near));
// calc the point that is in the middle of the view, and lies on the far plane
let cameraFar = camera.position.clone().add(cameraDir.clone().multiplyScalar(camera.far));
// just build near and far planes by normal+point
frustum.planes[0].setFromNormalAndCoplanarPoint(cameraDir, cameraNear);
frustum.planes[1].setFromNormalAndCoplanarPoint(cameraDirInv, cameraFar);
}
// next 4 planes (left, right, top and bottom) are built by 3 points:
// camera postion + two points on the far plane
// each time we build a ray casting from camera through mouse coordinate,
// and finding intersection with far plane.
//
// To build a plane we need 2 intersections with far plane.
// This is why mouse coordinate will be duplicated and
// "adjusted" either in vertical or horizontal direction
// build frustrum plane on the left
if (true) {
let ray = new THREE.Ray();
ray.origin.setFromMatrixPosition(camera.matrixWorld);
// Here's the example, - we take X coordinate of a mouse, and Y...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.