IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
ModelRotator.cs
1 // Model Rotator
2 // based on SimpleMouseRotator from unity utility
3 
4 using System;
5 using UnityEngine;
6 using UnityEngine.EventSystems;
7 
8 public class ModelRotator : MonoBehaviour
9 {
10  public float rotationSpeedMouse = 2.0f;
11  public float rotationSpeedVive = 200.0f;
12 
13  private Quaternion targetRotation;
14  public float autoRotateSpeed = 720f;
15  private float rotationStartTime = 0;
16  private float rotationTime = 0.3f;
17 
18  private Vector3 previousVivePos;
19  private bool rotating = false;
20 
21  void Start()
22  {
23  targetRotation = transform.localRotation;
24  }
25 
26  private void Update()
27  {
28  if (UI.Core.instance.layoutSystem.activeScreen == UI.Screen.center) {
29 
30  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
31  if (inputDevice.getDeviceType() == InputDeviceManager.InputDeviceType.Mouse) {
32  // Let mouse handle rotation:
33  if (Input.GetMouseButton (1) || Input.GetKey (KeyCode.M)) {
34  float inputH = -Input.GetAxis ("Mouse X");
35  float inputV = -Input.GetAxis ("Mouse Y");
36 
37  Vector3 upVector = Camera.main.transform.up;
38  Vector3 rightVector = Camera.main.transform.right;
39  transform.RotateAround (transform.position, upVector, inputH * rotationSpeedMouse);
40  transform.RotateAround (transform.position, rightVector, -inputV * rotationSpeedMouse);
41 
42  targetRotation = transform.localRotation; // Make sure it doesn't auto-rotate back.
43  }
44  } else if( inputDevice.getDeviceType() == InputDeviceManager.InputDeviceType.ViveController ) {
45  // Let left Vive controller handle rotation:
46  LeftController lc = InputDeviceManager.instance.leftController;
47  if (lc != null) {
48  UnityEngine.EventSystems.PointerEventData.FramePressState triggerState = lc.triggerButtonState;
49  if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Pressed) {
50  rotating = true;
51  previousVivePos = lc.transform.localPosition;
52  } else if (triggerState == UnityEngine.EventSystems.PointerEventData.FramePressState.Released) {
53  rotating = false;
54  previousVivePos = new Vector3 (0, 0, 0);
55  }
56  if (rotating) {
57  Vector3 upVector = Camera.main.transform.up;
58  Vector3 rightVector = Camera.main.transform.right;
59  transform.RotateAround (transform.position, upVector, (previousVivePos.x - lc.transform.localPosition.x) * rotationSpeedVive);
60  transform.RotateAround (transform.position, rightVector, -(previousVivePos.y - lc.transform.localPosition.y) * rotationSpeedVive);
61  targetRotation = transform.localRotation; // Make sure it doesn't auto-rotate back.
62  previousVivePos = lc.transform.localPosition;
63  }
64  }
65  }
66  }
67 
68  // Slowly rotate towards target, if any:
69  //float step = Time.time;
70  transform.localRotation = Quaternion.Slerp( transform.localRotation, targetRotation, (Time.time - rotationStartTime)/rotationTime );
71 
72  }
73 
74  public void setTargetOrientation( Quaternion orientation, float timeForRotation = 0f )
75  {
76  targetRotation = orientation;
77  rotationStartTime = Time.time;
78  if (timeForRotation == 0f) {
79  transform.localRotation = orientation;
80  rotationTime = 1f; // Avoid division by zero
81  } else {
82  rotationTime = timeForRotation;
83  }
84  }
85 }
86