Caius Eugene
Caius Eugene

Reputation: 855

Resetting the camera far plane

I'm trying to update make my camera far clip plane to sit on Vector3(0,0,0) no matter how close or far the camera gets, I've managed to find a way of updating the far clip plane dynamically but I can't get this plane to face my camera.

Thanks, C.

var matrix = new THREE.Matrix4();
matrix.extractRotation(camera.matrix);

var direction = new THREE.Vector3();
direction.subVectors( new THREE.Vector3(0,0,0), camera.position );
direction.normalize();

var N = new THREE.Vector3(0, 1, 0);
N.applyMatrix4(matrix);

var planePos = new THREE.Vector3(0,0,0);

var clipPlane = new THREE.Plane();
clipPlane.setFromNormalAndCoplanarPoint(N, planePos);
clipPlane.applyMatrix4(camera.matrixWorldInverse);

clipPlane = new THREE.Vector4(clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.constant);

var q = new THREE.Vector4();
var projectionMatrix = camera.projectionMatrix;

q.x = (sgn(clipPlane.x) + projectionMatrix.elements[8]) / projectionMatrix.elements[0];
q.y = (sgn(clipPlane.y) + projectionMatrix.elements[9]) / projectionMatrix.elements[5];
q.z = -1.0;
q.w = (1.0 + projectionMatrix.elements[10]) / camera.projectionMatrix.elements[14];

// Calculate the scaled plane vector
var c = new THREE.Vector4();
c = clipPlane.multiplyScalar(2000.0 ); //clipPlane.multiplyScalar(2.0 / clipPlane.dot(q)); /// clipPlane.dot(q)

// Replace the third row of the projection matrix
projectionMatrix.elements[2] = c.x;
projectionMatrix.elements[6] = c.y;
projectionMatrix.elements[10] = c.z + 1.0;
projectionMatrix.elements[14] = c.w;

Upvotes: 1

Views: 693

Answers (1)

WestLangley
WestLangley

Reputation: 104833

If you want to reset the far plane parameter for a camera, you can use this pattern

camera.far = new_value;
camera.updateProjectionMatrix();

In your particular case, you can do this:

camera.far = camera.position.length();
camera.updateProjectionMatrix();

three.js r.72

Upvotes: 3

Related Questions