IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
AnnotationRotater.cs
1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.EventSystems;
4 
5 public class AnnotationRotater : MonoBehaviour {
6 
7  private GameObject meshPositionNode;
8  private bool rotate;
9  private Vector3 localRotationAxis;
10 
11 
12  public void startRotate (BaseEventData eventData) {
13  PointerEventData data = eventData as PointerEventData;
14  if (data.button != PointerEventData.InputButton.Left) {
15  return;
16  }
17  this.transform.GetChild (0).gameObject.SetActive (true);
18  Vector3 localUp = this.transform.parent.InverseTransformDirection (this.transform.parent.forward);
19  localRotationAxis = Vector3.Cross (localUp, this.transform.localPosition);
20 
21 
22  rotate = true;
23  }
24 
25  public void stopRotate (BaseEventData eventData) {
26  PointerEventData data = eventData as PointerEventData;
27  if (data.button != PointerEventData.InputButton.Left) {
28  return;
29  }
30  this.transform.GetChild (0).gameObject.SetActive (false);
31  rotate = false;
32  }
33 
34  private void RotateObject () {
35  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
36  float dist = 0f;
37  Plane intersectPlane = new Plane ();
38 
39  Vector3 normal = this.transform.parent.TransformDirection(localRotationAxis.normalized);
40  intersectPlane.SetNormalAndPosition (normal, this.transform.parent.position);
41  //Debug.DrawLine (this.transform.parent.position, this.transform.parent.position + normal);
42  Ray intersectRay = inputDevice.createRay ();
43  //Move Ray to CameraCenter
44  intersectRay.origin = inputDevice.getEventCamera ().transform.position;
45  if (intersectPlane.Raycast (intersectRay, out dist)) {
46  Vector3 intersectPoint = intersectRay.GetPoint (dist);
47  //intersectPoint local
48 
49  //Debug.DrawLine (this.transform.parent.position, this.transform.position);
50  //Debug.DrawLine (this.transform.parent.position, intersectPoint);
51  Vector3 posDir = this.transform.position - this.transform.parent.position;
52  Vector3 intersectDir = intersectPoint - this.transform.parent.position;
53  float angle = Vector3.Angle (posDir, intersectDir);
54 
55  //angle pos or neg ?
56  if(Vector3.Dot(Vector3.Cross(posDir, intersectDir), normal) < 0) {
57  angle = -angle;
58  }
59 
60  //Debug.Log (angle);
61  this.GetComponentInParent<Annotation> ().rotateMesh(Quaternion.AngleAxis (angle, localRotationAxis));
62 
63  }
64  }
65 
66  // Update is called once per frame
67  void Update () {
68  this.transform.parent.rotation = this.GetComponentInParent<Annotation>().myAnnotationMesh.transform.rotation;
69  if(rotate) {
70  RotateObject ();
71  }
72  }
73 }