/
index.ts
719 lines (656 loc) · 23.8 KB
/
index.ts
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
import {
DEFAULT_CONTROLS_SEPARATION,
DEFAULT_EYE_ROTATION_SCALE,
DEFAULT_PLANE_SIZE_SCALE,
DEFAULT_ROTATION_RADIUS_SCALE,
DEFAULT_TRANSLATION_DISTANCE_SCALE,
} from "../utils/constants";
import Translation from "./handles/translation";
import Rotation from "./handles/rotation";
import Pick from "./handles/pick";
import PickPlane from "./handles/pick-plane";
import {
DEFAULT_HANDLE_GROUP_NAME,
IHandle,
PickGroup,
PickPlaneGroup,
RotationGroup,
TranslationGroup,
} from "./handles";
import RotationEye from "./handles/rotation-eye";
import { Camera, Group, MathUtils, Mesh, Object3D, Quaternion, Vector3 } from "three";
export enum ANCHOR_MODE {
/**
* In this mode the Controls do not inherit the orientation of the object
* as it is rotated.
*/
FIXED = "fixed",
/**
* In this mode the Controls rotate as the object is rotated.
*/
INHERIT = "inherit",
}
/**
* The first number is the unit limit allowed in the -ve direction.
* The second number is the unit limit allowed in the +ve direction.
*
* All calculations are with respect to anchor position which is the object's
* position when [[setTranslationLimit]] is called.
* `{ x: [-1, 2], y: false, z: false }` - sets the translation limit to `-1` unit
* in the -x-direction, `+2` units in the +x-direction, and no limit on the
* y and z-direction.
*
* Setting the limit to `false` disables the limit in all directions.
*/
export interface TranslationLimit {
x: [number, number] | false;
y: [number, number] | false;
z: [number, number] | false;
}
export interface IControlsOptions {
/**
* the anchor mode for the controls
* @default [[ANCHOR_MODE.FIXED]]
*/
mode?: ANCHOR_MODE;
/**
* distance between the position of the object and the position of the
* handles (in case of translation handles), or the radius (in case of rotation handles),
* or the size of the plane (in case of plane handles)
* @default 0.5
*/
separation?: number;
/**
* uses THREE.Mesh.computeBounds to set the separation; if separation
* is provided in addition to this option, it is added to the computed bounds
* @default false
*/
useComputedBounds?: boolean;
/**
* the quaternion applied to the whole Controls instance (handles get rotated relatively)
* @default undefined
*/
orientation?: {
x: number;
y: number;
z: number;
w: number;
};
/**
* hides other handles of a Controls instance when drag starts
* @default true
*/
hideOtherHandlesOnDrag?: boolean;
/**
* hides all other Controls instances when drag starts
* @default true
*/
hideOtherControlsInstancesOnDrag?: boolean;
/**
* displays the plane in which the drag interaction takes place
* (useful for debugging)
* @default false
*/
showHelperPlane?: boolean;
/**
* enables damping for the controls
* @default true
*/
isDampingEnabled?: boolean;
/**
* sets the scaling factor for the radius of rotation handles
* @default 1.0
*/
rotationRadiusScale?: number;
/**
* sets the scaling factor for the radius of rotation handles in eye plane
* @default 1.25
*/
eyeRotationRadiusScale?: number;
/**
* sets the width and height scale for the pick plane handles
* @default 0.75
*/
pickPlaneSizeScale?: number;
/**
* sets the scaling for distance between translation handles' start and the
* center of the controls
* @default 1.0
*/
translationDistanceScale?: number;
/**
* For translation handles: highlights the axis along which the object moves.
* For rotation handles: highlights the axis of rotation.
* Not available on other handles.
* @default true
*/
highlightAxis?: boolean;
/**
* Enables snap to grid (nearest integer coordinate) for all translation type handles:
* [[TranslationGroup]], [[PickGroup]], and [[PickPlaneGroup]]
* @default { x: false, y: false, z: false }
*/
snapTranslation?: {
x: boolean;
y: boolean;
z: boolean;
};
}
/**
* Controls is the main class in this library.
* It is a subclass of THREE.Group, so its properties like `position` and
* `quaternion` can be modified as desired.
* The `children` are the control handles (like `rotationX`).
* All translations and rotations are setup with respect to the global coordinate system.
* @noInheritDoc
*/
export default class Controls extends Group {
/**
* handle which translates the object in the eye-plane
*/
public readonly pick: Pick;
/**
* handle which translates the object in XY plane
*/
public readonly pickPlaneXY: PickPlane;
/**
* handle which translates the object in YZ plane
*/
public readonly pickPlaneYZ: PickPlane;
/**
* handle which translates the object in ZX plane
*/
public readonly pickPlaneZX: PickPlane;
/**
* handle which translates the object along the x-axis; displayed in the
* +ve x-axis direction
*/
public readonly translationXP: Translation;
/**
* handle which translates the object along the y-axis; displayed in the
* +ve y-axis direction
*/
public readonly translationYP: Translation;
/**
* handle which translates the object along the z-axis; displayed in the
* +ve z-axis direction
*/
public readonly translationZP: Translation;
/**
* handle which translates the object along the x-axis; displayed in the
* -ve x-axis direction
*/
public readonly translationXN: Translation;
/**
* handle which translates the object along the y-axis; displayed in the
* -ve y-axis direction
*/
public readonly translationYN: Translation;
/**
* handle which translates the object along the z-axis; displayed in the
* -ve z-axis direction
*/
public readonly translationZN: Translation;
/**
* handle which rotates the object along the x-axis
*/
public readonly rotationX: Rotation;
/**
* handle which rotates the object along the y-axis
*/
public readonly rotationY: Rotation;
/**
* handle which rotates the object along the z-axis
*/
public readonly rotationZ: Rotation;
/**
* handle which rotates the object in the eye-plane
*/
public readonly rotationEye: RotationEye;
private handleTargetQuaternion = new Quaternion();
private objectWorldPosition = new Vector3();
private objectTargetPosition = new Vector3();
private objectTargetQuaternion = new Quaternion();
private objectParentWorldPosition = new Vector3();
private objectParentWorldQuaternion = new Quaternion();
private objectParentWorldScale = new Vector3();
private deltaPosition = new Vector3();
private normalizedHandleParallelVectorCache = new Vector3();
private touch1 = new Vector3();
private touch2 = new Vector3();
private boundingSphereRadius = 0;
private dragStartPoint = new Vector3();
private dragIncrementalStartPoint = new Vector3();
private handles: Set<IHandle> = new Set();
private isBeingDraggedTranslation = false;
private isBeingDraggedRotation = false;
private dampingFactor = 0.8;
private readonly useComputedBounds: boolean;
private readonly separation: number;
private initialSelfQuaternion = new Quaternion();
private readonly minTranslationCache = new Vector3();
private readonly maxTranslationCache = new Vector3();
private readonly options: IControlsOptions;
private readonly mode: ANCHOR_MODE;
private readonly translationDistanceScale: number;
private readonly rotationRadiusScale: number;
private readonly eyeRotationRadiusScale: number;
private readonly pickPlaneSizeScale: number;
private translationLimit?: TranslationLimit | false = false;
private translationAnchor: Vector3 | null = null;
/**
* enables damping for the controls
* @default true
*/
public isDampingEnabled: boolean;
/**
* hides other handles of a Controls instance when drag starts
* @default true
*/
public hideOtherHandlesOnDrag: boolean;
/**
* hides all other Controls instances when drag starts
* @default true
*/
public hideOtherControlsInstancesOnDrag: boolean;
/**
* displays the plane in which the drag interaction takes place
* (useful for debugging)
* @default false
*/
public showHelperPlane: boolean;
/**
* For translation handles: highlights the axis along which the object moves.
* For rotation handles: highlights the axis of rotation.
* Not available on other handles.
* @default true
*/
public highlightAxis: boolean;
/**
* Enables snap to grid (nearest integer coordinate) for all translation type handles:
* [[TranslationGroup]], [[PickGroup]], and [[PickPlaneGroup]]
* @default { x: false, y: false, z: false }
*/
public snapTranslation: {
x: boolean;
y: boolean;
z: boolean;
};
/**
*
* @param object - the object provided by the user
* @param camera - the THREE.Camera instance used in the scene
* @param options
*/
constructor(public object: Object3D, private camera: Camera, options?: IControlsOptions) {
super();
this.options = options || {};
this.mode = this.options?.mode ?? ANCHOR_MODE.FIXED;
this.hideOtherHandlesOnDrag = this.options?.hideOtherHandlesOnDrag ?? true;
this.hideOtherControlsInstancesOnDrag = this.options?.hideOtherControlsInstancesOnDrag ?? true;
this.showHelperPlane = this.options?.showHelperPlane ?? false;
this.highlightAxis = this.options?.highlightAxis ?? true;
this.useComputedBounds = this.options?.useComputedBounds ?? false;
this.snapTranslation = this.options?.snapTranslation ?? {
x: false,
y: false,
z: false,
};
this.separation = this.options?.separation ?? DEFAULT_CONTROLS_SEPARATION;
this.isDampingEnabled = this.options?.isDampingEnabled ?? true;
this.rotationRadiusScale = this.options?.rotationRadiusScale ?? DEFAULT_ROTATION_RADIUS_SCALE;
this.eyeRotationRadiusScale =
this.options?.eyeRotationRadiusScale ?? DEFAULT_EYE_ROTATION_SCALE;
this.pickPlaneSizeScale = this.options?.pickPlaneSizeScale ?? DEFAULT_PLANE_SIZE_SCALE;
this.translationDistanceScale =
this.options?.translationDistanceScale ?? DEFAULT_TRANSLATION_DISTANCE_SCALE;
if (this.options.orientation !== undefined) {
const { x, y, z, w } = this.options.orientation;
this.initialSelfQuaternion.set(x, y, z, w).normalize();
this.quaternion.copy(this.initialSelfQuaternion);
}
this.computeObjectBounds();
this.pick = new Pick();
this.pickPlaneXY = new PickPlane(
"yellow",
this.boundingSphereRadius * this.pickPlaneSizeScale,
this.boundingSphereRadius * this.pickPlaneSizeScale
);
this.pickPlaneYZ = new PickPlane(
"cyan",
this.boundingSphereRadius * this.pickPlaneSizeScale,
this.boundingSphereRadius * this.pickPlaneSizeScale
);
this.pickPlaneZX = new PickPlane(
"pink",
this.boundingSphereRadius * this.pickPlaneSizeScale,
this.boundingSphereRadius * this.pickPlaneSizeScale
);
this.translationXP = new Translation("red");
this.translationYP = new Translation("green");
this.translationZP = new Translation("blue");
this.translationXN = new Translation("red");
this.translationYN = new Translation("green");
this.translationZN = new Translation("blue");
this.rotationX = new Rotation("red", this.boundingSphereRadius * this.rotationRadiusScale);
this.rotationY = new Rotation("green", this.boundingSphereRadius * this.rotationRadiusScale);
this.rotationZ = new Rotation("blue", this.boundingSphereRadius * this.rotationRadiusScale);
this.rotationEye = new RotationEye(
"yellow",
this.boundingSphereRadius * this.eyeRotationRadiusScale
);
this.setupDefaultTranslation();
this.setupDefaultRotation();
this.setupDefaultEyeRotation();
this.setupDefaultPickPlane();
this.setupDefaultPick();
}
private setupDefaultPickPlane = () => {
this.pickPlaneXY.name = DEFAULT_HANDLE_GROUP_NAME.PICK_PLANE_XY;
this.pickPlaneYZ.name = DEFAULT_HANDLE_GROUP_NAME.PICK_PLANE_YZ;
this.pickPlaneZX.name = DEFAULT_HANDLE_GROUP_NAME.PICK_PLANE_ZX;
this.pickPlaneYZ.up = new Vector3(1, 0, 0);
this.pickPlaneZX.up = new Vector3(0, 1, 0);
this.pickPlaneXY.up = new Vector3(0, 0, 1);
this.pickPlaneYZ.rotateY(Math.PI / 2);
this.pickPlaneZX.rotateX(Math.PI / 2);
this.setupHandle(this.pickPlaneXY);
this.setupHandle(this.pickPlaneYZ);
this.setupHandle(this.pickPlaneZX);
};
public setupHandle = (handle: IHandle) => {
this.handles.add(handle);
this.add(handle);
};
private setupDefaultPick = () => {
this.pick.name = DEFAULT_HANDLE_GROUP_NAME.PICK;
this.setupHandle(this.pick);
};
private setupDefaultEyeRotation = () => {
this.rotationEye.name = DEFAULT_HANDLE_GROUP_NAME.ER;
this.rotationEye.camera = this.camera;
this.setupHandle(this.rotationEye);
};
private setupDefaultTranslation = () => {
this.translationXP.name = DEFAULT_HANDLE_GROUP_NAME.XPT;
this.translationYP.name = DEFAULT_HANDLE_GROUP_NAME.YPT;
this.translationZP.name = DEFAULT_HANDLE_GROUP_NAME.ZPT;
this.translationXN.name = DEFAULT_HANDLE_GROUP_NAME.XNT;
this.translationYN.name = DEFAULT_HANDLE_GROUP_NAME.YNT;
this.translationZN.name = DEFAULT_HANDLE_GROUP_NAME.ZNT;
this.translationXP.translateX(this.boundingSphereRadius * this.translationDistanceScale);
this.translationYP.translateY(this.boundingSphereRadius * this.translationDistanceScale);
this.translationZP.translateZ(this.boundingSphereRadius * this.translationDistanceScale);
this.translationXN.translateX(-this.boundingSphereRadius * this.translationDistanceScale);
this.translationYN.translateY(-this.boundingSphereRadius * this.translationDistanceScale);
this.translationZN.translateZ(-this.boundingSphereRadius * this.translationDistanceScale);
this.translationXP.rotateZ(-Math.PI / 2);
this.translationZP.rotateX(Math.PI / 2);
this.translationXN.rotateZ(Math.PI / 2);
this.translationYN.rotateX(Math.PI);
this.translationZN.rotateX(-Math.PI / 2);
this.translationXP.up = new Vector3(0, 1, 0);
this.translationYP.up = new Vector3(0, 0, 1);
this.translationZP.up = new Vector3(0, 1, 0);
this.translationXN.up = new Vector3(0, -1, 0);
this.translationYN.up = new Vector3(0, 0, -1);
this.translationZN.up = new Vector3(0, -1, 0);
this.translationXP.parallel = new Vector3(1, 0, 0);
this.translationYP.parallel = new Vector3(0, 1, 0);
this.translationZP.parallel = new Vector3(0, 0, 1);
this.translationXN.parallel = new Vector3(-1, 0, 0);
this.translationYN.parallel = new Vector3(0, -1, 0);
this.translationZN.parallel = new Vector3(0, 0, -1);
this.setupHandle(this.translationXP);
this.setupHandle(this.translationYP);
this.setupHandle(this.translationZP);
this.setupHandle(this.translationXN);
this.setupHandle(this.translationYN);
this.setupHandle(this.translationZN);
};
private setupDefaultRotation = () => {
this.rotationX.name = DEFAULT_HANDLE_GROUP_NAME.XR;
this.rotationY.name = DEFAULT_HANDLE_GROUP_NAME.YR;
this.rotationZ.name = DEFAULT_HANDLE_GROUP_NAME.ZR;
this.rotationX.up = new Vector3(1, 0, 0);
this.rotationY.up = new Vector3(0, 1, 0);
this.rotationZ.up = new Vector3(0, 0, 1);
this.rotationY.rotateX(Math.PI / 2);
this.rotationX.rotateY(Math.PI / 2);
this.rotationX.rotateZ(Math.PI);
this.setupHandle(this.rotationX);
this.setupHandle(this.rotationY);
this.setupHandle(this.rotationZ);
};
private computeObjectBounds = () => {
if (this.useComputedBounds) {
if (this.object.type === "Mesh") {
const geometry = (this.object as Mesh).geometry;
geometry.computeBoundingSphere();
const { boundingSphere } = geometry;
const radius = boundingSphere?.radius ?? 0;
this.boundingSphereRadius = radius / 2 + this.separation;
return;
} else {
console.warn(
`Bounds can only be computed for object of type THREE.Mesh,
received object with type: ${this.object.type}. Falling back to using
default separation.
`
);
}
}
this.boundingSphereRadius = this.separation;
};
/**
* Puts a limit on the object's translation anchored at the current position.
*
* `{ x: [-1, 2], y: false, z: false }` - sets the translation limit to `-1` unit
* in the -x-direction, `+2` units in the +x-direction, and no limit on the
* y and z-direction.
*
* Setting the limit to `false` disables the limit in all directions.
* @param limit
*/
public setTranslationLimit = (limit: TranslationLimit | false) => {
this.translationLimit = limit;
this.translationAnchor = limit ? this.position.clone() : null;
};
/**
* @internal
*/
processDragStart = (args: { point: Vector3; handle: IHandle }) => {
const { point, handle } = args;
this.dragStartPoint.copy(point);
this.dragIncrementalStartPoint.copy(point);
this.isBeingDraggedTranslation =
handle instanceof TranslationGroup ||
handle instanceof PickGroup ||
handle instanceof PickPlaneGroup;
this.isBeingDraggedRotation = handle instanceof RotationGroup;
};
/**
* @internal
*/
processDragEnd = (args: { handle: IHandle }) => {
const { handle } = args;
const { x: xSnap, y: ySnap, z: zSnap } = this.snapTranslation;
const snap = [xSnap, ySnap, zSnap];
if (
handle instanceof TranslationGroup ||
handle instanceof PickPlaneGroup ||
handle instanceof PickGroup
) {
const xyz = this.object.position.toArray();
const floor = xyz.map(Math.floor);
const ceil = xyz.map(Math.ceil);
const snapFloor = xyz.map((p, index) => ceil[index] - p >= p - floor[index]);
const position = xyz.map((p, index) => {
if (!snap[index]) {
return p;
}
return snapFloor[index] ? floor[index] : ceil[index];
});
this.object.position.fromArray(position);
}
this.isBeingDraggedTranslation = false;
this.isBeingDraggedRotation = false;
};
/**
* Only takes effect if [[IControlsOptions.isDampingEnabled]] is true.
* @param dampingFactor - value between 0 and 1, acts like a weight on the controls
*/
public setDampingFactor = (dampingFactor = 0) =>
(this.dampingFactor = MathUtils.clamp(dampingFactor, 0, 1));
/**
* @internal
*/
processDrag = (args: { point: Vector3; handle: IHandle; dragRatio?: number }) => {
const { point, handle, dragRatio = 1 } = args;
const k = Math.exp(-this.dampingFactor * Math.abs(dragRatio ** 3));
if (handle instanceof TranslationGroup) {
this.deltaPosition.copy(point).sub(this.dragIncrementalStartPoint);
this.normalizedHandleParallelVectorCache
.copy(handle.parallel.normalize())
.applyQuaternion(this.quaternion);
const delta = this.deltaPosition.dot(this.normalizedHandleParallelVectorCache);
this.deltaPosition
.copy(this.normalizedHandleParallelVectorCache)
.multiplyScalar(this.isDampingEnabled ? k * delta : delta);
this.position.copy(this.getLimitedTranslation(this.deltaPosition));
} else if (handle instanceof PickGroup || handle instanceof PickPlaneGroup) {
this.deltaPosition
.copy(point)
.sub(this.dragIncrementalStartPoint)
.multiplyScalar(this.isDampingEnabled ? k : 1);
this.position.copy(this.getLimitedTranslation(this.deltaPosition));
} else {
this.touch1.copy(this.dragIncrementalStartPoint).sub(this.objectWorldPosition).normalize();
this.touch2.copy(point).sub(this.objectWorldPosition).normalize();
this.handleTargetQuaternion.setFromUnitVectors(this.touch1, this.touch2);
if (this.mode === ANCHOR_MODE.FIXED) {
this.detachHandleUpdateQuaternionAttach(handle, this.handleTargetQuaternion);
}
}
this.objectTargetQuaternion.premultiply(this.handleTargetQuaternion);
this.dragIncrementalStartPoint.copy(point);
};
private getLimitedTranslation = (translation: Vector3) => {
const position = translation.add(this.position);
if (!this.translationAnchor || !this.translationLimit) {
return position;
}
const { x: xLimit, y: yLimit, z: zLimit } = this.translationLimit;
const { x: xAnchor, y: yAnchor, z: zAnchor } = this.translationAnchor;
const { x, y, z } = position;
this.minTranslationCache.set(
xLimit ? xAnchor + xLimit[0] : x,
yLimit ? yAnchor + yLimit[0] : y,
zLimit ? zAnchor + zLimit[0] : z
);
this.maxTranslationCache.set(
xLimit ? xAnchor + xLimit[1] : x,
yLimit ? yAnchor + yLimit[1] : y,
zLimit ? zAnchor + zLimit[1] : z
);
return position.clamp(this.minTranslationCache, this.maxTranslationCache);
};
private detachObjectUpdatePositionAttach = (parent: Object3D | null, object: Object3D) => {
if (parent !== null && this.parent !== null && this.parent.parent !== null) {
const scene = this.parent.parent;
if (scene.type !== "Scene") {
throw new Error("freeform controls must be attached to the scene");
}
scene.attach(object);
object.position.copy(this.objectTargetPosition);
parent.attach(object);
}
};
private detachHandleUpdateQuaternionAttach = (handle: IHandle, quaternion: Quaternion) => {
if (this.parent !== null && this.parent.parent) {
const scene = this.parent.parent;
if (scene.type !== "Scene") {
throw new Error("freeform controls must be attached to the scene");
}
scene.attach(handle);
handle.applyQuaternion(quaternion);
this.attach(handle);
}
};
/**
* Applies supplied visibility to the supplied handle names.
* Individual handle's visibility can also be changed by modifying the `visibility`
* property on the handle directly.
* @param handleNames
* @param visibility
*/
public showByNames = (
handleNames: Array<DEFAULT_HANDLE_GROUP_NAME | string>,
visibility = true
) => {
const handleNamesMap: { [name: string]: IHandle | undefined } = {};
this.handles.forEach((handle) => {
handleNamesMap[handle.name] = handle;
});
handleNames.map((handleName) => {
const handle = handleNamesMap[handleName];
if (handle === undefined) {
throw new Error(`handle: ${handleName} not found`);
}
handle.visible = visibility;
});
};
/**
* Applies supplied visibility to all handles
* @param visibility
*/
public showAll = (visibility = true) => {
this.handles.forEach((handle) => {
handle.visible = visibility;
});
};
/**
* @internal
*/
public getInteractiveObjects(): Object3D[] {
const interactiveObjects: Object3D[] = [];
this.handles.forEach((handle) => {
if (!handle.visible) {
return;
}
interactiveObjects.push(...handle.getInteractiveObjects());
});
return interactiveObjects;
}
/**
* @internal
*/
updateMatrixWorld = (force?: boolean) => {
this.object.updateMatrixWorld(force);
this.object.getWorldPosition(this.objectWorldPosition);
const parent = this.object.parent;
if (parent !== null) {
parent.matrixWorld.decompose(
this.objectParentWorldPosition,
this.objectParentWorldQuaternion,
this.objectParentWorldScale
);
}
this.objectParentWorldQuaternion.invert();
this.objectTargetPosition.copy(this.position);
this.objectTargetQuaternion.premultiply(this.objectParentWorldQuaternion);
if (this.isBeingDraggedTranslation) {
this.detachObjectUpdatePositionAttach(parent, this.object);
} else if (this.isBeingDraggedRotation) {
this.object.quaternion.copy(this.objectTargetQuaternion);
this.detachObjectUpdatePositionAttach(parent, this.object);
} else {
this.position.copy(this.objectWorldPosition);
}
this.object.getWorldQuaternion(this.objectTargetQuaternion);
if (this.mode === ANCHOR_MODE.INHERIT && !this.isBeingDraggedTranslation) {
this.quaternion.copy(this.initialSelfQuaternion).premultiply(this.objectTargetQuaternion);
}
super.updateMatrixWorld(force);
};
}