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

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
	Object.defineProperties( this.params, {
		PointCloud: {
			get: function () {
M
Mugen87 已提交
28

R
Rich Harris 已提交
29 30
				console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' );
				return this.Points;
M
Mugen87 已提交
31

R
Rich Harris 已提交
32 33 34 35
			}
		}
	} );

M
Mr.doob 已提交
36
}
M
Mr.doob 已提交
37

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

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

R
Rich Harris 已提交
42
}
M
Mr.doob 已提交
43

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

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

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

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

R
Rich Harris 已提交
52
		var children = object.children;
M
Mr.doob 已提交
53

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

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

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

M
Mr.doob 已提交
60
	}
M
Mr.doob 已提交
61

R
Rich Harris 已提交
62
}
63

64
Object.assign( Raycaster.prototype, {
65

R
Rich Harris 已提交
66
	linePrecision: 1,
67

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

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

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

R
Rich Harris 已提交
74
	},
75

R
Rich Harris 已提交
76
	setFromCamera: function ( coords, camera ) {
77

M
Mr.doob 已提交
78
		if ( ( camera && camera.isPerspectiveCamera ) ) {
79

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

M
Mr.doob 已提交
83
		} else if ( ( camera && camera.isOrthographicCamera ) ) {
84

R
Rich Harris 已提交
85 86
			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 );
87

R
Rich Harris 已提交
88
		} else {
89

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

		}
93

R
Rich Harris 已提交
94
	},
M
Mr.doob 已提交
95

96
	intersectObject: function ( object, recursive, optionalTarget ) {
97

98
		var intersects = optionalTarget || [];
M
Mr.doob 已提交
99

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

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

R
Rich Harris 已提交
104
		return intersects;
M
Mr.doob 已提交
105

R
Rich Harris 已提交
106
	},
M
Mr.doob 已提交
107

108
	intersectObjects: function ( objects, recursive, optionalTarget ) {
M
Mr.doob 已提交
109

110
		var intersects = optionalTarget || [];
M
Mr.doob 已提交
111

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

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

R
Rich Harris 已提交
117
		}
118

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

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

R
Rich Harris 已提交
123
		}
M
Mr.doob 已提交
124

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

R
Rich Harris 已提交
127
		return intersects;
M
Mr.doob 已提交
128

R
Rich Harris 已提交
129 130
	}

131
} );
M
Mr.doob 已提交
132 133


134
export { Raycaster };