-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
/
Copy pathTransformControls.js
1868 lines (1273 loc) · 46.4 KB
/
TransformControls.js
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
BoxGeometry,
BufferGeometry,
Controls,
CylinderGeometry,
DoubleSide,
Euler,
Float32BufferAttribute,
Line,
LineBasicMaterial,
Matrix4,
Mesh,
MeshBasicMaterial,
Object3D,
OctahedronGeometry,
PlaneGeometry,
Quaternion,
Raycaster,
SphereGeometry,
TorusGeometry,
Vector3
} from 'three';
const _raycaster = new Raycaster();
const _tempVector = new Vector3();
const _tempVector2 = new Vector3();
const _tempQuaternion = new Quaternion();
const _unit = {
X: new Vector3( 1, 0, 0 ),
Y: new Vector3( 0, 1, 0 ),
Z: new Vector3( 0, 0, 1 )
};
/**
* Fires if any type of change (object or property change) is performed. Property changes
* are separate events you can add event listeners to. The event type is "propertyname-changed".
*
* @event TransformControls#change
* @type {Object}
*/
const _changeEvent = { type: 'change' };
/**
* Fires if a pointer (mouse/touch) becomes active.
*
* @event TransformControls#mouseDown
* @type {Object}
*/
const _mouseDownEvent = { type: 'mouseDown', mode: null };
/**
* Fires if a pointer (mouse/touch) is no longer active.
*
* @event TransformControls#mouseUp
* @type {Object}
*/
const _mouseUpEvent = { type: 'mouseUp', mode: null };
/**
* Fires if the controlled 3D object is changed.
*
* @event TransformControls#objectChange
* @type {Object}
*/
const _objectChangeEvent = { type: 'objectChange' };
/**
* This class can be used to transform objects in 3D space by adapting a similar interaction model
* of DCC tools like Blender. Unlike other controls, it is not intended to transform the scene's camera.
*
* `TransformControls` expects that its attached 3D object is part of the scene graph.
*
* @augments Controls
*/
class TransformControls extends Controls {
/**
* Constructs a new controls instance.
*
* @param {Camera} camera - The camera of the rendered scene.
* @param {?HTMLDOMElement} domElement - The HTML element used for event listeners.
*/
constructor( camera, domElement = null ) {
super( undefined, domElement );
const root = new TransformControlsRoot( this );
this._root = root;
const gizmo = new TransformControlsGizmo();
this._gizmo = gizmo;
root.add( gizmo );
const plane = new TransformControlsPlane();
this._plane = plane;
root.add( plane );
const scope = this;
// Defined getter, setter and store for a property
function defineProperty( propName, defaultValue ) {
let propValue = defaultValue;
Object.defineProperty( scope, propName, {
get: function () {
return propValue !== undefined ? propValue : defaultValue;
},
set: function ( value ) {
if ( propValue !== value ) {
propValue = value;
plane[ propName ] = value;
gizmo[ propName ] = value;
scope.dispatchEvent( { type: propName + '-changed', value: value } );
scope.dispatchEvent( _changeEvent );
}
}
} );
scope[ propName ] = defaultValue;
plane[ propName ] = defaultValue;
gizmo[ propName ] = defaultValue;
}
// Define properties with getters/setter
// Setting the defined property will automatically trigger change event
// Defined properties are passed down to gizmo and plane
/**
* The camera of the rendered scene.
*
* @name TransformControls#camera
* @type {Camera}
*/
defineProperty( 'camera', camera );
defineProperty( 'object', undefined );
defineProperty( 'enabled', true );
/**
* The current transformation axis.
*
* @name TransformControls#axis
* @type {string}
*/
defineProperty( 'axis', null );
/**
* The current transformation axis.
*
* @name TransformControls#mode
* @type {('translate'|'rotate'|'scale')}
* @default 'translate'
*/
defineProperty( 'mode', 'translate' );
/**
* By default, 3D objects are continuously translated. If you set this property to a numeric
* value (world units), you can define in which steps the 3D object should be translated.
*
* @name TransformControls#translationSnap
* @type {?number}
* @default null
*/
defineProperty( 'translationSnap', null );
/**
* By default, 3D objects are continuously rotated. If you set this property to a numeric
* value (radians), you can define in which steps the 3D object should be rotated.
*
* @name TransformControls#rotationSnap
* @type {?number}
* @default null
*/
defineProperty( 'rotationSnap', null );
/**
* By default, 3D objects are continuously scaled. If you set this property to a numeric
* value, you can define in which steps the 3D object should be scaled.
*
* @name TransformControls#scaleSnap
* @type {?number}
* @default null
*/
defineProperty( 'scaleSnap', null );
/**
* Defines in which coordinate space transformations should be performed.
*
* @name TransformControls#space
* @type {('world'|'local')}
* @default 'world'
*/
defineProperty( 'space', 'world' );
/**
* The size of the helper UI (axes/planes).
*
* @name TransformControls#size
* @type {number}
* @default 1
*/
defineProperty( 'size', 1 );
/**
* Whether dragging is currently performed or not.
*
* @name TransformControls#dragging
* @type {boolean}
* @readonly
* @default false
*/
defineProperty( 'dragging', false );
/**
* Whether the x-axis helper should be visible or not.
*
* @name TransformControls#showX
* @type {boolean}
* @default true
*/
defineProperty( 'showX', true );
/**
* Whether the y-axis helper should be visible or not.
*
* @name TransformControls#showY
* @type {boolean}
* @default true
*/
defineProperty( 'showY', true );
/**
* Whether the z-axis helper should be visible or not.
*
* @name TransformControls#showZ
* @type {boolean}
* @default true
*/
defineProperty( 'showZ', true );
/**
* The minimum allowed X position during translation.
*
* @name TransformControls#minX
* @type {number}
* @default -Infinity
*/
defineProperty( 'minX', - Infinity );
/**
* The maximum allowed X position during translation.
*
* @name TransformControls#maxX
* @type {number}
* @default Infinity
*/
defineProperty( 'maxX', Infinity );
/**
* The minimum allowed y position during translation.
*
* @name TransformControls#minY
* @type {number}
* @default -Infinity
*/
defineProperty( 'minY', - Infinity );
/**
* The maximum allowed Y position during translation.
*
* @name TransformControls#maxY
* @type {number}
* @default Infinity
*/
defineProperty( 'maxY', Infinity );
/**
* The minimum allowed z position during translation.
*
* @name TransformControls#minZ
* @type {number}
* @default -Infinity
*/
defineProperty( 'minZ', - Infinity );
/**
* The maximum allowed Z position during translation.
*
* @name TransformControls#maxZ
* @type {number}
* @default Infinity
*/
defineProperty( 'maxZ', Infinity );
// Reusable utility variables
const worldPosition = new Vector3();
const worldPositionStart = new Vector3();
const worldQuaternion = new Quaternion();
const worldQuaternionStart = new Quaternion();
const cameraPosition = new Vector3();
const cameraQuaternion = new Quaternion();
const pointStart = new Vector3();
const pointEnd = new Vector3();
const rotationAxis = new Vector3();
const rotationAngle = 0;
const eye = new Vector3();
// TODO: remove properties unused in plane and gizmo
defineProperty( 'worldPosition', worldPosition );
defineProperty( 'worldPositionStart', worldPositionStart );
defineProperty( 'worldQuaternion', worldQuaternion );
defineProperty( 'worldQuaternionStart', worldQuaternionStart );
defineProperty( 'cameraPosition', cameraPosition );
defineProperty( 'cameraQuaternion', cameraQuaternion );
defineProperty( 'pointStart', pointStart );
defineProperty( 'pointEnd', pointEnd );
defineProperty( 'rotationAxis', rotationAxis );
defineProperty( 'rotationAngle', rotationAngle );
defineProperty( 'eye', eye );
this._offset = new Vector3();
this._startNorm = new Vector3();
this._endNorm = new Vector3();
this._cameraScale = new Vector3();
this._parentPosition = new Vector3();
this._parentQuaternion = new Quaternion();
this._parentQuaternionInv = new Quaternion();
this._parentScale = new Vector3();
this._worldScaleStart = new Vector3();
this._worldQuaternionInv = new Quaternion();
this._worldScale = new Vector3();
this._positionStart = new Vector3();
this._quaternionStart = new Quaternion();
this._scaleStart = new Vector3();
this._getPointer = getPointer.bind( this );
this._onPointerDown = onPointerDown.bind( this );
this._onPointerHover = onPointerHover.bind( this );
this._onPointerMove = onPointerMove.bind( this );
this._onPointerUp = onPointerUp.bind( this );
if ( domElement !== null ) {
this.connect();
}
}
connect() {
this.domElement.addEventListener( 'pointerdown', this._onPointerDown );
this.domElement.addEventListener( 'pointermove', this._onPointerHover );
this.domElement.addEventListener( 'pointerup', this._onPointerUp );
this.domElement.style.touchAction = 'none'; // disable touch scroll
}
disconnect() {
this.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
this.domElement.removeEventListener( 'pointermove', this._onPointerHover );
this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
this.domElement.removeEventListener( 'pointerup', this._onPointerUp );
this.domElement.style.touchAction = 'auto';
}
/**
* Returns the visual representation of the controls. Add the helper to your scene to
* visually transform the attached 3D object.
*
* @return {TransformControlsRoot} The helper.
*/
getHelper() {
return this._root;
}
pointerHover( pointer ) {
if ( this.object === undefined || this.dragging === true ) return;
if ( pointer !== null ) _raycaster.setFromCamera( pointer, this.camera );
const intersect = intersectObjectWithRay( this._gizmo.picker[ this.mode ], _raycaster );
if ( intersect ) {
this.axis = intersect.object.name;
} else {
this.axis = null;
}
}
pointerDown( pointer ) {
if ( this.object === undefined || this.dragging === true || ( pointer != null && pointer.button !== 0 ) ) return;
if ( this.axis !== null ) {
if ( pointer !== null ) _raycaster.setFromCamera( pointer, this.camera );
const planeIntersect = intersectObjectWithRay( this._plane, _raycaster, true );
if ( planeIntersect ) {
this.object.updateMatrixWorld();
this.object.parent.updateMatrixWorld();
this._positionStart.copy( this.object.position );
this._quaternionStart.copy( this.object.quaternion );
this._scaleStart.copy( this.object.scale );
this.object.matrixWorld.decompose( this.worldPositionStart, this.worldQuaternionStart, this._worldScaleStart );
this.pointStart.copy( planeIntersect.point ).sub( this.worldPositionStart );
}
this.dragging = true;
_mouseDownEvent.mode = this.mode;
this.dispatchEvent( _mouseDownEvent );
}
}
pointerMove( pointer ) {
const axis = this.axis;
const mode = this.mode;
const object = this.object;
let space = this.space;
if ( mode === 'scale' ) {
space = 'local';
} else if ( axis === 'E' || axis === 'XYZE' || axis === 'XYZ' ) {
space = 'world';
}
if ( object === undefined || axis === null || this.dragging === false || ( pointer !== null && pointer.button !== - 1 ) ) return;
if ( pointer !== null ) _raycaster.setFromCamera( pointer, this.camera );
const planeIntersect = intersectObjectWithRay( this._plane, _raycaster, true );
if ( ! planeIntersect ) return;
this.pointEnd.copy( planeIntersect.point ).sub( this.worldPositionStart );
if ( mode === 'translate' ) {
// Apply translate
this._offset.copy( this.pointEnd ).sub( this.pointStart );
if ( space === 'local' && axis !== 'XYZ' ) {
this._offset.applyQuaternion( this._worldQuaternionInv );
}
if ( axis.indexOf( 'X' ) === - 1 ) this._offset.x = 0;
if ( axis.indexOf( 'Y' ) === - 1 ) this._offset.y = 0;
if ( axis.indexOf( 'Z' ) === - 1 ) this._offset.z = 0;
if ( space === 'local' && axis !== 'XYZ' ) {
this._offset.applyQuaternion( this._quaternionStart ).divide( this._parentScale );
} else {
this._offset.applyQuaternion( this._parentQuaternionInv ).divide( this._parentScale );
}
object.position.copy( this._offset ).add( this._positionStart );
// Apply translation snap
if ( this.translationSnap ) {
if ( space === 'local' ) {
object.position.applyQuaternion( _tempQuaternion.copy( this._quaternionStart ).invert() );
if ( axis.search( 'X' ) !== - 1 ) {
object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
}
if ( axis.search( 'Y' ) !== - 1 ) {
object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
}
if ( axis.search( 'Z' ) !== - 1 ) {
object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
}
object.position.applyQuaternion( this._quaternionStart );
}
if ( space === 'world' ) {
if ( object.parent ) {
object.position.add( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
}
if ( axis.search( 'X' ) !== - 1 ) {
object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap;
}
if ( axis.search( 'Y' ) !== - 1 ) {
object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap;
}
if ( axis.search( 'Z' ) !== - 1 ) {
object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap;
}
if ( object.parent ) {
object.position.sub( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) );
}
}
}
object.position.x = Math.max( this.minX, Math.min( this.maxX, object.position.x ) );
object.position.y = Math.max( this.minY, Math.min( this.maxY, object.position.y ) );
object.position.z = Math.max( this.minZ, Math.min( this.maxZ, object.position.z ) );
} else if ( mode === 'scale' ) {
if ( axis.search( 'XYZ' ) !== - 1 ) {
let d = this.pointEnd.length() / this.pointStart.length();
if ( this.pointEnd.dot( this.pointStart ) < 0 ) d *= - 1;
_tempVector2.set( d, d, d );
} else {
_tempVector.copy( this.pointStart );
_tempVector2.copy( this.pointEnd );
_tempVector.applyQuaternion( this._worldQuaternionInv );
_tempVector2.applyQuaternion( this._worldQuaternionInv );
_tempVector2.divide( _tempVector );
if ( axis.search( 'X' ) === - 1 ) {
_tempVector2.x = 1;
}
if ( axis.search( 'Y' ) === - 1 ) {
_tempVector2.y = 1;
}
if ( axis.search( 'Z' ) === - 1 ) {
_tempVector2.z = 1;
}
}
// Apply scale
object.scale.copy( this._scaleStart ).multiply( _tempVector2 );
if ( this.scaleSnap ) {
if ( axis.search( 'X' ) !== - 1 ) {
object.scale.x = Math.round( object.scale.x / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
}
if ( axis.search( 'Y' ) !== - 1 ) {
object.scale.y = Math.round( object.scale.y / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
}
if ( axis.search( 'Z' ) !== - 1 ) {
object.scale.z = Math.round( object.scale.z / this.scaleSnap ) * this.scaleSnap || this.scaleSnap;
}
}
} else if ( mode === 'rotate' ) {
this._offset.copy( this.pointEnd ).sub( this.pointStart );
const ROTATION_SPEED = 20 / this.worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) );
let _inPlaneRotation = false;
if ( axis === 'XYZE' ) {
this.rotationAxis.copy( this._offset ).cross( this.eye ).normalize();
this.rotationAngle = this._offset.dot( _tempVector.copy( this.rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED;
} else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) {
this.rotationAxis.copy( _unit[ axis ] );
_tempVector.copy( _unit[ axis ] );
if ( space === 'local' ) {
_tempVector.applyQuaternion( this.worldQuaternion );
}
_tempVector.cross( this.eye );
// When _tempVector is 0 after cross with this.eye the vectors are parallel and should use in-plane rotation logic.
if ( _tempVector.length() === 0 ) {
_inPlaneRotation = true;
} else {
this.rotationAngle = this._offset.dot( _tempVector.normalize() ) * ROTATION_SPEED;
}
}
if ( axis === 'E' || _inPlaneRotation ) {
this.rotationAxis.copy( this.eye );
this.rotationAngle = this.pointEnd.angleTo( this.pointStart );
this._startNorm.copy( this.pointStart ).normalize();
this._endNorm.copy( this.pointEnd ).normalize();
this.rotationAngle *= ( this._endNorm.cross( this._startNorm ).dot( this.eye ) < 0 ? 1 : - 1 );
}
// Apply rotation snap
if ( this.rotationSnap ) this.rotationAngle = Math.round( this.rotationAngle / this.rotationSnap ) * this.rotationSnap;
// Apply rotate
if ( space === 'local' && axis !== 'E' && axis !== 'XYZE' ) {
object.quaternion.copy( this._quaternionStart );
object.quaternion.multiply( _tempQuaternion.setFromAxisAngle( this.rotationAxis, this.rotationAngle ) ).normalize();
} else {
this.rotationAxis.applyQuaternion( this._parentQuaternionInv );
object.quaternion.copy( _tempQuaternion.setFromAxisAngle( this.rotationAxis, this.rotationAngle ) );
object.quaternion.multiply( this._quaternionStart ).normalize();
}
}
this.dispatchEvent( _changeEvent );
this.dispatchEvent( _objectChangeEvent );
}
pointerUp( pointer ) {
if ( pointer !== null && pointer.button !== 0 ) return;
if ( this.dragging && ( this.axis !== null ) ) {
_mouseUpEvent.mode = this.mode;
this.dispatchEvent( _mouseUpEvent );
}
this.dragging = false;
this.axis = null;
}
dispose() {
this.disconnect();
this._root.dispose();
}
/**
* Sets the 3D object that should be transformed and ensures the controls UI is visible.
*
* @param {Object3D} object - The 3D object that should be transformed.
* @return {TransformControls} A reference to this controls.
*/
attach( object ) {
this.object = object;
this._root.visible = true;
return this;
}
/**
* Removes the current 3D object from the controls and makes the helper UI invisible.
*
* @return {TransformControls} A reference to this controls.
*/
detach() {
this.object = undefined;
this.axis = null;
this._root.visible = false;
return this;
}
/**
* Resets the object's position, rotation and scale to when the current transform began.
*/
reset() {
if ( ! this.enabled ) return;
if ( this.dragging ) {
this.object.position.copy( this._positionStart );
this.object.quaternion.copy( this._quaternionStart );
this.object.scale.copy( this._scaleStart );
this.dispatchEvent( _changeEvent );
this.dispatchEvent( _objectChangeEvent );
this.pointStart.copy( this.pointEnd );
}
}
/**
* Returns the raycaster that is used for user interaction. This object is shared between all
* instances of `TransformControls`.
*
* @returns {Raycaster} The internal raycaster.
*/
getRaycaster() {
return _raycaster;
}
/**
* Returns the transformation mode.
*
* @returns {'translate'|'rotate'|'scale'} The transformation mode.
*/
getMode() {
return this.mode;
}
/**
* Sets the given transformation mode.
*
* @param {'translate'|'rotate'|'scale'} mode - The transformation mode to set.
*/
setMode( mode ) {
this.mode = mode;
}
/**
* Sets the translation snap.
*
* @param {?number} translationSnap - The translation snap to set.
*/
setTranslationSnap( translationSnap ) {
this.translationSnap = translationSnap;
}
/**
* Sets the rotation snap.
*
* @param {?number} rotationSnap - The rotation snap to set.
*/
setRotationSnap( rotationSnap ) {
this.rotationSnap = rotationSnap;
}
/**
* Sets the scale snap.
*
* @param {?number} scaleSnap - The scale snap to set.
*/
setScaleSnap( scaleSnap ) {
this.scaleSnap = scaleSnap;
}
/**
* Sets the size of the helper UI.
*
* @param {number} size - The size to set.
*/
setSize( size ) {
this.size = size;
}
/**
* Sets the coordinate space in which transformations are applied.
*
* @param {'world'|'local'} space - The space to set.
*/
setSpace( space ) {
this.space = space;
}
}
// mouse / touch event handlers
function getPointer( event ) {
if ( this.domElement.ownerDocument.pointerLockElement ) {
return {
x: 0,
y: 0,
button: event.button
};
} else {
const rect = this.domElement.getBoundingClientRect();
return {
x: ( event.clientX - rect.left ) / rect.width * 2 - 1,
y: - ( event.clientY - rect.top ) / rect.height * 2 + 1,
button: event.button
};
}
}
function onPointerHover( event ) {
if ( ! this.enabled ) return;
switch ( event.pointerType ) {
case 'mouse':
case 'pen':
this.pointerHover( this._getPointer( event ) );
break;
}
}
function onPointerDown( event ) {
if ( ! this.enabled ) return;
if ( ! document.pointerLockElement ) {
this.domElement.setPointerCapture( event.pointerId );
}
this.domElement.addEventListener( 'pointermove', this._onPointerMove );
this.pointerHover( this._getPointer( event ) );
this.pointerDown( this._getPointer( event ) );
}
function onPointerMove( event ) {
if ( ! this.enabled ) return;
this.pointerMove( this._getPointer( event ) );
}
function onPointerUp( event ) {
if ( ! this.enabled ) return;
this.domElement.releasePointerCapture( event.pointerId );
this.domElement.removeEventListener( 'pointermove', this._onPointerMove );
this.pointerUp( this._getPointer( event ) );
}
function intersectObjectWithRay( object, raycaster, includeInvisible ) {
const allIntersections = raycaster.intersectObject( object, true );
for ( let i = 0; i < allIntersections.length; i ++ ) {
if ( allIntersections[ i ].object.visible || includeInvisible ) {
return allIntersections[ i ];
}
}
return false;
}
//
// Reusable utility variables
const _tempEuler = new Euler();
const _alignVector = new Vector3( 0, 1, 0 );
const _zeroVector = new Vector3( 0, 0, 0 );
const _lookAtMatrix = new Matrix4();
const _tempQuaternion2 = new Quaternion();
const _identityQuaternion = new Quaternion();
const _dirVector = new Vector3();
const _tempMatrix = new Matrix4();
const _unitX = new Vector3( 1, 0, 0 );
const _unitY = new Vector3( 0, 1, 0 );