-
Notifications
You must be signed in to change notification settings - Fork 4
/
LocatorGizmo.ts
153 lines (133 loc) · 3.14 KB
/
LocatorGizmo.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
import { Entity } from '../core/Entity';
import {PrimitiveMode} from '../definitions/PrimitiveMode';
import {VertexAttribute} from '../definitions/VertexAttribute';
import { Mesh } from '../geometry/Mesh';
import {Primitive} from '../geometry/Primitive';
import {EntityHelper, IMeshEntity} from '../helpers/EntityHelper';
import { Vector3 } from '../math/Vector3';
import { Gizmo } from './Gizmo';
/**
* Locator Gizmo class
*/
export class LocatorGizmo extends Gizmo {
private static __mesh: Mesh;
private static __length = 1;
/**
* Constructor
* @param target the object which this gizmo belong to
*/
constructor(target: IMeshEntity) {
super(target);
}
///
///
/// Accessors
///
///
get isSetup(): boolean {
if (this.__topEntity != null) {
return true;
} else {
return false;
}
}
set length(val: number) {
LocatorGizmo.__length = val;
}
get length(): number {
return LocatorGizmo.__length;
}
///
///
/// Friends Members
///
///
/**
* @internal
* setup entities of Gizmo if not done yet
*/
_setup(): void {
if (this.__toSkipSetup()) {
return;
}
this.__topEntity = EntityHelper.createMeshEntity();
this.__topEntity!.tryToSetUniqueName(
`LocatorGizmo_of_${this.__target.uniqueName}`,
true
);
this.__topEntity!.getSceneGraph()!.toMakeWorldMatrixTheSameAsLocalMatrix =
true;
this.__target
.getSceneGraph()!
._addGizmoChild(this.__topEntity!.getSceneGraph());
const sceneGraphComponent = this.__topEntity!.tryToGetMesh()!;
LocatorGizmo.__mesh = new Mesh();
LocatorGizmo.__mesh.addPrimitive(LocatorGizmo.__generatePrimitive());
sceneGraphComponent.setMesh(LocatorGizmo.__mesh);
this.setGizmoTag();
}
/**
* @internal
* update the transform and etc of the gizmo
*/
_update(): void {
if (this.__topEntity == null) {
return;
}
const sg = this.__target.getSceneGraph()!;
const aabb = sg.worldAABB;
this.__topEntity.getTransform()!.translate = aabb.centerPoint;
this.__topEntity.getTransform()!.scale = Vector3.fromCopyArray([
Math.min(1, aabb.isVanilla() ? 1 : aabb.sizeX / 2),
Math.min(1, aabb.isVanilla() ? 1 : aabb.sizeY / 2),
Math.min(1, aabb.isVanilla() ? 1 : aabb.sizeZ / 2),
]);
}
///
///
/// Private Static Members
///
///
private static __generatePrimitive(): Primitive {
const positions = new Float32Array([
// X axis
0,
0,
0,
this.__length,
0,
0,
// Y axis
0,
0,
0,
0,
this.__length,
0,
// Z axis
0,
0,
0,
0,
0,
this.__length,
]);
const color = new Float32Array([
// X axis as Red
1, 0, 0, 1, 0, 0,
// Y axis as Green
0, 1, 0, 0, 1, 0,
// Z axis as Blue
0, 0, 1, 0, 0, 1,
]);
const primitive = Primitive.createPrimitive({
attributeSemantics: [
VertexAttribute.Position.XYZ,
VertexAttribute.Color0.XYZ,
],
attributes: [positions, color],
primitiveMode: PrimitiveMode.Lines,
});
return primitive;
}
}