/Assets/Interactive360/Plugins/GoogleVR/Scripts/Utilities/GvrMathHelpers.cs

https://bitbucket.org/nostalgicbear/hazardperception · C# · 107 lines · 73 code · 18 blank · 16 comment · 11 complexity · 0b941e03e68f99b0baf1a982ac56dcc9 MD5 · raw file

  1. // Copyright 2017 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. using UnityEngine;
  15. using UnityEngine.EventSystems;
  16. using UnityEngine.VR;
  17. using System.Collections;
  18. /// Helper functions to perform common math operations for Gvr.
  19. public static class GvrMathHelpers {
  20. private static Vector2 sphericalCoordinatesResult;
  21. public static Vector3 GetIntersectionPosition(Camera cam, RaycastResult raycastResult) {
  22. // Check for camera
  23. if (cam == null) {
  24. return Vector3.zero;
  25. }
  26. float intersectionDistance = raycastResult.distance + cam.nearClipPlane;
  27. Vector3 intersectionPosition = cam.transform.position + cam.transform.forward * intersectionDistance;
  28. return intersectionPosition;
  29. }
  30. public static Vector2 GetViewportCenter() {
  31. int viewportWidth = Screen.width;
  32. int viewportHeight = Screen.height;
  33. if (UnityEngine.XR.XRSettings.enabled) {
  34. viewportWidth = UnityEngine.XR.XRSettings.eyeTextureWidth;
  35. viewportHeight = UnityEngine.XR.XRSettings.eyeTextureHeight;
  36. }
  37. return new Vector2(0.5f * viewportWidth, 0.5f * viewportHeight);
  38. }
  39. public static Vector2 NormalizedCartesianToSpherical(Vector3 cartCoords) {
  40. cartCoords.Normalize();
  41. if (cartCoords.x == 0) {
  42. cartCoords.x = Mathf.Epsilon;
  43. }
  44. float outPolar = Mathf.Atan(cartCoords.z / cartCoords.x);
  45. if (cartCoords.x < 0) {
  46. outPolar += Mathf.PI;
  47. }
  48. float outElevation = Mathf.Asin(cartCoords.y);
  49. sphericalCoordinatesResult.x = outPolar;
  50. sphericalCoordinatesResult.y = outElevation;
  51. return sphericalCoordinatesResult;
  52. }
  53. public static float EaseOutCubic(float min, float max, float value) {
  54. if (min > max) {
  55. Debug.LogError("Invalid values passed to EaseOutCubic, max must be greater than min. " +
  56. "min: " + min + ", max: " + max);
  57. return value;
  58. }
  59. value = Mathf.Clamp01(value);
  60. value -= 1.0f;
  61. float delta = max - min;
  62. float result = delta * (value * value * value + 1.0f) + min;
  63. return result;
  64. }
  65. /// Converts a float array of length 16 into a column-major 4x4 matrix.
  66. public static Matrix4x4 ConvertFloatArrayToMatrix(float[] floatArray) {
  67. Matrix4x4 result = new Matrix4x4();
  68. if (floatArray == null || floatArray.Length != 16) {
  69. throw new System.ArgumentException("floatArray must not be null and have a length of 16.");
  70. }
  71. result[0, 0] = floatArray[0];
  72. result[1, 0] = floatArray[1];
  73. result[2, 0] = floatArray[2];
  74. result[3, 0] = floatArray[3];
  75. result[0, 1] = floatArray[4];
  76. result[1, 1] = floatArray[5];
  77. result[2, 1] = floatArray[6];
  78. result[3, 1] = floatArray[7];
  79. result[0, 2] = floatArray[8];
  80. result[1, 2] = floatArray[9];
  81. result[2, 2] = floatArray[10];
  82. result[3, 2] = floatArray[11];
  83. result[0, 3] = floatArray[12];
  84. result[1, 3] = floatArray[13];
  85. result[2, 3] = floatArray[14];
  86. result[3, 3] = floatArray[15];
  87. return result;
  88. }
  89. }