/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
- // Copyright 2017 Google Inc. All rights reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.VR;
- using System.Collections;
- /// Helper functions to perform common math operations for Gvr.
- public static class GvrMathHelpers {
- private static Vector2 sphericalCoordinatesResult;
- public static Vector3 GetIntersectionPosition(Camera cam, RaycastResult raycastResult) {
- // Check for camera
- if (cam == null) {
- return Vector3.zero;
- }
- float intersectionDistance = raycastResult.distance + cam.nearClipPlane;
- Vector3 intersectionPosition = cam.transform.position + cam.transform.forward * intersectionDistance;
- return intersectionPosition;
- }
- public static Vector2 GetViewportCenter() {
- int viewportWidth = Screen.width;
- int viewportHeight = Screen.height;
- if (UnityEngine.XR.XRSettings.enabled) {
- viewportWidth = UnityEngine.XR.XRSettings.eyeTextureWidth;
- viewportHeight = UnityEngine.XR.XRSettings.eyeTextureHeight;
- }
- return new Vector2(0.5f * viewportWidth, 0.5f * viewportHeight);
- }
- public static Vector2 NormalizedCartesianToSpherical(Vector3 cartCoords) {
- cartCoords.Normalize();
- if (cartCoords.x == 0) {
- cartCoords.x = Mathf.Epsilon;
- }
- float outPolar = Mathf.Atan(cartCoords.z / cartCoords.x);
- if (cartCoords.x < 0) {
- outPolar += Mathf.PI;
- }
- float outElevation = Mathf.Asin(cartCoords.y);
- sphericalCoordinatesResult.x = outPolar;
- sphericalCoordinatesResult.y = outElevation;
- return sphericalCoordinatesResult;
- }
- public static float EaseOutCubic(float min, float max, float value) {
- if (min > max) {
- Debug.LogError("Invalid values passed to EaseOutCubic, max must be greater than min. " +
- "min: " + min + ", max: " + max);
- return value;
- }
- value = Mathf.Clamp01(value);
- value -= 1.0f;
- float delta = max - min;
- float result = delta * (value * value * value + 1.0f) + min;
- return result;
- }
- /// Converts a float array of length 16 into a column-major 4x4 matrix.
- public static Matrix4x4 ConvertFloatArrayToMatrix(float[] floatArray) {
- Matrix4x4 result = new Matrix4x4();
- if (floatArray == null || floatArray.Length != 16) {
- throw new System.ArgumentException("floatArray must not be null and have a length of 16.");
- }
- result[0, 0] = floatArray[0];
- result[1, 0] = floatArray[1];
- result[2, 0] = floatArray[2];
- result[3, 0] = floatArray[3];
- result[0, 1] = floatArray[4];
- result[1, 1] = floatArray[5];
- result[2, 1] = floatArray[6];
- result[3, 1] = floatArray[7];
- result[0, 2] = floatArray[8];
- result[1, 2] = floatArray[9];
- result[2, 2] = floatArray[10];
- result[3, 2] = floatArray[11];
- result[0, 3] = floatArray[12];
- result[1, 3] = floatArray[13];
- result[2, 3] = floatArray[14];
- result[3, 3] = floatArray[15];
- return result;
- }
- }