@@ -37,6 +37,80 @@ static class SkyConstants
37
37
public const short skyLevels = 6 ;
38
38
}
39
39
40
+ public class SkySteps
41
+ {
42
+ // Size of the sun- and moon-position lookup table arrays.
43
+ // Must be an integral divisor of 1440 (which is the number of minutes in a day).
44
+ public int MaxSteps = 72 ;
45
+ public double OldClockTime ;
46
+ public int Step1 , Step2 ;
47
+
48
+ /// <summary>
49
+ /// Returns the advance of time in units of 20 mins (1200 seconds).
50
+ /// Allows for an offset in hours from a control in the DispatchViewer.
51
+ /// This is a user convenience to reveal in daylight what might be hard to see at night.
52
+ /// </summary>
53
+ /// <returns></returns>
54
+ private float CelestialDiff ( double clockTime )
55
+ {
56
+ var diffS = clockTime - ( OldClockTime - DaylightOffsetS ) ;
57
+ return ( float ) diffS / 1200 ;
58
+ }
59
+
60
+ private float DaylightOffsetS
61
+ {
62
+ get
63
+ {
64
+ return ( Program . DebugViewer == null ) ? 0f : ( float ) Program . DebugViewer . DaylightOffsetHrs * 60 * 60 ;
65
+ }
66
+ }
67
+
68
+ public void SetSunAndMoonDirection ( ref Vector3 solarDirection , ref Vector3 lunarDirection
69
+ , ref Vector3 [ ] solarPosArray , ref Vector3 [ ] lunarPosArray
70
+ , double clockTime )
71
+ {
72
+ // Current solar and lunar position are calculated by interpolation in the lookup arrays.
73
+ // The arrays have intervals of 1200 secs or 20 mins.
74
+ // Using the Lerp() function, so need to calculate the in-between differential
75
+ // The rest of this increments/decrements the array indices and checks for overshoot/undershoot.
76
+ while ( clockTime >= ( OldClockTime - DaylightOffsetS + 1200 ) ) // Plus key, or normal forward in time; <CSComment> better so in case of fast forward
77
+ {
78
+ OldClockTime = OldClockTime + 1200 ;
79
+ Step1 ++ ;
80
+ Step2 ++ ;
81
+ if ( Step2 >= MaxSteps ) // Midnight.
82
+ {
83
+ Step2 = 0 ;
84
+ }
85
+ if ( Step1 >= MaxSteps ) // Midnight.
86
+ {
87
+ Step1 = 0 ;
88
+ }
89
+ }
90
+ if ( clockTime <= ( OldClockTime - DaylightOffsetS ) ) // Minus key
91
+ {
92
+ OldClockTime = OldClockTime - 1200 ;
93
+ Step1 -- ;
94
+ Step2 -- ;
95
+ if ( Step1 < 0 ) // Midnight.
96
+ {
97
+ Step1 = MaxSteps - 1 ;
98
+ }
99
+ if ( Step2 < 0 ) // Midnight.
100
+ {
101
+ Step2 = MaxSteps - 1 ;
102
+ }
103
+ }
104
+ var diff = CelestialDiff ( clockTime ) ;
105
+ solarDirection . X = MathHelper . Lerp ( solarPosArray [ Step1 ] . X , solarPosArray [ Step2 ] . X , diff ) ;
106
+ solarDirection . Y = MathHelper . Lerp ( solarPosArray [ Step1 ] . Y , solarPosArray [ Step2 ] . Y , diff ) ;
107
+ solarDirection . Z = MathHelper . Lerp ( solarPosArray [ Step1 ] . Z , solarPosArray [ Step2 ] . Z , diff ) ;
108
+ lunarDirection . X = MathHelper . Lerp ( lunarPosArray [ Step1 ] . X , lunarPosArray [ Step2 ] . X , diff ) ;
109
+ lunarDirection . Y = MathHelper . Lerp ( lunarPosArray [ Step1 ] . Y , lunarPosArray [ Step2 ] . Y , diff ) ;
110
+ lunarDirection . Z = MathHelper . Lerp ( lunarPosArray [ Step1 ] . Z , lunarPosArray [ Step2 ] . Z , diff ) ;
111
+ }
112
+ }
113
+
40
114
public class SkyViewer
41
115
{
42
116
Viewer Viewer ;
@@ -62,11 +136,9 @@ public struct Date
62
136
public int ordinalDate ;
63
137
} ;
64
138
public Date date ;
65
- // Size of the sun- and moon-position lookup table arrays.
66
- // Must be an integral divisor of 1440 (which is the number of minutes in a day).
67
- private int maxSteps = 72 ;
68
- private double oldClockTime ;
69
- int step1 , step2 ;
139
+
140
+ private SkySteps skySteps = new SkySteps ( ) ;
141
+
70
142
// Phase of the moon
71
143
public int moonPhase ;
72
144
// Wind speed and direction
@@ -105,10 +177,10 @@ public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
105
177
{
106
178
// First time around, initialize the following items:
107
179
worldLoc = new WorldLatLon ( ) ;
108
- oldClockTime = Viewer . Simulator . ClockTime % 86400 ;
109
- while ( oldClockTime < 0 ) oldClockTime += 86400 ;
110
- step1 = step2 = ( int ) ( oldClockTime / 1200 ) ;
111
- step2 = step2 < maxSteps - 1 ? step2 + 1 : 0 ; // limit to max. steps in case activity starts near midnight
180
+ skySteps . OldClockTime = Viewer . Simulator . ClockTime % 86400 ;
181
+ while ( skySteps . OldClockTime < 0 ) skySteps . OldClockTime += 86400 ;
182
+ skySteps . Step1 = skySteps . Step2 = ( int ) ( skySteps . OldClockTime / 1200 ) ;
183
+ skySteps . Step2 = skySteps . Step2 < skySteps . MaxSteps - 1 ? skySteps . Step2 + 1 : 0 ; // limit to max. steps in case activity starts near midnight
112
184
113
185
// Get the current latitude and longitude coordinates
114
186
worldLoc . ConvertWTC ( Viewer . Camera . TileX , Viewer . Camera . TileZ , Viewer . Camera . Location , ref latitude , ref longitude ) ;
@@ -122,83 +194,23 @@ public void PrepareFrame(RenderFrame frame, ElapsedTime elapsedTime)
122
194
date . year = 2017 ;
123
195
}
124
196
// Fill in the sun- and moon-position lookup tables
125
- for ( int i = 0 ; i < maxSteps ; i ++ )
197
+ for ( int i = 0 ; i < skySteps . MaxSteps ; i ++ )
126
198
{
127
- solarPosArray [ i ] = SunMoonPos . SolarAngle ( latitude , longitude , ( ( float ) i / maxSteps ) , date ) ;
128
- lunarPosArray [ i ] = SunMoonPos . LunarAngle ( latitude , longitude , ( ( float ) i / maxSteps ) , date ) ;
199
+ solarPosArray [ i ] = SunMoonPos . SolarAngle ( latitude , longitude , ( ( float ) i / skySteps . MaxSteps ) , date ) ;
200
+ lunarPosArray [ i ] = SunMoonPos . LunarAngle ( latitude , longitude , ( ( float ) i / skySteps . MaxSteps ) , date ) ;
129
201
}
130
202
// Phase of the moon is generated at random
131
203
moonPhase = Viewer . Random . Next ( 8 ) ;
132
204
if ( moonPhase == 6 && date . ordinalDate > 45 && date . ordinalDate < 330 )
133
205
moonPhase = 3 ; // Moon dog only occurs in winter
134
206
}
135
207
136
-
137
- // Current solar and lunar position are calculated by interpolation in the lookup arrays.
138
- // The arrays have intervals of 1200 secs or 20 mins.
139
- // Using the Lerp() function, so need to calculate the in-between differential
140
- // The rest of this increments/decrements the array indices and checks for overshoot/undershoot.
141
- while ( Viewer . Simulator . ClockTime >= ( oldClockTime - DaylightOffsetS + 1200 ) ) // Plus key, or normal forward in time; <CSComment> better so in case of fast forward
142
- {
143
- oldClockTime = oldClockTime + 1200 ;
144
- step1 ++ ;
145
- step2 ++ ;
146
- if ( step2 >= maxSteps ) // Midnight.
147
- {
148
- step2 = 0 ;
149
- }
150
- if ( step1 >= maxSteps ) // Midnight.
151
- {
152
- step1 = 0 ;
153
- }
154
- }
155
- if ( Viewer . Simulator . ClockTime <= ( oldClockTime - DaylightOffsetS ) ) // Minus key
156
- {
157
- oldClockTime = oldClockTime - 1200 ;
158
- step1 -- ;
159
- step2 -- ;
160
- if ( step1 < 0 ) // Midnight.
161
- {
162
- step1 = maxSteps - 1 ;
163
- }
164
- if ( step2 < 0 ) // Midnight.
165
- {
166
- step2 = maxSteps - 1 ;
167
- }
168
- }
169
- solarDirection . X = MathHelper . Lerp ( solarPosArray [ step1 ] . X , solarPosArray [ step2 ] . X , CelestialDiff ) ;
170
- solarDirection . Y = MathHelper . Lerp ( solarPosArray [ step1 ] . Y , solarPosArray [ step2 ] . Y , CelestialDiff ) ;
171
- solarDirection . Z = MathHelper . Lerp ( solarPosArray [ step1 ] . Z , solarPosArray [ step2 ] . Z , CelestialDiff ) ;
172
- lunarDirection . X = MathHelper . Lerp ( lunarPosArray [ step1 ] . X , lunarPosArray [ step2 ] . X , CelestialDiff ) ;
173
- lunarDirection . Y = MathHelper . Lerp ( lunarPosArray [ step1 ] . Y , lunarPosArray [ step2 ] . Y , CelestialDiff ) ;
174
- lunarDirection . Z = MathHelper . Lerp ( lunarPosArray [ step1 ] . Z , lunarPosArray [ step2 ] . Z , CelestialDiff ) ;
208
+ skySteps . SetSunAndMoonDirection ( ref solarDirection , ref lunarDirection , ref solarPosArray , ref lunarPosArray ,
209
+ Viewer . Simulator . ClockTime ) ;
175
210
176
211
frame . AddPrimitive ( Material , Primitive , RenderPrimitiveGroup . Sky , ref XNASkyWorldLocation ) ;
177
212
}
178
213
179
- /// <summary>
180
- /// Returns the advance of time in units of 20 mins (1200 seconds).
181
- /// Allows for an offset in hours from a control in the DispatchViewer.
182
- /// This is a user convenience to reveal in daylight what might be hard to see at night.
183
- /// </summary>
184
- /// <returns></returns>
185
- private float CelestialDiff
186
- {
187
- get
188
- {
189
- var diffS = Viewer . Simulator . ClockTime - ( oldClockTime - DaylightOffsetS ) ;
190
- return ( float ) diffS / 1200 ;
191
- }
192
- }
193
-
194
- private float DaylightOffsetS
195
- {
196
- get
197
- {
198
- return ( Program . DebugViewer == null ) ? 0f : ( float ) Program . DebugViewer . DaylightOffsetHrs * 60 * 60 ;
199
- }
200
- }
201
-
202
214
public void LoadPrep ( )
203
215
{
204
216
worldLoc = new WorldLatLon ( ) ;
0 commit comments