Browse Source

Merge pull request #7649 from simonThiele/unittests

Unittests
Mr.doob 9 years ago
parent
commit
f760e3c147

+ 58 - 0
test/unit/core/Face3.js

@@ -0,0 +1,58 @@
+/**
+ * @author simonThiele / https://github.com/simonThiele
+ */
+
+module( "Face3" );
+
+test( "copy", function() {
+	var instance = new THREE.Face3(0, 1, 2, new THREE.Vector3(0, 1, 0), new THREE.Color(0.25, 0.5, 0.75), 2);
+	var copiedInstance = instance.copy(instance);
+
+	checkCopy(copiedInstance);
+	checkVertexAndColors(copiedInstance);
+});
+
+test( "copy", function() {
+	var instance = new THREE.Face3(0, 1, 2,
+		[new THREE.Vector3(0, 1, 0), new THREE.Vector3(1, 0, 1)],
+		[new THREE.Color(0.25, 0.5, 0.75), new THREE.Color(1, 0, 0.4)],
+		2);
+	var copiedInstance = instance.copy(instance);
+
+	checkCopy(copiedInstance);
+	checkVertexAndColorArrays(copiedInstance);
+});
+
+test( "clone", function() {
+	var instance = new THREE.Face3(0, 1, 2, new THREE.Vector3(0, 1, 0), new THREE.Color(0.25, 0.5, 0.75), 2);
+	var copiedInstance = instance.clone();
+
+	checkCopy(copiedInstance);
+	checkVertexAndColors(copiedInstance);
+});
+
+function checkCopy(copiedInstance) {
+	ok( copiedInstance instanceof THREE.Face3, "copy created the correct type" );
+	ok(
+		copiedInstance.a === 0 &&
+		copiedInstance.b === 1 &&
+		copiedInstance.c === 2 &&
+		copiedInstance.materialIndex === 2
+		,"properties where copied" );
+}
+
+function checkVertexAndColors(copiedInstance) {
+	ok(
+		copiedInstance.normal.x === 0 && copiedInstance.normal.y === 1 && copiedInstance.normal.z === 0 &&
+		copiedInstance.color.r === 0.25 && copiedInstance.color.g === 0.5 && copiedInstance.color.b === 0.75
+		,"properties where copied" );
+}
+
+function checkVertexAndColorArrays(copiedInstance) {
+	ok(
+		copiedInstance.vertexNormals[0].x === 0 && copiedInstance.vertexNormals[0].y === 1 && copiedInstance.vertexNormals[0].z === 0 &&
+		copiedInstance.vertexNormals[1].x === 1 && copiedInstance.vertexNormals[1].y === 0 && copiedInstance.vertexNormals[1].z === 1 &&
+		copiedInstance.vertexColors[0].r === 0.25 && copiedInstance.vertexColors[0].g === 0.5 && copiedInstance.vertexColors[0].b === 0.75 &&
+		copiedInstance.vertexColors[1].r === 1 && copiedInstance.vertexColors[1].g === 0 && copiedInstance.vertexColors[1].b === 0.4
+		,"properties where copied" );
+}

+ 107 - 0
test/unit/core/Geometry.js

