-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
axisDragGizmo.ts
286 lines (259 loc) · 11.9 KB
/
axisDragGizmo.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
import type { Observer } from "../Misc/observable";
import { Observable } from "../Misc/observable";
import type { Nullable } from "../types";
import type { PointerInfo } from "../Events/pointerEvents";
import { Vector3 } from "../Maths/math.vector";
import { TransformNode } from "../Meshes/transformNode";
import type { Node } from "../node";
import { Mesh } from "../Meshes/mesh";
import { CreateCylinder } from "../Meshes/Builders/cylinderBuilder";
import { PointerDragBehavior } from "../Behaviors/Meshes/pointerDragBehavior";
import type { GizmoAxisCache, IGizmo } from "./gizmo";
import { Gizmo } from "./gizmo";
import { UtilityLayerRenderer } from "../Rendering/utilityLayerRenderer";
import { StandardMaterial } from "../Materials/standardMaterial";
import type { Scene } from "../scene";
import type { PositionGizmo } from "./positionGizmo";
import { Color3 } from "../Maths/math.color";
/**
* Interface for axis drag gizmo
*/
export interface IAxisDragGizmo extends IGizmo {
/** Drag behavior responsible for the gizmos dragging interactions */
dragBehavior: PointerDragBehavior;
/** Drag distance in babylon units that the gizmo will snap to when dragged */
snapDistance: number;
/**
* Event that fires each time the gizmo snaps to a new location.
* * snapDistance is the the change in distance
*/
onSnapObservable: Observable<{ snapDistance: number }>;
/** If the gizmo is enabled */
isEnabled: boolean;
}
/**
* Single axis drag gizmo
*/
export class AxisDragGizmo extends Gizmo implements IAxisDragGizmo {
/**
* Drag behavior responsible for the gizmos dragging interactions
*/
public dragBehavior: PointerDragBehavior;
protected _pointerObserver: Nullable<Observer<PointerInfo>> = null;
/**
* Drag distance in babylon units that the gizmo will snap to when dragged (Default: 0)
*/
public snapDistance = 0;
/**
* Event that fires each time the gizmo snaps to a new location.
* * snapDistance is the the change in distance
*/
public onSnapObservable = new Observable<{ snapDistance: number }>();
protected _isEnabled: boolean = true;
protected _parent: Nullable<PositionGizmo> = null;
protected _gizmoMesh: Mesh;
protected _coloredMaterial: StandardMaterial;
protected _hoverMaterial: StandardMaterial;
protected _disableMaterial: StandardMaterial;
protected _dragging: boolean = false;
/**
* @param scene
* @param material
* @param thickness
* @param isCollider
* @hidden
*/
public static _CreateArrow(scene: Scene, material: StandardMaterial, thickness: number = 1, isCollider = false): TransformNode {
const arrow = new TransformNode("arrow", scene);
const cylinder = CreateCylinder("cylinder", { diameterTop: 0, height: 0.075, diameterBottom: 0.0375 * (1 + (thickness - 1) / 4), tessellation: 96 }, scene);
const line = CreateCylinder("cylinder", { diameterTop: 0.005 * thickness, height: 0.275, diameterBottom: 0.005 * thickness, tessellation: 96 }, scene);
// Position arrow pointing in its drag axis
cylinder.parent = arrow;
cylinder.material = material;
cylinder.rotation.x = Math.PI / 2;
cylinder.position.z += 0.3;
line.parent = arrow;
line.material = material;
line.position.z += 0.275 / 2;
line.rotation.x = Math.PI / 2;
if (isCollider) {
line.visibility = 0;
cylinder.visibility = 0;
}
return arrow;
}
/**
* @param scene
* @param arrow
* @hidden
*/
public static _CreateArrowInstance(scene: Scene, arrow: TransformNode): TransformNode {
const instance = new TransformNode("arrow", scene);
for (const mesh of arrow.getChildMeshes()) {
const childInstance = (mesh as Mesh).createInstance(mesh.name);
childInstance.parent = instance;
}
return instance;
}
/**
* Creates an AxisDragGizmo
* @param dragAxis The axis which the gizmo will be able to drag on
* @param color The color of the gizmo
* @param gizmoLayer The utility layer the gizmo will be added to
* @param parent
* @param thickness display gizmo axis thickness
*/
constructor(
dragAxis: Vector3,
color: Color3 = Color3.Gray(),
gizmoLayer: UtilityLayerRenderer = UtilityLayerRenderer.DefaultUtilityLayer,
parent: Nullable<PositionGizmo> = null,
thickness: number = 1
) {
super(gizmoLayer);
this._parent = parent;
// Create Material
this._coloredMaterial = new StandardMaterial("", gizmoLayer.utilityLayerScene);
this._coloredMaterial.diffuseColor = color;
this._coloredMaterial.specularColor = color.subtract(new Color3(0.1, 0.1, 0.1));
this._hoverMaterial = new StandardMaterial("", gizmoLayer.utilityLayerScene);
this._hoverMaterial.diffuseColor = Color3.Yellow();
this._disableMaterial = new StandardMaterial("", gizmoLayer.utilityLayerScene);
this._disableMaterial.diffuseColor = Color3.Gray();
this._disableMaterial.alpha = 0.4;
// Build Mesh + Collider
const arrow = AxisDragGizmo._CreateArrow(gizmoLayer.utilityLayerScene, this._coloredMaterial, thickness);
const collider = AxisDragGizmo._CreateArrow(gizmoLayer.utilityLayerScene, this._coloredMaterial, thickness + 4, true);
// Add to Root Node
this._gizmoMesh = new Mesh("", gizmoLayer.utilityLayerScene);
this._gizmoMesh.addChild(arrow as Mesh);
this._gizmoMesh.addChild(collider as Mesh);
this._gizmoMesh.lookAt(this._rootMesh.position.add(dragAxis));
this._gizmoMesh.scaling.scaleInPlace(1 / 3);
this._gizmoMesh.parent = this._rootMesh;
let currentSnapDragDistance = 0;
const tmpVector = new Vector3();
const tmpVector2 = new Vector3();
const tmpSnapEvent = { snapDistance: 0 };
// Add drag behavior to handle events when the gizmo is dragged
this.dragBehavior = new PointerDragBehavior({ dragAxis: dragAxis });
this.dragBehavior.moveAttached = false;
this.dragBehavior.updateDragPlane = false;
this._rootMesh.addBehavior(this.dragBehavior);
this.dragBehavior.onDragObservable.add((event) => {
if (this.attachedNode) {
this._handlePivot();
// Keep world translation and use it to update world transform
// if the node has parent, the local transform properties (position, rotation, scale)
// will be recomputed in _matrixChanged function
let matrixChanged: boolean = false;
// Snapping logic
if (this.snapDistance == 0) {
this.attachedNode.getWorldMatrix().getTranslationToRef(tmpVector2);
tmpVector2.addInPlace(event.delta);
if (this.dragBehavior.validateDrag(tmpVector2)) {
if ((this.attachedNode as any).position) {
// Required for nodes like lights
(this.attachedNode as any).position.addInPlaceFromFloats(event.delta.x, event.delta.y, event.delta.z);
}
// use _worldMatrix to not force a matrix update when calling GetWorldMatrix especially with Cameras
this.attachedNode.getWorldMatrix().addTranslationFromFloats(event.delta.x, event.delta.y, event.delta.z);
this.attachedNode.updateCache();
matrixChanged = true;
}
} else {
currentSnapDragDistance += event.dragDistance;
if (Math.abs(currentSnapDragDistance) > this.snapDistance) {
const dragSteps = Math.floor(Math.abs(currentSnapDragDistance) / this.snapDistance);
currentSnapDragDistance = currentSnapDragDistance % this.snapDistance;
event.delta.normalizeToRef(tmpVector);
tmpVector.scaleInPlace(this.snapDistance * dragSteps);
this.attachedNode.getWorldMatrix().getTranslationToRef(tmpVector2);
tmpVector2.addInPlace(tmpVector);
if (this.dragBehavior.validateDrag(tmpVector2)) {
this.attachedNode.getWorldMatrix().addTranslationFromFloats(tmpVector.x, tmpVector.y, tmpVector.z);
this.attachedNode.updateCache();
tmpSnapEvent.snapDistance = this.snapDistance * dragSteps;
this.onSnapObservable.notifyObservers(tmpSnapEvent);
matrixChanged = true;
}
}
}
if (matrixChanged) {
this._matrixChanged();
}
}
});
this.dragBehavior.onDragStartObservable.add(() => {
this._dragging = true;
});
this.dragBehavior.onDragEndObservable.add(() => {
this._dragging = false;
});
const light = gizmoLayer._getSharedGizmoLight();
light.includedOnlyMeshes = light.includedOnlyMeshes.concat(this._rootMesh.getChildMeshes(false));
const cache: GizmoAxisCache = {
gizmoMeshes: arrow.getChildMeshes() as Mesh[],
colliderMeshes: collider.getChildMeshes() as Mesh[],
material: this._coloredMaterial,
hoverMaterial: this._hoverMaterial,
disableMaterial: this._disableMaterial,
active: false,
dragBehavior: this.dragBehavior,
};
this._parent?.addToAxisCache(collider as Mesh, cache);
this._pointerObserver = gizmoLayer.utilityLayerScene.onPointerObservable.add((pointerInfo) => {
if (this._customMeshSet) {
return;
}
this._isHovered = !!(cache.colliderMeshes.indexOf(<Mesh>pointerInfo?.pickInfo?.pickedMesh) != -1);
if (!this._parent) {
const material = this.dragBehavior.enabled ? (this._isHovered || this._dragging ? this._hoverMaterial : this._coloredMaterial) : this._disableMaterial;
this._setGizmoMeshMaterial(cache.gizmoMeshes, material);
}
});
this.dragBehavior.onEnabledObservable.add((newState) => {
this._setGizmoMeshMaterial(cache.gizmoMeshes, newState ? cache.material : cache.disableMaterial);
});
}
protected _attachedNodeChanged(value: Nullable<Node>) {
if (this.dragBehavior) {
this.dragBehavior.enabled = value ? true : false;
}
}
/**
* If the gizmo is enabled
*/
public set isEnabled(value: boolean) {
this._isEnabled = value;
if (!value) {
this.attachedMesh = null;
this.attachedNode = null;
} else {
if (this._parent) {
this.attachedMesh = this._parent.attachedMesh;
this.attachedNode = this._parent.attachedNode;
}
}
}
public get isEnabled(): boolean {
return this._isEnabled;
}
/**
* Disposes of the gizmo
*/
public dispose() {
this.onSnapObservable.clear();
this.gizmoLayer.utilityLayerScene.onPointerObservable.remove(this._pointerObserver);
this.dragBehavior.detach();
if (this._gizmoMesh) {
this._gizmoMesh.dispose();
}
[this._coloredMaterial, this._hoverMaterial, this._disableMaterial].forEach((matl) => {
if (matl) {
matl.dispose();
}
});
super.dispose();
}
}