OvrvisionTracker.cs
3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using UnityEngine;
using System.Collections;
/// <summary>
/// This class provides main interface to the Ovrvision Ex
/// </summary>
public class OvrvisionTracker : MonoBehaviour {
//var
public int markerID = 0;
public bool MovieOVRCameraRig = false;
//define
private const int MARKERGET_ARG10 = 10;
private GameObject OvrvisionProCameraObj = null;
private float hideTime;
public float timeToDisappear = 0.1f;
private Vector3 offsetPos = new Vector3(-0.032f,0.0f,0.0f);
// ------ Function ------
// Tracker initialization
public void Start()
{
if (GameObject.Find("OvrvisionProCamera"))
OvrvisionProCameraObj = GameObject.Find("OvrvisionProCamera");
if (!MovieOVRCameraRig)
{
if (GameObject.Find("LeftCamera"))
this.transform.parent = GameObject.Find("LeftCamera").transform;
}
this.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
hideTime = timeToDisappear;
}
// UpdateTracker
public void UpdateTransform (float[] markerGet, int elementNo) {
int i = elementNo * MARKERGET_ARG10;
if (!MovieOVRCameraRig)
{
this.transform.localPosition = new Vector3(markerGet[i + 1] + offsetPos.x, markerGet[i + 2] + offsetPos.y, markerGet[i + 3] + offsetPos.z);
this.transform.localRotation = new Quaternion(markerGet[i + 4], markerGet[i + 5], markerGet[i + 6], markerGet[i + 7]);
}
else
{
if (OvrvisionProCameraObj != null)
{
Vector3 pos = new Vector3(markerGet[i + 1], markerGet[i + 2], markerGet[i + 3]);
Quaternion qat = new Quaternion(markerGet[i + 4], markerGet[i + 5], markerGet[i + 6], markerGet[i + 7]);
setCameraTrackerPosition(pos, qat, OvrvisionProCameraObj);
}
}
hideTime = 0.0f;
}
public void UpdateTransformNone () {
if (!MovieOVRCameraRig)
{
hideTime += Time.deltaTime;
if (hideTime >= timeToDisappear)
{
this.transform.localPosition = new Vector3(-10000.0f, -10000.0f, -10000.0f);
hideTime = timeToDisappear;
}
}
else
{
if (OvrvisionProCameraObj != null)
{
OvrvisionProCameraObj.transform.localPosition = new Vector3(-10000.0f, -10000.0f, -10000.0f);
}
}
}
// TrackerPosition to CameraPosition
private void setCameraTrackerPosition(Vector3 p, Quaternion q, GameObject cameraobj)
{
// todo, make a function out of this, otherwhise its the same as metaioTracker.cs
Matrix4x4 rotationMatrix = new Matrix4x4();
rotationMatrix.SetTRS(Vector3.zero,
q,
new Vector3(1.0f, 1.0f, 1.0f));
Matrix4x4 translationMatrix = new Matrix4x4();
translationMatrix.SetTRS(p,
new Quaternion(0.0f, 0.0f, 0.0f, 1.0f),
new Vector3(1.0f, 1.0f, 1.0f));
//split up rotation and translation
Matrix4x4 composed = translationMatrix * rotationMatrix;
//from world to camera so we have to invert the matrix
Matrix4x4 invertedMatrix = composed.inverse;
//center the camera in front of goal - z-axis
cameraobj.transform.position = invertedMatrix.GetColumn(3);
cameraobj.transform.rotation = QuaternionFromMatrix(invertedMatrix);
}
private void NormalizeQuaternion(ref Quaternion q)
{
float sum = 0;
for (int i = 0; i < 4; ++i)
sum += q[i] * q[i];
float magnitudeInverse = 1 / Mathf.Sqrt(sum);
for (int i = 0; i < 4; ++i)
q[i] *= magnitudeInverse;
}
private Quaternion QuaternionFromMatrix(Matrix4x4 m)
{
Quaternion q = new Quaternion();
q.w = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] + m[1, 1] + m[2, 2])) / 2;
q.x = Mathf.Sqrt(Mathf.Max(0, 1 + m[0, 0] - m[1, 1] - m[2, 2])) / 2;
q.y = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] + m[1, 1] - m[2, 2])) / 2;
q.z = Mathf.Sqrt(Mathf.Max(0, 1 - m[0, 0] - m[1, 1] + m[2, 2])) / 2;
q.x *= Mathf.Sign(q.x * (m[2, 1] - m[1, 2]));
q.y *= Mathf.Sign(q.y * (m[0, 2] - m[2, 0]));
q.z *= Mathf.Sign(q.z * (m[1, 0] - m[0, 1]));
return q;
}
}