-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Expand file tree
/
Copy pathSelector.js
More file actions
295 lines (157 loc) · 5.83 KB
/
Copy pathSelector.js
File metadata and controls
295 lines (157 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
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
67
68
69
70
71
72
73
74
75
76
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
111
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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import * as THREE from 'three';
const mouse = new THREE.Vector2();
const raycaster = new THREE.Raycaster();
const _box = new THREE.Box3();
const _vector = new THREE.Vector3();
const _deltaMatrix = new THREE.Matrix4();
const _objectMatrix = new THREE.Matrix4();
const _parentMatrixInverse = new THREE.Matrix4();
class Selector {
constructor( editor ) {
const signals = editor.signals;
this.editor = editor;
this.signals = signals;
this.selection = [];
// an intermediate group used as pivot when transforming multiple objects
this.group = new THREE.Group();
this._groupMatrixWorldInverse = new THREE.Matrix4();
this._memberStates = [];
// signals
signals.intersectionsDetected.add( ( intersects, shiftKey ) => {
if ( intersects.length > 0 ) {
// Resolve helpers to their actual objects
const objects = [];
for ( let i = 0; i < intersects.length; i ++ ) {
let object = intersects[ i ].object;
if ( object.userData.object !== undefined ) {
object = object.userData.object;
}
if ( objects.indexOf( object ) === - 1 ) {
objects.push( object );
}
}
if ( shiftKey === true && this.selection.length > 0 && editor.selected !== editor.scene && editor.selected !== editor.camera ) {
// Shift-click toggles membership in the current selection
this.toggle( objects[ 0 ] );
} else {
// Cycle through objects if the first one is already selected
const index = objects.indexOf( editor.selected );
if ( index !== - 1 && index < objects.length - 1 ) {
this.select( objects[ index + 1 ] );
} else {
this.select( objects[ 0 ] );
}
}
} else {
if ( shiftKey !== true ) this.select( null ); // keep the selection when shift-clicking empty space
}
} );
signals.objectChanged.add( ( object ) => {
if ( this.selection.length < 2 ) return;
if ( object === this.group ) {
// the group has been transformed (e.g. via TransformControls), sync its members
this.applyGroupTransform();
} else if ( this.selection.indexOf( object ) !== - 1 ) {
// a member has been changed independently (e.g. via undo/redo), re-anchor the pivot
this.updateGroup();
}
} );
}
getIntersects( raycaster ) {
const objects = [];
this.editor.scene.traverseVisible( function ( child ) {
objects.push( child );
} );
this.editor.sceneHelpers.traverseVisible( function ( child ) {
if ( child.name === 'picker' || child.userData.object !== undefined ) objects.push( child );
} );
return raycaster.intersectObjects( objects, false );
}
getPointerIntersects( point, camera ) {
mouse.set( ( point.x * 2 ) - 1, - ( point.y * 2 ) + 1 );
raycaster.setFromCamera( mouse, camera );
return this.getIntersects( raycaster );
}
getSelectionBox( target ) {
target.makeEmpty();
for ( let i = 0; i < this.selection.length; i ++ ) {
const object = this.selection[ i ];
target.expandByObject( object, true );
target.expandByPoint( object.getWorldPosition( _vector ) ); // objects without geometry (e.g. lights)
}
return target;
}
select( object ) {
this.setSelection( object === null ? [] : [ object ] );
}
toggle( object ) {
const selection = this.selection.slice();
const index = selection.indexOf( object );
if ( index === - 1 ) {
selection.push( object );
} else {
selection.splice( index, 1 );
}
this.setSelection( selection );
}
setSelection( objects ) {
const editor = this.editor;
const hadGroupSelection = this.group.parent !== null;
this.selection = objects.slice();
if ( objects.length > 1 ) {
editor.sceneHelpers.add( this.group );
this.updateGroup();
editor.selected = null;
editor.config.setKey( 'selected', null );
this.signals.objectSelected.dispatch( null );
} else {
const object = ( objects.length === 1 ) ? objects[ 0 ] : null;
editor.sceneHelpers.remove( this.group );
if ( editor.selected === object && hadGroupSelection === false ) return;
editor.selected = object;
editor.config.setKey( 'selected', ( object !== null ) ? object.uuid : null );
this.signals.objectSelected.dispatch( object );
}
}
updateGroup() {
const group = this.group;
// use the center of the selection's AABB as pivot point
this.getSelectionBox( _box ).getCenter( group.position );
group.quaternion.identity();
group.scale.set( 1, 1, 1 );
group.updateWorldMatrix();
this._groupMatrixWorldInverse.copy( group.matrixWorld ).invert();
// members with a selected ancestor are updated by that ancestor's transform
const members = this.selection.filter( ( object ) => hasSelectedAncestor( object, this.selection ) === false );
this._memberStates = members.map( ( object ) => ( { object: object, matrixWorld: object.matrixWorld.clone() } ) );
}
applyGroupTransform() {
const group = this.group;
group.updateWorldMatrix();
_deltaMatrix.multiplyMatrices( group.matrixWorld, this._groupMatrixWorldInverse );
const states = this._memberStates;
for ( let i = 0; i < states.length; i ++ ) {
const object = states[ i ].object;
const parent = object.parent;
_objectMatrix.multiplyMatrices( _deltaMatrix, states[ i ].matrixWorld );
_parentMatrixInverse.copy( parent.matrixWorld ).invert();
_objectMatrix.premultiply( _parentMatrixInverse );
_objectMatrix.decompose( object.position, object.quaternion, object.scale );
object.updateWorldMatrix( false, true );
const helper = this.editor.helpers[ object.id ];
if ( helper !== undefined && helper.isSkeletonHelper !== true ) helper.update();
}
}
deselect() {
this.select( null );
}
}
function hasSelectedAncestor( object, selection ) {
let parent = object.parent;
while ( parent !== null ) {
if ( selection.indexOf( parent ) !== - 1 ) return true;
parent = parent.parent;
}
return false;
}
export { Selector };