@@ -5,10 +5,6 @@ import {
5
5
Quaternion ,
6
6
} from 'three' ;
7
7
8
- /**
9
- * This is a class to check whether objects are in a selection area in 3D space
10
- */
11
-
12
8
const _frustum = new Frustum ( ) ;
13
9
const _center = new Vector3 ( ) ;
14
10
@@ -33,34 +29,103 @@ const _matrix = new Matrix4();
33
29
const _quaternion = new Quaternion ( ) ;
34
30
const _scale = new Vector3 ( ) ;
35
31
32
+ /**
33
+ * This class can be used to select 3D objects in a scene with a selection box.
34
+ * It is recommended to visualize the selected area with the help of {@link SelectionHelper}.
35
+ *
36
+ * ```js
37
+ * const selectionBox = new SelectionBox( camera, scene );
38
+ * const selectedObjects = selectionBox.select( startPoint, endPoint );
39
+ * ```
40
+ */
36
41
class SelectionBox {
37
42
43
+ /**
44
+ * Constructs a new selection box.
45
+ *
46
+ * @param {Camera } camera - The camera the scene is rendered with.
47
+ * @param {Scene } scene - The scene.
48
+ * @param {number } [deep=Number.MAX_VALUE] - How deep the selection frustum of perspective cameras should extend.
49
+ */
38
50
constructor ( camera , scene , deep = Number . MAX_VALUE ) {
39
51
52
+ /**
53
+ * The camera the scene is rendered with.
54
+ *
55
+ * @type {Camera }
56
+ */
40
57
this . camera = camera ;
58
+
59
+ /**
60
+ * The camera the scene is rendered with.
61
+ *
62
+ * @type {Scene }
63
+ */
41
64
this . scene = scene ;
65
+
66
+ /**
67
+ * The start point of the selction.
68
+ *
69
+ * @type {Vector3 }
70
+ */
42
71
this . startPoint = new Vector3 ( ) ;
72
+
73
+ /**
74
+ * The end point of the selction.
75
+ *
76
+ * @type {Vector3 }
77
+ */
43
78
this . endPoint = new Vector3 ( ) ;
79
+
80
+ /**
81
+ * The selected 3D objects.
82
+ *
83
+ * @type {Array<Object3D> }
84
+ */
44
85
this . collection = [ ] ;
86
+
87
+ /**
88
+ * The selected instance IDs of instanced meshes.
89
+ *
90
+ * @type {Object }
91
+ */
45
92
this . instances = { } ;
93
+
94
+ /**
95
+ * How deep the selection frustum of perspective cameras should extend.
96
+ *
97
+ * @type {number }
98
+ * @default Number.MAX_VALUE
99
+ */
46
100
this . deep = deep ;
47
101
48
102
}
49
103
104
+ /**
105
+ * This method selects 3D objects in the scene based on the given start
106
+ * and end point. If no parameters are provided, the method uses the start
107
+ * and end values of the respective members.
108
+ *
109
+ * @param {Vector3 } [startPoint] - The start point.
110
+ * @param {Vector3 } [endPoint] - The end point.
111
+ * @return {Array<Object3D> } The selected 3D objects.
112
+ */
50
113
select ( startPoint , endPoint ) {
51
114
52
115
this . startPoint = startPoint || this . startPoint ;
53
116
this . endPoint = endPoint || this . endPoint ;
54
117
this . collection = [ ] ;
55
118
56
- this . updateFrustum ( this . startPoint , this . endPoint ) ;
57
- this . searchChildInFrustum ( _frustum , this . scene ) ;
119
+ this . _updateFrustum ( this . startPoint , this . endPoint ) ;
120
+ this . _searchChildInFrustum ( _frustum , this . scene ) ;
58
121
59
122
return this . collection ;
60
123
61
124
}
62
125
63
- updateFrustum ( startPoint , endPoint ) {
126
+ // private
127
+
128
+ _updateFrustum ( startPoint , endPoint ) {
64
129
65
130
startPoint = startPoint || this . startPoint ;
66
131
endPoint = endPoint || this . endPoint ;
@@ -170,7 +235,7 @@ class SelectionBox {
170
235
171
236
}
172
237
173
- searchChildInFrustum ( frustum , object ) {
238
+ _searchChildInFrustum ( frustum , object ) {
174
239
175
240
if ( object . isMesh || object . isLine || object . isPoints ) {
176
241
@@ -214,7 +279,7 @@ class SelectionBox {
214
279
215
280
for ( let x = 0 ; x < object . children . length ; x ++ ) {
216
281
217
- this . searchChildInFrustum ( frustum , object . children [ x ] ) ;
282
+ this . _searchChildInFrustum ( frustum , object . children [ x ] ) ;
218
283
219
284
}
220
285
0 commit comments