Raycaster.js 2.6 KB
Newer Older
R
Rich Harris 已提交
1 2
import { Ray } from '../math/Ray';

3
/**
M
Mr.doob 已提交
4
 * @author mrdoob / http://mrdoob.com/
5
 * @author bhouston / http://clara.io/
6
 * @author stephomi / http://stephaneginier.com/
7
 */
M
Mr.doob 已提交
8

M
Mr.doob 已提交
9
function Raycaster( origin, direction, near, far ) {
M
Mr.doob 已提交
10

R
Rich Harris 已提交
11 12
	this.ray = new Ray( origin, direction );
	// direction is assumed to be normalized (for accurate distance calculations)
13

R
Rich Harris 已提交
14 15
	this.near = near || 0;
	this.far = far || Infinity;
16

R
Rich Harris 已提交
17 18 19 20 21 22
	this.params = {
		Mesh: {},
		Line: {},
		LOD: {},
		Points: { threshold: 1 },
		Sprite: {}
M
Mr.doob 已提交
23
	};
M
Mr.doob 已提交
24

R
Rich Harris 已提交
25 26 27 28 29 30 31 32 33
	Object.defineProperties( this.params, {
		PointCloud: {
			get: function () {
				console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' );
				return this.Points;
			}
		}
	} );

M
Mr.doob 已提交
34
}
M
Mr.doob 已提交
35

R
Rich Harris 已提交
36
function ascSort( a, b ) {
M
Mr.doob 已提交
37

R
Rich Harris 已提交
38
	return a.distance - b.distance;
M
Mr.doob 已提交
39

R
Rich Harris 已提交
40
}
M
Mr.doob 已提交
41

R
Rich Harris 已提交
42
function intersectObject( object, raycaster, intersects, recursive ) {
43

R
Rich Harris 已提交
44
	if ( object.visible === false ) return;
M
Mr.doob 已提交
45

R
Rich Harris 已提交
46
	object.raycast( raycaster, intersects );
M
Mr.doob 已提交
47

R
Rich Harris 已提交
48
	if ( recursive === true ) {
M
Mr.doob 已提交
49

R
Rich Harris 已提交
50
		var children = object.children;
M
Mr.doob 已提交
51

R
Rich Harris 已提交
52
		for ( var i = 0, l = children.length; i < l; i ++ ) {
M
Mr.doob 已提交
53

R
Rich Harris 已提交
54
			intersectObject( children[ i ], raycaster, intersects, true );
M
Mr.doob 已提交
55

M
Mr.doob 已提交
56
		}
M
Mr.doob 已提交
57

M
Mr.doob 已提交
58
	}
M
Mr.doob 已提交
59

R
Rich Harris 已提交
60
}
61

R
Rich Harris 已提交
62
//
M
Mr.doob 已提交
63

R
Rich Harris 已提交
64
Raycaster.prototype = {
65

R
Rich Harris 已提交
66
	constructor: Raycaster,
67

R
Rich Harris 已提交
68
	linePrecision: 1,
69

R
Rich Harris 已提交
70
	set: function ( origin, direction ) {
M
Mr.doob 已提交
71

R
Rich Harris 已提交
72
		// direction is assumed to be normalized (for accurate distance calculations)
73

R
Rich Harris 已提交
74
		this.ray.set( origin, direction );
75

R
Rich Harris 已提交
76
	},
77

R
Rich Harris 已提交
78
	setFromCamera: function ( coords, camera ) {
79

R
Rich Harris 已提交
80
		if ( (camera && camera.isPerspectiveCamera) ) {
81

R
Rich Harris 已提交
82 83
			this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
			this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
84

R
Rich Harris 已提交
85
		} else if ( (camera && camera.isOrthographicCamera) ) {
86

R
Rich Harris 已提交
87 88
			this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
			this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
89

R
Rich Harris 已提交
90
		} else {
91

R
Rich Harris 已提交
92 93 94
			console.error( 'THREE.Raycaster: Unsupported camera type.' );

		}
95

R
Rich Harris 已提交
96
	},
M
Mr.doob 已提交
97

R
Rich Harris 已提交
98
	intersectObject: function ( object, recursive ) {
99

R
Rich Harris 已提交
100
		var intersects = [];
M
Mr.doob 已提交
101

R
Rich Harris 已提交
102
		intersectObject( object, this, intersects, recursive );
M
Mr.doob 已提交
103

R
Rich Harris 已提交
104
		intersects.sort( ascSort );
M
Mr.doob 已提交
105

R
Rich Harris 已提交
106
		return intersects;
M
Mr.doob 已提交
107

R
Rich Harris 已提交
108
	},
M
Mr.doob 已提交
109

R
Rich Harris 已提交
110
	intersectObjects: function ( objects, recursive ) {
M
Mr.doob 已提交
111

R
Rich Harris 已提交
112
		var intersects = [];
M
Mr.doob 已提交
113

R
Rich Harris 已提交
114
		if ( Array.isArray( objects ) === false ) {
M
Mr.doob 已提交
115

R
Rich Harris 已提交
116 117
			console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
			return intersects;
M
Mr.doob 已提交
118

R
Rich Harris 已提交
119
		}
120

R
Rich Harris 已提交
121
		for ( var i = 0, l = objects.length; i < l; i ++ ) {
M
Mr.doob 已提交
122

R
Rich Harris 已提交
123
			intersectObject( objects[ i ], this, intersects, recursive );
M
Mr.doob 已提交
124

R
Rich Harris 已提交
125
		}
M
Mr.doob 已提交
126

R
Rich Harris 已提交
127
		intersects.sort( ascSort );
M
Mr.doob 已提交
128

R
Rich Harris 已提交
129
		return intersects;
M
Mr.doob 已提交
130

R
Rich Harris 已提交
131 132 133
	}

};
M
Mr.doob 已提交
134 135


136
export { Raycaster };