-
Notifications
You must be signed in to change notification settings - Fork 0
/
bruneton_render_fs.glsl
190 lines (150 loc) · 6.13 KB
/
bruneton_render_fs.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
uniform vec3 worldCamera; // camera position in world space
uniform vec3 worldSunDir; // sun direction in world space
uniform sampler2DArray fftWavesSampler;
uniform vec4 GRID_SIZES;
uniform sampler3D slopeVarianceSampler;
uniform vec4 seaColour; // sea bottom color
uniform float f_depthCoef;
in vec2 u; // coordinates in world space used to compute P(u)
in vec3 P; // wave point P(u) in world space
in float flogz;
#define M_PI 3.141592653589
// ---------------------------------------------------------------------------
// REFLECTED SUN RADIANCE
// ---------------------------------------------------------------------------
// assumes x>0
float erfc(float x)
{
return 2.0 * exp(-x * x) / (2.319 * x + sqrt(4.0 + 1.52 * x * x));
}
float Lambda(float cosTheta, float sigmaSq)
{
float v = cosTheta / sqrt((1.0 - cosTheta * cosTheta) * (2.0 * sigmaSq));
return max(0.0, (exp(-v * v) - v * sqrt(M_PI) * erfc(v)) / (2.0 * v * sqrt(M_PI)));
//return (exp(-v * v)) / (2.0 * v * sqrt(M_PI)); // approximate, faster formula
}
// L, V, N, Tx, Ty in world space
float reflectedSunRadiance(vec3 L, vec3 V, vec3 N, vec3 Tx, vec3 Ty, vec2 sigmaSq)
{
vec3 H = normalize(L + V);
float zetax = dot(H, Tx) / dot(H, N);
float zetay = dot(H, Ty) / dot(H, N);
float zL = dot(L, N); // cos of source zenith angle
float zV = dot(V, N); // cos of receiver zenith angle
float zH = dot(H, N); // cos of facet normal zenith angle
float zH2 = zH * zH;
float p = exp(-0.5 * (zetax * zetax / sigmaSq.x + zetay * zetay / sigmaSq.y)) / (2.0 * M_PI * sqrt(sigmaSq.x * sigmaSq.y));
float tanV = atan(dot(V, Ty), dot(V, Tx));
float cosV2 = 1.0 / (1.0 + tanV * tanV);
float sigmaV2 = sigmaSq.x * cosV2 + sigmaSq.y * (1.0 - cosV2);
float tanL = atan(dot(L, Ty), dot(L, Tx));
float cosL2 = 1.0 / (1.0 + tanL * tanL);
float sigmaL2 = sigmaSq.x * cosL2 + sigmaSq.y * (1.0 - cosL2);
float fresnel = 0.02 + 0.98 * pow(1.0 - dot(V, H), 5.0);
zL = max(zL, 0.01);
zV = max(zV, 0.01);
return fresnel * p / ((1.0 + Lambda(zL, sigmaL2) + Lambda(zV, sigmaV2)) * zV * zH2 * zH2 * 4.0);
}
// ---------------------------------------------------------------------------
// REFLECTED SKY RADIANCE
// ---------------------------------------------------------------------------
// manual anisotropic filter
vec4 myTexture2DGrad(sampler2D tex, vec2 u, vec2 s, vec2 t)
{
const float TEX_SIZE = 512.0; // 'tex' size in pixels
const int N = 1; // use (2*N+1)^2 samples
vec4 r = vec4(0.0);
float l = max(0.0, log2(max(length(s), length(t)) * TEX_SIZE) - 0.0);
for (int i = -N; i <= N; ++i) {
for (int j = -N; j <= N; ++j) {
r += texture2DLod(tex, u + (s * float(i) + t * float(j)) / float(N), l);
}
}
return r / pow(2.0 * float(N) + 1.0, 2.0);
}
// V, N, Tx, Ty in world space
vec2 U(vec2 zeta, vec3 V, vec3 N, vec3 Tx, vec3 Ty)
{
vec3 f = normalize(vec3(-zeta, 1.0)); // tangent space
vec3 F = f.x * Tx + f.y * Ty + f.z * N; // world space
vec3 R = 2.0 * dot(F, V) * F - V;
return R.xy / (1.0 + R.z);
}
float meanFresnel(float cosThetaV, float sigmaV)
{
return pow(1.0 - cosThetaV, 5.0 * exp(-2.69 * sigmaV)) / (1.0 + 22.7 * pow(sigmaV, 1.5));
}
// V, N in world space
float meanFresnel(vec3 V, vec3 N, vec2 sigmaSq)
{
vec2 v = V.xy; // view direction in wind space
vec2 t = v * v / (1.0 - V.z * V.z); // cos^2 and sin^2 of view direction
float sigmaV2 = dot(t, sigmaSq); // slope variance in view direction
return meanFresnel(dot(V, N), sqrt(sigmaV2));
}
// V, N, Tx, Ty in world space;
vec3 meanSkyRadiance(vec3 V, vec3 N, vec3 Tx, vec3 Ty, vec2 sigmaSq)
{/*
vec4 result = vec4(0.0);
const float eps = 0.001;
vec2 u0 = U(vec2(0.0), V, N, Tx, Ty);
vec2 dux = 2.0 * (U(vec2(eps, 0.0), V, N, Tx, Ty) - u0) / eps * sqrt(sigmaSq.x);
vec2 duy = 2.0 * (U(vec2(0.0, eps), V, N, Tx, Ty) - u0) / eps * sqrt(sigmaSq.y);
result = texture2DGrad(skySampler, u0 * (0.5 / 1.1) + 0.5, dux * (0.5 / 1.1), duy * (0.5 / 1.1));
//if texture2DLod and texture2DGrad are not defined, you can use this (no filtering):
//result = texture2D(skySampler, u0 * (0.5 / 1.1) + 0.5);
return result.rgb;
*/
// HACK
return vec3(1,1,1);
}
// ----------------------------------------------------------------------------
void main()
{
vec3 V = normalize(worldCamera - P);
const vec2 slopes =
texture2DArray(fftWavesSampler, vec3(u / GRID_SIZES.x, 1.0)).xy +
texture2DArray(fftWavesSampler, vec3(u / GRID_SIZES.y, 1.0)).zw +
texture2DArray(fftWavesSampler, vec3(u / GRID_SIZES.z, 2.0)).xy +
texture2DArray(fftWavesSampler, vec3(u / GRID_SIZES.w, 2.0)).zw
;
vec3 N = normalize(vec3(-slopes.x,-slopes.y,1));
if (dot(V, N) < 0.0) {
N = reflect(N, V); // reflects backfacing normals
}
float Jxx = dFdx(u.x);
float Jxy = dFdy(u.x);
float Jyx = dFdx(u.y);
float Jyy = dFdy(u.y);
float A = Jxx * Jxx + Jyx * Jyx;
float B = Jxx * Jxy + Jyx * Jyy;
float C = Jxy * Jxy + Jyy * Jyy;
const float SCALE = 10.0;
float ua = pow(A / SCALE, 0.25);
float ub = 0.5 + 0.5 * B / sqrt(A * C);
float uc = pow(C / SCALE, 0.25);
vec2 sigmaSq = texture3D(slopeVarianceSampler, vec3(ua, ub, uc)).xw;
sigmaSq = max(sigmaSq, 2e-5);
vec3 Ty = normalize(vec3(0.0, N.z, -N.y));
vec3 Tx = cross(Ty, N);
float fresnel = 0.02 + 0.98 * meanFresnel(V, N, sigmaSq);
vec3 Lsun = vec3(1);
vec3 Esky;
vec3 extinction;
// sunRadianceAndSkyIrradiance(worldCamera + earthPos, worldSunDir, Lsun, Esky);
gl_FragColor = vec4(0.0, 0.0, 0.0, seaColour.a);
//#ifdef SUN_CONTRIB
gl_FragColor.rgb += reflectedSunRadiance(worldSunDir, V, N, Tx, Ty, sigmaSq) * Lsun;
//#endif
//#ifdef SKY_CONTRIB
gl_FragColor.rgb += fresnel * meanSkyRadiance(V, N, Tx, Ty, sigmaSq);
//#endif
//#ifdef SEA_CONTRIB
vec3 Lsea = seaColour.rgb * Esky / M_PI;
gl_FragColor.rgb += (1.0 - fresnel) * Lsea;
//#endif
//#if !defined(SEA_CONTRIB) && !defined(SKY_CONTRIB) && !defined(SUN_CONTRIB)
gl_FragColor.rgb += seaColour.rgb * (Lsun * max(dot(N, worldSunDir), 0.0) + Esky) / M_PI;
//#endif
gl_FragDepth = log2(flogz) * 0.5 * f_depthCoef; // DEPTH HACK
}