IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
AnnotationScaler.cs
1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.EventSystems;
4 
5 public class AnnotationScaler : MonoBehaviour {
6 
7  public GameObject arrow1;
8  public GameObject arrow2;
9 
10  public float maxScale = 15f;
11  public float minScale = 0.5f;
12 
13  private bool reScale = false;
14  private Vector3 rescaleDirection;
15 
16  public void rescaleAnnotation(BaseEventData eventData) {
17  PointerEventData data = eventData as PointerEventData;
18  if (data.button != PointerEventData.InputButton.Left) {
19  return;
20  }
21  rescaleDirection = Vector3.Scale(arrow1.transform.localPosition,arrow1.transform.localPosition).normalized;
22  reScale = true;
23  }
24 
25  public void stopRescale(BaseEventData eventData) {
26  PointerEventData data = eventData as PointerEventData;
27  if (data.button != PointerEventData.InputButton.Left) {
28  return;
29  }
30  reScale = false;
31  }
32 
33  private void rescaleMesh() {
34  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
35  Plane intersectPlane = new Plane ();
36  float dist = 0f;
37  Ray intersectRay = inputDevice.createRay ();
38  //Move Ray to CameraCenter
39  intersectRay.origin = inputDevice.getEventCamera ().transform.position;
40 
41  //update plane
42  Vector3 camAnnoVec = (intersectRay.origin - this.transform.position);
43  Vector3 globalDir = this.transform.TransformDirection (rescaleDirection);
44  Vector3 planeVec = Vector3.Cross (globalDir, camAnnoVec);
45  Vector3 normal = Vector3.Cross (globalDir, planeVec);
46  normal.Normalize ();
47 
48  intersectPlane.SetNormalAndPosition (normal, this.transform.position);
49 
50  if (intersectPlane.Raycast (intersectRay, out dist)) {
51  Vector3 intersectPoint = intersectRay.GetPoint (dist);
52  //intersectPoint is Local
53  intersectPoint = this.transform.InverseTransformPoint (intersectPoint);
54 
55  //reduce to point on scale direction
56  intersectPoint = Vector3.Scale (intersectPoint, rescaleDirection);
57  //Intersect Point always Positiv
58  intersectPoint = Vector3.Scale (intersectPoint, intersectPoint.normalized);
59  Vector3 newScale = (intersectPoint.normalized * intersectPoint.magnitude) * 2;
60 
61  if (newScale.magnitude > maxScale || newScale.magnitude < minScale) {
62  return;
63  }
64 
65  arrow2.transform.localPosition = 0.5f * newScale + newScale.normalized;
66  arrow1.transform.localPosition = -1f * arrow2.transform.localPosition;
67 
68  Vector3 actScale = this.GetComponentInParent<Annotation> ().getMeshScale ();
69  //Add scale only in one Direction
70  newScale = actScale - Vector3.Scale (actScale, rescaleDirection) + newScale;
71 
72 
73  this.GetComponentInParent<Annotation> ().rescaleMesh (newScale);
74 
75 
76  }
77 
78  }
79 
80  public void Start() {
81  Vector3 scale = this.GetComponentInParent<Annotation> ().getMeshScale ();
82  Vector3 rescaleDir = Vector3.Scale(arrow1.transform.localPosition,arrow1.transform.localPosition).normalized;
83  scale = Vector3.Scale (rescaleDir, scale);
84  arrow2.transform.localPosition = 0.5f * scale + scale.normalized;
85  arrow1.transform.localPosition = -1f * arrow2.transform.localPosition;
86  }
87 
88  public void Update() {
89  this.transform.rotation = this.GetComponentInParent<Annotation>().myAnnotationMesh.transform.rotation;
90  if(reScale) {
91  rescaleMesh ();
92  }
93  }
94 }