-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathrotate-gizmo.js
534 lines (464 loc) · 15.1 KB
/
rotate-gizmo.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
import { math } from '../../core/math/math.js';
import { Color } from '../../core/math/color.js';
import { Quat } from '../../core/math/quat.js';
import { Mat4 } from '../../core/math/mat4.js';
import { Vec3 } from '../../core/math/vec3.js';
import { PROJECTION_PERSPECTIVE } from '../../scene/constants.js';
import { ArcShape } from './shape/arc-shape.js';
import { GIZMOSPACE_LOCAL, GIZMOAXIS_FACE, GIZMOAXIS_X, GIZMOAXIS_Y, GIZMOAXIS_Z } from './constants.js';
import { TransformGizmo } from './transform-gizmo.js';
/**
* @import { CameraComponent } from '../../framework/components/camera/component.js'
* @import { GraphNode } from '../../scene/graph-node.js'
* @import { Layer } from '../../scene/layer.js'
*/
// temporary variables
const tmpV1 = new Vec3();
const tmpV2 = new Vec3();
const tmpV3 = new Vec3();
const tmpV4 = new Vec3();
const tmpM1 = new Mat4();
const tmpQ1 = new Quat();
const tmpQ2 = new Quat();
// constants
const FACING_THRESHOLD = 0.9;
const GUIDE_ANGLE_COLOR = new Color(0, 0, 0, 0.3);
/**
* Rotation gizmo.
*
* @category Gizmo
*/
class RotateGizmo extends TransformGizmo {
_shapes = {
z: new ArcShape(this._device, {
axis: GIZMOAXIS_Z,
layers: [this._layer.id],
shading: this._shading,
rotation: new Vec3(90, 0, 90),
defaultColor: this._meshColors.axis.z,
hoverColor: this._meshColors.hover.z,
sectorAngle: 180
}),
x: new ArcShape(this._device, {
axis: GIZMOAXIS_X,
layers: [this._layer.id],
shading: this._shading,
rotation: new Vec3(0, 0, -90),
defaultColor: this._meshColors.axis.x,
hoverColor: this._meshColors.hover.x,
sectorAngle: 180
}),
y: new ArcShape(this._device, {
axis: GIZMOAXIS_Y,
layers: [this._layer.id],
shading: this._shading,
rotation: new Vec3(0, 0, 0),
defaultColor: this._meshColors.axis.y,
hoverColor: this._meshColors.hover.y,
sectorAngle: 180
}),
face: new ArcShape(this._device, {
axis: GIZMOAXIS_FACE,
layers: [this._layer.id],
shading: this._shading,
rotation: this._getLookAtEulerAngles(this._camera.entity.getPosition()),
defaultColor: this._meshColors.axis.f,
hoverColor: this._meshColors.hover.f,
ringRadius: 0.55
})
};
/**
* Internal selection starting angle in world space.
*
* @type {number}
* @private
*/
_selectionStartAngle = 0;
/**
* Internal mapping from each attached node to their starting rotation in local space.
*
* @type {Map<GraphNode, Quat>}
* @private
*/
_nodeLocalRotations = new Map();
/**
* Internal mapping from each attached node to their starting rotation in world space.
*
* @type {Map<GraphNode, Quat>}
* @private
*/
_nodeRotations = new Map();
/**
* Internal mapping from each attached node to their offset position from the gizmo.
*
* @type {Map<GraphNode, Vec3>}
* @private
*/
_nodeOffsets = new Map();
/**
* Internal color for guide angle starting line.
*
* @type {Color}
* @private
*/
_guideAngleStartColor = GUIDE_ANGLE_COLOR.clone();
/**
* Internal vector for the start point of the guide line angle.
*
* @type {Vec3}
* @private
*/
_guideAngleStart = new Vec3();
/**
* Internal vector for the end point of the guide line angle.
*
* @type {Vec3}
* @private
*/
_guideAngleEnd = new Vec3();
/**
* @override
*/
snapIncrement = 5;
/**
* This forces the rotation to always be calculated based on the mouse position around the gizmo.
*
* @type {boolean}
*/
orbitRotation = false;
/**
* Creates a new RotateGizmo object.
*
* @param {CameraComponent} camera - The camera component.
* @param {Layer} layer - The render layer.
* @example
* const gizmo = new pc.RotateGizmo(app, camera, layer);
*/
constructor(camera, layer) {
super(camera, layer);
this._createTransform();
this.on(TransformGizmo.EVENT_TRANSFORMSTART, (point, x, y) => {
// store start angle
this._selectionStartAngle = this._calculateAngle(point, x, y);
// store initial node rotations
this._storeNodeRotations();
// store guide points
this._storeGuidePoints();
// drag handle for disk (arc <-> circle)
this._drag(true);
});
this.on(TransformGizmo.EVENT_TRANSFORMMOVE, (point, x, y) => {
const axis = this._selectedAxis;
let angleDelta = this._calculateAngle(point, x, y) - this._selectionStartAngle;
if (this.snap) {
angleDelta = Math.round(angleDelta / this.snapIncrement) * this.snapIncrement;
}
this._setNodeRotations(axis, angleDelta);
this._updateGuidePoints(angleDelta);
});
this.on(TransformGizmo.EVENT_TRANSFORMEND, () => {
this._drag(false);
});
this.on(TransformGizmo.EVENT_NODESDETACH, () => {
this._nodeLocalRotations.clear();
this._nodeRotations.clear();
this._nodeOffsets.clear();
});
this._app.on('prerender', () => {
this._shapesLookAtCamera();
if (this._dragging) {
const gizmoPos = this.root.getPosition();
this._drawGuideAngleLine(gizmoPos, this._selectedAxis,
this._guideAngleStart, this._guideAngleStartColor);
this._drawGuideAngleLine(gizmoPos, this._selectedAxis, this._guideAngleEnd);
}
});
}
/**
* Sets the XYZ tube radius.
*
* @type {number}
*/
set xyzTubeRadius(value) {
this._setDiskProp('tubeRadius', value);
}
/**
* Gets the XYZ tube radius.
*
* @type {number}
*/
get xyzTubeRadius() {
return this._shapes.x.tubeRadius;
}
/**
* Sets the XYZ ring radius.
*
* @type {number}
*/
set xyzRingRadius(value) {
this._setDiskProp('ringRadius', value);
}
/**
* Gets the XYZ ring radius.
*
* @type {number}
*/
get xyzRingRadius() {
return this._shapes.x.ringRadius;
}
/**
* Sets the face tube radius.
*
* @type {number}
*/
set faceTubeRadius(value) {
this._shapes.face.tubeRadius = value;
}
/**
* Gets the face tube radius.
*
* @type {number}
*/
get faceTubeRadius() {
return this._shapes.face.tubeRadius;
}
/**
* Sets the face ring radius.
*
* @type {number}
*/
set faceRingRadius(value) {
this._shapes.face.ringRadius = value;
}
/**
* Gets the face ring radius.
*
* @type {number}
*/
get faceRingRadius() {
return this._shapes.face.ringRadius;
}
/**
* Sets the ring tolerance.
*
* @type {number}
*/
set ringTolerance(value) {
this._setDiskProp('tolerance', value);
this._shapes.face.tolerance = value;
}
/**
* Gets the ring tolerance.
*
* @type {number}
*/
get ringTolerance() {
return this._shapes.x.tolerance;
}
/**
* @param {string} prop - The property.
* @param {any} value - The value.
* @private
*/
_setDiskProp(prop, value) {
this._shapes.x[prop] = value;
this._shapes.y[prop] = value;
this._shapes.z[prop] = value;
}
/**
* @private
*/
_storeGuidePoints() {
const gizmoPos = this.root.getPosition();
const axis = this._selectedAxis;
const isFacing = axis === GIZMOAXIS_FACE;
const scale = isFacing ? this.faceRingRadius : this.xyzRingRadius;
this._guideAngleStart.copy(this._selectionStartPoint).sub(gizmoPos).normalize();
this._guideAngleStart.mulScalar(scale);
this._guideAngleEnd.copy(this._guideAngleStart);
}
/**
* @param {number} angleDelta - The angle delta.
* @private
*/
_updateGuidePoints(angleDelta) {
const axis = this._selectedAxis;
const isFacing = axis === GIZMOAXIS_FACE;
if (isFacing) {
tmpV1.copy(this.facing);
} else {
tmpV1.set(0, 0, 0);
tmpV1[axis] = 1;
this._rootStartRot.transformVector(tmpV1, tmpV1);
}
tmpQ1.setFromAxisAngle(tmpV1, angleDelta);
tmpQ1.transformVector(this._guideAngleStart, this._guideAngleEnd);
}
/**
* @param {Vec3} pos - The position.
* @param {string} axis - The axis.
* @param {Vec3} point - The point.
* @param {Color} [color] - The color.
* @private
*/
_drawGuideAngleLine(pos, axis, point, color = this._guideColors[axis]) {
tmpV1.set(0, 0, 0);
tmpV2.copy(point).mulScalar(this._scale);
this._app.drawLine(tmpV1.add(pos), tmpV2.add(pos), color, false, this._layer);
}
/**
* @param {Vec3} position - The position.
* @returns {Vec3} The look at euler angles.
* @private
*/
_getLookAtEulerAngles(position) {
tmpV1.set(0, 0, 0);
tmpM1.setLookAt(tmpV1, position, Vec3.UP);
tmpQ1.setFromMat4(tmpM1);
tmpQ1.getEulerAngles(tmpV1);
tmpV1.x += 90;
return tmpV1;
}
/**
* @private
*/
_shapesLookAtCamera() {
// face shape
if (this._camera.projection === PROJECTION_PERSPECTIVE) {
this._shapes.face.entity.lookAt(this._camera.entity.getPosition());
this._shapes.face.entity.rotateLocal(90, 0, 0);
} else {
tmpQ1.copy(this._camera.entity.getRotation()).getEulerAngles(tmpV1);
this._shapes.face.entity.setEulerAngles(tmpV1);
this._shapes.face.entity.rotateLocal(-90, 0, 0);
}
// axes shapes
const facingDir = tmpV1.copy(this.facing);
tmpQ1.copy(this.root.getRotation()).invert().transformVector(facingDir, facingDir);
let angle = Math.atan2(facingDir.z, facingDir.y) * math.RAD_TO_DEG;
this._shapes.x.entity.setLocalEulerAngles(0, angle - 90, -90);
angle = Math.atan2(facingDir.x, facingDir.z) * math.RAD_TO_DEG;
this._shapes.y.entity.setLocalEulerAngles(0, angle, 0);
angle = Math.atan2(facingDir.y, facingDir.x) * math.RAD_TO_DEG;
this._shapes.z.entity.setLocalEulerAngles(90, 0, angle + 90);
}
/**
* @param {boolean} state - The state.
* @private
*/
_drag(state) {
for (const axis in this._shapes) {
const shape = this._shapes[axis];
if (axis === this._selectedAxis) {
shape.drag(state);
} else {
shape.hide(state);
}
}
this.fire(TransformGizmo.EVENT_RENDERUPDATE);
}
/**
* @private
*/
_storeNodeRotations() {
const gizmoPos = this.root.getPosition();
for (let i = 0; i < this.nodes.length; i++) {
const node = this.nodes[i];
this._nodeLocalRotations.set(node, node.getLocalRotation().clone());
this._nodeRotations.set(node, node.getRotation().clone());
this._nodeOffsets.set(node, node.getPosition().clone().sub(gizmoPos));
}
}
/**
* @param {string} axis - The axis.
* @param {number} angleDelta - The angle delta.
* @private
*/
_setNodeRotations(axis, angleDelta) {
const gizmoPos = this.root.getPosition();
const isFacing = axis === GIZMOAXIS_FACE;
// calculate rotation from axis and angle
tmpQ1.setFromAxisAngle(this._dirFromAxis(axis, tmpV1), angleDelta);
for (let i = 0; i < this.nodes.length; i++) {
const node = this.nodes[i];
if (!isFacing && this._coordSpace === GIZMOSPACE_LOCAL) {
const rot = this._nodeLocalRotations.get(node);
if (!rot) {
continue;
}
tmpQ2.copy(rot).mul(tmpQ1);
node.setLocalRotation(tmpQ2);
} else {
const rot = this._nodeRotations.get(node);
if (!rot) {
continue;
}
const offset = this._nodeOffsets.get(node);
if (!offset) {
continue;
}
tmpV1.copy(offset);
tmpQ1.transformVector(tmpV1, tmpV1);
tmpQ2.copy(tmpQ1).mul(rot);
// N.B. Rotation via quaternion when scale inverted causes scale warping?
node.setEulerAngles(tmpQ2.getEulerAngles());
node.setPosition(tmpV1.add(gizmoPos));
}
}
if (this._coordSpace === GIZMOSPACE_LOCAL) {
this._updateRotation();
}
}
/**
* @param {number} x - The x coordinate.
* @param {number} y - The y coordinate.
* @returns {Vec3} The point in world space.
* @protected
*/
_screenToPoint(x, y) {
const mouseWPos = this._camera.screenToWorld(x, y, 1);
const axis = this._selectedAxis;
const ray = this._createRay(mouseWPos);
const plane = this._createPlane(axis, axis === GIZMOAXIS_FACE, false);
const point = new Vec3();
plane.intersectsRay(ray, point);
return point;
}
/**
* @param {Vec3} point - The point.
* @param {number} x - The x coordinate.
* @param {number} y - The y coordinate.
* @returns {number} The angle.
* @protected
*/
_calculateAngle(point, x, y) {
const gizmoPos = this.root.getPosition();
const axis = this._selectedAxis;
const plane = this._createPlane(axis, axis === GIZMOAXIS_FACE, false);
let angle = 0;
// calculate angle
const facingDir = tmpV2.copy(this.facing);
const facingDot = plane.normal.dot(facingDir);
if (this.orbitRotation || Math.abs(facingDot) > FACING_THRESHOLD) {
// plane facing camera so based on mouse position around gizmo
tmpV1.sub2(point, gizmoPos);
// transform point so it's facing the camera
tmpQ1.copy(this._camera.entity.getRotation()).invert().transformVector(tmpV1, tmpV1);
// calculate angle
angle = Math.sign(facingDot) * Math.atan2(tmpV1.y, tmpV1.x) * math.RAD_TO_DEG;
} else {
// convert rotation axis to screen space
tmpV1.copy(gizmoPos);
tmpV2.cross(plane.normal, facingDir).normalize().add(gizmoPos);
// convert world space vectors to screen space
this._camera.worldToScreen(tmpV1, tmpV3);
this._camera.worldToScreen(tmpV2, tmpV4);
// angle is dot product with mouse position
tmpV1.sub2(tmpV4, tmpV3).normalize();
tmpV2.set(x, y, 0);
angle = tmpV1.dot(tmpV2);
}
return angle;
}
}
export { RotateGizmo };