AR Design
UBC EML collab with UBC SALA - visualizing IoT data in AR
SlicingPlaneController.cs
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License. See LICENSE in the project root for license information.
3 
5 using UnityEngine;
6 
7 namespace HoloToolkit.Unity
8 {
13  public class SlicingPlaneController : MonoBehaviour, IManipulationHandler
14  {
15  public float KeyboardMovementSpeed = 1.5f;
16  public float GestureMovementSpeed = 1.0f;
17 
18  public GameObject PlaneX;
19  public GameObject PlaneY;
20  public GameObject PlaneZ;
21 
22  private bool wasThickSliceEnabled;
23 
24  public bool ThickSliceEnabled = true;
25 
26  public Material SliceMaterial;
27  public Material ThickSliceMaterial;
28 
29  public void SetMaterial(Material mat)
30  {
31  PlaneX.GetComponent<Renderer>().sharedMaterial = mat;
32  PlaneY.GetComponent<Renderer>().sharedMaterial = mat;
33  PlaneZ.GetComponent<Renderer>().sharedMaterial = mat;
34  }
35 
36  private void OnEnable()
37  {
38  //force initial material setting in case user has made strange editor changes
39  wasThickSliceEnabled = !ThickSliceEnabled;
40 
41  InputManager.Instance.AddGlobalListener(this.gameObject);
42  }
43 
44  private void OnDisable()
45  {
46  InputManager.Instance.RemoveGlobalListener(this.gameObject);
47  }
48 
49  public void SetThickSliceEnabled(bool on)
50  {
51  this.ThickSliceEnabled = on;
52  }
53 
54  private void Update()
55  {
56  HandleDebugKeys();
57 
58  Vector4 planeEquation;
59 
60  var forward = this.transform.forward;
61  var pos = this.transform.position;
62 
63  planeEquation.x = forward.x;
64  planeEquation.y = forward.y;
65  planeEquation.z = forward.z;
66 
67  //dot product
68  planeEquation.w = (planeEquation.x * pos.x +
69  planeEquation.y * pos.y +
70  planeEquation.z * pos.z);
71 
72  Shader.SetGlobalVector("CutPlane", planeEquation);
73 
74  var cornerPos = this.transform.localPosition;
75  var slabMin = new Vector4(cornerPos.x - 0.5f, cornerPos.y - 0.5f, cornerPos.z - 0.5f, 0.0f);
76  var slabMax = new Vector4(cornerPos.x + 0.5f, cornerPos.y + 0.5f, cornerPos.z + 0.5f, 0.0f);
77 
78  Shader.SetGlobalVector("SlabMin", slabMin);
79  Shader.SetGlobalVector("SlabMax", slabMax);
80 
81  if (ThickSliceEnabled != wasThickSliceEnabled)
82  {
83  SetMaterial(ThickSliceEnabled ? ThickSliceMaterial : SliceMaterial);
84  wasThickSliceEnabled = ThickSliceEnabled;
85  }
86 
87  Shader.SetGlobalMatrix("_SlicingWorldToLocal", this.transform.worldToLocalMatrix);
88  }
89 
90  void HandleDebugKeys()
91  {
92  float movementDelta = KeyboardMovementSpeed * Time.deltaTime;
93 
94  var positionDeltaX = this.transform.localRotation * new Vector3(movementDelta, 0.0f, 0.0f);
95  var positionDeltaY = this.transform.localRotation * new Vector3(0.0f, movementDelta, 0.0f);
96  var positionDeltaZ = this.transform.localRotation * new Vector3(0.0f, 0.0f, movementDelta);
97 
98  if (Input.GetKey(KeyCode.L)) { this.transform.localPosition += positionDeltaX; }
99  if (Input.GetKey(KeyCode.J)) { this.transform.localPosition -= positionDeltaX; }
100 
101  if (Input.GetKey(KeyCode.I)) { this.transform.localPosition += positionDeltaY; }
102  if (Input.GetKey(KeyCode.K)) { this.transform.localPosition -= positionDeltaY; }
103 
104  if (Input.GetKey(KeyCode.RightBracket)) { this.transform.localPosition += positionDeltaZ; }
105  if (Input.GetKey(KeyCode.LeftBracket)) { this.transform.localPosition -= positionDeltaZ; }
106  }
108  {
109  }
110 
112  {
113  float movementDelta = GestureMovementSpeed * Time.deltaTime;
114 
115  //TODO: consider tether filter
116  this.transform.localPosition += this.transform.localRotation * eventData.CumulativeDelta * movementDelta;
117  }
118 
120  {
121  }
122 
124  {
125  }
126  }
127 }
Input Manager is responsible for managing input sources and dispatching relevant events to the approp...
Definition: InputManager.cs:19
void OnManipulationStarted(ManipulationEventData eventData)
Describes an input event that involves content manipulation.
void OnManipulationCompleted(ManipulationEventData eventData)
void OnManipulationCanceled(ManipulationEventData eventData)
Controls slicing planes for use in volumetric rendering Planes represent manipulatable viewable regio...
Vector3 CumulativeDelta
The amount of manipulation that has occurred. Usually in the form of delta position of a hand...
static T Instance
Returns the Singleton instance of the classes type. If no instance is found, then we search for an in...
Definition: Singleton.cs:26
void OnManipulationUpdated(ManipulationEventData eventData)
Interface to implement to react to manipulation gestures.