Skip to content

Commit edcbc59

Browse files
committedDec 2, 2017
Only load the textures if the object is actually visible on the screen
1 parent 27642dd commit edcbc59

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed
 

‎src/Kopernicus.OnDemand/ScaledSpaceDemand.cs

+31-16
Original file line numberDiff line numberDiff line change
@@ -53,31 +53,36 @@ public class ScaledSpaceDemand : MonoBehaviour
5353
// The number of timestamp ticks in a second
5454
private long unloadDelay;
5555

56+
// The body we're attached to
57+
private CelestialBody body;
58+
5659
// Start(), get the scaled Mesh renderer
5760
void Start()
5861
{
5962
unloadDelay = System.Diagnostics.Stopwatch.Frequency * OnDemandStorage.onDemandUnloadDelay;
6063
scaledRenderer = GetComponent<MeshRenderer>();
64+
body = PSystemManager.Instance.localBodies.Find(b => b.scaledBody == gameObject);
6165
UnloadTextures();
6266
}
6367

6468
void LateUpdate()
6569
{
6670
// If we are rendered, load the textures
67-
if (IsInView(ScaledCamera.Instance.cam, gameObject) && (!isLoaded || isLoaded && unloadTime != 0))
71+
Boolean isInView = IsInView(ScaledCamera.Instance.cam, body);
72+
if (isInView && (!isLoaded || isLoaded && unloadTime != 0))
6873
{
6974
// It is supposed to be loaded now so clear the unload time
7075
unloadTime = 0;
7176

7277
// Load it
7378
LoadTextures();
7479
}
75-
else if (!IsInView(ScaledCamera.Instance.cam, gameObject) && isLoaded && unloadTime == 0)
80+
else if (!isInView && isLoaded && unloadTime == 0)
7681
{
7782
// Set the time at which to unload
7883
unloadTime = System.Diagnostics.Stopwatch.GetTimestamp() + unloadDelay;
7984
}
80-
85+
8186
// If we aren't loaded or we're not wanting to unload then do nothing
8287
if (!isLoaded || unloadTime == 0)
8388
return;
@@ -86,20 +91,23 @@ void LateUpdate()
8691
if (System.Diagnostics.Stopwatch.GetTimestamp() > unloadTime)
8792
UnloadTextures();
8893
}
89-
94+
9095
void LoadTextures()
9196
{
9297
Debug.Log("[OD] --> ScaledSpaceDemand.LoadTextures loading " + texture + " and " + normals);
98+
9399
// Load Diffuse
94100
if (OnDemandStorage.TextureExists(texture))
95101
{
96-
scaledRenderer.material.SetTexture("_MainTex", OnDemandStorage.LoadTexture(texture, false, true, true));
102+
scaledRenderer.material.SetTexture("_MainTex",
103+
OnDemandStorage.LoadTexture(texture, false, true, true));
97104
}
98105

99106
// Load Normals
100107
if (OnDemandStorage.TextureExists(normals))
101108
{
102-
scaledRenderer.material.SetTexture("_BumpMap", OnDemandStorage.LoadTexture(normals, false, true, false));
109+
scaledRenderer.material.SetTexture("_BumpMap",
110+
OnDemandStorage.LoadTexture(normals, false, true, false));
103111
}
104112

105113
// Events
@@ -112,6 +120,7 @@ void LoadTextures()
112120
void UnloadTextures()
113121
{
114122
Debug.Log("[OD] <--- ScaledSpaceDemand.UnloadTextures destroying " + texture + " and " + normals);
123+
115124
// Kill Diffuse
116125
if (OnDemandStorage.TextureExists(texture))
117126
{
@@ -130,34 +139,40 @@ void UnloadTextures()
130139
// Flags
131140
isLoaded = false;
132141
}
133-
134-
private bool IsInView(Camera cam, GameObject toCheck)
142+
143+
private bool IsInView(Camera cam, CelestialBody body)
135144
{
136-
Vector3 pointOnScreen = cam.WorldToScreenPoint(toCheck.GetComponentInChildren<Renderer>().bounds.center);
137-
145+
Vector3 pointOnScreen =
146+
cam.WorldToScreenPoint(body.scaledBody.GetComponentInChildren<Renderer>().bounds.center);
147+
138148
//Is in front
139149
if (pointOnScreen.z < 0)
140150
{
141151
return false;
142152
}
143-
153+
144154
//Is in FOV
145155
if (pointOnScreen.x < 0 || pointOnScreen.x > Screen.width ||
146156
pointOnScreen.y < 0 || pointOnScreen.y > Screen.height)
147157
{
148158
return false;
149159
}
150-
160+
151161
RaycastHit hit;
152-
if (Physics.Linecast(cam.transform.position, toCheck.GetComponentInChildren<Renderer>().bounds.center, out hit))
162+
if (Physics.Linecast(cam.transform.position,
163+
body.scaledBody.GetComponentInChildren<Renderer>().bounds.center, out hit))
153164
{
154-
if (hit.transform.name != toCheck.name)
165+
if (hit.transform.name != body.scaledBody.name)
155166
{
156167
return false;
157168
}
158169
}
159-
return true;
170+
Single pixelSize = (Single) body.Radius * 2 * Mathf.Rad2Deg * Screen.height /
171+
(Vector3.Distance(cam.transform.position,
172+
body.scaledBody.GetComponentInChildren<Renderer>().bounds.center) *
173+
cam.fieldOfView);
174+
return pixelSize >= 1;
160175
}
161176
}
162177
}
163-
}
178+
}

0 commit comments

Comments
 (0)
Please sign in to comment.