comparison Orchestland/Assets/OVR/Scripts/OVRCommon.cs @ 1:f7675884f2a1

Add Orchestland project
author Daiki OYAKAWA <e135764@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:09:20 +0900
parents
children
comparison
equal deleted inserted replaced
0:347d21cdfc22 1:f7675884f2a1
1 /************************************************************************************
2
3 Copyright : Copyright 2014 Oculus VR, LLC. All Rights reserved.
4
5 Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License");
6 you may not use the Oculus VR Rift SDK except in compliance with the License,
7 which is provided at the time of installation or download, or which
8 otherwise accompanies this software in either electronic or hard copy form.
9
10 You may obtain a copy of the License at
11
12 http://www.oculusvr.com/licenses/LICENSE-3.2
13
14 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19
20 ************************************************************************************/
21
22 using UnityEngine;
23 using System.Collections.Generic;
24 using Ovr;
25
26 /// <summary>
27 /// Miscellaneous extension methods that any script can use.
28 /// </summary>
29 public static class OVRExtensions
30 {
31 /// <summary>
32 /// Converts a plain C# matrix to a Unity matrix.
33 /// </summary>
34 /// <returns>The matrix as a Unity Matrix4x4.</returns>
35 /// <param name="ovrMat">The matrix as a Matrix4f.</param>
36 public static Matrix4x4 ToMatrix4x4(this Matrix4f ovrMat)
37 {
38 Matrix4x4 mat = new Matrix4x4();
39
40 mat[0, 0] = ovrMat.m[0, 0];
41 mat[0, 1] = ovrMat.m[0, 1];
42 mat[0, 2] = ovrMat.m[0, 2];
43 mat[0, 3] = ovrMat.m[0, 3];
44 mat[1, 0] = ovrMat.m[1, 0];
45 mat[1, 1] = ovrMat.m[1, 1];
46 mat[1, 2] = ovrMat.m[1, 2];
47 mat[1, 3] = ovrMat.m[1, 3];
48 mat[2, 0] = ovrMat.m[2, 0];
49 mat[2, 1] = ovrMat.m[2, 1];
50 mat[2, 2] = ovrMat.m[2, 2];
51 mat[2, 3] = ovrMat.m[2, 3];
52 mat[3, 0] = ovrMat.m[3, 0];
53 mat[3, 1] = ovrMat.m[3, 1];
54 mat[3, 2] = ovrMat.m[3, 2];
55 mat[3, 3] = ovrMat.m[3, 3];
56
57 return mat;
58 }
59
60 /// <summary>
61 /// Converts a plain C# Sizei to a Unity Vector2.
62 /// </summary>
63 /// <returns>The size as a Unity Vector2.</returns>
64 /// <param name="size">The size as a C# Sizei.</param>
65 public static Vector2 ToVector2(this Sizei size)
66 {
67 return new Vector2(size.w, size.h);
68 }
69
70 /// <summary>
71 /// Converts a plain C# Vector2i to a Unity Vector2.
72 /// </summary>
73 /// <returns>The vector as a Unity Vector2.</returns>
74 /// <param name="size">The vector as a C# Vector2i.</param>
75 public static Vector2 ToVector2(this Vector2i vec)
76 {
77 return new Vector2(vec.x, vec.y);
78 }
79
80 /// <summary>
81 /// Converts a plain C# Vector2 to a Unity Vector2.
82 /// </summary>
83 /// <returns>The vector as a Unity Vector2.</returns>
84 /// <param name="size">The vector as a C# Vector2.</param>
85 public static Vector2 ToVector2(this Vector2f vec)
86 {
87 return new Vector2(vec.x, vec.y);
88 }
89
90 /// <summary>
91 /// Converts a plain C# Vector3 to a Unity Vector3.
92 /// </summary>
93 /// <returns>The vector as a Unity Vector3.</returns>
94 /// <param name="size">The vector as a C# Vector3.</param>
95 public static Vector3 ToVector3(this Vector3f vec, bool rhToLh = true)
96 {
97 Vector3 v = new Vector3(vec.x, vec.y, vec.z);
98
99 if (rhToLh)
100 v.z = -v.z;
101
102 return v;
103 }
104
105 /// <summary>
106 /// Converts a plain C# Quatf to a Unity Quaternion.
107 /// </summary>
108 /// <returns>The quaternion as a Unity Quaternion.</returns>
109 /// <param name="size">The quaternion as a C# Quatf.</param>
110 public static Quaternion ToQuaternion(this Quatf quat, bool rhToLh = true)
111 {
112 Quaternion q = new Quaternion(quat.x, quat.y, quat.z, quat.w);
113
114 if (rhToLh)
115 {
116 q.x = -q.x;
117 q.y = -q.y;
118 }
119
120 return q;
121 }
122
123 /// <summary>
124 /// Converts a plain C# Posef to a Unity OVRPose.
125 /// </summary>
126 /// <returns>The pose as a Unity OVRPose.</returns>
127 /// <param name="size">The pose as a C# Posef.</param>
128 public static OVRPose ToPose(this Posef pose, bool rhToLh = true)
129 {
130 return new OVRPose
131 {
132 position = pose.Position.ToVector3(rhToLh),
133 orientation = pose.Orientation.ToQuaternion(rhToLh)
134 };
135 }
136 }
137
138 /// <summary>
139 /// An affine transformation built from a Unity position and orientation.
140 /// </summary>
141 public struct OVRPose
142 {
143 /// <summary>
144 /// The position.
145 /// </summary>
146 public Vector3 position;
147
148 /// <summary>
149 /// The orientation.
150 /// </summary>
151 public Quaternion orientation;
152 }
153
154 /// <summary>
155 /// Selects a human eye.
156 /// </summary>
157 public enum OVREye
158 {
159 Center = -1,
160 Left = Ovr.Eye.Left,
161 Right = Ovr.Eye.Right,
162 Count = Ovr.Eye.Count,
163 }