Raycaster.tests.js 6.4 KB
Newer Older
T
Tristan VALCKE 已提交
1 2 3 4 5
/* global QUnit */

import { Raycaster } from '../../../../src/core/Raycaster';
import { Vector3 } from '../../../../src/math/Vector3';
import { Mesh } from '../../../../src/objects/Mesh';
M
Mugen87 已提交
6
import { SphereGeometry } from '../../../../src/geometries/SphereGeometry';
7 8 9
import { BufferGeometry } from '../../../../src/core/BufferGeometry';
import { Line } from '../../../../src/objects/Line.js';
import { Points } from '../../../../src/objects/Points.js';
T
Tristan VALCKE 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
import { PerspectiveCamera } from '../../../../src/cameras/PerspectiveCamera';
import { OrthographicCamera } from '../../../../src/cameras/OrthographicCamera';

function checkRayDirectionAgainstReferenceVector( rayDirection, refVector, assert ) {

	assert.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 new Raycaster(
		new Vector3( 0, 0, 0 ),
		new 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() {

M
Mugen87 已提交
67
	return new Mesh( new SphereGeometry( 1, 100, 100 ) );
T
Tristan VALCKE 已提交
68 69 70 71 72

}

export default QUnit.module( 'Core', () => {

73
	QUnit.module( 'Raycaster', () => {
T
Tristan VALCKE 已提交
74 75

		// INSTANCING
76
		QUnit.todo( "Instancing", ( assert ) => {
T
Tristan VALCKE 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

			assert.ok( false, "everything's gonna be alright" );

		} );

		// PUBLIC STUFF
		QUnit.test( "set", ( assert ) => {

			var origin = new Vector3( 0, 0, 0 );
			var direction = new Vector3( 0, 0, - 1 );
			var a = new Raycaster( origin.clone(), direction.clone() );

			assert.deepEqual( a.ray.origin, origin, "Origin is correct" );
			assert.deepEqual( a.ray.direction, direction, "Direction is correct" );

			origin.set( 1, 1, 1 );
			direction.set( - 1, 0, 0 );
			a.set( origin, direction );

			assert.deepEqual( a.ray.origin, origin, "Origin was set correctly" );
			assert.deepEqual( a.ray.direction, direction, "Direction was set correctly" );

		} );

		QUnit.test( "setFromCamera (Perspective)", ( assert ) => {

			var raycaster = new Raycaster();
			var rayDirection = raycaster.ray.direction;
			var camera = new PerspectiveCamera( 90, 1, 1, 1000 );

			raycaster.setFromCamera( {
				x: 0,
				y: 0
			}, camera );
111
			assert.ok( rayDirection.x === 0 && rayDirection.y === 0 && rayDirection.z === - 1,
T
Tristan VALCKE 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
				"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 Vector3( x, y, - 1 ).normalize();

					checkRayDirectionAgainstReferenceVector( rayDirection, refVector, assert );

				}

			}

		} );

		QUnit.test( "setFromCamera (Orthographic)", ( assert ) => {

			var raycaster = new Raycaster();
			var rayOrigin = raycaster.ray.origin;
			var rayDirection = raycaster.ray.direction;
			var camera = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1000 );
			var expectedOrigin = new Vector3( 0, 0, 0 );
			var expectedDirection = new Vector3( 0, 0, - 1 );

			raycaster.setFromCamera( {
				x: 0,
				y: 0
			}, camera );
			assert.deepEqual( rayOrigin, expectedOrigin, "Ray origin has the right coordinates" );
			assert.deepEqual( rayDirection, expectedDirection, "Camera and Ray are pointing towards -z" );

		} );

		QUnit.test( "intersectObject", ( assert ) => {

			var raycaster = getRaycaster();
			var objectsToCheck = getObjectsToCheck();

			assert.ok( raycaster.intersectObject( objectsToCheck[ 0 ] ).length === 1,
				"no recursive search should lead to one hit" );

			assert.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 ++ ) {

				assert.ok( intersections[ i ].distance <= intersections[ i + 1 ].distance, "intersections are sorted" );

			}

		} );

		QUnit.test( "intersectObjects", ( assert ) => {

			var raycaster = getRaycaster();
			var objectsToCheck = getObjectsToCheck();

			assert.ok( raycaster.intersectObjects( objectsToCheck ).length === 1,
				"no recursive search should lead to one hit" );

			assert.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 ++ ) {

				assert.ok( intersections[ i ].distance <= intersections[ i + 1 ].distance, "intersections are sorted" );

			}

		} );

193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
		QUnit.test( "Line intersection threshold", ( assert ) => {

			var raycaster = getRaycaster();
			var points = [ new Vector3( -2, -10, -5 ), new Vector3( -2, 10, -5 ) ];
			var geometry = new BufferGeometry().setFromPoints( points );
			var line = new Line( geometry, null );

			raycaster.params.Line.threshold = 1.999;
			assert.ok( raycaster.intersectObject( line ).length === 0,
				"no Line intersection with a not-large-enough threshold" );

			raycaster.params.Line.threshold = 2.001;
			assert.ok( raycaster.intersectObject( line ).length === 1,
				"successful Line intersection with a large-enough threshold" );

		} );

		QUnit.test( "Points intersection threshold", ( assert ) => {

			var raycaster = getRaycaster();
			var coordinates = [ new Vector3( -2, 0, -5 ) ];
			var geometry = new BufferGeometry().setFromPoints( coordinates );
			var points = new Points( geometry, null );

			raycaster.params.Points.threshold = 1.999;
			assert.ok( raycaster.intersectObject( points ).length === 0,
				"no Points intersection with a not-large-enough threshold" );

			raycaster.params.Points.threshold = 2.001;
			assert.ok( raycaster.intersectObject( points ).length === 1,
				"successful Points intersection with a large-enough threshold" );

		} );


T
Tristan VALCKE 已提交
228 229 230
	} );

} );