@@ -0,0 +1,107 @@
+/**
+ * @author simonThiele / https://github.com/simonThiele
+ */
+
+module( "Geometry" );
+
+test( "rotateX", function() {
+	var geometry = getGeometry();
+
+	var matrix = new THREE.Matrix4();
+	matrix.makeRotationX( Math.PI / 2 ); // 90 degree
+
+	geometry.applyMatrix( matrix );
+
+	var v0 = geometry.vertices[0], v1 = geometry.vertices[1], v2 = geometry.vertices[2];
+	ok ( v0.x === -0.5 && v0.y === 0 && v0.z === 0, "first vertex was rotated" );
+	ok ( v1.x === 0.5 && v1.y === 0 && v1.z === 0, "second vertex was rotated" );
+	ok ( v2.x === 0 && v2.y < Number.EPSILON && v2.z === 1, "third vertex was rotated" );
+});
+
+
+test( "rotateY", function() {
+	var geometry = getGeometry();
+
+	var matrix = new THREE.Matrix4();
+	matrix.makeRotationY( Math.PI ); // 180 degrees
+
+	geometry.applyMatrix( matrix );
+
+	var v0 = geometry.vertices[0], v1 = geometry.vertices[1], v2 = geometry.vertices[2];
+	ok ( v0.x === 0.5 && v0.y === 0 && v0.z < Number.EPSILON, "first vertex was rotated" );
+	ok ( v1.x === -0.5 && v1.y === 0 && v1.z < Number.EPSILON, "second vertex was rotated" );
+	ok ( v2.x === 0 && v2.y === 1 && v2.z === 0, "third vertex was rotated" );
+});
+
+test( "rotateZ", function() {
+	var geometry = getGeometry();
+
+	var matrix = new THREE.Matrix4();
+	matrix.makeRotationZ( Math.PI / 2 * 3 ); // 270 degrees
+
+	geometry.applyMatrix( matrix );
+
+	var v0 = geometry.vertices[0], v1 = geometry.vertices[1], v2 = geometry.vertices[2];
+	ok ( v0.x < Number.EPSILON && v0.y === 0.5 && v0.z === 0, "first vertex was rotated" );
+	ok ( v1.x < Number.EPSILON && v1.y === -0.5 && v1.z === 0, "second vertex was rotated" );
+	ok ( v2.x === 1 && v2.y < Number.EPSILON && v2.z === 0, "third vertex was rotated" );
+});
+
+test( "fromBufferGeometry", function() {
+	var bufferGeometry = new THREE.BufferGeometry();
+	bufferGeometry.addAttribute('position', new THREE.BufferAttribute(new Float32Array( [1, 2, 3, 4, 5, 6, 7, 8, 9] ), 3 ) );
+	bufferGeometry.addAttribute('color', new THREE.BufferAttribute(new Float32Array( [0, 0, 0, 0.5, 0.5, 0.5, 1, 1, 1] ), 3 ) );
+	bufferGeometry.addAttribute('normal', new THREE.BufferAttribute(new Float32Array( [0, 1, 0, 1, 0, 1, 1, 1, 0] ), 3 ) );
+	bufferGeometry.addAttribute('uv', new THREE.BufferAttribute(new Float32Array( [0, 0, 0, 1, 1, 1] ), 2 ) );
+	bufferGeometry.addAttribute('uv2', new THREE.BufferAttribute(new Float32Array( [0, 0, 0, 1, 1, 1] ), 2 ) );
+
+	var geometry = new THREE.Geometry().fromBufferGeometry( bufferGeometry );
+
+	var colors = geometry.colors;
+	ok (
+		colors[0].r === 0 && colors[0].g === 0 && colors[0].b === 0 &&
+		colors[1].r === 0.5 && colors[1].g === 0.5 && colors[1].b === 0.5 &&
+		colors[2].r === 1 && colors[2].g === 1 && colors[2].b === 1
+		, "colors were created well" );
+
+	var vertices = geometry.vertices;
+	ok (
+		vertices[0].x === 1 && vertices[0].y === 2 && vertices[0].z === 3 &&
+		vertices[1].x === 4 && vertices[1].y === 5 && vertices[1].z === 6 &&
+		vertices[2].x === 7 && vertices[2].y === 8 && vertices[2].z === 9
+		, "vertices were created well" );
+
+	var vNormals = geometry.faces[0].vertexNormals;
+	ok (
+		vNormals[0].x === 0 && vNormals[0].y === 1 && vNormals[0].z === 0 &&
+		vNormals[1].x === 1 && vNormals[1].y === 0 && vNormals[1].z === 1 &&
+		vNormals[2].x === 1 && vNormals[2].y === 1 && vNormals[2].z === 0
+		, "vertex normals were created well" );
+});
+
+test( "normalize", function() {
+	var geometry = getGeometry();
+	geometry.computeLineDistances();
+
+	var distances = geometry.lineDistances;
+	ok( distances[0] === 0, "distance to the 1st point is 0" );
+	ok( distances[1] === 1 + distances[0], "distance from the 1st to the 2nd is sqrt(2nd - 1st) + distance - 1" );
+	ok( distances[2] === Math.sqrt( 0.5 * 0.5 + 1 ) + distances[1], "distance from the 1st to the 3nd is sqrt(3rd - 2nd) + distance - 1" );
+});
+
+function getGeometryByParams( x1, y1, z1, x2, y2, z2, x3, y3, z3 ) {
+	var geometry = new THREE.Geometry();
+
+	// a triangle
+	geometry.vertices = [
+		new THREE.Vector3( x1, y1, z1 ),
+		new THREE.Vector3( x2, y2, z2 ),
+		new THREE.Vector3( x3, y3, z3 )
+	];
+
+	return geometry;
+}
+
+function getGeometry() {
+	return getGeometryByParams( -0.5, 0, 0, 0.5, 0, 0, 0, 1, 0 );
+}

