SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
GyroCam.cs
Go to the documentation of this file.
1 
18 using UnityEngine;
19 using System.Collections;
23 public class GyroCam : MonoBehaviour
24 {
25  private bool gyroBool;
26  private Gyroscope gyro;
27  private Quaternion rotFix;
28 
29  private bool gyroAngleDetected = false;
30  private Quaternion androidRotFix;
31  private float angle = 0;
32 
33 
34  public bool GyroAngleDetected
35  {
36  get {
37  return this.gyroAngleDetected;
38  }
39  }
40 
41 
42  public void Start ()
43  {
44  Transform currentParent = transform.parent;
45  GameObject camParent = new GameObject ("GyroCamParent");
46  camParent.transform.position = transform.position;
47  transform.parent = camParent.transform;
48  GameObject camGrandparent = new GameObject ("GyroCamGrandParent");
49  camGrandparent.transform.position = transform.position;
50  camParent.transform.parent = camGrandparent.transform;
51  camGrandparent.transform.parent = currentParent;
52 
53  #if UNITY_3_4
54  gyroBool = Input.isGyroAvailable;
55  #else
56  gyroBool = SystemInfo.supportsGyroscope;
57  #endif
58 
59  if (gyroBool) {
60 
61  gyro = Input.gyro;
62  gyro.enabled = true;
63 
64  if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
65  camParent.transform.eulerAngles = new Vector3 (90, 180, 0);
66  } else if (Screen.orientation == ScreenOrientation.Portrait) {
67  camParent.transform.eulerAngles = new Vector3 (90, 180, 0);
68  } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
69  camParent.transform.eulerAngles = new Vector3 (90, 180, 0);
70  } else if (Screen.orientation == ScreenOrientation.LandscapeRight) {
71  camParent.transform.eulerAngles = new Vector3 (90, 180, 0);
72  } else {
73  camParent.transform.eulerAngles = new Vector3 (90, 180, 0);
74  }
75 
76  if (Screen.orientation == ScreenOrientation.LandscapeLeft) {
77  rotFix = new Quaternion (0, 0, 1, 0);
78  } else if (Screen.orientation == ScreenOrientation.Portrait) {
79  rotFix = new Quaternion (0, 0, 1, 0);
80  } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) {
81  rotFix = new Quaternion (0, 0, 1, 0);
82  } else if (Screen.orientation == ScreenOrientation.LandscapeRight) {
83  rotFix = new Quaternion (0, 0, 1, 0);
84  } else {
85  rotFix = new Quaternion (0, 0, 1, 0);
86  }
87 
88 
89  //Screen.sleepTimeout = 0;
90  } else
91  {
92  Debug.Log("No Gyro");
93  }
94  }
95 
96 
97 public void Update ()
98  {
99  if (gyroBool) {// & this.gameObject.camera.enabled
100  {
101  Quaternion quatMap;
102  #if UNITY_IPHONE
103  quatMap = gyro.attitude;
104  #elif UNITY_ANDROID
105  quatMap = new Quaternion(gyro.attitude.x,gyro.attitude.y,gyro.attitude.z,gyro.attitude.w);
106 // androidRotFix = quatMap * rotFix;
107 // angle = Quaternion.Angle(transform.localRotation, androidRotFix);
108 //
109 // if(angle > 1.5f)
110 // {
111 // this.gyroAngleDetected = true;
112 // }
113 // else
114 // {
115 // this.gyroAngleDetected = false;
116 // }
117 
118 // Debug.Log("Angle: " + gyroAngleDetected.ToString()+" " + angle.ToString());
119  #endif
120 
121  transform.localRotation = quatMap*rotFix;
122  }
123  }
124  }
125 //
126 // float scroll = 0f;
127 //
128 // public void OnGUI ()
129 // {
130 // if (gyroBool & this.gameObject.camera.enabled) {
131 // // Add scrollbar to rotate around Y axis 0-360 for manual adjustment
133 // scroll = GUI.VerticalScrollbar (new Rect (Screen.width - 35, 25, 30, Screen.height / 2), scroll, 0.1f, 0f, 0.9f);
134 // }
138 //
139 // string info = transform.position.ToString()+" "+ transform.eulerAngles.ToString();
140 //
141 // GUI.Label(new Rect(0,150,400,100), scroll.ToString());
142 //
143 // }
144 }
145 
146 
bool gyroAngleDetected
Definition: GyroCam.cs:29
bool gyroBool
Definition: GyroCam.cs:25
void Update()
Definition: GyroCam.cs:97
void Start()
Definition: GyroCam.cs:42
Quaternion rotFix
Definition: GyroCam.cs:27
Gyroscope-controlled camera for iPhone Android revised 2.26.12
Definition: GyroCam.cs:23
int transform(const BasicImage< S > &in, BasicImage< T > &out, const TooN::Matrix< 2 > &M, const TooN::Vector< 2 > &inOrig, const TooN::Vector< 2 > &outOrig, const T defaultValue=T())
Definition: vision.h:304
Quaternion androidRotFix
Definition: GyroCam.cs:30
float angle
Definition: GyroCam.cs:31
Gyroscope gyro
Definition: GyroCam.cs:26