-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
BoundingBoxRig.cs
618 lines (534 loc) · 21.6 KB
/
BoundingBoxRig.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// 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 System.Collections.Generic;
using UnityEngine;
namespace HoloToolkit.Unity.UX
{
/// <summary>
/// Constructs the scale and rotate gizmo handles for the Bounding Box
/// </summary>
public class BoundingBoxRig : MonoBehaviour
{
[Header("Flattening")]
[SerializeField]
[Tooltip("Choose this option if Rig is to be applied to a 2D object.")]
private BoundingBox.FlattenModeEnum flattenedAxis = default(BoundingBox.FlattenModeEnum);
public BoundingBox.FlattenModeEnum FlattenedAxis
{
get
{
return flattenedAxis;
}
set
{
flattenedAxis = value;
}
}
[Header("Customization Settings")]
[SerializeField]
private Material scaleHandleMaterial;
public Material ScaleHandleMaterial
{
get
{
return scaleHandleMaterial;
}
set
{
scaleHandleMaterial = value;
}
}
[SerializeField]
private Material rotateHandleMaterial;
public Material RotateHandleMaterial
{
get
{
return rotateHandleMaterial;
}
set
{
rotateHandleMaterial = value;
}
}
[SerializeField]
private Material interactingMaterial;
public Material InteractingMaterial
{
get
{
return interactingMaterial;
}
set
{
interactingMaterial = value;
}
}
[Header("Behavior")]
[SerializeField]
private float scaleRate = 1.0f;
public float ScaleRate
{
get
{
return scaleRate;
}
set
{
scaleRate = value;
}
}
[SerializeField]
private float appBarHoverOffsetZ = 0.05f;
public float AppBarHoverOffsetZ
{
get
{
return appBarHoverOffsetZ;
}
set
{
appBarHoverOffsetZ = value;
}
}
[SerializeField]
[Tooltip("This is the maximum scale that one grab can accomplish.")]
private float maxScale = 2.0f;
public float MaxScale
{
get
{
return maxScale;
}
set
{
maxScale = value;
}
}
[SerializeField]
private BoundingBoxGizmoHandleRotationType rotationType = BoundingBoxGizmoHandleRotationType.objectCoordinates;
public BoundingBoxGizmoHandleRotationType RotationType
{
get
{
return rotationType;
}
set
{
rotationType = value;
}
}
[SerializeField]
private BoundingBoxGizmoHandleHandMotionType handMotionToRotate = BoundingBoxGizmoHandleHandMotionType.handRotatesToRotateObject;
public BoundingBoxGizmoHandleHandMotionType HandMotionToRotate
{
get
{
return handMotionToRotate;
}
set
{
handMotionToRotate = value;
}
}
[SerializeField]
private bool rotateAroundPivot = false;
public bool RotateAroundPivot
{
get
{
return rotateAroundPivot;
}
set
{
rotateAroundPivot = value;
}
}
[Header("Preset Components")]
[SerializeField]
[Tooltip("To visualize the object bounding box, drop the MixedRealityToolkit/UX/Prefabs/BoundingBoxes/BoundingBoxBasic.prefab here.")]
private BoundingBox boundingBoxPrefab;
public BoundingBox BoundingBoxPrefab
{
get
{
return boundingBoxPrefab;
}
set
{
boundingBoxPrefab = value;
}
}
[SerializeField]
[Tooltip("AppBar prefab.")]
private AppBar appBarPrefab = null;
public AppBar AppBarPrefab
{
get
{
return appBarPrefab;
}
set
{
appBarPrefab = value;
}
}
private BoundingBox boxInstance;
private GameObject objectToBound;
private AppBar appBarInstance;
private GameObject[] rotateHandles;
private GameObject[] cornerHandles;
private List<Vector3> handleCentroids;
private GameObject transformRig;
private BoundingBoxGizmoHandle[] rigScaleGizmoHandles;
private BoundingBoxGizmoHandle[] rigRotateGizmoHandles;
private bool showRig = false;
private Vector3 scaleHandleSize = new Vector3(0.04f, 0.04f, 0.04f);
private Vector3 rotateHandleSize = new Vector3(0.04f, 0.04f, 0.04f);
private bool destroying = false;
public void Activate()
{
InputManager.Instance.RaiseBoundingBoxRigActivated(gameObject);
ShowRig = true;
}
public void Deactivate()
{
InputManager.Instance.RaiseBoundingBoxRigDeactivated(gameObject);
ShowRig = false;
}
public void FocusOnHandle(GameObject handle)
{
if (handle != null)
{
for (int i = 0; i < rotateHandles.Length; ++i)
{
rotateHandles[i].SetActive(rotateHandles[i].gameObject == handle);
}
for (int i = 0; i < cornerHandles.Length; ++i)
{
cornerHandles[i].SetActive(cornerHandles[i].gameObject == handle);
}
}
else
{
for (int i = 0; i < rotateHandles.Length; ++i)
{
rotateHandles[i].SetActive(true);
}
for (int i = 0; i < cornerHandles.Length; ++i)
{
cornerHandles[i].SetActive(true);
}
}
}
private void Start()
{
objectToBound = this.gameObject;
boxInstance = Instantiate(BoundingBoxPrefab) as BoundingBox;
boxInstance.Target = objectToBound;
boxInstance.FlattenPreference = flattenedAxis;
BuildRig();
appBarInstance = Instantiate(appBarPrefab) as AppBar;
appBarInstance.BoundingBox = boxInstance;
appBarInstance.HoverOffsetZ = appBarHoverOffsetZ;
boxInstance.IsVisible = false;
}
private void Update()
{
if (destroying == false && ShowRig)
{
UpdateBoundsPoints();
UpdateHandles();
}
}
private void OnDestroy()
{
destroying = true;
ShowRig = false;
ClearHandles();
}
private void UpdateBoundsPoints()
{
handleCentroids = GetBounds();
}
private void CreateHandles()
{
ClearHandles();
UpdateCornerHandles();
UpdateRotateHandles();
ParentHandles();
UpdateHandles();
}
private void UpdateCornerHandles()
{
if (handleCentroids != null)
{
GetBounds();
}
if (cornerHandles == null)
{
cornerHandles = new GameObject[handleCentroids.Count];
rigScaleGizmoHandles = new BoundingBoxGizmoHandle[handleCentroids.Count];
for (int i = 0; i < cornerHandles.Length; ++i)
{
cornerHandles[i] = GameObject.CreatePrimitive(PrimitiveType.Cube);
cornerHandles[i].GetComponent<Renderer>().material = scaleHandleMaterial;
cornerHandles[i].transform.localScale = scaleHandleSize;
cornerHandles[i].name = "Corner " + i.ToString();
rigScaleGizmoHandles[i] = cornerHandles[i].AddComponent<BoundingBoxGizmoHandle>();
rigScaleGizmoHandles[i].Rig = this;
rigScaleGizmoHandles[i].ScaleRate = scaleRate;
rigScaleGizmoHandles[i].MaxScale = maxScale;
rigScaleGizmoHandles[i].TransformToAffect = objectToBound.transform;
rigScaleGizmoHandles[i].Axis = BoundingBoxGizmoHandleAxisToAffect.Y;
rigScaleGizmoHandles[i].AffineType = BoundingBoxGizmoHandleTransformType.Scale;
}
}
for (int i = 0; i < cornerHandles.Length; ++i)
{
cornerHandles[i].transform.position = handleCentroids[i];
cornerHandles[i].transform.localRotation = objectToBound.transform.rotation;
}
}
private void UpdateRotateHandles()
{
if (handleCentroids != null)
{
GetBounds();
}
if (rotateHandles == null)
{
rotateHandles = new GameObject[12];
rigRotateGizmoHandles = new BoundingBoxGizmoHandle[12];
for (int i = 0; i < rotateHandles.Length; ++i)
{
rotateHandles[i] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
rotateHandles[i].GetComponent<Renderer>().material = rotateHandleMaterial;
rotateHandles[i].transform.localScale = rotateHandleSize;
rotateHandles[i].name = "Middle " + i.ToString();
rigRotateGizmoHandles[i] = rotateHandles[i].AddComponent<BoundingBoxGizmoHandle>();
rigRotateGizmoHandles[i].RotateAroundPivot = rotateAroundPivot;
rigRotateGizmoHandles[i].Rig = this;
rigRotateGizmoHandles[i].HandMotionForRotation = handMotionToRotate;
rigRotateGizmoHandles[i].RotationCoordinateSystem = rotationType;
rigRotateGizmoHandles[i].TransformToAffect = objectToBound.transform;
rigRotateGizmoHandles[i].AffineType = BoundingBoxGizmoHandleTransformType.Rotation;
}
//set axis to affect
rigRotateGizmoHandles[0].Axis = BoundingBoxGizmoHandleAxisToAffect.Y;
rigRotateGizmoHandles[1].Axis = BoundingBoxGizmoHandleAxisToAffect.Y;
rigRotateGizmoHandles[2].Axis = BoundingBoxGizmoHandleAxisToAffect.Y;
rigRotateGizmoHandles[3].Axis = BoundingBoxGizmoHandleAxisToAffect.Y;
rigRotateGizmoHandles[4].Axis = BoundingBoxGizmoHandleAxisToAffect.Z;
rigRotateGizmoHandles[5].Axis = BoundingBoxGizmoHandleAxisToAffect.Z;
rigRotateGizmoHandles[6].Axis = BoundingBoxGizmoHandleAxisToAffect.Z;
rigRotateGizmoHandles[7].Axis = BoundingBoxGizmoHandleAxisToAffect.Z;
rigRotateGizmoHandles[8].Axis = BoundingBoxGizmoHandleAxisToAffect.X;
rigRotateGizmoHandles[9].Axis = BoundingBoxGizmoHandleAxisToAffect.X;
rigRotateGizmoHandles[10].Axis = BoundingBoxGizmoHandleAxisToAffect.X;
rigRotateGizmoHandles[11].Axis = BoundingBoxGizmoHandleAxisToAffect.X;
//set lefthandedness
rigRotateGizmoHandles[0].IsLeftHandedRotation = false;
rigRotateGizmoHandles[1].IsLeftHandedRotation = false;
rigRotateGizmoHandles[2].IsLeftHandedRotation = false;
rigRotateGizmoHandles[3].IsLeftHandedRotation = false;
rigRotateGizmoHandles[4].IsLeftHandedRotation = false;
rigRotateGizmoHandles[5].IsLeftHandedRotation = false;
rigRotateGizmoHandles[6].IsLeftHandedRotation = true;
rigRotateGizmoHandles[7].IsLeftHandedRotation = true;
rigRotateGizmoHandles[8].IsLeftHandedRotation = false;
rigRotateGizmoHandles[9].IsLeftHandedRotation = true;
rigRotateGizmoHandles[10].IsLeftHandedRotation = false;
rigRotateGizmoHandles[11].IsLeftHandedRotation = true;
}
rotateHandles[0].transform.localPosition = (handleCentroids[2] + handleCentroids[0]) * 0.5f;
rotateHandles[1].transform.localPosition = (handleCentroids[3] + handleCentroids[1]) * 0.5f;
rotateHandles[2].transform.localPosition = (handleCentroids[6] + handleCentroids[4]) * 0.5f;
rotateHandles[3].transform.localPosition = (handleCentroids[7] + handleCentroids[5]) * 0.5f;
rotateHandles[4].transform.localPosition = (handleCentroids[0] + handleCentroids[1]) * 0.5f;
rotateHandles[5].transform.localPosition = (handleCentroids[2] + handleCentroids[3]) * 0.5f;
rotateHandles[6].transform.localPosition = (handleCentroids[4] + handleCentroids[5]) * 0.5f;
rotateHandles[7].transform.localPosition = (handleCentroids[6] + handleCentroids[7]) * 0.5f;
rotateHandles[8].transform.localPosition = (handleCentroids[0] + handleCentroids[4]) * 0.5f;
rotateHandles[9].transform.localPosition = (handleCentroids[1] + handleCentroids[5]) * 0.5f;
rotateHandles[10].transform.localPosition = (handleCentroids[2] + handleCentroids[6]) * 0.5f;
rotateHandles[11].transform.localPosition = (handleCentroids[3] + handleCentroids[7]) * 0.5f;
}
private void ParentHandles()
{
transformRig.transform.position = boxInstance.transform.position;
transformRig.transform.rotation = boxInstance.transform.rotation;
Vector3 invScale = objectToBound.transform.localScale;
transformRig.transform.localScale = new Vector3(0.5f / invScale.x, 0.5f / invScale.y, 0.5f / invScale.z);
transformRig.transform.parent = objectToBound.transform;
}
private void UpdateHandles()
{
UpdateCornerHandles();
UpdateRotateHandles();
}
private void ClearCornerHandles()
{
if (cornerHandles != null)
{
for (int i = 0; i < cornerHandles.Length; ++i)
{
GameObject.Destroy(cornerHandles[i]);
}
cornerHandles = null;
handleCentroids = null;
}
cornerHandles = null;
handleCentroids = null;
}
private void ClearRotateHandles()
{
if (rotateHandles != null && rotateHandles.Length > 0 && rotateHandles[0] != null)
{
for (int i = 0; i < rotateHandles.Length; ++i)
{
if (rotateHandles[i] != null)
{
Destroy(rotateHandles[i]);
rotateHandles[i] = null;
}
}
}
rotateHandles = null;
}
private void ClearHandles()
{
ClearCornerHandles();
ClearRotateHandles();
}
private GameObject BuildRig()
{
Vector3 scale = objectToBound.transform.localScale;
GameObject rig = new GameObject();
rig.name = "center";
rig.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
rig.transform.localScale = new Vector3(1.0f / scale.x, 1.0f / scale.y, 1.0f / scale.z);
GameObject upperLeftFront = new GameObject();
upperLeftFront.name = "upperleftfront";
upperLeftFront.transform.SetPositionAndRotation(new Vector3(0.5f, 0.5f, 0.5f), Quaternion.identity);
upperLeftFront.transform.localScale = new Vector3(1, 1, 1);
upperLeftFront.transform.parent = rig.transform;
GameObject upperLeftBack = new GameObject();
upperLeftBack.name = "upperleftback";
upperLeftBack.transform.SetPositionAndRotation(new Vector3(0.5f, 0.5f, -0.5f), Quaternion.identity);
upperLeftBack.transform.localScale = new Vector3(1, 1, 1);
upperLeftBack.transform.parent = rig.transform;
GameObject lowerLeftFront = new GameObject();
lowerLeftFront.name = "lowerleftfront";
lowerLeftFront.transform.SetPositionAndRotation(new Vector3(0.5f, -0.5f, 0.5f), Quaternion.identity);
lowerLeftFront.transform.localScale = new Vector3(1, 1, 1);
lowerLeftFront.transform.parent = rig.transform;
GameObject lowerLeftBack = new GameObject();
lowerLeftBack.name = "lowerleftback";
lowerLeftBack.transform.SetPositionAndRotation(new Vector3(0.5f, -0.5f, -0.5f), Quaternion.identity);
lowerLeftBack.transform.localScale = new Vector3(1, 1, 1);
lowerLeftBack.transform.parent = rig.transform;
GameObject upperRightFront = new GameObject();
upperRightFront.name = "upperrightfront";
upperRightFront.transform.SetPositionAndRotation(new Vector3(-0.5f, 0.5f, 0.5f), Quaternion.identity);
upperRightFront.transform.localScale = new Vector3(1, 1, 1);
upperRightFront.transform.parent = rig.transform;
GameObject upperRightBack = new GameObject();
upperRightBack.name = "upperrightback";
upperRightBack.transform.SetPositionAndRotation(new Vector3(-0.5f, 0.5f, -0.5f), Quaternion.identity);
upperRightBack.transform.localScale = new Vector3(1, 1, 1);
upperRightBack.transform.parent = rig.transform;
GameObject lowerRightFront = new GameObject();
lowerRightFront.name = "lowerrightfront";
lowerRightFront.transform.SetPositionAndRotation(new Vector3(-0.5f, -0.5f, 0.5f), Quaternion.identity);
lowerRightFront.transform.localScale = new Vector3(1, 1, 1);
lowerRightFront.transform.parent = rig.transform;
GameObject lowerRightBack = new GameObject();
lowerRightBack.name = "lowerrightback";
lowerRightBack.transform.SetPositionAndRotation(new Vector3(-0.5f, -0.5f, -0.5f), Quaternion.identity);
lowerRightBack.transform.localScale = new Vector3(1, 1, 1);
lowerRightBack.transform.parent = rig.transform;
transformRig = rig;
return rig;
}
private bool ShowRig
{
get
{
return showRig;
}
set
{
if (destroying == false)
{
if (value == true)
{
UpdateBoundsPoints();
UpdateHandles();
}
if (boxInstance != null)
{
boxInstance.IsVisible = value;
}
if (cornerHandles != null && rotateHandles != null)
{
foreach (GameObject handle in cornerHandles)
{
handle.SetActive(value);
}
foreach (GameObject handle in rotateHandles)
{
handle.SetActive(value);
}
}
showRig = value;
}
}
}
public List<Vector3> GetBounds()
{
if (objectToBound != null)
{
List<Vector3> bounds = new List<Vector3>();
LayerMask mask = new LayerMask();
GameObject clone = GameObject.Instantiate(boxInstance.gameObject);
clone.transform.localRotation = Quaternion.identity;
clone.transform.position = Vector3.zero;
BoundingBox.GetMeshFilterBoundsPoints(clone, bounds, mask);
Vector3 centroid = boxInstance.TargetBoundsCenter;
GameObject.Destroy(clone);
#if UNITY_2017_1_OR_NEWER
Matrix4x4 m = Matrix4x4.Rotate(objectToBound.transform.rotation);
for (int i = 0; i < bounds.Count; ++i)
{
bounds[i] = m.MultiplyPoint(bounds[i]);
bounds[i] += boxInstance.TargetBoundsCenter;
}
#endif // UNITY_2017_1_OR_NEWER
return bounds;
}
return null;
}
private BoundingBox.FlattenModeEnum GetBestAxisToFlatten()
{
int index = handleCentroids.Count - 8;
float width = (handleCentroids[index + 0] - handleCentroids[index + 4]).magnitude;
float height = (handleCentroids[index + 0] - handleCentroids[index + 2]).magnitude;
float depth = (handleCentroids[index + 0] - handleCentroids[index + 1]).magnitude;
if (width < height && width < depth)
{
return BoundingBox.FlattenModeEnum.FlattenX;
}
else if (height < width && height < depth)
{
return BoundingBox.FlattenModeEnum.FlattenY;
}
else if (depth < height && depth < width)
{
return BoundingBox.FlattenModeEnum.FlattenZ;
}
return BoundingBox.FlattenModeEnum.DoNotFlatten;
}
}
}