@@ -58,11 +58,26 @@ void Start()
58
58
{
59
59
unloadDelay = System . Diagnostics . Stopwatch . Frequency * OnDemandStorage . onDemandUnloadDelay ;
60
60
scaledRenderer = GetComponent < MeshRenderer > ( ) ;
61
- OnBecameInvisible ( ) ;
61
+ UnloadTextures ( ) ;
62
62
}
63
63
64
64
void LateUpdate ( )
65
65
{
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
+
66
81
// If we aren't loaded or we're not wanting to unload then do nothing
67
82
if ( ! isLoaded || unloadTime == 0 )
68
83
return ;
@@ -71,32 +86,7 @@ void LateUpdate()
71
86
if ( System . Diagnostics . Stopwatch . GetTimestamp ( ) > unloadTime )
72
87
UnloadTextures ( ) ;
73
88
}
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
+
100
90
void LoadTextures ( )
101
91
{
102
92
Debug . Log ( "[OD] --> ScaledSpaceDemand.LoadTextures loading " + texture + " and " + normals ) ;
@@ -140,6 +130,34 @@ void UnloadTextures()
140
130
// Flags
141
131
isLoaded = false ;
142
132
}
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
+ }
143
161
}
144
162
}
145
163
}
0 commit comments