-
Notifications
You must be signed in to change notification settings - Fork 1
/
renderSkyVolume.frag
299 lines (260 loc) · 9.92 KB
/
renderSkyVolume.frag
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
#version 300 es
precision highp float;
precision highp int;
precision highp sampler3D;
uniform vec2 uWindowSize;
uniform float uT;
uniform float uTModded;
uniform vec3 uLightPos;
uniform vec3 uLightColor;
uniform float uLightMagnitude;
uniform float uR1;
uniform float uRPlanet;
uniform float uRCloud0;
uniform float uRCloud1;
uniform int uSkyScattOctaves;
uniform vec3 uSkyRayleighScattering;
uniform vec3 uMieScattering;
uniform vec3 uMieAbsorbtion;
uniform float uMiePhaseG;
uniform float uSkyRayleighScatteringMul;
uniform float uRayleighScaleDiv;
uniform float uMieScaleDiv;
uniform float uSkyAbsorptionMul;
uniform float uSkyMarchSamples;
uniform mat4 uViewMat;
uniform sampler2D uSkyOpticalDepthToSun;
uniform float uRenderLayer;
uniform float uLastLayer;
in vec2 vTexCoord;
layout (location=0) out vec4 scatteredLightOut;
layout (location=1) out vec4 transmittanceOut;
const float PI = 3.14159265359;
const float PI2 = PI * 2.;
const float inv4PI = 1. / (4. * PI);
const float PI_OVER_180 = PI / 180.0;
const float COT_HALF_FOV = 1. / (tan((30.) * PI_OVER_180));
const vec3 PLANET_ORIGIN = vec3(0.);
// all sky related code is based on this:
// https://github.com/sebh/UnrealEngineSkyAtmosphere
const float RAYLEIGH_SCATTERING_MULTIPLIER = .03624;
const float MIE_SCATTERING_MULTIPLIER = .00692;
const float MIE_ABSORBTION_MULTIPLIER = .00077;
const float OZONE_ABSORBTION_LAYER_WIDTH = 25.;
const float OZONE_LINEAR_TERM0 = 1. / 15.;
const float OZONE_CONSTANT_TERM0 = -2. / 3.;
const float OZONE_LINEAR_TERM1 = -1. / 15.;
const float OZONE_CONSTANT_TERM1 = 8. / 3.;
const vec3 OZONE_ABSORBTION = vec3(.000650, .001881, .000085);
struct ParticipatingMedia {
vec3 rayleighScattering;
vec3 mieScattering;
vec3 scattering;
vec3 extinction;
};
struct LightData {
vec3 scatteredLight;
vec3 transmittance;
};
//note: uniformly distributed, normalized rand, [0;1[
float nrand(vec2 n) {
return fract(sin(dot(n.xy, vec2(12.9898, 78.233)))* 43758.5453);
}
float n1rand(vec2 n) {
float t = fract(uTModded);
float nrnd0 = nrand(n + 0.07 * t);
return nrnd0;
}
float isotropicPhaseFunction () {
return inv4PI;
}
float phaseHG (float g, float cosTheta) {
float g2 = pow(g, 2.);
return inv4PI * ((1. - g2) / pow(1. + g2 - 2. * g * cosTheta, 1.5));
}
float rayleighPhase (float cosTheta) {
return (3. * (cosTheta * cosTheta + 1.)) /(PI * 16.);
}
vec4 getRay () {
float r = uWindowSize.x / uWindowSize.y;
vec2 xy = vTexCoord - .5;
xy.x *= r;
float z = .5 * COT_HALF_FOV;
vec3 ray = normalize(vec3(xy, -z));
return vec4(ray, 0.);
}
// - r0: ray origin
// - rd: normalized ray direction
// - s0: sphere center
// - sR: sphere radius
// - Returns distance from r0 to first intersecion with sphere,
// or -1.0 if no intersection.
float raySphereIntersect(vec3 r0, vec3 rd, vec3 s0, float sR, bool farthest) {
float a = dot(rd, rd);
vec3 s0_r0 = r0 - s0;
float b = 2.0 * dot(rd, s0_r0);
float c = dot(s0_r0, s0_r0) - (sR * sR);
float delta = b * b - 4.0*a*c;
if (delta < 0.0 || a == 0.0) {
return -1.0;
}
float sol0 = (-b - sqrt(delta)) / (2.0*a);
float sol1 = (-b + sqrt(delta)) / (2.0*a);
if (sol0 < 0.0 && sol1 < 0.0) {
return -1.0;
}
if (sol0 < 0.0) {
return max(0.0, sol1);
} else if (sol1 < 0.0) {
return max(0.0, sol0);
}
if (farthest) {
return max(0.0, max(sol0, sol1));
} else {
return max(0.0, min(sol0, sol1));
}
}
void LutTransmittanceParamsToUv(in float viewHeight, in float viewZenithCosAngle, out vec2 uv) {
float H = sqrt(max(0.0f, uR1 * uR1 - uRPlanet * uRPlanet));
float rho = sqrt(max(0.0f, viewHeight * viewHeight - uRPlanet * uRPlanet));
float discriminant = viewHeight * viewHeight * (viewZenithCosAngle * viewZenithCosAngle - 1.0) + uR1 * uR1;
float d = max(0.0, (-viewHeight * viewZenithCosAngle + sqrt(discriminant))); // Distance to atmosphere boundary
float d_min = uR1 - viewHeight;
float d_max = rho + H;
float x_mu = (d - d_min) / (d_max - d_min);
float x_r = rho / H;
uv = vec2(x_mu, x_r);
//uv = vec2(fromUnitToSubUvs(uv.x, TRANSMITTANCE_TEXTURE_WIDTH), fromUnitToSubUvs(uv.y, TRANSMITTANCE_TEXTURE_HEIGHT)); // No real impact so off
}
ParticipatingMedia getSkyParticipatingMedia(const vec3 samplePos, const float RAYLEIGH_EXP_SCALE, const float MIE_EXP_SCALE) {
float sampleAltitude = length(samplePos) - uRPlanet;
float rayleightDensity = exp(-sampleAltitude * RAYLEIGH_EXP_SCALE);
float mieDensity = exp(-sampleAltitude * MIE_EXP_SCALE);
float ozoneDensity = clamp(sampleAltitude < OZONE_ABSORBTION_LAYER_WIDTH ? OZONE_LINEAR_TERM0 * sampleAltitude + OZONE_CONSTANT_TERM0 : OZONE_LINEAR_TERM1 * sampleAltitude + OZONE_CONSTANT_TERM1, 0., 1.);
vec3 mieAbsorbtion = uMieAbsorbtion * mieDensity;
vec3 mieScattering = uMieScattering * mieDensity;
vec3 rayleighScattering = uSkyRayleighScattering * uSkyRayleighScatteringMul * rayleightDensity;
vec3 ozoneAbsorbtion = OZONE_ABSORBTION * ozoneDensity * uSkyAbsorptionMul;
vec3 sigmaScattering = mieScattering + rayleighScattering;
vec3 sigmaAbsorption = mieAbsorbtion + ozoneAbsorbtion;
vec3 sigmaExtinction = max(vec3(.000000001), sigmaAbsorption + sigmaScattering);
return ParticipatingMedia(rayleighScattering, mieScattering, sigmaScattering, sigmaExtinction);
}
vec4 getSkyOpticalDepthToSun (vec3 worldPos, vec3 dirToLight) {
float sampleAltitude = length(worldPos);
vec3 upVector = worldPos / sampleAltitude;
float sunZenithCosAngle = dot(dirToLight, upVector);
vec2 uv;
LutTransmittanceParamsToUv(sampleAltitude, sunZenithCosAngle, uv);
return textureLod(uSkyOpticalDepthToSun, uv, 0.);
}
vec3 approximateSkyMultipleScattering (vec3 lightVal, vec3 rayleighScattering, vec3 mieScattering, float cosTheta, vec3 opticalDepthToSun, int octaves) {
int i;
float a = 1.;
float b = 1.;
float c = 1.;
float attenuation = .35;
float lightContribution = .53;
float excentricityAttenuation = .7;
vec3 approximatedMultipleScattering = vec3(0.);
float rayleighPhaseVal;
float miePhaseVal;
vec3 sigmaScattering;
for (i = 0; i < octaves; i++) {
a *= attenuation;
b *= lightContribution;
c *= excentricityAttenuation;
rayleighPhaseVal = rayleighPhase(cosTheta * c);
miePhaseVal = phaseHG(uMiePhaseG * c, cosTheta * c);
sigmaScattering = miePhaseVal * mieScattering + rayleighPhaseVal * rayleighScattering;
approximatedMultipleScattering += lightVal * b * sigmaScattering * exp(-opticalDepthToSun * a);
}
return approximatedMultipleScattering;
}
LightData marchSkyLayer (vec3 marchOrigin, vec3 rayDir, float marchDist, float steps) {
vec3 samplePos;
float stepSize = marchDist / steps;
vec3 sampleToCameraDir;
vec3 lightVal = uLightColor * uLightMagnitude;
float RPlanetDist;
vec3 dirToLight = normalize(uLightPos);
float cosTheta = dot(-dirToLight, -rayDir);
float rayleighPhaseVal = rayleighPhase(cosTheta);
float miePhaseVal = phaseHG(uMiePhaseG, cosTheta);
vec3 scattering;
vec3 scatteredLightDS;
vec3 transmittanceDS;
vec3 scatteredLightIntegrated = vec3(0.);
vec3 transmittance = vec3(1.);
vec3 sigmaScattering;
ParticipatingMedia participatingMedia;
vec3 opticalDepthToSun;
// float depth = stepSize * n1rand(vTexCoord) + .001;
float depth = .001;
float RAYLEIGH_EXP_SCALE = 1. / uRayleighScaleDiv;
float MIE_EXP_SCALE = 1. / uMieScaleDiv;
while (depth < marchDist) {
samplePos = marchOrigin + rayDir * depth;
participatingMedia = getSkyParticipatingMedia(samplePos, RAYLEIGH_EXP_SCALE, MIE_EXP_SCALE);
RPlanetDist = raySphereIntersect(samplePos, dirToLight, PLANET_ORIGIN, uRPlanet, false);
if (RPlanetDist != -1.) {
scattering = vec3(0.);
} else {
opticalDepthToSun = getSkyOpticalDepthToSun(samplePos, dirToLight).xyz;
sigmaScattering = participatingMedia.mieScattering * miePhaseVal + participatingMedia.rayleighScattering * rayleighPhaseVal;
scattering = lightVal * sigmaScattering * exp(-opticalDepthToSun);
scattering += approximateSkyMultipleScattering(lightVal, participatingMedia.rayleighScattering, participatingMedia.mieScattering, cosTheta, opticalDepthToSun, uSkyScattOctaves);
}
transmittanceDS = exp(-participatingMedia.extinction * stepSize);
scatteredLightDS = (scattering - scattering * transmittanceDS) / participatingMedia.extinction;
scatteredLightIntegrated += transmittance * scatteredLightDS;
transmittance *= transmittanceDS;
depth += stepSize;
}
return LightData(scatteredLightIntegrated, transmittance);
}
void marchAtmosphere (vec3 cameraPos, vec3 rayDir) {
float RPlanetDist;
float R1Dist;
float R1DistFar;
float skyMarchDist = 0.;
vec3 skyMarchOrigin = vec3(0.);
float camDist = length(cameraPos);
if (camDist < uRPlanet) {
return;
}
R1Dist = raySphereIntersect(cameraPos, rayDir, PLANET_ORIGIN, uR1, false);
// RPlanetDist = raySphereIntersect(cameraPos, rayDir, PLANET_ORIGIN, uRPlanet, false);
if (R1Dist == -1.) {
return;
}
// march entire atmosphere always to avoid issues caused by low resolution (large squares on horizon)
if (camDist < uR1) {
skyMarchOrigin = cameraPos;
skyMarchDist = R1Dist;
// if (RPlanetDist == -1.) {
// skyMarchDist = R1Dist;
// } else {
// skyMarchDist = RPlanetDist;
// }
} else {
skyMarchOrigin = cameraPos + rayDir * R1Dist;
R1DistFar = raySphereIntersect(cameraPos, rayDir, PLANET_ORIGIN, uR1, true);
skyMarchDist = R1DistFar - R1Dist;
// if (RPlanetDist == -1.) {
// R1DistFar = raySphereIntersect(cameraPos, rayDir, PLANET_ORIGIN, uR1, true);
// skyMarchDist = R1DistFar - R1Dist;
// } else {
// skyMarchDist = RPlanetDist - R1Dist;
// }
}
float layerP = uRenderLayer / uLastLayer;
LightData skyLayer = marchSkyLayer(skyMarchOrigin, rayDir, skyMarchDist * layerP, uSkyMarchSamples * layerP);
scatteredLightOut = vec4(skyLayer.scatteredLight, 1.);
transmittanceOut = vec4(skyLayer.transmittance, 1.);
}
void main () {
vec3 cameraPos = (uViewMat[3]).xyz;
vec3 rayDir = (uViewMat * getRay()).xyz;
marchAtmosphere(cameraPos, rayDir);
}