IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
AnnotationMoveVive.cs
1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.EventSystems;
4 
5 public class AnnotationMoveVive : MonoBehaviour {
6 
7 
8  //Vive moving attributs
9  private bool move;
10  private bool rotateWithVive;
11  private GameObject controllerRight;
12  private GameObject controllerLeft;
13  private Vector3 vecToMeshCenterLocal;
14  private Vector3 hitPointStartWorld;
15  private Vector3 hitPointStartLocal;
16  private Vector3 prevHitPointScale;
17  private Vector3 prevleftPos;
18  private Vector3 prevRightPos;
19  private Vector3 startScale;
20  private float disControllerToMesh;
21  private Quaternion offsetRot;
22  private Quaternion annotationRot;
23 
24  public float maxScale = 50f;
25  public float minScale = 5f;
26  public float twoControllerRescaleFactor = 4f;
27 
28  // Use this for initialization
29  void Start () {
30 
31  }
32 
33  // Update is called once per frame
34  void Update () {
35  if(move) {
36  moveWithVive3 ();
37  }
38  }
39 
40  private void dropMovement () {
41  move = false;
42  controllerRight = null;
43  //Movement active
44  GetComponentInParent<Annotation>().ViveMovement(false);
45  this.GetComponentInParent<Annotation> ().saveAnnotationChanges ();
46 
47 
48  }
49 
50 
51  public void AnnotationClicked(PointerEventData eventData, bool rotateVive) {
52  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
53  if (inputDevice.getDeviceType () == InputDeviceManager.InputDeviceType.ViveController) {
54  rotateWithVive = rotateVive;
55  hitPointStartWorld = eventData.pointerCurrentRaycast.worldPosition;
56  hitPointStartLocal = this.transform.InverseTransformPoint (hitPointStartWorld);
57  Vector3 vecToMeshCenter = this.transform.position - hitPointStartWorld;
58  startScale = this.transform.localScale;
59  vecToMeshCenterLocal = this.transform.InverseTransformDirection (vecToMeshCenter);
60 
61  //Movement active
62  GetComponentInParent<Annotation>().ViveMovement(true);
63 
64  moveAnnotationWithVive3 ();
65  }
66  }
67 
68 
69 
70 
71 
72  //########### Vive movement method 1
73 
74  //Called by annotation click with vive
75  private void moveAnnotationWithVive1 () {
76  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
77  if (inputDevice.getDeviceType () == InputDeviceManager.InputDeviceType.ViveController) {
78  GameObject.Find("GlobalScript").GetComponent<HierarchicalInputModule>().disableLayer("UIOrgans");
79  controllerRight = (inputDevice as Controller).gameObject;
80  disControllerToMesh = (hitPointStartWorld - controllerRight.transform.position).magnitude;
81  annotationRot = this.transform.rotation;
82  move = true;
83  }
84  }
85 
86  //Called every Frame
87  private void moveWithVive1 () {
88  if (controllerRight.GetComponent<Controller> ().triggerPressed ()) {
89  Quaternion newRotation = controllerRight.transform.rotation;
90  this.transform.rotation = newRotation * Quaternion.Inverse(offsetRot) * annotationRot;
91  transform.parent.position = controllerRight.transform.position
92  + controllerRight.transform.forward.normalized * disControllerToMesh
93  + this.transform.TransformDirection(vecToMeshCenterLocal);
94  } else {
95  dropMovement ();
96  }
97 
98  }
99 
100  //########### Vive movement method 2
101 
102  //Called by annotation click with vive
103  private void moveAnnotationWithVive2 () {
104  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
105  if (inputDevice.getDeviceType () == InputDeviceManager.InputDeviceType.ViveController) {
106  GameObject.Find("GlobalScript").GetComponent<HierarchicalInputModule>().disableLayer("UIOrgans");
107  controllerRight = (inputDevice as Controller).gameObject;
108  disControllerToMesh = (hitPointStartWorld - controllerRight.transform.position).magnitude;
109  offsetRot = controllerRight.transform.rotation;
110  annotationRot = this.transform.rotation;
111  move = true;
112  }
113  }
114 
115  //Called every Frame
116  private void moveWithVive2 () {
117  if (controllerRight.GetComponent<Controller> ().triggerPressed ()) {
118  if(rotateWithVive) {
119  //rotate
120  Vector3 hitPoint = this.transform.InverseTransformPoint(controllerRight.transform.position + controllerRight.transform.forward.normalized * disControllerToMesh);
121  Vector3 oldDir = hitPointStartLocal - this.transform.localPosition;
122  Vector3 newDir = hitPoint - this.transform.localPosition;
123  Vector3 rotAxis = Vector3.Cross (oldDir, newDir);
124  float angle = Vector3.Angle (oldDir, newDir);
125  this.transform.localRotation *= Quaternion.AngleAxis(angle, rotAxis);
126 
127  } else {
128  //move
129  transform.parent.position = controllerRight.transform.position
130  + controllerRight.transform.forward.normalized * disControllerToMesh
131  + this.transform.TransformDirection(vecToMeshCenterLocal);
132  }
133  } else {
134  dropMovement ();
135  }
136 
137  }
138 
139 
140  //########### Vive movement method 3 (kombo method 1 & 2)
141 
142  //Called by annotation click with vive
143  private void moveAnnotationWithVive3 () {
144  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
145  GameObject.Find("GlobalScript").GetComponent<HierarchicalInputModule>().disableLayer("UIOrgans");
146  controllerRight = (inputDevice as Controller).gameObject;
147  disControllerToMesh = (hitPointStartWorld - controllerRight.transform.position).magnitude;
148  offsetRot = controllerRight.transform.rotation;
149  annotationRot = this.transform.rotation;
150  move = true;
151  }
152 
153  //Called every Frame
154  private void moveWithVive3 () {
155  if (controllerRight.GetComponent<Controller> ().triggerPressed ()) {
156  if(rotateWithVive) {
157  //rotate
158  Vector3 hitPoint = this.transform.InverseTransformPoint(controllerRight.transform.position + controllerRight.transform.forward.normalized * disControllerToMesh);
159  Vector3 oldDir = hitPointStartLocal - this.transform.localPosition;
160  Vector3 newDir = hitPoint - this.transform.localPosition;
161 
162  Vector3 rotAxis = Vector3.Cross (oldDir, newDir);
163  float angle = Vector3.Angle (oldDir, newDir);
164  this.transform.localRotation *= Quaternion.AngleAxis(angle, rotAxis);
165 
166  //scale
167  rescaleStick(hitPoint);
168 
169  } else {
170  //move
171  Quaternion newRotation = controllerRight.transform.rotation;
172  this.transform.rotation = newRotation * Quaternion.Inverse(offsetRot) * annotationRot;
173  transform.parent.position = controllerRight.transform.position
174  + controllerRight.transform.forward.normalized * disControllerToMesh
175  + this.transform.TransformDirection(vecToMeshCenterLocal);
176 
177  }
178  } else {
179  dropMovement ();
180  }
181 
182  }
183 
184  //########### Vive movement method 4 (2 controller)
185 
186  //Called by annotation click with vive
187  private void moveAnnotationWithVive4 () {
188  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
189  if (inputDevice.getDeviceType () == InputDeviceManager.InputDeviceType.ViveController) {
190  GameObject.Find ("GlobalScript").GetComponent<HierarchicalInputModule> ().disableLayer ("UIOrgans");
191  controllerRight = (inputDevice as Controller).gameObject;
192  controllerLeft = InputDeviceManager.instance.leftController.gameObject;
193  disControllerToMesh = (hitPointStartWorld - controllerRight.transform.position).magnitude;
194  offsetRot = controllerRight.transform.rotation;
195  annotationRot = this.transform.rotation;
196  prevleftPos = controllerLeft.transform.position;
197  prevRightPos = controllerRight.transform.position;
198  move = true;
199  }
200  }
201 
202  //Called every Frame
203  private void moveWithVive4 () {
204 
205  if (controllerRight.GetComponent<Controller> ().triggerPressed ()) {
206  Vector3 oldDir = prevleftPos - prevRightPos;
207  Vector3 newDir = controllerLeft.transform.position - controllerRight.transform.position;
208  oldDir = this.transform.InverseTransformDirection(oldDir);
209  newDir = this.transform.InverseTransformDirection(newDir);
210 
211  if(rotateWithVive) {
212  //scale
213  rescaleTwoControlers(oldDir, newDir);
214 
215  } else {
216  //move
217  //position
218  transform.parent.position = controllerRight.transform.position
219  + controllerRight.transform.forward.normalized * disControllerToMesh
220  + this.transform.TransformDirection(vecToMeshCenterLocal);
221 
222  //rotation
223  Vector3 rotAxis = Vector3.Cross (oldDir, newDir);
224  float angle = Vector3.Angle (oldDir, newDir);
225  this.transform.localRotation *= Quaternion.AngleAxis(angle, rotAxis);
226 
227 
228  }
229  prevleftPos = controllerLeft.transform.position;
230  prevRightPos = controllerRight.transform.position;
231 
232  } else {
233  dropMovement ();
234  }
235 
236  }
237 
238 
239  private void rescaleStick(Vector3 hitPoint) {
240  Vector3 newScale;
241  if(GetComponentInParent<Annotation>().myType == AnnotationControl.AnnotationType.sphere) {
242 
243  float dist = (transform.TransformPoint(hitPoint) - transform.position).magnitude;
244  float refValue = (hitPointStartWorld - transform.position).magnitude;
245  float d = dist * (startScale.x / refValue);
246  newScale = new Vector3(d,d,d);
247  this.transform.localScale = newScale;
248  } else if(GetComponentInParent<Annotation>().myType == AnnotationControl.AnnotationType.plane) {
249  Vector3 delta = new Vector3(Mathf.Abs(hitPoint.x) - Mathf.Abs(hitPointStartLocal.x),
250  Mathf.Abs(hitPoint.y) - Mathf.Abs(hitPointStartLocal.y),
251  Mathf.Abs(hitPoint.z) - Mathf.Abs(hitPointStartLocal.z));
252  newScale = delta * 2 + this.transform.localScale;
253  if (Mathf.Max(newScale.x, newScale.y) > maxScale || Mathf.Min(newScale.x, newScale.y) < minScale) {
254  return;
255  }
256  this.transform.localScale = newScale;
257  }
258  }
259 
260  private void rescaleTwoControlers(Vector3 oldDir, Vector3 newDir) {
261  Vector3 newScale;
262  if(GetComponentInParent<Annotation>().myType == AnnotationControl.AnnotationType.sphere) {
263  float d = newDir.magnitude * (this.transform.localScale.x / oldDir.magnitude);
264  newScale = new Vector3(d,d,d);
265  this.transform.localScale = newScale;
266  } else if(GetComponentInParent<Annotation>().myType == AnnotationControl.AnnotationType.plane) {
267  Vector3 delta = new Vector3(Mathf.Abs(newDir.x) - Mathf.Abs(oldDir.x),
268  Mathf.Abs(newDir.y) - Mathf.Abs(oldDir.y),
269  Mathf.Abs(newDir.z) - Mathf.Abs(oldDir.z));
270  newScale = delta * 2f * twoControllerRescaleFactor + this.transform.localScale;
271  if (Mathf.Max(newScale.x, newScale.y) > maxScale || Mathf.Min(newScale.x, newScale.y) < minScale) {
272  return;
273  }
274  this.transform.localScale = newScale;
275  }
276  }
277 }
bool triggerPressed()
Definition: Controller.cs:180