-
Notifications
You must be signed in to change notification settings - Fork 2
/
handle.js
179 lines (160 loc) · 5.51 KB
/
handle.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
/* global THREE, Part, Robot */
class Handle { // eslint-disable-line no-unused-vars
constructor(robotController, domElement, camera, scene, orbitControls) {
this.robotController = robotController;
this.scene = scene;
this.mode = 'select';
this.part = null;
this.control = new THREE.TransformControls(camera, domElement);
this.control.isTransformControls = true; // To be detected correctly by OutlinePass.
this.control.visible = false;
this.control.enabled = false;
this.control.setSpace('local');
this.control.addEventListener('dragging-changed', (event) => {
orbitControls.enabled = !event.value;
});
this.control.addEventListener('change', (event) => {
if (!this.target)
return;
if (this.part && this.mode === 'translate') {
var position = this.target.position;
this.robotController.translatePart(this.part, position);
}
if (this.part && this.mode === 'rotate') {
var quaternion = this.target.quaternion;
this.robotController.rotatePart(this.part, quaternion);
}
});
}
attachToObject(object) {
this.detach();
this.part = object.mediator.model;
this.part.addObserver('Translated', (d) => {
this._updateTargetPosition();
});
this.part.addObserver('Rotated', (d) => {
this._updateTargetPosition();
});
this.target = new THREE.Object3D();
this.target.userData.isHandleTarget = true;
this._updateTargetPosition();
object.parent.add(this.target);
this.control.attach(this.target);
this.setMode(this.mode); // update visibility.
// Uncomment to visualize the target:
// var geometry = new THREE.BoxGeometry(0.05, 0.05, 0.05);
// var material = new THREE.MeshBasicMaterial({color: 0x00ff00, transparent: true, opacity: 0.5});
// var cube = new THREE.Mesh(geometry, material);
// this.target.add(cube);
}
setMode(mode) {
this.mode = mode;
if (mode === 'select') {
this.control.visible = false;
this.control.enabled = false;
} else if (mode === 'translate') {
this.control.visible = true;
this.control.enabled = true;
this.control.setMode('translate');
} else if (mode === 'rotate') {
this.control.visible = true;
this.control.enabled = true;
this.control.setMode('rotate');
}
this._updateConstraints();
}
detach() {
this.control.detach();
if (this.target) {
this.target.parent.remove(this.target);
this.target = null;
}
this.part = null;
}
showHandle() {
this.scene.add(this.control);
}
hideHandle() {
this.scene.remove(this.control);
}
isDragging() {
return this.control.dragging;
}
_updateTargetPosition() {
if (!this.target)
return;
this.target.position.copy(new THREE.Vector3(
this.part.translation[0],
this.part.translation[1],
this.part.translation[2]
));
this.target.quaternion.copy(new THREE.Quaternion(
this.part.quaternion[0],
this.part.quaternion[1],
this.part.quaternion[2],
this.part.quaternion[3]
));
this.target.updateMatrix();
}
_updateConstraints() {
if (!this.part)
return;
var parentPart = this.part.parent;
console.assert(parentPart);
if (this.mode !== 'select') {
if (parentPart instanceof Robot) {
this.control.rotationSnap = null;
this.control.translationSnap = null;
this.control.showX = true;
this.control.showY = true;
this.control.showZ = true;
return;
}
if (parentPart instanceof Part) {
var slotName = parentPart.slotName(this.part);
if (slotName) {
var slotData = parentPart.asset.slots[slotName];
this.control.rotationSnap = slotData.rotationSnap === -1 ? null : slotData.rotationSnap;
this.control.translationSnap = slotData.translationSnap === -1 ? null : slotData.translationSnap;
if (this.mode === 'rotate') {
if (this.control.rotationSnap === 0) {
this.control.showX = false;
this.control.showY = false;
this.control.showZ = false;
} else if (slotData.rotationGizmoVisibility === undefined) {
this.control.showX = true;
this.control.showY = true;
this.control.showZ = true;
} else {
this.control.showX = slotData.rotationGizmoVisibility[0];
this.control.showY = slotData.rotationGizmoVisibility[1];
this.control.showZ = slotData.rotationGizmoVisibility[2];
}
return;
} else if (this.mode === 'translate') {
if (this.control.translationSnap === 0) {
this.control.showX = false;
this.control.showY = false;
this.control.showZ = false;
} else if (slotData.translationHandleVisibility === undefined) {
this.control.showX = true;
this.control.showY = true;
this.control.showZ = true;
} else {
this.control.showX = slotData.translationHandleVisibility[0];
this.control.showY = slotData.translationHandleVisibility[1];
this.control.showZ = slotData.translationHandleVisibility[2];
}
return;
}
}
}
}
// select mode or invalid structure.
this.control.rotationSnap = null;
this.control.translationSnap = null;
this.control.showX = false;
this.control.showY = false;
this.control.showZ = false;
}
}