瀏覽代碼

Added StereoCamera. See #7036.

Mr.doob 9 年之前
父節點
當前提交
2ee214de8e
共有 2 個文件被更改,包括 89 次插入0 次删除
  1. 88 0
      src/cameras/StereoCamera.js
  2. 1 0
      utils/build/includes/common.json

+ 88 - 0
src/cameras/StereoCamera.js

@@ -0,0 +1,88 @@
+/**
+ * @author mrdoob / http://mrdoob.com/
+ */
+
+THREE.StereoCamera = function ( fov, aspect, near, far ) {
+
+	THREE.PerspectiveCamera.call( this, fov, aspect, near, far );
+
+	this.type = 'StereoCamera';
+
+	this.focalLength = 125;
+
+	this.cameraL = new THREE.PerspectiveCamera();
+	this.cameraL.matrixAutoUpdate = false;
+
+	this.cameraR = new THREE.PerspectiveCamera();
+	this.cameraR.matrixAutoUpdate = false;
+
+};
+
+THREE.StereoCamera.prototype = Object.create( THREE.PerspectiveCamera.prototype );
+THREE.StereoCamera.prototype.constructor = THREE.StereoCamera;
+
+THREE.StereoCamera.prototype.updateMatrixWorld = ( function () {
+
+	var focalLength, fov, aspect, near, far;
+
+	var eyeRight = new THREE.Matrix4();
+	var eyeLeft = new THREE.Matrix4();
+
+	return function updateMatrixWorld ( force ) {
+
+		THREE.Object3D.prototype.updateMatrixWorld.call( this, force );
+
+		var needsUpdate = focalLength !== this.focalLength || fov !== this.fov ||
+											aspect !== this.aspect || near !== this.near ||
+											far !== this.far;
+
+		if ( needsUpdate ) {
+
+			// Off-axis stereoscopic effect based on
+			// http://paulbourke.net/stereographics/stereorender/
+
+			focalLength = this.focalLength;
+			fov = this.fov;
+			aspect = this.aspect;
+			near = this.near;
+			far = this.far;
+
+			var projectionMatrix = camera.projectionMatrix.clone();
+			var eyeSep = focalLength / 30 * 0.5;
+			var eyeSepOnProjection = eyeSep * near / focalLength;
+			var ymax = near * Math.tan( THREE.Math.degToRad( fov * 0.5 ) );
+			var xmin, xmax;
+
+			// translate xOffset
+
+			eyeLeft.elements[ 12 ] = - eyeSep;
+			eyeRight.elements[ 12 ] = eyeSep;
+
+			// for left eye
+
+			xmin = - ymax * aspect + eyeSepOnProjection;
+			xmax = ymax * aspect + eyeSepOnProjection;
+
+			projectionMatrix.elements[ 0 ] = 2 * near / ( xmax - xmin );
+			projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
+
+			this.cameraL.projectionMatrix.copy( projectionMatrix );
+
+			// for right eye
+
+			xmin = - ymax * aspect - eyeSepOnProjection;
+			xmax = ymax * aspect - eyeSepOnProjection;
+
+			projectionMatrix.elements[ 0 ] = 2 * near / ( xmax - xmin );
+			projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
+
+			this.cameraR.projectionMatrix.copy( projectionMatrix );
+
+		}
+
+		this.cameraL.matrixWorld.copy( this.matrixWorld ).multiply( eyeLeft );
+		this.cameraR.matrixWorld.copy( this.matrixWorld ).multiply( eyeRight );
+
+	};
+
+} )();

+ 1 - 0
utils/build/includes/common.json

@@ -62,6 +62,7 @@
 	"src/cameras/CubeCamera.js",
 	"src/cameras/OrthographicCamera.js",
 	"src/cameras/PerspectiveCamera.js",
+	"src/cameras/StereoCamera.js",
 	"src/lights/Light.js",
 	"src/lights/LightShadow.js",
 	"src/lights/AmbientLight.js",