Skip to content

Commit 27642dd

Browse files
committedDec 1, 2017
Fix OnDemand - Scatterer compatibility
1 parent b899d54 commit 27642dd

File tree

1 file changed

+45
-27
lines changed

1 file changed

+45
-27
lines changed
 

‎src/Kopernicus.OnDemand/ScaledSpaceDemand.cs

+45-27
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,26 @@ void Start()
5858
{
5959
unloadDelay = System.Diagnostics.Stopwatch.Frequency * OnDemandStorage.onDemandUnloadDelay;
6060
scaledRenderer = GetComponent<MeshRenderer>();
61-
OnBecameInvisible();
61+
UnloadTextures();
6262
}
6363

6464
void LateUpdate()
6565
{
66+
// If we are rendered, load the textures
67+
if (IsInView(ScaledCamera.Instance.cam, gameObject) && (!isLoaded || isLoaded && unloadTime != 0))
68+
{
69+
// It is supposed to be loaded now so clear the unload time
70+
unloadTime = 0;
71+
72+
// Load it
73+
LoadTextures();
74+
}
75+
else if (!IsInView(ScaledCamera.Instance.cam, gameObject) && isLoaded && unloadTime == 0)
76+
{
77+
// Set the time at which to unload
78+
unloadTime = System.Diagnostics.Stopwatch.GetTimestamp() + unloadDelay;
79+
}
80+
6681
// If we aren't loaded or we're not wanting to unload then do nothing
6782
if (!isLoaded || unloadTime == 0)
6883
return;
@@ -71,32 +86,7 @@ void LateUpdate()
7186
if (System.Diagnostics.Stopwatch.GetTimestamp() > unloadTime)
7287
UnloadTextures();
7388
}
74-
75-
// OnBecameVisible(), load the texture
76-
void OnBecameVisible()
77-
{
78-
// It is supposed to be loaded now so clear the unload time
79-
unloadTime = 0;
80-
81-
// If it is already loaded then do nothing
82-
if (isLoaded)
83-
return;
84-
85-
// Otherwise we load it
86-
LoadTextures();
87-
}
88-
89-
// OnBecameInvisible(), kill the texture
90-
void OnBecameInvisible()
91-
{
92-
// If it's not loaded then do nothing
93-
if (!isLoaded)
94-
return;
95-
96-
// Set the time at which to unload
97-
unloadTime = System.Diagnostics.Stopwatch.GetTimestamp() + unloadDelay;
98-
}
99-
89+
10090
void LoadTextures()
10191
{
10292
Debug.Log("[OD] --> ScaledSpaceDemand.LoadTextures loading " + texture + " and " + normals);
@@ -140,6 +130,34 @@ void UnloadTextures()
140130
// Flags
141131
isLoaded = false;
142132
}
133+
134+
private bool IsInView(Camera cam, GameObject toCheck)
135+
{
136+
Vector3 pointOnScreen = cam.WorldToScreenPoint(toCheck.GetComponentInChildren<Renderer>().bounds.center);
137+
138+
//Is in front
139+
if (pointOnScreen.z < 0)
140+
{
141+
return false;
142+
}
143+
144+
//Is in FOV
145+
if (pointOnScreen.x < 0 || pointOnScreen.x > Screen.width ||
146+
pointOnScreen.y < 0 || pointOnScreen.y > Screen.height)
147+
{
148+
return false;
149+
}
150+
151+
RaycastHit hit;
152+
if (Physics.Linecast(cam.transform.position, toCheck.GetComponentInChildren<Renderer>().bounds.center, out hit))
153+
{
154+
if (hit.transform.name != toCheck.name)
155+
{
156+
return false;
157+
}
158+
}
159+
return true;
160+
}
143161
}
144162
}
145163
}

0 commit comments

Comments
 (0)
Please sign in to comment.