-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
BoundingBoxGizmoHandle.cs
426 lines (382 loc) · 14.8 KB
/
BoundingBoxGizmoHandle.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
using HoloToolkit.Unity.InputModule;
using UnityEngine;
#if UNITY_WSA && UNITY_2017_2_OR_NEWER
using UnityEngine.XR.WSA;
#endif
namespace HoloToolkit.Unity.UX
{
/// <summary>
/// Logic for the gizmo handles in Bounding Box
/// </summary>
public class BoundingBoxGizmoHandle : MonoBehaviour, IInputHandler, ISourceStateHandler
{
private BoundingBoxRig rig;
private Transform transformToAffect;
private BoundingBoxGizmoHandleTransformType affineType;
private BoundingBoxGizmoHandleAxisToAffect axis;
private Vector3 initialHandPosition;
private Vector3 initialScale;
private Vector3 initialPosition;
private Vector3 initialOrientation;
private Quaternion initialRotation;
private Quaternion initialHandOrientation;
private Vector3 initialScaleOrigin;
private InputEventData inputDownEventData;
private bool isHandRotationAvailable;
private bool isLeftHandedRotation = false;
private Vector3 rotationFromPositionScale = new Vector3(-300.0f, -300.0f, -300.0f);
private float minimumScaleNav = 0.001f;
private float scaleRate = 1.0f;
private float maxScale = 10.0f;
private BoundingBoxGizmoHandleRotationType rotationCoordinateSystem;
private BoundingBoxGizmoHandleHandMotionType handMotionForRotation;
private Vector3 lastHandWorldPos = Vector3.zero;
public BoundingBoxGizmoHandleTransformType AffineType
{
get
{
return affineType;
}
set
{
affineType = value;
}
}
public BoundingBoxGizmoHandleAxisToAffect Axis
{
get
{
return axis;
}
set
{
axis = value;
}
}
public bool IsLeftHandedRotation
{
get
{
return isLeftHandedRotation;
}
set
{
isLeftHandedRotation = value;
}
}
public Transform TransformToAffect
{
get
{
return transformToAffect;
}
set
{
transformToAffect = value;
}
}
public BoundingBoxGizmoHandleRotationType RotationCoordinateSystem
{
get
{
return rotationCoordinateSystem;
}
set
{
rotationCoordinateSystem = value;
}
}
public BoundingBoxGizmoHandleHandMotionType HandMotionForRotation
{
get
{
return handMotionForRotation;
}
set
{
handMotionForRotation = value;
}
}
public float ScaleRate
{
get
{
return scaleRate;
}
set
{
scaleRate = value;
}
}
public float MaxScale
{
get
{
return maxScale;
}
set
{
maxScale = value;
}
}
public BoundingBoxRig Rig
{
get
{
return rig;
}
set
{
rig = value;
}
}
public bool RotateAroundPivot { get; set; }
private void Start()
{
isHandRotationAvailable = true;
#if UNITY_WSA && UNITY_2017_2_OR_NEWER
if (HolographicSettings.IsDisplayOpaque == false)
{
isHandRotationAvailable = false;
}
#endif
cachedRenderer = gameObject.GetComponent<Renderer>();
}
private void Update()
{
if (inputDownEventData != null)
{
Vector3 currentHandPosition = Vector3.zero;
Quaternion currentHandOrientation = Quaternion.identity;
//set values from hand
currentHandPosition = GetHandPosition(inputDownEventData.SourceId);
if (isHandRotationAvailable)
{
currentHandOrientation = GetHandOrientation(inputDownEventData.SourceId);
}
//calculate affines
if (this.AffineType == BoundingBoxGizmoHandleTransformType.Scale)
{
ApplyScale(currentHandPosition);
}
else if (this.AffineType == BoundingBoxGizmoHandleTransformType.Rotation)
{
if (isHandRotationAvailable && handMotionForRotation == BoundingBoxGizmoHandleHandMotionType.handRotatesToRotateObject)
{
ApplyRotation(currentHandOrientation);
}
else
{
ApplyRotation(currentHandPosition);
}
}
lastHandWorldPos = currentHandPosition;
}
}
private Vector3 GetHandPosition(uint sourceId)
{
Vector3 handPosition = new Vector3(0, 0, 0);
inputDownEventData.InputSource.TryGetGripPosition(sourceId, out handPosition);
return handPosition;
}
private Quaternion GetHandOrientation(uint sourceId)
{
Quaternion handOrientation = Quaternion.identity;
inputDownEventData.InputSource.TryGetGripRotation(sourceId, out handOrientation);
return handOrientation;
}
private void ApplyScale(Vector3 currentHandPosition)
{
if ((transformToAffect.position - initialHandPosition).magnitude > minimumScaleNav)
{
float scaleScalar = (currentHandPosition - transformToAffect.position).magnitude / (transformToAffect.position - initialHandPosition).magnitude;
scaleScalar = Mathf.Pow(scaleScalar, scaleRate);
Vector3 changeScale = new Vector3(scaleScalar, scaleScalar, scaleScalar);
changeScale = GetBoundedScaleChange(changeScale);
Vector3 newScale = changeScale;
newScale.Scale(initialScale);
//scale from object center
transformToAffect.localScale = newScale;
//now handle offset
Vector3 currentScaleOrigin = initialScaleOrigin;
currentScaleOrigin.Scale(changeScale);
Vector3 postScaleOffset = currentScaleOrigin - initialScaleOrigin;
//translate so that scale is effectively from opposite corner
transformToAffect.position = initialPosition - postScaleOffset;
}
}
private void ApplyRotation(Quaternion currentHandOrientation)
{
#if UNITY_2017_1_OR_NEWER
Matrix4x4 m = Matrix4x4.Rotate(initialHandOrientation);
Vector3 initRay = new Vector3(0, 0, 1);
initRay = m.MultiplyPoint(initRay);
initRay.Normalize();
m = Matrix4x4.Rotate(currentHandOrientation);
Vector3 currentRay = new Vector3(0, 0, 1);
currentRay = m.MultiplyPoint(currentRay);
currentRay.Normalize();
float angle = Vector3.Dot(initRay, currentRay);
angle = Mathf.Acos(angle) * Mathf.Rad2Deg;
if (Mathf.Abs(initRay.y - currentRay.y) < Mathf.Abs(initRay.x - currentRay.x))
{
if (Vector3.Cross(initRay, currentRay).y > 0)
{
angle = -angle;
}
}
else if (Vector3.Cross(initRay, currentRay).x > 0)
{
angle = -angle;
}
if (rotationCoordinateSystem == BoundingBoxGizmoHandleRotationType.globalCoordinates)
{
Vector3 newEulers = (Axis == BoundingBoxGizmoHandleAxisToAffect.X ? new Vector3(angle, 0, 0) : Axis == BoundingBoxGizmoHandleAxisToAffect.Y ? new Vector3(0, angle, 0) : new Vector3(0, 0, angle));
newEulers += initialOrientation;
transformToAffect.rotation = Quaternion.Euler(newEulers);
}
else
{
Vector3 axis = (Axis == BoundingBoxGizmoHandleAxisToAffect.X ? new Vector3(1, 0, 0) : Axis == BoundingBoxGizmoHandleAxisToAffect.Y ? new Vector3(0, 1, 0) : new Vector3(0, 0, 1));
transformToAffect.localRotation = initialRotation;
transformToAffect.Rotate(axis, angle * 5.0f);
}
#endif // UNITY_2017_1_OR_NEWER
}
private void ApplyRotation(Vector3 currentHandPosition)
{
if (RotateAroundPivot)
{
ApplyRotationPivot(currentHandPosition);
}
else
{
ApplyRotationContinuous(currentHandPosition);
}
}
private void ApplyRotationContinuous(Vector3 currentHandPosition)
{
Vector3 initialRay = initialHandPosition - transformToAffect.position;
initialRay.Normalize();
Vector3 currentRay = currentHandPosition - transformToAffect.position;
currentRay.Normalize();
Vector3 delta = currentRay - initialRay;
delta.Scale(rotationFromPositionScale);
Vector3 newEulers = new Vector3(0, 0, 0);
if (Axis == BoundingBoxGizmoHandleAxisToAffect.X)
{
newEulers = new Vector3(-delta.y, 0, 0);
}
else if (Axis == BoundingBoxGizmoHandleAxisToAffect.Y)
{
newEulers = new Vector3(0, delta.x, 0);
}
else if (Axis == BoundingBoxGizmoHandleAxisToAffect.Z)
{
newEulers = new Vector3(0, 0, delta.y);
}
if (IsLeftHandedRotation)
{
newEulers.Scale(new Vector3(-1.0f, -1.0f, -1.0f));
}
if (rotationCoordinateSystem == BoundingBoxGizmoHandleRotationType.globalCoordinates)
{
newEulers += initialOrientation;
transformToAffect.rotation = Quaternion.Euler(newEulers);
}
else
{
Vector3 axis = (Axis == BoundingBoxGizmoHandleAxisToAffect.X ? new Vector3(1, 0, 0) : Axis == BoundingBoxGizmoHandleAxisToAffect.Y ? new Vector3(0, 1, 0) : new Vector3(0, 0, 1));
transformToAffect.localRotation = initialRotation;
float angle = newEulers.x != 0 ? newEulers.x : newEulers.y != 0 ? newEulers.y : newEulers.z;
transformToAffect.Rotate(axis, angle * 2.0f);
}
}
private void ApplyRotationPivot(Vector3 currentHandPosition)
{
Vector3 delta = currentHandPosition - lastHandWorldPos;
if (delta.sqrMagnitude == 0) { return; }
delta.Scale(rotationFromPositionScale);
var pivotToHandleDir = (transform.position - transformToAffect.position).normalized;
switch (Axis)
{
default:
case BoundingBoxGizmoHandleAxisToAffect.X:
transformToAffect.Rotate(Vector3.right, Vector3.Dot(delta, Vector3.Cross(pivotToHandleDir, transformToAffect.right)), Space.Self);
break;
case BoundingBoxGizmoHandleAxisToAffect.Y:
transformToAffect.Rotate(Vector3.up, Vector3.Dot(delta, Vector3.Cross(pivotToHandleDir, transformToAffect.up)), Space.Self);
break;
case BoundingBoxGizmoHandleAxisToAffect.Z:
transformToAffect.Rotate(Vector3.forward, Vector3.Dot(delta, Vector3.Cross(pivotToHandleDir, transformToAffect.forward)), Space.Self);
break;
}
}
private Vector3 GetBoundedScaleChange(Vector3 scale)
{
Vector3 maximumScale = new Vector3(initialScale.x * maxScale, initialScale.y * maxScale, initialScale.z * maxScale);
Vector3 intendedFinalScale = new Vector3(initialScale.x, initialScale.y, initialScale.z);
intendedFinalScale.Scale(scale);
if (intendedFinalScale.x > maximumScale.x || intendedFinalScale.y > maximumScale.y || intendedFinalScale.z > maximumScale.z)
{
return new Vector3(maximumScale.x / initialScale.x, maximumScale.y / initialScale.y, maximumScale.z / initialScale.z);
}
return scale;
}
private Renderer cachedRenderer;
private void ResetRigHandles()
{
inputDownEventData = null;
if (this.AffineType == BoundingBoxGizmoHandleTransformType.Scale)
{
cachedRenderer.sharedMaterial = Rig.ScaleHandleMaterial;
}
else
{
cachedRenderer.sharedMaterial = Rig.RotateHandleMaterial;
}
HoloToolkit.Unity.InputModule.InputManager.Instance.PopModalInputHandler();
Rig.FocusOnHandle(null);
}
public void OnInputDown(InputEventData eventData)
{
inputDownEventData = eventData;
initialHandPosition = GetHandPosition(eventData.SourceId);
lastHandWorldPos = initialHandPosition;
initialScale = transformToAffect.localScale;
initialPosition = transformToAffect.position;
initialOrientation = transformToAffect.rotation.eulerAngles;
initialRotation = transformToAffect.rotation;
initialHandOrientation = GetHandOrientation(eventData.SourceId);
initialScaleOrigin = transformToAffect.position - this.transform.position;
HoloToolkit.Unity.InputModule.InputManager.Instance.PushModalInputHandler(gameObject);
cachedRenderer.sharedMaterial = Rig.InteractingMaterial;
Rig.FocusOnHandle(this.gameObject);
eventData.Use();
}
public void OnInputUp(InputEventData eventData)
{
inputDownEventData = null;
ResetRigHandles();
if (eventData != null)
{
eventData.Use();
}
}
public void OnSourceDetected(SourceStateEventData eventData)
{
}
public void OnSourceLost(SourceStateEventData eventData)
{
if ((inputDownEventData != null) &&
(eventData.SourceId == inputDownEventData.SourceId))
{
inputDownEventData = null;
ResetRigHandles();
eventData.Use();
}
}
}
}