重置相机远平面

时间:2015-10-08 09:04:31

标签: camera three.js

我正在尝试更新使我的相机远剪裁平面坐在Vector3(0,0,0)上,无论相机有多近或多远,我都设法找到一种更新远剪裁平面的方法动态,但我不能让这架飞机面对我的相机。

谢谢,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;

1 个答案:

答案 0 :(得分:2)

如果要重置相机的far平面参数,可以使用此模式

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

在您的特定情况下,您可以这样做:

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

three.js r.72