-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFromSpaceTest.cs
executable file
·342 lines (255 loc) · 15.9 KB
/
FromSpaceTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AtmosphericScatteringTest
{
public partial class FromSpaceTest : Form
{
public FromSpaceTest()
{
InitializeComponent();
nmrCamPosZ.Value = 500;
nmrLightPosX.Value = 1000;
nmrLightPosY.Value = 1000;
nmrLightPosZ.Value = 1000;
nmrESun.Value = 15;
nmrG.Value = -0.980M;
nmrWavelengthR.Value = 0.731M;
nmrWavelengthG.Value = 0.612M;
nmrWavelengthB.Value = 0.455M;
nmrSamples.Value = 3M;
nmrKr.Value = 0.0025M;
nmrKm.Value = 0.0015M;
nmrRatio.Value = 1.025M;
nmrScaleDepth.Value = 0.25M;
pnlAtmosphere.BackColor = Color.Black;
pnlGround.BackColor = Color.Black;
btnGenerate_Click(null, null);
}
private void btnGenerate_Click(object sender, EventArgs e)
{
btnGenerate.Enabled = false;
Vector3 v3CamPos = new Vector3((float)nmrCamPosX.Value, (float)nmrCamPosY.Value, (float)nmrCamPosZ.Value);
Vector3 v3LightPos = new Vector3((float)nmrLightPosX.Value, (float)nmrLightPosY.Value, (float)nmrLightPosZ.Value);
Vector3 wavelength = new Vector3((float)nmrWavelengthR.Value, (float)nmrWavelengthG.Value, (float)nmrWavelengthB.Value);
int samples = (int)nmrSamples.Value;
v3LightPos.Normalize();
float Kr = (float)nmrKr.Value;
float Km = (float)nmrKm.Value;
float ESun = (float)nmrESun.Value;
float G = (float)nmrG.Value;
float radiusRatio = (float)nmrRatio.Value;
float fScaleDepth = (float)nmrScaleDepth.Value;
pnlAtmosphere.BackgroundImage = CreateSkyFromSpace(200, samples, radiusRatio, fScaleDepth, Kr, Km, ESun, G, v3CamPos, Vector3.Zero, v3LightPos, wavelength);
pnlGround.BackgroundImage = CreateGroundFromSpace(200, samples, radiusRatio, fScaleDepth, Kr, Km, ESun, G, v3CamPos, Vector3.Zero, v3LightPos, wavelength);
btnGenerate.Enabled = true;
}
protected Bitmap CreateSkyFromSpace(float radius, int samples, float radiusRatio, float fScaleDepth, float Kr, float Km, float ESun, float G, Vector3 cameraPosition, Vector3 planetPosition, Vector3 lightPosition, Vector3 wavelength)
{
float fInnerRadius = radius / radiusRatio; // The inner (planetary) radius
float fInnerRadius2 = fInnerRadius * fInnerRadius; // fInnerRadius^2
float fOuterRadius = radius; // The outer (atmosphere) radius
float fOuterRadius2 = fOuterRadius * fOuterRadius; // fOuterRadius^2
Vector3 v3CameraPos = cameraPosition; // The camera's current position
Vector3 v3LightPos = Vector3.Normalize(lightPosition); // The Light Position
Vector3 v3InvWavelength = new Vector3(1 / (float)Math.Pow(wavelength.X, 4), 1 / (float)Math.Pow(wavelength.Y, 4), 1 / (float)Math.Pow(wavelength.Z, 4)); // 1 / pow(wavelength, 4) for the red, green, and blue channels
float fKrESun = Kr * ESun; // Kr * ESun
float fKmESun = Km * ESun; // Km * ESun
float fKr4PI = Kr * 4.0f * (float)Math.PI; // Kr * 4 * PI
float fKm4PI = Km * 4.0f * (float)Math.PI; ; // Km * 4 * PI
float fScale = 1 / (fOuterRadius - fInnerRadius); // 1 / (fOuterRadius - fInnerRadius)
float fScaleOverScaleDepth = fScale / fScaleDepth; // fScale / fScaleDepth
int nSamples = samples;
float fCameraHeight = (v3CameraPos).Length(); // The camera's current height
float fCameraHeight2 = fCameraHeight * fCameraHeight; // fCameraHeight^2
float fg = G;
float fg2 = G * G;
float fSamples = (float)nSamples;
int width = (int)Math.Ceiling(fOuterRadius);
int height = (int)Math.Ceiling(fOuterRadius);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width * 2, height * 2);
for (int iy = 0; iy < height * 2; iy++)
{
for (int ix = 0; ix < width * 2; ix++)
{
// Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
float k = 1 - ((float)(ix - radius) * (float)(ix - radius)) / (radius * radius) - ((float)(iy - radius) * (float)(iy - radius)) / (radius * radius);
if (k <= 0) continue;
Vector3 v3Pos = new Vector3(ix - width, iy - height, (float)Math.Sqrt(k) * radius);
Vector3 v3Ray = v3Pos - v3CameraPos;
float fFar = v3Ray.Length();
v3Ray /= fFar;
// Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
float B = 2.0f * Vector3.Dot(v3CameraPos, v3Ray);
float C = fCameraHeight2 - fOuterRadius2;
float fDet = Math.Max(0.0f, B * B - 4.0f * C);
float fNear = 0.5f * (-B - (float)Math.Sqrt(fDet));
// Calculate the ray's starting position, then calculate its scattering offset
Vector3 v3Start = v3CameraPos + v3Ray * fNear;
fFar -= fNear;
float fStartAngle = Vector3.Dot(v3Ray, v3Start) / fOuterRadius;
float fStartDepth = (float)Math.Exp(-1.0 / fScaleDepth);
float fStartOffset = fStartDepth * Scale(fStartAngle, fScaleDepth);
// Initialize the scattering loop variables
float fSampleLength = fFar / fSamples;
float fScaledLength = fSampleLength * fScale;
Vector3 v3SampleRay = v3Ray * fSampleLength;
Vector3 v3SamplePoint = v3Start + v3SampleRay * 0.5f;
// Now loop through the sample rays
Vector3 v3FrontColor = new Vector3(0);
for (int i = 0; i < nSamples; i++)
{
float fHeight = v3SamplePoint.Length();
float fDepth = (float)Math.Exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
float fLightAngle = Vector3.Dot(v3LightPos, v3SamplePoint) / fHeight;
float fLightDepth = Scale(fLightAngle, fScaleDepth);
if (fLightDepth < float.Epsilon)
{
continue;
}
float fCameraAngle = Vector3.Dot(-v3Ray, v3SamplePoint) / fHeight;
//fCameraAngle = 1;
float fCameraDepth = Scale(fCameraAngle, fScaleDepth);
float fScatter = (fStartOffset + fDepth * (fLightDepth - fCameraDepth));
Vector3 v3Attenuate = Exp((v3InvWavelength * fKr4PI + new Vector3(fKm4PI)) * -fScatter);
v3FrontColor += v3Attenuate * (fDepth * fScaledLength);
v3SamplePoint += v3SampleRay;
}
// Finally, scale the Mie and Rayleigh colors and set up the varying variables for the pixel shader
Vector3 vMieColor = v3FrontColor * fKmESun;
Vector3 vRayleighColor = v3FrontColor * (v3InvWavelength * fKrESun);
float fCos = Vector3.Dot(-v3Ray, v3LightPos);
float fCos2 = fCos * fCos;
float fRayleighPhase = 0.75f * (1.0f + fCos2);
float fMiePhase = 1.5f * ((1.0f - fg2) / (2.0f + fg2)) * (1.0f + fCos2) / (float)Math.Pow(1.0f + fg2 - 2.0f * fg * fCos, 1.5f);
Vector3 color = fRayleighPhase * vRayleighColor + fMiePhase * vMieColor;
bitmap.SetPixel(ix, iy, FromRGBA(color.X, color.Y, color.Z, color.Z));
}
}
return bitmap;
}
protected Bitmap CreateGroundFromSpace(float radius, int samples, float radiusRatio, float fScaleDepth, float Kr, float Km, float ESun, float G, Vector3 cameraPosition, Vector3 planetPosition, Vector3 lightPosition, Vector3 wavelength)
{
float fInnerRadius = radius / radiusRatio; // The inner (planetary) radius
float fInnerRadius2 = fInnerRadius * fInnerRadius; // fInnerRadius^2
float fOuterRadius = radius; // The outer (atmosphere) radius
float fOuterRadius2 = fOuterRadius * fOuterRadius; // fOuterRadius^2
Vector3 v3CameraPos = cameraPosition; // The camera's current position
Vector3 v3LightPos = Vector3.Normalize(lightPosition); // The Light Position
Vector3 v3InvWavelength = new Vector3(1 / (float)Math.Pow(wavelength.X, 4), 1 / (float)Math.Pow(wavelength.Y, 4), 1 / (float)Math.Pow(wavelength.Z, 4)); // 1 / pow(wavelength, 4) for the red, green, and blue channels
float fKrESun = Kr * ESun; // Kr * ESun
float fKmESun = Km * ESun; // Km * ESun
float fKr4PI = Kr * 4.0f * (float)Math.PI; // Kr * 4 * PI
float fKm4PI = Km * 4.0f * (float)Math.PI; ; // Km * 4 * PI
float fScale = 1 / (fOuterRadius - fInnerRadius); // 1 / (fOuterRadius - fInnerRadius)
float fScaleOverScaleDepth = fScale / fScaleDepth; // fScale / fScaleDepth
int nSamples = 5;
float fCameraHeight = (v3CameraPos).Length(); // The camera's current height
float fCameraHeight2 = fCameraHeight * fCameraHeight; // fCameraHeight^2
float fg = G;
float fg2 = G * G;
float fInvScaleDepth = (1.0f / fScaleDepth);
float fSamples = (float)nSamples;
int width = (int)Math.Ceiling(fOuterRadius);
int height = (int)Math.Ceiling(fOuterRadius);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(width * 2, height * 2);
for (int iy = 0; iy < height * 2; iy++)
{
for (int ix = 0; ix < width * 2; ix++)
{
// Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
float k = 1 - ((float)(ix - radius) * (float)(ix - radius)) / (radius * radius) - ((float)(iy - radius) * (float)(iy - radius)) / (radius * radius);
if (k <= 0) continue;
Vector3 v3Pos = new Vector3(ix - width, iy - height, (float)Math.Sqrt(k) * radius);
Vector3 v3Ray = v3Pos - v3CameraPos;
float fFar = v3Ray.Length();
v3Ray /= fFar;
// Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
float B = 2.0f * Vector3.Dot(v3CameraPos, v3Ray);
float C = fCameraHeight2 - fOuterRadius2;
float fDet = Math.Max(0.0f, B * B - 4.0f * C);
float fNear = 0.5f * (-B - (float)Math.Sqrt(fDet));
// Calculate the ray's starting position, then calculate its scattering offset
Vector3 v3Start = v3CameraPos + v3Ray * fNear;
fFar -= fNear;
float fDepth = (float)Math.Exp((fInnerRadius - fOuterRadius) / fScaleDepth);
float fCameraAngle = Vector3.Dot(-v3Ray, v3Pos) / v3Pos.Length();
float fLightAngle = Vector3.Dot(v3LightPos, v3Pos) / v3Pos.Length();
float fCameraScale = Scale(fCameraAngle, fScaleDepth);
float fLightScale = Scale(fLightAngle, fScaleDepth);
if (fLightScale < float.Epsilon)
{
continue;
}
float fCameraOffset = fDepth * fCameraScale;
float fTemp = (fLightScale + fCameraScale);
// Initialize the scattering loop variables
float fSampleLength = fFar / fSamples;
float fScaledLength = fSampleLength * fScale;
Vector3 v3SampleRay = v3Ray * fSampleLength;
Vector3 v3SamplePoint = v3Start + v3SampleRay * 0.5f;
// Now loop through the sample rays
Vector3 v3FrontColor = new Vector3(0);
Vector3 v3Attenuate = new Vector3(0);
for (int i = 0; i < nSamples; i++)
{
float fHeight = v3SamplePoint.Length();
float fSampleDepth = (float)Math.Exp(fScaleOverScaleDepth * (fInnerRadius - fHeight));
float fScatter = fSampleDepth * fTemp - fCameraOffset;
Vector3 expComponent = (v3InvWavelength * fKr4PI + new Vector3(fKm4PI)) * -fScatter;
v3Attenuate = Exp(expComponent);
v3FrontColor += v3Attenuate * (fSampleDepth * fScaledLength);
v3SamplePoint += v3SampleRay;
}
// scattering colors
Vector3 vRayleighColor = v3FrontColor * (v3InvWavelength * fKrESun + new Vector3(fKmESun));
Vector3 vMieColor = v3Attenuate;
Vector3 color = vRayleighColor + new Vector3(0.25f, 0.25f, 0.3f) * vMieColor;
bitmap.SetPixel(ix, iy, FromRGBA(color.X, color.Y, color.Z, 1));
}
}
return bitmap;
}
// Returns the near intersection point of a line and a sphere
float GetNearIntersection(ref Vector3 position, ref Vector3 ray, float distanceSquared, float radiusSquared)
{
float B = Vector3.Dot(position, ray) * 2;
float C = distanceSquared - radiusSquared;
double fDet = Math.Max(0.0, B * B - 4.0 * C);
return 0.5f * (-B - (float)Math.Sqrt(fDet));
}
float Scale(float fCos, float fScaleDepth)
{
float x = 1.0f - fCos;
return fScaleDepth * (float)Math.Exp(-0.00287 + x * (0.459 + x * (3.83 + x * (-6.80 + x * 5.25))));
}
Color FromRGBA(float red, float green, float blue, float alpha)
{
if(float.IsInfinity(red) ||float.IsInfinity(green) ||float.IsInfinity(blue) ||float.IsInfinity(alpha)) {
//return Color.Purple;
return Color.Transparent;
}
if (float.IsNaN(red) || float.IsNaN(green) || float.IsNaN(blue) || float.IsNaN(alpha))
{
//return Color.Pink;
return Color.Transparent;
}
red = MathHelper.Clamp(red, 0, 1f);
green = MathHelper.Clamp(green, 0, 1f);
blue = MathHelper.Clamp(blue, -0, 1f);
alpha = MathHelper.Clamp(alpha, 0, 1f);
return Color.FromArgb((int)((alpha) * 255), (int)((red) * 255), (int)((green) * 255), (int)((blue) * 255));
}
Vector3 Exp(Vector3 v)
{
return new Vector3((float)Math.Exp(v.X), (float)Math.Exp(v.Y), (float)Math.Exp(v.Z));
}
}
}