+ 3 - 3
test/unit/core/InstancedBufferAttribute.js

@@ -14,9 +14,9 @@ test( "can be created", function() {
 });
 
 test( "copy", function() {
-	var array = new Float32Array([1, 2, 3, 7, 8, 9]);
-	var instance = new THREE.InstancedBufferAttribute(array, 2, 123);
-	var copiedInstance = instance.copy(instance);
+	var array = new Float32Array( [1, 2, 3, 7, 8, 9] );
+	var instance = new THREE.InstancedBufferAttribute( array, 2, 123 );
+	var copiedInstance = instance.copy( instance );
 
 	ok( copiedInstance instanceof THREE.InstancedBufferAttribute, "the clone has the correct type" );
 	ok( copiedInstance.itemSize === 2, "itemSize was copied" );

+ 8 - 6
test/unit/core/InstancedBufferGeometry.js

@@ -7,8 +7,10 @@ module( "InstancedBufferGeometry" );
 function createClonableMock() {
 	return {
 		callCount: 0,
+
 		clone: function() {
 			this.callCount++;
+
 			return this;
 		}
 	}
@@ -23,13 +25,13 @@ test( "copy", function() {
 
 	var instance = new THREE.InstancedBufferGeometry();
 
-	instance.addGroup(0, 10, instanceMock1);
-	instance.addGroup(10, 5, instanceMock2);
-	instance.setIndex(indexMock);
-	instance.addAttribute('attributeMock1', attributeMock1);
-	instance.addAttribute('attributeMock2', attributeMock2);
+	instance.addGroup( 0, 10, instanceMock1 );
+	instance.addGroup( 10, 5, instanceMock2 );
+	instance.setIndex( indexMock );
+	instance.addAttribute( 'attributeMock1', attributeMock1 );
+	instance.addAttribute( 'attributeMock2', attributeMock2 );
 
-	var copiedInstance = instance.copy(instance);
+	var copiedInstance = instance.copy( instance );
 
 	ok( copiedInstance instanceof THREE.InstancedBufferGeometry, "the clone has the correct type" );
 

+ 2 - 2
test/unit/core/InterleavedBuffer.js

@@ -4,10 +4,10 @@
 
 module( "InterleavedBuffer" );
 
-function checkInstanceAgainstCopy(instance, copiedInstance) {
+function checkInstanceAgainstCopy( instance, copiedInstance ) {
 	ok( copiedInstance instanceof THREE.InterleavedBuffer, "the clone has the correct type" );
 
-	for (var i = 0; i < instance.array.length; i++) {
+	for ( var i = 0; i < instance.array.length; i++ ) {
 		ok( copiedInstance.array[i] === instance.array[i], "array was copied" );
 	}
 

+ 119 - 0
test/unit/core/Raycaster.js

@@ -0,0 +1,119 @@
+/**
+ * @author simonThiele / https://github.com/simonThiele
+ */
+
+module( "Raycaster" );
+
+test( "intersectObjects", function() {
+	var raycaster = getRaycaster();
+	var objectsToCheck = getObjectsToCheck();
+
+	ok ( raycaster.intersectObjects( objectsToCheck ).length === 1,
+		"no recursive search should lead to one hit" );
+
+	ok ( raycaster.intersectObjects( objectsToCheck, true ).length === 3,
+		"recursive search should lead to three hits" );
+
+	var intersections = raycaster.intersectObjects( objectsToCheck, true );
+	for ( var i = 0; i < intersections.length - 1; i++ ) {
+
+	  ok( intersections[ i ].distance <= intersections[ i + 1 ].distance, "intersections are sorted" );
+
+	}
+});
+
+test( "intersectObject", function() {
+	var raycaster = getRaycaster();
+	var objectsToCheck = getObjectsToCheck();
+
+	ok ( raycaster.intersectObject( objectsToCheck[ 0 ] ).length === 1,
+		"no recursive search should lead to one hit" );
+
+	ok ( raycaster.intersectObject( objectsToCheck[ 0 ], true ).length === 3,
+		"recursive search should lead to three hits" );
+
+	var intersections = raycaster.intersectObject( objectsToCheck[ 0 ], true );
+	for ( var i = 0; i < intersections.length - 1; i++ ) {
+
+	  ok( intersections[ i ].distance <= intersections[ i + 1 ].distance, "intersections are sorted" );
+
+	}
+});
+
+test( "setFromCamera", function() {
+	var raycaster = new THREE.Raycaster();
+	var rayDirection = raycaster.ray.direction;
+	var camera = new THREE.PerspectiveCamera( 90, 1, 1, 1000 );
+
+	raycaster.setFromCamera( { x : 0, y: 0 }, camera );
+	ok( rayDirection.x === 0, rayDirection.y === 0, rayDirection.z === -1,
+		"camera is looking straight to -z and so does the ray in the middle of the screen" );
+
+	var step = 0.1;
+
+	for ( var x = -1; x <= 1; x += step ) {
+
+		for ( var y = -1; y <= 1; y += step ) {
+
+			raycaster.setFromCamera( { x, y }, camera );
+
+			var refVector = new THREE.Vector3( x, y, -1 ).normalize();
+
+			checkRayDirectionAgainstReferenceVector( rayDirection, refVector );
+
+		}
+
+	}
+});
+
+function checkRayDirectionAgainstReferenceVector( rayDirection, refVector ) {
+	ok( refVector.x - rayDirection.x <= Number.EPSILON &&
+			refVector.y - rayDirection.y <= Number.EPSILON &&
+			refVector.z - rayDirection.z <= Number.EPSILON,
+			"camera is pointing to the same direction as expected" );
+}
+
+function getRaycaster() {
+	return raycaster = new THREE.Raycaster(
+		new THREE.Vector3( 0, 0, 0 ),
+		new THREE.Vector3( 0, 0, -1 ),
+		1,
+		100
+	);
+}
+
+function getObjectsToCheck() {
+	var objects = [];
+
+	var sphere1 = getSphere();
+	sphere1.position.set( 0, 0, -10 );
+	sphere1.name = 1;
+	objects.push( sphere1 );
+
+	var sphere11 = getSphere();
+	sphere11.position.set( 0, 0, 1 );
+	sphere11.name = 11;
+	sphere1.add( sphere11 );
+
+	var sphere12 = getSphere();
+	sphere12.position.set( 0, 0, -1 );
+	sphere12.name = 12;
+	sphere1.add( sphere12 );
+
+	var sphere2 = getSphere();
+	sphere2.position.set( -5, 0, -5 );
+	sphere2.name = 2;
+	objects.push( sphere2 );
+
+	for ( var i = 0; i < objects.length; i++ ) {
+
+		objects[ i ].updateMatrixWorld();
+
+	}
+
+	return objects;
+}
+
+function getSphere() {
+	return new THREE.Mesh( new THREE.SphereGeometry( 1, 100, 100 ) );
+}

+ 3 - 0
test/unit/unittests_three.html

@@ -28,6 +28,9 @@
   <script src="core/InterleavedBuffer.js"></script>
   <script src="core/InterleavedBufferAttribute.js"></script>
 
+  <script src="core/Raycaster.js"></script>
+  <script src="core/Face3.js"></script>
+  <script src="core/Geometry.js"></script>
   <script src="core/BufferAttribute.js"></script>
   <script src="core/BufferGeometry.js"></script>
   <script src="core/Clock.js"></script>