2 using System.Collections;
3 using UnityEngine.EventSystems;
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;
24 public float maxScale = 50f;
25 public float minScale = 5f;
26 public float twoControllerRescaleFactor = 4f;
40 private void dropMovement () {
42 controllerRight = null;
44 GetComponentInParent<Annotation>().ViveMovement(
false);
45 this.GetComponentInParent<
Annotation> ().saveAnnotationChanges ();
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);
62 GetComponentInParent<Annotation>().ViveMovement(
true);
64 moveAnnotationWithVive3 ();
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;
87 private void moveWithVive1 () {
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);
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;
116 private void moveWithVive2 () {
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);
129 transform.parent.position = controllerRight.transform.position
130 + controllerRight.transform.forward.normalized * disControllerToMesh
131 + this.transform.TransformDirection(vecToMeshCenterLocal);
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;
154 private void moveWithVive3 () {
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;
162 Vector3 rotAxis = Vector3.Cross (oldDir, newDir);
163 float angle = Vector3.Angle (oldDir, newDir);
164 this.transform.localRotation *= Quaternion.AngleAxis(angle, rotAxis);
167 rescaleStick(hitPoint);
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);
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;
203 private void moveWithVive4 () {
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);
213 rescaleTwoControlers(oldDir, newDir);
218 transform.parent.position = controllerRight.transform.position
219 + controllerRight.transform.forward.normalized * disControllerToMesh
220 + this.transform.TransformDirection(vecToMeshCenterLocal);
223 Vector3 rotAxis = Vector3.Cross (oldDir, newDir);
224 float angle = Vector3.Angle (oldDir, newDir);
225 this.transform.localRotation *= Quaternion.AngleAxis(angle, rotAxis);
229 prevleftPos = controllerLeft.transform.position;
230 prevRightPos = controllerRight.transform.position;
239 private void rescaleStick(Vector3 hitPoint) {
241 if(GetComponentInParent<Annotation>().myType ==
AnnotationControl.AnnotationType.sphere) {
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) {
256 this.transform.localScale = newScale;
260 private void rescaleTwoControlers(Vector3 oldDir, Vector3 newDir) {
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) {
274 this.transform.localScale = newScale;