A game about forced loneliness, made by TACStudios
at master 67 lines 2.4 kB view raw
1using System.Collections; 2using System.Collections.Generic; 3using Unity.Mathematics; 4using UnityEngine; 5 6namespace UnityEngine.Rendering 7{ 8 // All this is just a copy of C++ LODGroupManager code. 9 internal static class LODGroupRenderingUtils 10 { 11 public static float CalculateFOVHalfAngle(float fieldOfView) 12 { 13 return Mathf.Tan(Mathf.Deg2Rad * fieldOfView * 0.5f); 14 } 15 16 public static float CalculateScreenRelativeMetric(LODParameters lodParams, float lodBias) 17 { 18 float screenRelativeMetric; 19 if (lodParams.isOrthographic) 20 { 21 screenRelativeMetric = 2.0F * lodParams.orthoSize; 22 } 23 else 24 { 25 // Half angle at 90 degrees is 1.0 (So we skip halfAngle / 1.0 calculation) 26 float halfAngle = CalculateFOVHalfAngle(lodParams.fieldOfView); 27 screenRelativeMetric = 2.0f * halfAngle; 28 } 29 30 return screenRelativeMetric / lodBias; 31 } 32 33 public static float CalculatePerspectiveDistance(Vector3 objPosition, Vector3 camPosition, float sqrScreenRelativeMetric) 34 { 35 return Mathf.Sqrt(CalculateSqrPerspectiveDistance(objPosition, camPosition, sqrScreenRelativeMetric)); 36 } 37 38 public static float CalculateSqrPerspectiveDistance(Vector3 objPosition, Vector3 camPosition, float sqrScreenRelativeMetric) 39 { 40 return (objPosition - camPosition).sqrMagnitude * sqrScreenRelativeMetric; 41 } 42 43 public static Vector3 GetWorldReferencePoint(this LODGroup lodGroup) 44 { 45 return lodGroup.transform.TransformPoint(lodGroup.localReferencePoint); 46 } 47 48 public static float GetWorldSpaceScale(this LODGroup lodGroup) 49 { 50 Vector3 scale = lodGroup.transform.lossyScale; 51 float largestAxis = Mathf.Abs(scale.x); 52 largestAxis = Mathf.Max(largestAxis, Mathf.Abs(scale.y)); 53 largestAxis = Mathf.Max(largestAxis, Mathf.Abs(scale.z)); 54 return largestAxis; 55 } 56 57 public static float GetWorldSpaceSize(this LODGroup lodGroup) 58 { 59 return lodGroup.GetWorldSpaceScale() * lodGroup.size; 60 } 61 62 public static float CalculateLODDistance(float relativeScreenHeight, float size) 63 { 64 return size / relativeScreenHeight; 65 } 66 } 67}