-
Notifications
You must be signed in to change notification settings - Fork 2
/
TransformGizmo.qml
468 lines (408 loc) · 17.1 KB
/
TransformGizmo.qml
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
/*
qt3d-transform-gizmo
Copyright (C) 2020 Federico Ferri
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick.Scene3D 2.0
import QtQuick 2.2 as QQ2
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Logic 2.0
import Qt3D.Extras 2.0
import Qt3D.Animation 2.9
Entity {
id: root
property real size: 1
readonly property real beamRadius: size * 0.035
property Layer layer
property var cameraController
property Camera camera
property Scene3D scene3d
property Transform targetTransform
property Entity previousParent
property real linearSpeed: 0.01
property real angularSpeed: 2.0
property bool visible: false
property vector3d absolutePosition: Qt.vector3d(0, 0, 0)
property real hoverHilightFactor: 1.44
property real hoverZoomFactor: 1.5
property int mode: TransformGizmo.Mode.Translation
property bool canTranslate: true
property bool canRotate: true
property bool canScale: false
property var hoverElements: new Set()
property var hoverElement: TransformGizmo.UIElement.None
property var activeElement: TransformGizmo.UIElement.None
components: [ownTransform, layer]
enum Mode {
Translation,
Rotation,
Scale
}
enum UIElement {
None,
ModeSwitcher,
BeamX,
BeamY,
BeamZ,
PlaneXY,
PlaneXZ,
PlaneYZ
}
// called by ObjectPickers of individual UI elements:
function trackUIElement(element, active) {
if(active) hoverElements.add(element)
else hoverElements.delete(element)
var newHoverElement = TransformGizmo.UIElement.None
for(var x of [TransformGizmo.UIElement.ModeSwitcher, TransformGizmo.UIElement.BeamX, TransformGizmo.UIElement.BeamY, TransformGizmo.UIElement.BeamZ, TransformGizmo.UIElement.PlaneXY, TransformGizmo.UIElement.PlaneXZ, TransformGizmo.UIElement.PlaneYZ])
if(newHoverElement === TransformGizmo.UIElement.None && hoverElements.has(x))
newHoverElement = x
hoverElement = newHoverElement
}
function getMatrix(entity) {
var t = getTransform(entity)
if(t) return t.matrix
return Qt.matrix4x4(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)
}
function getAbsoluteMatrix() {
var entity = root
var m = Qt.matrix4x4(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)
while(entity) {
m = getMatrix(entity).times(m)
entity = entity.parent
}
return m
}
function project(v, modelView, projection, viewport) {
// ported from qtbase/src/gui/math3d/qvector3d.cpp
var tmp = Qt.vector4d(v.x, v.y, v.z, 1)
tmp = projection.times(modelView).times(tmp)
if(Math.abs(tmp.z) < 0.00001)
tmp.z = 1
tmp = tmp.times(1 / tmp.w)
tmp = tmp.times(0.5).plus(Qt.vector4d(0.5, 0.5, 0.5, 0.5))
tmp.x = tmp.x * viewport.width + viewport.x
tmp.y = tmp.y * viewport.height + viewport.y
return Qt.vector3d(tmp.x, tmp.y, tmp.z)
}
function projectMotion(dx, dy) {
var mtx = getAbsoluteMatrix()
var mv = camera.viewMatrix.times(mtx)
var p = camera.projectionMatrix
var v = Qt.rect(0, 0, scene3d.width, scene3d.height)
var s0 = project(Qt.vector3d(0,0,0),mv,p,v)
var sx = project(Qt.vector3d(1,0,0),mv,p,v).minus(s0)
var sy = project(Qt.vector3d(0,1,0),mv,p,v).minus(s0)
var sz = project(Qt.vector3d(0,0,1),mv,p,v).minus(s0)
sx.z = sy.z = sz.z = 0
sx = sx.normalized()
sy = sy.normalized()
sz = sz.normalized()
var d = Qt.vector3d(dx, dy, 0)
var px = d.dotProduct(sx)
var py = d.dotProduct(sy)
var pz = d.dotProduct(sz)
return Qt.vector3d(px, py, pz)
}
function fixOwnTransform() {
// cancel rotation component of parent's (target) transform
var t = targetTransform.matrix
var i = t.inverted()
i = i.times(Qt.matrix4x4(1,0,0,t.m14,0,1,0,t.m24,0,0,1,t.m34,0,0,0,1))
ownTransform.matrix = i
updateAbsolutePosition()
}
function updateAbsolutePosition() {
// compute absolute position to expose as a property
var m = getAbsoluteMatrix()
absolutePosition = Qt.vector3d(m.m14, m.m24, m.m34)
}
function qmlInstanceOf(obj, className) {
return obj.toString().indexOf(className + "(") === 0;
}
function getTransform(entity) {
if(entity instanceof Entity)
for(var i = 0; i < entity.components.length; i++)
if(qmlInstanceOf(entity.components[i], "Qt3DCore::QTransform"))
return entity.components[i]
}
function attachTo(entity) {
var t = getTransform(entity)
if(t) {
targetTransform = t
previousParent = root.parent
root.parent = entity
fixOwnTransform()
visible = true
}
}
function detach() {
targetTransform = null
root.parent = previousParent
visible = false
ownTransform.matrix = Qt.matrix4x4(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)
updateAbsolutePosition()
}
function angleAxisToQuat(angle, x, y, z) {
var a = angle * Math.PI / 180.0;
var s = Math.sin(a * 0.5);
var c = Math.cos(a * 0.5);
return Qt.quaternion(c, x * s, y * s, z * s);
}
function multiplyQuaternion(q1, q2) {
return Qt.quaternion(q1.scalar * q2.scalar - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z,
q1.scalar * q2.x + q1.x * q2.scalar + q1.y * q2.z - q1.z * q2.y,
q1.scalar * q2.y + q1.y * q2.scalar + q1.z * q2.x - q1.x * q2.z,
q1.scalar * q2.z + q1.z * q2.scalar + q1.x * q2.y - q1.y * q2.x);
}
function switchMode() {
var modes = [
...(canTranslate ? [TransformGizmo.Mode.Translation] : []),
...(canRotate ? [TransformGizmo.Mode.Rotation] : []),
...(canScale ? [TransformGizmo.Mode.Scale] : []),
]
mode = (modes.indexOf(mode) + 1) % modes.length
}
function translate(dx, dy, dz) {
if(!targetTransform) return
targetTransform.translation.x += linearSpeed * dx
targetTransform.translation.y += linearSpeed * dy
targetTransform.translation.z += linearSpeed * dz
}
function rotate(dx, dy, dz) {
if(!targetTransform) return
targetTransform.rotation = multiplyQuaternion(angleAxisToQuat(angularSpeed * dx, 1, 0, 0), targetTransform.rotation)
targetTransform.rotation = multiplyQuaternion(angleAxisToQuat(angularSpeed * dy, 0, 1, 0), targetTransform.rotation)
targetTransform.rotation = multiplyQuaternion(angleAxisToQuat(angularSpeed * dz, 0, 0, 1), targetTransform.rotation)
}
function scale(dx, dy, dz) {
if(!targetTransform) return
targetTransform.scale3D.x += linearSpeed * dx
targetTransform.scale3D.y += linearSpeed * dy
targetTransform.scale3D.z += linearSpeed * dz
}
Transform {
id: ownTransform
}
MouseDevice {
id: mouseDev
}
MouseHandler {
sourceDevice: mouseDev
property point lastPos
onPressed: {
if(mouse.button == Qt.RightButton) {
detach()
return
}
if(mouse.button == Qt.LeftButton) {
if(hoverElement === TransformGizmo.UIElement.None) return
lastPos = Qt.point(mouse.x, mouse.y)
if(cameraController) cameraController.enabled = false
activeElement = hoverElement
return
}
}
onPositionChanged: {
if(activeElement === TransformGizmo.UIElement.None) return
var dx = mouse.x - lastPos.x
var dy = mouse.y - lastPos.y
var d = projectMotion(dx, -dy)
switch(activeElement) {
case TransformGizmo.UIElement.BeamX:
case TransformGizmo.UIElement.BeamY:
case TransformGizmo.UIElement.BeamZ:
var x = activeElement === TransformGizmo.UIElement.BeamX
var y = activeElement === TransformGizmo.UIElement.BeamY
var z = activeElement === TransformGizmo.UIElement.BeamZ
switch(mode) {
case TransformGizmo.Mode.Translation: translate(x * d.x, y * d.y, z * d.z); break
case TransformGizmo.Mode.Rotation: rotate(x * d.x, y * d.y, z * d.z); break
case TransformGizmo.Mode.Scale: scale(x * d.x, y * d.y, z * d.z); break
}
break;
case TransformGizmo.UIElement.PlaneXY: translate(d.x, d.y, 0); break
case TransformGizmo.UIElement.PlaneXZ: translate(d.x, 0, d.z); break
case TransformGizmo.UIElement.PlaneYZ: translate(0, d.y, d.z); break
}
lastPos = Qt.point(mouse.x, mouse.y)
}
onReleased: {
if(activeElement === TransformGizmo.UIElement.None) return
if(cameraController) cameraController.enabled = true
activeElement = TransformGizmo.UIElement.None
}
}
QQ2.Loader {
active: !!targetTransform
sourceComponent: QQ2.Connections {
target: targetTransform
onMatrixChanged: fixOwnTransform()
}
}
Entity {
id: modeSwitcher
readonly property color color: "#333"
readonly property bool hover: root.hoverElement === TransformGizmo.UIElement.ModeSwitcher
readonly property bool active: root.activeElement === TransformGizmo.UIElement.ModeSwitcher
readonly property bool hilighted: active || (root.activeElement === TransformGizmo.UIElement.None && hover)
components: [modeSwitcherSphere, modeSwitcherMaterial, modeSwitcherPicker]
SphereMesh {
id: modeSwitcherSphere
readonly property real radius0: beamRadius * 2
readonly property real radius1: root.hoverZoomFactor * radius0
radius: modeSwitcher.hilighted ? radius1 : radius0
enabled: root.visible
}
PhongMaterial {
id: modeSwitcherMaterial
ambient: modeSwitcher.hilighted ? Qt.lighter(modeSwitcher.color, root.hoverHilightFactor) : modeSwitcher.color
}
ObjectPicker {
id: modeSwitcherPicker
hoverEnabled: true
onClicked: root.switchMode()
onEntered: root.trackUIElement(TransformGizmo.UIElement.ModeSwitcher, true)
onExited: root.trackUIElement(TransformGizmo.UIElement.ModeSwitcher, false)
}
}
NodeInstantiator {
id: beams
model: [
{r: Qt.vector3d( 0, 0, -90), v: Qt.vector3d(1, 0, 0), color: "#f33", element: TransformGizmo.UIElement.BeamX},
{r: Qt.vector3d( 0, 0, 0), v: Qt.vector3d(0, 1, 0), color: "#3f3", element: TransformGizmo.UIElement.BeamY},
{r: Qt.vector3d(90, 0, 0), v: Qt.vector3d(0, 0, 1), color: "#33f", element: TransformGizmo.UIElement.BeamZ}
]
delegate: Entity {
components: [beamTransform]
Transform {
id: beamTransform
translation: modelData.v.times(modeSwitcherSphere.radius0 * 1.1)
rotationX: modelData.r.x
rotationY: modelData.r.y
rotationZ: modelData.r.z
}
Entity {
id: beam
readonly property bool hover: root.hoverElement === modelData.element
readonly property bool active: root.activeElement === modelData.element
readonly property bool hilighted: active || (root.activeElement === TransformGizmo.UIElement.None && hover)
readonly property color color: modelData.color
components: [beamPicker]
ObjectPicker {
id: beamPicker
hoverEnabled: true
onEntered: root.trackUIElement(modelData.element, true)
onExited: root.trackUIElement(modelData.element, false)
}
PhongMaterial {
id: beamMaterial
ambient: beam.hilighted ? Qt.lighter(beam.color, root.hoverHilightFactor) : beam.color
}
Entity {
components: [lineMesh, lineTransform, beamMaterial]
CylinderMesh {
id: lineMesh
enabled: root.visible
radius: root.beamRadius
length: root.size * 0.8
}
Transform {
id: lineTransform
translation: Qt.vector3d(0, lineMesh.length / 2, 0)
}
}
Entity {
components: [translateMesh, translateTransform, beamMaterial]
ConeMesh {
id: translateMesh
enabled: root.visible && root.mode === TransformGizmo.Mode.Translation
bottomRadius: root.beamRadius * 2
topRadius: 0
length: root.size * 0.2
}
Transform {
id: translateTransform
translation: Qt.vector3d(0, lineMesh.length + translateMesh.length / 2, 0)
}
}
Entity {
components: [rotateMesh, rotateTransform, beamMaterial]
CylinderMesh {
id: rotateMesh
enabled: root.visible && root.mode === TransformGizmo.Mode.Rotation
radius: root.beamRadius * 2
length: root.beamRadius * 2
}
Transform {
id: rotateTransform
translation: Qt.vector3d(0, lineMesh.length + rotateMesh.length / 2, 0)
}
}
Entity {
components: [scaleMesh, scaleTransform, beamMaterial]
CuboidMesh {
id: scaleMesh
enabled: root.visible && root.mode === TransformGizmo.Mode.Scale
xExtent: root.beamRadius * 3
yExtent: root.beamRadius * 3
zExtent: root.beamRadius * 3
}
Transform {
id: scaleTransform
translation: Qt.vector3d(0, lineMesh.length + scaleMesh.xExtent / 2, 0)
}
}
}
}
}
NodeInstantiator {
id: planes
model: [
{v: Qt.vector3d(1, 1, 0), element: TransformGizmo.UIElement.PlaneXY},
{v: Qt.vector3d(1, 0, 1), element: TransformGizmo.UIElement.PlaneXZ},
{v: Qt.vector3d(0, 1, 1), element: TransformGizmo.UIElement.PlaneYZ},
]
delegate: Entity {
id: plane
readonly property bool hover: root.hoverElement === modelData.element
readonly property bool active: root.activeElement === modelData.element
readonly property bool hilighted: active || (root.activeElement === TransformGizmo.UIElement.None && hover)
readonly property color color: "#dd6"
components: [cuboid, planeTransform, planeMaterial, planePicker]
CuboidMesh {
id: cuboid
readonly property real squareSize: root.size * 0.3
readonly property real squareThickness: root.beamRadius * 0.5
enabled: root.visible
xExtent: modelData.v.x ? squareSize : squareThickness
yExtent: modelData.v.y ? squareSize : squareThickness
zExtent: modelData.v.z ? squareSize : squareThickness
}
Transform {
id: planeTransform
translation: modelData.v.times(root.beamRadius + root.size * 0.025 + cuboid.squareSize / 2)
}
PhongMaterial {
id: planeMaterial
ambient: plane.hilighted ? Qt.lighter(plane.color, root.hoverHilightFactor) : plane.color
}
ObjectPicker {
id: planePicker
hoverEnabled: true
onEntered: root.trackUIElement(modelData.element, true)
onExited: root.trackUIElement(modelData.element, false)
}
}
}
}