forked from wwwtyro/glsl-atmosphere
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.glsl
285 lines (222 loc) · 8.08 KB
/
index.glsl
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
#define PI 3.141592
#define iSteps 16
#define jSteps 8
#define iStepGrowth 1.15
#define jStepGrowth 1.15
// Frostbite, see the `a` in:
// https://youtu.be/zs0oYjwjNEo?t=5063
// something related to Bruneton's 2008 paper.
// not sure i'm applying this properly but whatever.
#define mieExtinctionMul 1.11 // 1.11 in frostbite
// why the fuck do i need to crank this shit
#define ozoMul 6.00
vec2 rsi(vec3 r0, vec3 rd, float sr) {
// ray-sphere intersection that assumes
// the sphere is centered at the origin.
// No intersection when result.x > result.y
float a = dot(rd, rd);
float b = 2.0 * dot(rd, r0);
float c = dot(r0, r0) - (sr * sr);
float d = (b*b) - 4.0*a*c;
if (d < 0.0) return vec2(1e5,-1e5);
return vec2(
(-b - sqrt(d))/(2.0*a),
(-b + sqrt(d))/(2.0*a)
);
}
float geometricSeries(float commonRatio, float numTerms) {
// Sum of first n terms in a geometric progression starting with 1:
// a * ( 1 - r^n ) / ( 1 - r ), here a is 1.
return (1.0 - pow(commonRatio, numTerms))
/ (1.0 - commonRatio);
}
vec3 atmosphere1(
vec3 r, vec3 r0, vec3 pSun,
float rPlanet, float rAtmos,
vec3 kRlh, float kMie, float shRlh, float shMie,
float g) {
// Normalize the sun and view directions.
pSun = normalize(pSun);
r = normalize(r);
// Calculate the step size of the primary ray.
vec2 p = rsi(r0, r, rAtmos);
if (p.x > p.y) return vec3(0,0,0);
p.y = min(p.y, rsi(r0, r, rPlanet).x);
float iDist = p.y - p.x;
float iStepSize = iDist
/ geometricSeries(iStepGrowth, float(iSteps));
// Initialize the primary ray time.
float iTime = 0.0;
// Initialize accumulators for Rayleigh and Mie scattering.
vec3 totalRlh = vec3(0,0,0);
vec3 totalMie = vec3(0,0,0);
// Initialize optical depth accumulators for the primary ray.
float iOdRlh = 0.0;
float iOdMie = 0.0;
// Calculate the Rayleigh and Mie phases.
float mu = dot(r, pSun);
float mumu = mu * mu;
float gg = g * g;
float phaseRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);
float phaseMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
// Sample the primary ray.
for (int i = 0; i < iSteps; i++) {
if (i != -1) {
// Calculate the primary ray sample position.
vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);
// Calculate the height of the sample.
float iHeight = length(iPos) - rPlanet;
// Calculate the optical depth of the Rayleigh and Mie scattering for this step.
float odStepRlh = exp(-iHeight / shRlh) * iStepSize;
float odStepMie = exp(-iHeight / shMie) * iStepSize;
// Accumulate optical depth.
iOdRlh += odStepRlh;
iOdMie += odStepMie;
// Calculate the step size of the secondary ray.
float jStepSize = rsi(iPos, pSun, rAtmos).y
/ geometricSeries(jStepGrowth, float(jSteps));
// Initialize the secondary ray time.
float jTime = 0.0;
// Initialize optical depth accumulators for the secondary ray.
float jOdRlh = 0.0;
float jOdMie = 0.0;
// Sample the secondary ray.
for (int j = 0; j < jSteps; j++) {
// Calculate the secondary ray sample position.
vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);
// Calculate the height of the sample.
float jHeight = length(jPos) - rPlanet;
// Accumulate the optical depth.
jOdRlh += exp(-jHeight / shRlh) * jStepSize;
jOdMie += exp(-jHeight / shMie) * jStepSize;
// Increment the secondary ray time.
jTime += jStepSize;
jStepSize *= jStepGrowth;
}
// Calculate attenuation.
vec3 attn = exp(
-( kMie * (iOdMie + jOdMie)
+ kRlh * (iOdRlh + jOdRlh)));
// Accumulate scattering.
totalRlh += odStepRlh * attn;
totalMie += odStepMie * attn;
}
// Increment the primary ray time.
iTime += iStepSize;
iStepSize *= iStepGrowth;
}
// Calculate and return the final color.
return phaseRlh * kRlh * totalRlh
+ phaseMie * kMie * totalMie;
}
vec3 atmosphere2(
vec3 r, vec3 r0, vec3 pSun,
float rPlanet, float rAtmos,
vec3 kRlh, float kMie, vec3 kOzo,
float shRlh, float shMie,
float g) {
// Normalize the sun and view directions.
pSun = normalize(pSun);
r = normalize(r);
// Calculate the step size of the primary ray.
vec2 p = rsi(r0, r, rAtmos);
if (p.x > p.y) return vec3(0,0,0);
p.y = min(p.y, rsi(r0, r, rPlanet).x);
float iDist = p.y - p.x;
float iStepSize = iDist
/ geometricSeries(iStepGrowth, float(iSteps));
// Initialize the primary ray time.
float iTime = 0.0;
// Initialize accumulators for Rayleigh and Mie scattering.
vec3 totalRlh = vec3(0,0,0);
vec3 totalMie = vec3(0,0,0);
// Initialize optical depth accumulators for the primary ray.
float iOdRlh = 0.0;
float iOdMie = 0.0;
// Calculate the Rayleigh and Mie phases.
float mu = dot(r, pSun);
float mumu = mu * mu;
float gg = g * g;
float phaseRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);
float phaseMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));
// Sample the primary ray.
for (int i = 0; i < iSteps; i++) {
if (i != -1) {
// Calculate the primary ray sample position.
vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);
// Calculate the height of the sample.
float iHeight = length(iPos) - rPlanet;
// Calculate the optical depth of the Rayleigh and Mie scattering for this step.
float odStepRlh = exp(-iHeight / shRlh) * iStepSize;
float odStepMie = exp(-iHeight / shMie) * iStepSize;
// Accumulate optical depth.
iOdRlh += odStepRlh;
iOdMie += odStepMie;
// Calculate the step size of the secondary ray.
float jStepSize = rsi(iPos, pSun, rAtmos).y
/ geometricSeries(jStepGrowth, float(jSteps));
// Initialize the secondary ray time.
float jTime = 0.0;
// Initialize optical depth accumulators for the secondary ray.
float jOdRlh = 0.0;
float jOdMie = 0.0;
// Sample the secondary ray.
for (int j = 0; j < jSteps; j++) {
// Calculate the secondary ray sample position.
vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);
// Calculate the height of the sample.
float jHeight = length(jPos) - rPlanet;
// Accumulate the optical depth.
jOdRlh += exp(-jHeight / shRlh) * jStepSize;
jOdMie += exp(-jHeight / shMie) * jStepSize;
// Increment the secondary ray time.
jTime += jStepSize;
jStepSize *= jStepGrowth;
}
// Calculate attenuation.
vec3 attn = exp(
-( kMie * (iOdMie + jOdMie) * mieExtinctionMul
+ kRlh * (iOdRlh + jOdRlh)
+ kOzo * (iOdRlh + jOdRlh) * ozoMul));
// Accumulate scattering.
totalRlh += odStepRlh * attn;
totalMie += odStepMie * attn;
}
// Increment the primary ray time.
iTime += iStepSize;
iStepSize *= iStepGrowth;
}
// Calculate and return the final color.
return phaseRlh * kRlh * totalRlh
+ phaseMie * kMie * totalMie;
}
//
vec3 atmosphere(
vec3 r, vec3 r0, vec3 pSun,
float rPlanet, float rAtmos,
vec3 kRlh, float kMie, vec3 kOzo,
float shRlh, float shMie,
float g)
{
if (sin(r.x * 10.0) < 0.0) {
/*
return vec3(0, 0, 0);
/*/
return atmosphere1(
r, r0, pSun,
rPlanet, rAtmos,
kRlh, kMie, shRlh, shMie,
g);
//*/
}
else {
return atmosphere2(
r, r0, pSun,
rPlanet, rAtmos,
kRlh, kMie, kOzo,
shRlh, shMie,
g);
}
}
//
#pragma glslify: export(atmosphere)