Browse Source

Merge pull request #7171 from simonThiele/unittests

Unittests for Cameras
Mr.doob 10 years ago
parent
commit
17b9a3891d

+ 13 - 3
test/unit/cameras/Camera.js

@@ -5,8 +5,18 @@
 module( "Camera" );
 
 test( "lookAt", function() {
-	var obj = new THREE.Camera();
-	obj.lookAt(new THREE.Vector3(0, 1, -1));
+	var cam = new THREE.Camera();
+	cam.lookAt(new THREE.Vector3(0, 1, -1));
 
-	ok( obj.rotation.x * (180 / Math.PI) === 45 , "x is equal" );
+	ok( cam.rotation.x * (180 / Math.PI) === 45 , "x is equal" );
+});
+
+test( "clone", function() {
+	var cam = new THREE.Camera();
+	cam.lookAt(new THREE.Vector3(1, 2, 3));
+
+	var clonedCam = cam.clone();
+
+	ok( cam.matrixWorldInverse.equals(clonedCam.matrixWorldInverse) , "matrixWorldInverse is equal" );
+	ok( cam.projectionMatrix.equals(clonedCam.projectionMatrix) , "projectionMatrix is equal" );
 });

+ 41 - 0
test/unit/cameras/OrthographicCamera.js

@@ -0,0 +1,41 @@
+/**
+ * @author simonThiele / https://github.com/simonThiele
+ */
+
+module( "OrthographicCamera" );
+
+test( "updateProjectionMatrix", function() {
+	var left = -1, right = 1, top = 1, bottom = -1, near = 1, far = 3;
+	cam = new THREE.OrthographicCamera(left, right, top, bottom, near, far);
+
+	// updateProjectionMatrix is called in contructor
+	var pMatrix = cam.projectionMatrix.elements;
+
+	// orthographic projection is given my the 4x4 Matrix
+	// 2/r-l		0			 0		-(l+r/r-l)
+	//   0		2/t-b		 0		-(t+b/t-b)
+	//   0			0		-2/f-n	-(f+n/f-n)
+	//   0			0			 0				1
+
+	ok( pMatrix[0] === 2 / ( right - left ), "m[0,0] === 2 / (r - l)" );
+	ok( pMatrix[5] === 2 / ( top - bottom ), "m[1,1] === 2 / (t - b)" );
+	ok( pMatrix[10] === -2 / ( far - near ), "m[2,2] === -2 / (f - n)" );
+	ok( pMatrix[12] === - ( ( right + left ) / ( right - left ) ), "m[3,0] === -(r+l/r-l)" );
+	ok( pMatrix[13] === - ( ( top + bottom ) / ( top - bottom ) ), "m[3,1] === -(t+b/b-t)" );
+	ok( pMatrix[14] === - ( ( far + near ) / ( far - near ) ), "m[3,2] === -(f+n/f-n)" );
+});
+
+test( "clone", function() {
+	var left = -1.5, right = 1.5, top = 1, bottom = -1, near = 0.1, far = 42;
+	var cam = new THREE.OrthographicCamera(left, right, top, bottom, near, far);
+
+	var clonedCam = cam.clone();
+
+	ok( cam.left === clonedCam.left , "left is equal" );
+	ok( cam.right === clonedCam.right , "right is equal" );
+	ok( cam.top === clonedCam.top , "top is equal" );
+	ok( cam.bottom === clonedCam.bottom , "bottom is equal" );
+	ok( cam.near === clonedCam.near , "near is equal" );
+	ok( cam.far === clonedCam.far , "far is equal" );
+	ok( cam.zoom === clonedCam.zoom , "zoom is equal" );
+});

+ 56 - 0
test/unit/cameras/PerspectiveCamera.js

@@ -0,0 +1,56 @@
+/**
+ * @author simonThiele / https://github.com/simonThiele
+ */
+
+module( "PerspectiveCamera" );
+
+test( "updateProjectionMatrix", function() {
+	var near = 1,
+			far = 3,
+			bottom = -1,
+			top = 1,
+			aspect = 16 / 9,
+			left = -top * aspect,
+			right = -bottom * aspect,
+			fov = 90;
+
+	var cam = new THREE.PerspectiveCamera( fov, aspect, near, far );
+
+	// updateProjectionMatrix is called in contructor
+	var m = cam.projectionMatrix.elements;
+
+	// perspective projection is given my the 4x4 Matrix
+	// 2n/r-l		0			l+r/r-l				 0
+	//   0		2n/t-b	t+b/t-b				 0
+	//   0			0		-(f+n/f-n)	-(2fn/f-n)
+	//   0			0				0						 1
+
+	ok( m[0] === ( 2 * near ) / ( right - left ), "m[0,0] === 2n/r-l" );
+	ok( m[5] === ( 2 * near ) / ( top - bottom ), "m[1,1] === 2n/r-l" );
+	ok( m[8] === ( right + left ) / ( right - left ), "m[2,0] === 2n/r-l" );
+	ok( m[9] === ( top + bottom ) / ( top - bottom ), "m[2,1] === 2n/r-l" );
+	ok( m[10] === - ( far + near ) / ( far - near ), "m[2,2] === 2n/r-l" );
+	ok( m[14] === - ( 2 * near * far ) / ( far - near ), "m[3,2] === 2n/r-l" );
+});
+
+test( "clone", function() {
+	var near = 1,
+			far = 3,
+			bottom = -1,
+			top = 1,
+			aspect = 16 / 9,
+			left = -top * aspect,
+			right = -bottom * aspect,
+			fov = 90;
+
+	var cam = new THREE.PerspectiveCamera( fov, aspect, near, far );
+
+	var clonedCam = cam.clone();
+
+	ok( cam.fov === clonedCam.fov , "fov is equal" );
+	ok( cam.aspect === clonedCam.aspect , "aspect is equal" );
+	ok( cam.near === clonedCam.near , "near is equal" );
+	ok( cam.far === clonedCam.far , "far is equal" );
+	ok( cam.zoom === clonedCam.zoom , "zoom is equal" );
+	ok( cam.projectionMatrix.equals(clonedCam.projectionMatrix) , "projectionMatrix is equal" );
+});

+ 4 - 2
test/unit/unittests_three.html

@@ -18,9 +18,11 @@
 
   <!-- add class-based unit tests below -->
 
-  <script src="core/Object3D.js"></script>
+  <script src="cameras/Camera.js"></script>
+  <script src="cameras/OrthographicCamera.js"></script>
+  <script src="cameras/PerspectiveCamera.js"></script>
 
-  <script src="Cameras/Camera.js"></script>
+  <script src="core/Object3D.js"></script>
 
   <script src="math/Constants.js"></script>
   <script src="math/Box2.js"></script>