/
gizmo.ts
415 lines (377 loc) · 17.2 KB
/
gizmo.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
import { Observer } from "../Misc/observable";
import { Nullable } from "../types";
import { WebVRFreeCamera } from "../Cameras/VR/webVRCamera";
import { Scene, IDisposable } from "../scene";
import { Quaternion, Vector3, Matrix } from "../Maths/math.vector";
import { AbstractMesh } from "../Meshes/abstractMesh";
import { Mesh } from "../Meshes/mesh";
import { Camera } from "../Cameras/camera";
import { TargetCamera } from "../Cameras/targetCamera";
import { Node } from "../node";
import { Bone } from "../Bones/bone";
import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer";
import { TransformNode } from '../Meshes/transformNode';
import { StandardMaterial } from '../Materials/standardMaterial';
import { PointerEventTypes, PointerInfo } from '../Events/pointerEvents';
import { LinesMesh } from '../Meshes/linesMesh';
import { PointerDragBehavior } from "../Behaviors/Meshes/pointerDragBehavior";
/**
* Cache built by each axis. Used for managing state between all elements of gizmo for enhanced UI
*/
export interface GizmoAxisCache {
/** Mesh used to render the Gizmo */
gizmoMeshes: Mesh[];
/** Mesh used to detect user interaction with Gizmo */
colliderMeshes: Mesh[];
/** Material used to indicate color of gizmo mesh */
material: StandardMaterial;
/** Material used to indicate hover state of the Gizmo */
hoverMaterial: StandardMaterial;
/** Material used to indicate disabled state of the Gizmo */
disableMaterial: StandardMaterial;
/** Used to indicate Active state of the Gizmo */
active: boolean;
/** DragBehavior */
dragBehavior: PointerDragBehavior;
}
/**
* Renders gizmos on top of an existing scene which provide controls for position, rotation, etc.
*/
export class Gizmo implements IDisposable {
/**
* The root mesh of the gizmo
*/
public _rootMesh: Mesh;
private _attachedMesh: Nullable<AbstractMesh> = null;
private _attachedNode: Nullable<Node> = null;
private _customRotationQuaternion: Nullable<Quaternion> = null;
/**
* Ratio for the scale of the gizmo (Default: 1)
*/
protected _scaleRatio = 1;
/**
* boolean updated by pointermove when a gizmo mesh is hovered
*/
protected _isHovered = false;
/**
* Ratio for the scale of the gizmo (Default: 1)
*/
public set scaleRatio(value: number) {
this._scaleRatio = value;
}
public get scaleRatio() {
return this._scaleRatio;
}
/**
* True when the mouse pointer is hovered a gizmo mesh
*/
public get isHovered() {
return this._isHovered;
}
/**
* If a custom mesh has been set (Default: false)
*/
protected _customMeshSet = false;
/**
* Mesh that the gizmo will be attached to. (eg. on a drag gizmo the mesh that will be dragged)
* * When set, interactions will be enabled
*/
public get attachedMesh() {
return this._attachedMesh;
}
public set attachedMesh(value) {
this._attachedMesh = value;
if (value) {
this._attachedNode = value;
}
this._rootMesh.setEnabled(value ? true : false);
this._attachedNodeChanged(value);
}
/**
* Node that the gizmo will be attached to. (eg. on a drag gizmo the mesh, bone or NodeTransform that will be dragged)
* * When set, interactions will be enabled
*/
public get attachedNode() {
return this._attachedNode;
}
public set attachedNode(value) {
this._attachedNode = value;
this._attachedMesh = null;
this._rootMesh.setEnabled(value ? true : false);
this._attachedNodeChanged(value);
}
/**
* Disposes and replaces the current meshes in the gizmo with the specified mesh
* @param mesh The mesh to replace the default mesh of the gizmo
*/
public setCustomMesh(mesh: Mesh) {
if (mesh.getScene() != this.gizmoLayer.utilityLayerScene) {
throw "When setting a custom mesh on a gizmo, the custom meshes scene must be the same as the gizmos (eg. gizmo.gizmoLayer.utilityLayerScene)";
}
this._rootMesh.getChildMeshes().forEach((c) => {
c.dispose();
});
mesh.parent = this._rootMesh;
this._customMeshSet = true;
}
protected _updateGizmoRotationToMatchAttachedMesh = true;
/**
* If set the gizmo's rotation will be updated to match the attached mesh each frame (Default: true)
*/
public set updateGizmoRotationToMatchAttachedMesh(value: boolean) {
this._updateGizmoRotationToMatchAttachedMesh = value;
}
public get updateGizmoRotationToMatchAttachedMesh() {
return this._updateGizmoRotationToMatchAttachedMesh;
}
/**
* If set the gizmo's position will be updated to match the attached mesh each frame (Default: true)
*/
public updateGizmoPositionToMatchAttachedMesh = true;
/**
* When set, the gizmo will always appear the same size no matter where the camera is (default: true)
*/
public updateScale = true;
protected _interactionsEnabled = true;
protected _attachedNodeChanged(value: Nullable<Node>) {
}
private _beforeRenderObserver: Nullable<Observer<Scene>>;
private _tempQuaternion = new Quaternion(0, 0, 0, 1);
private _tempVector = new Vector3();
private _tempVector2 = new Vector3();
private _tempMatrix1 = new Matrix();
private _tempMatrix2 = new Matrix();
private _rightHandtoLeftHandMatrix = Matrix.RotationY(Math.PI);
/**
* Creates a gizmo
* @param gizmoLayer The utility layer the gizmo will be added to
*/
constructor(
/** The utility layer the gizmo will be added to */
public gizmoLayer: UtilityLayerRenderer = UtilityLayerRenderer.DefaultUtilityLayer) {
this._rootMesh = new Mesh("gizmoRootNode", gizmoLayer.utilityLayerScene);
this._rootMesh.rotationQuaternion = Quaternion.Identity();
this._beforeRenderObserver = this.gizmoLayer.utilityLayerScene.onBeforeRenderObservable.add(() => {
this._update();
});
}
/**
* poseture that the gizmo will be display
* * When set null, default value will be used (Quaternion(0, 0, 0, 1))
*/
public get customRotationQuaternion(): Nullable<Quaternion> {
return this._customRotationQuaternion;
}
public set customRotationQuaternion(customRotationQuaternion: Nullable<Quaternion>) {
this._customRotationQuaternion = customRotationQuaternion;
}
/**
* Updates the gizmo to match the attached mesh's position/rotation
*/
protected _update() {
if (this.attachedNode) {
var effectiveNode = this.attachedNode;
if (this.attachedMesh) {
effectiveNode = this.attachedMesh._effectiveMesh || this.attachedNode;
}
// Position
if (this.updateGizmoPositionToMatchAttachedMesh) {
const row = effectiveNode.getWorldMatrix().getRow(3);
const position = row ? row.toVector3() : new Vector3(0, 0, 0);
this._rootMesh.position.copyFrom(position);
}
// Rotation
if (this.updateGizmoRotationToMatchAttachedMesh) {
effectiveNode.getWorldMatrix().decompose(undefined, this._rootMesh.rotationQuaternion!);
}
else {
if (this._customRotationQuaternion) {
this._rootMesh.rotationQuaternion!.copyFrom(this._customRotationQuaternion);
} else {
this._rootMesh.rotationQuaternion!.set(0, 0, 0, 1);
}
}
// Scale
if (this.updateScale) {
const activeCamera = this.gizmoLayer.utilityLayerScene.activeCamera!;
var cameraPosition = activeCamera.globalPosition;
if ((<WebVRFreeCamera>activeCamera).devicePosition) {
cameraPosition = (<WebVRFreeCamera>activeCamera).devicePosition;
}
this._rootMesh.position.subtractToRef(cameraPosition, this._tempVector);
var dist = this._tempVector.length() * this.scaleRatio;
this._rootMesh.scaling.set(dist, dist, dist);
// Account for handedness, similar to Matrix.decompose
if (effectiveNode._getWorldMatrixDeterminant() < 0) {
this._rootMesh.scaling.y *= -1;
}
} else {
this._rootMesh.scaling.setAll(this.scaleRatio);
}
}
}
/**
* computes the rotation/scaling/position of the transform once the Node world matrix has changed.
* @param value Node, TransformNode or mesh
*/
protected _matrixChanged() {
if (!this._attachedNode) {
return;
}
if ((<Camera>this._attachedNode)._isCamera) {
var camera = this._attachedNode as Camera;
var worldMatrix;
var worldMatrixUC;
if (camera.parent) {
var parentInv = this._tempMatrix2;
camera.parent._worldMatrix.invertToRef(parentInv);
this._attachedNode._worldMatrix.multiplyToRef(parentInv, this._tempMatrix1);
worldMatrix = this._tempMatrix1;
} else {
worldMatrix = this._attachedNode._worldMatrix;
}
if (camera.getScene().useRightHandedSystem) {
// avoid desync with RH matrix computation. Otherwise, rotation of PI around Y axis happens each frame resulting in axis flipped because worldMatrix is computed as inverse of viewMatrix.
this._rightHandtoLeftHandMatrix.multiplyToRef(worldMatrix, this._tempMatrix2);
worldMatrixUC = this._tempMatrix2;
} else {
worldMatrixUC = worldMatrix;
}
worldMatrixUC.decompose(this._tempVector2, this._tempQuaternion, this._tempVector);
var inheritsTargetCamera = this._attachedNode.getClassName() === "FreeCamera"
|| this._attachedNode.getClassName() === "FlyCamera"
|| this._attachedNode.getClassName() === "ArcFollowCamera"
|| this._attachedNode.getClassName() === "TargetCamera"
|| this._attachedNode.getClassName() === "TouchCamera"
|| this._attachedNode.getClassName() === "UniversalCamera";
if (inheritsTargetCamera) {
var targetCamera = this._attachedNode as TargetCamera;
targetCamera.rotation = this._tempQuaternion.toEulerAngles();
if (targetCamera.rotationQuaternion) {
targetCamera.rotationQuaternion.copyFrom(this._tempQuaternion);
targetCamera.rotationQuaternion.normalize();
}
}
camera.position.copyFrom(this._tempVector);
} else if ((<Mesh>this._attachedNode)._isMesh || this._attachedNode.getClassName() === "AbstractMesh" || this._attachedNode.getClassName() === "TransformNode" || this._attachedNode.getClassName() === "InstancedMesh") {
var transform = this._attachedNode as TransformNode;
if (transform.parent) {
var parentInv = this._tempMatrix1;
var localMat = this._tempMatrix2;
transform.parent.getWorldMatrix().invertToRef(parentInv);
this._attachedNode.getWorldMatrix().multiplyToRef(parentInv, localMat);
localMat.decompose(transform.scaling, this._tempQuaternion, transform.position);
} else {
this._attachedNode._worldMatrix.decompose(transform.scaling, this._tempQuaternion, transform.position);
}
if (!transform.billboardMode) {
if (transform.rotationQuaternion) {
transform.rotationQuaternion.copyFrom(this._tempQuaternion);
transform.rotationQuaternion.normalize();
} else {
transform.rotation = this._tempQuaternion.toEulerAngles();
}
}
} else if (this._attachedNode.getClassName() === "Bone") {
var bone = this._attachedNode as Bone;
const parent = bone.getParent();
if (parent) {
var invParent = this._tempMatrix1;
var boneLocalMatrix = this._tempMatrix2;
parent.getWorldMatrix().invertToRef(invParent);
bone.getWorldMatrix().multiplyToRef(invParent, boneLocalMatrix);
var lmat = bone.getLocalMatrix();
lmat.copyFrom(boneLocalMatrix);
} else {
var lmat = bone.getLocalMatrix();
lmat.copyFrom(bone.getWorldMatrix());
}
bone.markAsDirty();
}
}
/**
* refresh gizmo mesh material
* @param material material to apply
*/
protected _setGizmoMeshMaterial(gizmoMeshes: Mesh[], material: StandardMaterial) {
if (gizmoMeshes) {
gizmoMeshes.forEach((m: Mesh) => {
m.material = material;
if ((<LinesMesh>m).color) {
(<LinesMesh>m).color = material.diffuseColor;
}
});
}
}
/**
* Subscribes to pointer up, down, and hover events. Used for responsive gizmos.
* @param gizmoLayer The utility layer the gizmo will be added to
* @param gizmoAxisCache Gizmo axis definition used for reactive gizmo UI
* @returns {Observer<PointerInfo>} pointerObserver
*/
public static GizmoAxisPointerObserver(gizmoLayer: UtilityLayerRenderer, gizmoAxisCache: Map<Mesh, GizmoAxisCache>): Observer<PointerInfo> {
let dragging = false;
const pointerObserver = gizmoLayer.utilityLayerScene.onPointerObservable.add((pointerInfo) => {
if (pointerInfo.pickInfo) {
// On Hover Logic
if (pointerInfo.type === PointerEventTypes.POINTERMOVE) {
if (dragging) { return; }
gizmoAxisCache.forEach((cache) => {
if (cache.colliderMeshes && cache.gizmoMeshes) {
const isHovered = (cache.colliderMeshes?.indexOf((pointerInfo?.pickInfo?.pickedMesh as Mesh)) != -1);
const material = cache.dragBehavior.enabled ? (isHovered || cache.active ? cache.hoverMaterial : cache.material) : cache.disableMaterial;
cache.gizmoMeshes.forEach((m: Mesh) => {
m.material = material;
if ((m as LinesMesh).color) {
(m as LinesMesh).color = material.diffuseColor;
}
});
}
});
}
// On Mouse Down
if (pointerInfo.type === PointerEventTypes.POINTERDOWN) {
// If user Clicked Gizmo
if (gizmoAxisCache.has(pointerInfo.pickInfo.pickedMesh?.parent as Mesh)) {
dragging = true;
const statusMap = gizmoAxisCache.get(pointerInfo.pickInfo.pickedMesh?.parent as Mesh);
statusMap!.active = true;
gizmoAxisCache.forEach((cache) => {
const isHovered = (cache.colliderMeshes?.indexOf((pointerInfo?.pickInfo?.pickedMesh as Mesh)) != -1);
const material = ((isHovered || cache.active) && cache.dragBehavior.enabled) ? cache.hoverMaterial : cache.disableMaterial;
cache.gizmoMeshes.forEach((m: Mesh) => {
m.material = material;
if ((m as LinesMesh).color) {
(m as LinesMesh).color = material.diffuseColor;
}
});
});
}
}
// On Mouse Up
if (pointerInfo.type === PointerEventTypes.POINTERUP) {
gizmoAxisCache.forEach((cache) => {
cache.active = false;
dragging = false;
cache.gizmoMeshes.forEach((m: Mesh) => {
m.material = cache.dragBehavior.enabled ? cache.material : cache.disableMaterial;
if ((m as LinesMesh).color) {
(m as LinesMesh).color = cache.material.diffuseColor;
}
});
});
}
}
});
return pointerObserver!;
}
/**
* Disposes of the gizmo
*/
public dispose() {
this._rootMesh.dispose();
if (this._beforeRenderObserver) {
this.gizmoLayer.utilityLayerScene.onBeforeRenderObservable.remove(this._beforeRenderObserver);
}
}
}