@@ -53,31 +53,36 @@ public class ScaledSpaceDemand : MonoBehaviour
53
53
// The number of timestamp ticks in a second
54
54
private long unloadDelay ;
55
55
56
+ // The body we're attached to
57
+ private CelestialBody body ;
58
+
56
59
// Start(), get the scaled Mesh renderer
57
60
void Start ( )
58
61
{
59
62
unloadDelay = System . Diagnostics . Stopwatch . Frequency * OnDemandStorage . onDemandUnloadDelay ;
60
63
scaledRenderer = GetComponent < MeshRenderer > ( ) ;
64
+ body = PSystemManager . Instance . localBodies . Find ( b => b . scaledBody == gameObject ) ;
61
65
UnloadTextures ( ) ;
62
66
}
63
67
64
68
void LateUpdate ( )
65
69
{
66
70
// 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 ) )
68
73
{
69
74
// It is supposed to be loaded now so clear the unload time
70
75
unloadTime = 0 ;
71
76
72
77
// Load it
73
78
LoadTextures ( ) ;
74
79
}
75
- else if ( ! IsInView ( ScaledCamera . Instance . cam , gameObject ) && isLoaded && unloadTime == 0 )
80
+ else if ( ! isInView && isLoaded && unloadTime == 0 )
76
81
{
77
82
// Set the time at which to unload
78
83
unloadTime = System . Diagnostics . Stopwatch . GetTimestamp ( ) + unloadDelay ;
79
84
}
80
-
85
+
81
86
// If we aren't loaded or we're not wanting to unload then do nothing
82
87
if ( ! isLoaded || unloadTime == 0 )
83
88
return ;
@@ -86,20 +91,23 @@ void LateUpdate()
86
91
if ( System . Diagnostics . Stopwatch . GetTimestamp ( ) > unloadTime )
87
92
UnloadTextures ( ) ;
88
93
}
89
-
94
+
90
95
void LoadTextures ( )
91
96
{
92
97
Debug . Log ( "[OD] --> ScaledSpaceDemand.LoadTextures loading " + texture + " and " + normals ) ;
98
+
93
99
// Load Diffuse
94
100
if ( OnDemandStorage . TextureExists ( texture ) )
95
101
{
96
- scaledRenderer . material . SetTexture ( "_MainTex" , OnDemandStorage . LoadTexture ( texture , false , true , true ) ) ;
102
+ scaledRenderer . material . SetTexture ( "_MainTex" ,
103
+ OnDemandStorage . LoadTexture ( texture , false , true , true ) ) ;
97
104
}
98
105
99
106
// Load Normals
100
107
if ( OnDemandStorage . TextureExists ( normals ) )
101
108
{
102
- scaledRenderer . material . SetTexture ( "_BumpMap" , OnDemandStorage . LoadTexture ( normals , false , true , false ) ) ;
109
+ scaledRenderer . material . SetTexture ( "_BumpMap" ,
110
+ OnDemandStorage . LoadTexture ( normals , false , true , false ) ) ;
103
111
}
104
112
105
113
// Events
@@ -112,6 +120,7 @@ void LoadTextures()
112
120
void UnloadTextures ( )
113
121
{
114
122
Debug . Log ( "[OD] <--- ScaledSpaceDemand.UnloadTextures destroying " + texture + " and " + normals ) ;
123
+
115
124
// Kill Diffuse
116
125
if ( OnDemandStorage . TextureExists ( texture ) )
117
126
{
@@ -130,34 +139,40 @@ void UnloadTextures()
130
139
// Flags
131
140
isLoaded = false ;
132
141
}
133
-
134
- private bool IsInView ( Camera cam , GameObject toCheck )
142
+
143
+ private bool IsInView ( Camera cam , CelestialBody body )
135
144
{
136
- Vector3 pointOnScreen = cam . WorldToScreenPoint ( toCheck . GetComponentInChildren < Renderer > ( ) . bounds . center ) ;
137
-
145
+ Vector3 pointOnScreen =
146
+ cam . WorldToScreenPoint ( body . scaledBody . GetComponentInChildren < Renderer > ( ) . bounds . center ) ;
147
+
138
148
//Is in front
139
149
if ( pointOnScreen . z < 0 )
140
150
{
141
151
return false ;
142
152
}
143
-
153
+
144
154
//Is in FOV
145
155
if ( pointOnScreen . x < 0 || pointOnScreen . x > Screen . width ||
146
156
pointOnScreen . y < 0 || pointOnScreen . y > Screen . height )
147
157
{
148
158
return false ;
149
159
}
150
-
160
+
151
161
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 ) )
153
164
{
154
- if ( hit . transform . name != toCheck . name )
165
+ if ( hit . transform . name != body . scaledBody . name )
155
166
{
156
167
return false ;
157
168
}
158
169
}
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 ;
160
175
}
161
176
}
162
177
}
163
- }
178
+ }
0 commit comments