SteamVR_Utils.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. using System;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. using Valve.VR;
  6. public static class SteamVR_Utils
  7. {
  8. public static Quaternion Slerp(Quaternion A, Quaternion B, float t)
  9. {
  10. float num = Mathf.Clamp(A.x * B.x + A.y * B.y + A.z * B.z + A.w * B.w, -1f, 1f);
  11. if (num < 0f)
  12. {
  13. B = new Quaternion(-B.x, -B.y, -B.z, -B.w);
  14. num = -num;
  15. }
  16. float num4;
  17. float num5;
  18. if (1f - num > 0.0001f)
  19. {
  20. float num2 = Mathf.Acos(num);
  21. float num3 = Mathf.Sin(num2);
  22. num4 = Mathf.Sin((1f - t) * num2) / num3;
  23. num5 = Mathf.Sin(t * num2) / num3;
  24. }
  25. else
  26. {
  27. num4 = 1f - t;
  28. num5 = t;
  29. }
  30. return new Quaternion(num4 * A.x + num5 * B.x, num4 * A.y + num5 * B.y, num4 * A.z + num5 * B.z, num4 * A.w + num5 * B.w);
  31. }
  32. public static Vector3 Lerp(Vector3 A, Vector3 B, float t)
  33. {
  34. return new Vector3(SteamVR_Utils.Lerp(A.x, B.x, t), SteamVR_Utils.Lerp(A.y, B.y, t), SteamVR_Utils.Lerp(A.z, B.z, t));
  35. }
  36. public static float Lerp(float A, float B, float t)
  37. {
  38. return A + (B - A) * t;
  39. }
  40. public static double Lerp(double A, double B, double t)
  41. {
  42. return A + (B - A) * t;
  43. }
  44. public static float InverseLerp(Vector3 A, Vector3 B, Vector3 result)
  45. {
  46. return Vector3.Dot(result - A, B - A);
  47. }
  48. public static float InverseLerp(float A, float B, float result)
  49. {
  50. return (result - A) / (B - A);
  51. }
  52. public static double InverseLerp(double A, double B, double result)
  53. {
  54. return (result - A) / (B - A);
  55. }
  56. public static float Saturate(float A)
  57. {
  58. return (A >= 0f) ? ((A <= 1f) ? A : 1f) : 0f;
  59. }
  60. public static Vector2 Saturate(Vector2 A)
  61. {
  62. return new Vector2(SteamVR_Utils.Saturate(A.x), SteamVR_Utils.Saturate(A.y));
  63. }
  64. public static float Abs(float A)
  65. {
  66. return (A >= 0f) ? A : (-A);
  67. }
  68. public static Vector2 Abs(Vector2 A)
  69. {
  70. return new Vector2(SteamVR_Utils.Abs(A.x), SteamVR_Utils.Abs(A.y));
  71. }
  72. private static float _copysign(float sizeval, float signval)
  73. {
  74. return (Mathf.Sign(signval) != 1f) ? (-Mathf.Abs(sizeval)) : Mathf.Abs(sizeval);
  75. }
  76. public static Quaternion GetRotation(this Matrix4x4 matrix)
  77. {
  78. Quaternion result = default(Quaternion);
  79. result.w = Mathf.Sqrt(Mathf.Max(0f, 1f + matrix.m00 + matrix.m11 + matrix.m22)) / 2f;
  80. result.x = Mathf.Sqrt(Mathf.Max(0f, 1f + matrix.m00 - matrix.m11 - matrix.m22)) / 2f;
  81. result.y = Mathf.Sqrt(Mathf.Max(0f, 1f - matrix.m00 + matrix.m11 - matrix.m22)) / 2f;
  82. result.z = Mathf.Sqrt(Mathf.Max(0f, 1f - matrix.m00 - matrix.m11 + matrix.m22)) / 2f;
  83. result.x = SteamVR_Utils._copysign(result.x, matrix.m21 - matrix.m12);
  84. result.y = SteamVR_Utils._copysign(result.y, matrix.m02 - matrix.m20);
  85. result.z = SteamVR_Utils._copysign(result.z, matrix.m10 - matrix.m01);
  86. return result;
  87. }
  88. public static Vector3 GetPosition(this Matrix4x4 matrix)
  89. {
  90. float m = matrix.m03;
  91. float m2 = matrix.m13;
  92. float m3 = matrix.m23;
  93. return new Vector3(m, m2, m3);
  94. }
  95. public static Vector3 GetScale(this Matrix4x4 m)
  96. {
  97. float x = Mathf.Sqrt(m.m00 * m.m00 + m.m01 * m.m01 + m.m02 * m.m02);
  98. float y = Mathf.Sqrt(m.m10 * m.m10 + m.m11 * m.m11 + m.m12 * m.m12);
  99. float z = Mathf.Sqrt(m.m20 * m.m20 + m.m21 * m.m21 + m.m22 * m.m22);
  100. return new Vector3(x, y, z);
  101. }
  102. public static Mesh CreateHiddenAreaMesh(HiddenAreaMesh_t src, VRTextureBounds_t bounds)
  103. {
  104. if (src.unTriangleCount == 0U)
  105. {
  106. return null;
  107. }
  108. float[] array = new float[src.unTriangleCount * 3U * 2U];
  109. Marshal.Copy(src.pVertexData, array, 0, array.Length);
  110. Vector3[] array2 = new Vector3[src.unTriangleCount * 3U + 12U];
  111. int[] array3 = new int[src.unTriangleCount * 3U + 24U];
  112. float num = 2f * bounds.uMin - 1f;
  113. float num2 = 2f * bounds.uMax - 1f;
  114. float num3 = 2f * bounds.vMin - 1f;
  115. float num4 = 2f * bounds.vMax - 1f;
  116. int num5 = 0;
  117. int num6 = 0;
  118. while ((long)num5 < (long)((ulong)(src.unTriangleCount * 3U)))
  119. {
  120. float x = SteamVR_Utils.Lerp(num, num2, array[num6++]);
  121. float y = SteamVR_Utils.Lerp(num3, num4, array[num6++]);
  122. array2[num5] = new Vector3(x, y, 0f);
  123. array3[num5] = num5;
  124. num5++;
  125. }
  126. int num7 = (int)(src.unTriangleCount * 3U);
  127. int num8 = num7;
  128. array2[num8++] = new Vector3(-1f, -1f, 0f);
  129. array2[num8++] = new Vector3(num, -1f, 0f);
  130. array2[num8++] = new Vector3(-1f, 1f, 0f);
  131. array2[num8++] = new Vector3(num, 1f, 0f);
  132. array2[num8++] = new Vector3(num2, -1f, 0f);
  133. array2[num8++] = new Vector3(1f, -1f, 0f);
  134. array2[num8++] = new Vector3(num2, 1f, 0f);
  135. array2[num8++] = new Vector3(1f, 1f, 0f);
  136. array2[num8++] = new Vector3(num, num3, 0f);
  137. array2[num8++] = new Vector3(num2, num3, 0f);
  138. array2[num8++] = new Vector3(num, num4, 0f);
  139. array2[num8++] = new Vector3(num2, num4, 0f);
  140. int num9 = num7;
  141. array3[num9++] = num7;
  142. array3[num9++] = num7 + 1;
  143. array3[num9++] = num7 + 2;
  144. array3[num9++] = num7 + 2;
  145. array3[num9++] = num7 + 1;
  146. array3[num9++] = num7 + 3;
  147. array3[num9++] = num7 + 4;
  148. array3[num9++] = num7 + 5;
  149. array3[num9++] = num7 + 6;
  150. array3[num9++] = num7 + 6;
  151. array3[num9++] = num7 + 5;
  152. array3[num9++] = num7 + 7;
  153. array3[num9++] = num7 + 1;
  154. array3[num9++] = num7 + 4;
  155. array3[num9++] = num7 + 8;
  156. array3[num9++] = num7 + 8;
  157. array3[num9++] = num7 + 4;
  158. array3[num9++] = num7 + 9;
  159. array3[num9++] = num7 + 10;
  160. array3[num9++] = num7 + 11;
  161. array3[num9++] = num7 + 3;
  162. array3[num9++] = num7 + 3;
  163. array3[num9++] = num7 + 11;
  164. array3[num9++] = num7 + 6;
  165. return new Mesh
  166. {
  167. vertices = array2,
  168. triangles = array3,
  169. bounds = new Bounds(Vector3.zero, new Vector3(float.MaxValue, float.MaxValue, float.MaxValue))
  170. };
  171. }
  172. public static object CallSystemFn(SteamVR_Utils.SystemFn fn, params object[] args)
  173. {
  174. bool flag = !SteamVR.active && !SteamVR.usingNativeSupport;
  175. if (flag)
  176. {
  177. EVRInitError evrinitError = EVRInitError.None;
  178. OpenVR.Init(ref evrinitError, EVRApplicationType.VRApplication_Other);
  179. }
  180. CVRSystem system = OpenVR.System;
  181. object result = (system == null) ? null : fn(system, args);
  182. if (flag)
  183. {
  184. OpenVR.Shutdown();
  185. }
  186. return result;
  187. }
  188. public static void QueueEventOnRenderThread(int eventID)
  189. {
  190. }
  191. public class Event
  192. {
  193. public static void Listen(string message, SteamVR_Utils.Event.Handler action)
  194. {
  195. SteamVR_Utils.Event.Handler handler = SteamVR_Utils.Event.listeners[message] as SteamVR_Utils.Event.Handler;
  196. if (handler != null)
  197. {
  198. SteamVR_Utils.Event.listeners[message] = (SteamVR_Utils.Event.Handler)Delegate.Combine(handler, action);
  199. }
  200. else
  201. {
  202. SteamVR_Utils.Event.listeners[message] = action;
  203. }
  204. }
  205. public static void Remove(string message, SteamVR_Utils.Event.Handler action)
  206. {
  207. SteamVR_Utils.Event.Handler handler = SteamVR_Utils.Event.listeners[message] as SteamVR_Utils.Event.Handler;
  208. if (handler != null)
  209. {
  210. SteamVR_Utils.Event.listeners[message] = (SteamVR_Utils.Event.Handler)Delegate.Remove(handler, action);
  211. }
  212. }
  213. public static void Send(string message, params object[] args)
  214. {
  215. SteamVR_Utils.Event.Handler handler = SteamVR_Utils.Event.listeners[message] as SteamVR_Utils.Event.Handler;
  216. if (handler != null)
  217. {
  218. handler(args);
  219. }
  220. }
  221. private static Hashtable listeners = new Hashtable();
  222. public delegate void Handler(params object[] args);
  223. }
  224. [Serializable]
  225. public struct RigidTransform
  226. {
  227. public RigidTransform(Vector3 pos, Quaternion rot)
  228. {
  229. this.pos = pos;
  230. this.rot = rot;
  231. }
  232. public RigidTransform(Transform t)
  233. {
  234. this.pos = t.position;
  235. this.rot = t.rotation;
  236. }
  237. public RigidTransform(Transform from, Transform to)
  238. {
  239. Quaternion quaternion = Quaternion.Inverse(from.rotation);
  240. this.rot = quaternion * to.rotation;
  241. this.pos = quaternion * (to.position - from.position);
  242. }
  243. public RigidTransform(HmdMatrix34_t pose)
  244. {
  245. Matrix4x4 identity = Matrix4x4.identity;
  246. identity[0, 0] = pose.m0;
  247. identity[0, 1] = pose.m1;
  248. identity[0, 2] = -pose.m2;
  249. identity[0, 3] = pose.m3;
  250. identity[1, 0] = pose.m4;
  251. identity[1, 1] = pose.m5;
  252. identity[1, 2] = -pose.m6;
  253. identity[1, 3] = pose.m7;
  254. identity[2, 0] = -pose.m8;
  255. identity[2, 1] = -pose.m9;
  256. identity[2, 2] = pose.m10;
  257. identity[2, 3] = -pose.m11;
  258. this.pos = identity.GetPosition();
  259. this.rot = identity.GetRotation();
  260. }
  261. public RigidTransform(HmdMatrix44_t pose)
  262. {
  263. Matrix4x4 identity = Matrix4x4.identity;
  264. identity[0, 0] = pose.m0;
  265. identity[0, 1] = pose.m1;
  266. identity[0, 2] = -pose.m2;
  267. identity[0, 3] = pose.m3;
  268. identity[1, 0] = pose.m4;
  269. identity[1, 1] = pose.m5;
  270. identity[1, 2] = -pose.m6;
  271. identity[1, 3] = pose.m7;
  272. identity[2, 0] = -pose.m8;
  273. identity[2, 1] = -pose.m9;
  274. identity[2, 2] = pose.m10;
  275. identity[2, 3] = -pose.m11;
  276. identity[3, 0] = pose.m12;
  277. identity[3, 1] = pose.m13;
  278. identity[3, 2] = -pose.m14;
  279. identity[3, 3] = pose.m15;
  280. this.pos = identity.GetPosition();
  281. this.rot = identity.GetRotation();
  282. }
  283. public static SteamVR_Utils.RigidTransform identity
  284. {
  285. get
  286. {
  287. return new SteamVR_Utils.RigidTransform(Vector3.zero, Quaternion.identity);
  288. }
  289. }
  290. public static SteamVR_Utils.RigidTransform FromLocal(Transform t)
  291. {
  292. return new SteamVR_Utils.RigidTransform(t.localPosition, t.localRotation);
  293. }
  294. public HmdMatrix44_t ToHmdMatrix44()
  295. {
  296. Matrix4x4 matrix4x = Matrix4x4.TRS(this.pos, this.rot, Vector3.one);
  297. return new HmdMatrix44_t
  298. {
  299. m0 = matrix4x[0, 0],
  300. m1 = matrix4x[0, 1],
  301. m2 = -matrix4x[0, 2],
  302. m3 = matrix4x[0, 3],
  303. m4 = matrix4x[1, 0],
  304. m5 = matrix4x[1, 1],
  305. m6 = -matrix4x[1, 2],
  306. m7 = matrix4x[1, 3],
  307. m8 = -matrix4x[2, 0],
  308. m9 = -matrix4x[2, 1],
  309. m10 = matrix4x[2, 2],
  310. m11 = -matrix4x[2, 3],
  311. m12 = matrix4x[3, 0],
  312. m13 = matrix4x[3, 1],
  313. m14 = -matrix4x[3, 2],
  314. m15 = matrix4x[3, 3]
  315. };
  316. }
  317. public HmdMatrix34_t ToHmdMatrix34()
  318. {
  319. Matrix4x4 matrix4x = Matrix4x4.TRS(this.pos, this.rot, Vector3.one);
  320. return new HmdMatrix34_t
  321. {
  322. m0 = matrix4x[0, 0],
  323. m1 = matrix4x[0, 1],
  324. m2 = -matrix4x[0, 2],
  325. m3 = matrix4x[0, 3],
  326. m4 = matrix4x[1, 0],
  327. m5 = matrix4x[1, 1],
  328. m6 = -matrix4x[1, 2],
  329. m7 = matrix4x[1, 3],
  330. m8 = -matrix4x[2, 0],
  331. m9 = -matrix4x[2, 1],
  332. m10 = matrix4x[2, 2],
  333. m11 = -matrix4x[2, 3]
  334. };
  335. }
  336. public override bool Equals(object o)
  337. {
  338. if (o is SteamVR_Utils.RigidTransform)
  339. {
  340. SteamVR_Utils.RigidTransform rigidTransform = (SteamVR_Utils.RigidTransform)o;
  341. return this.pos == rigidTransform.pos && this.rot == rigidTransform.rot;
  342. }
  343. return false;
  344. }
  345. public override int GetHashCode()
  346. {
  347. return this.pos.GetHashCode() ^ this.rot.GetHashCode();
  348. }
  349. public static bool operator ==(SteamVR_Utils.RigidTransform a, SteamVR_Utils.RigidTransform b)
  350. {
  351. return a.pos == b.pos && a.rot == b.rot;
  352. }
  353. public static bool operator !=(SteamVR_Utils.RigidTransform a, SteamVR_Utils.RigidTransform b)
  354. {
  355. return a.pos != b.pos || a.rot != b.rot;
  356. }
  357. public static SteamVR_Utils.RigidTransform operator *(SteamVR_Utils.RigidTransform a, SteamVR_Utils.RigidTransform b)
  358. {
  359. return new SteamVR_Utils.RigidTransform
  360. {
  361. rot = a.rot * b.rot,
  362. pos = a.pos + a.rot * b.pos
  363. };
  364. }
  365. public void Inverse()
  366. {
  367. this.rot = Quaternion.Inverse(this.rot);
  368. this.pos = -(this.rot * this.pos);
  369. }
  370. public SteamVR_Utils.RigidTransform GetInverse()
  371. {
  372. SteamVR_Utils.RigidTransform result = new SteamVR_Utils.RigidTransform(this.pos, this.rot);
  373. result.Inverse();
  374. return result;
  375. }
  376. public void Multiply(SteamVR_Utils.RigidTransform a, SteamVR_Utils.RigidTransform b)
  377. {
  378. this.rot = a.rot * b.rot;
  379. this.pos = a.pos + a.rot * b.pos;
  380. }
  381. public Vector3 InverseTransformPoint(Vector3 point)
  382. {
  383. return Quaternion.Inverse(this.rot) * (point - this.pos);
  384. }
  385. public Vector3 TransformPoint(Vector3 point)
  386. {
  387. return this.pos + this.rot * point;
  388. }
  389. public static Vector3 operator *(SteamVR_Utils.RigidTransform t, Vector3 v)
  390. {
  391. return t.TransformPoint(v);
  392. }
  393. public static SteamVR_Utils.RigidTransform Interpolate(SteamVR_Utils.RigidTransform a, SteamVR_Utils.RigidTransform b, float t)
  394. {
  395. return new SteamVR_Utils.RigidTransform(Vector3.Lerp(a.pos, b.pos, t), Quaternion.Slerp(a.rot, b.rot, t));
  396. }
  397. public void Interpolate(SteamVR_Utils.RigidTransform to, float t)
  398. {
  399. this.pos = SteamVR_Utils.Lerp(this.pos, to.pos, t);
  400. this.rot = SteamVR_Utils.Slerp(this.rot, to.rot, t);
  401. }
  402. public Vector3 pos;
  403. public Quaternion rot;
  404. }
  405. public delegate object SystemFn(CVRSystem system, params object[] args);
  406. }