forked from Unity-Technologies/AutoLOD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLODGroupExtensions.cs
128 lines (108 loc) · 4.39 KB
/
LODGroupExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
namespace UnityEngine.Experimental.AutoLOD
{
public static class LODGroupExtensions
{
public static void SetEnabled(this LODGroup lodGroup, bool enabled)
{
if (lodGroup.enabled != enabled)
{
lodGroup.enabled = enabled;
lodGroup.SetRenderersEnabled(enabled);
}
}
public static void SetRenderersEnabled(this LODGroup lodGroup, bool enabled)
{
var lods = lodGroup.GetLODs();
SetRenderersEnabled(lods, enabled);
}
public static void SetEnabled(this LODVolume.LODGroupHelper lodGroupHelper, bool enabled)
{
var lodGroup = lodGroupHelper.lodGroup;
if (lodGroup.enabled != enabled)
{
lodGroup.enabled = enabled;
SetRenderersEnabled(lodGroupHelper.lods, enabled);
}
}
static void SetRenderersEnabled(LOD[] lods, bool enabled)
{
for (var i = 0; i < lods.Length; i++)
{
var lod = lods[i];
var renderers = lod.renderers;
foreach (var r in renderers)
{
if (r)
r.enabled = enabled;
}
}
}
public static int GetMaxLOD(this LODGroup lodGroup)
{
return lodGroup.lodCount - 1;
}
public static int GetCurrentLOD(this LODGroup lodGroup, Camera camera = null)
{
var lods = lodGroup.GetLODs();
var relativeHeight = lodGroup.GetRelativeHeight(camera ?? Camera.current);
var lodIndex = GetCurrentLOD(lods, lodGroup.GetMaxLOD(), relativeHeight, camera);
return lodIndex;
}
public static int GetMaxLOD(this LODVolume.LODGroupHelper lodGroupHelper)
{
return lodGroupHelper.maxLOD;
}
public static int GetCurrentLOD(this LODVolume.LODGroupHelper lodGroupHelper, Camera camera = null, Vector3? cameraPosition = null)
{
var lods = lodGroupHelper.lods;
camera = camera ?? Camera.current;
cameraPosition = cameraPosition ?? camera.transform.position;
var relativeHeight = lodGroupHelper.GetRelativeHeight(camera, cameraPosition.Value);
return GetCurrentLOD(lods, lodGroupHelper.GetMaxLOD(), relativeHeight, camera);
}
public static float GetWorldSpaceSize(this LODGroup lodGroup)
{
return GetWorldSpaceScale(lodGroup.transform) * lodGroup.size;
}
static int GetCurrentLOD(LOD[] lods, int maxLOD, float relativeHeight, Camera camera = null)
{
var lodIndex = maxLOD;
for (var i = 0; i < lods.Length; i++)
{
var lod = lods[i];
if (relativeHeight >= lod.screenRelativeTransitionHeight)
{
lodIndex = i;
break;
}
}
return lodIndex;
}
static float GetWorldSpaceScale(Transform t)
{
var scale = t.lossyScale;
float largestAxis = Mathf.Abs(scale.x);
largestAxis = Mathf.Max(largestAxis, Mathf.Abs(scale.y));
largestAxis = Mathf.Max(largestAxis, Mathf.Abs(scale.z));
return largestAxis;
}
static float DistanceToRelativeHeight(Camera camera, float distance, float size)
{
if (camera.orthographic)
return size * 0.5F / camera.orthographicSize;
var halfAngle = Mathf.Tan(Mathf.Deg2Rad * camera.fieldOfView * 0.5F);
var relativeHeight = size * 0.5F / (distance * halfAngle);
return relativeHeight;
}
static float GetRelativeHeight(this LODGroup lodGroup, Camera camera)
{
var distance = (lodGroup.transform.TransformPoint(lodGroup.localReferencePoint) - camera.transform.position).magnitude;
return DistanceToRelativeHeight(camera, distance, lodGroup.GetWorldSpaceSize());
}
static float GetRelativeHeight(this LODVolume.LODGroupHelper lodGroupHelper, Camera camera, Vector3 cameraPosition)
{
var distance = (lodGroupHelper.referencePoint - cameraPosition).magnitude;
return DistanceToRelativeHeight(camera, distance, lodGroupHelper.worldSpaceSize);
}
}
}