1
1
#version 330 core
2
- in vec3 ourColor;
3
- in vec3 TexCoords;
4
2
5
- // uniform vec2 resolution;
6
- const vec2 resolution = vec2 (512.0 );
3
+ uniform vec2 resolution;
7
4
uniform float time;
8
5
uniform mat4 MVPM;
9
- uniform int check;
10
- out vec4 color;
11
6
12
- // ----------------------------------------------------------------------------
13
- // Rayleigh and Mie scattering atmosphere system
14
- //
15
- // implementation of the techniques described here:
16
- // http://www.scratchapixel.com/old/lessons/3d-advanced-lessons/simulating-the-colors-of-the-sky/atmospheric-scattering/
17
- // ----------------------------------------------------------------------------
7
+ out vec4 color;
18
8
19
- #define _in(T) const in T
20
- #define _inout(T) inout T
21
- #define _out(T) out T
22
- #define _begin(type) type (
23
- #define _end )
24
- #define mul(a, b) (a) * (b)
9
+ /*
10
+ Atmospheric scattering based off of:
11
+ */
25
12
26
13
#define PI 3.14159265359
14
+ #define BIAS 1e-4 // small offset to avoid self-intersections
15
+
16
+ // scattering coefficients at sea level (m)
17
+ const vec3 betaR = vec3 (5.5e-6 , 13.0e-6 , 22.4e-6 ); // Rayleigh
18
+ const vec3 betaM = vec3 (21e-6 ); // Mie
19
+
20
+ // scale height (m)
21
+ // thickness of the atmosphere if its density were uniform
22
+ const float hR = 7994.0 ; // Rayleigh
23
+ const float hM = 1200.0 ; // Mie
24
+
25
+ const float earth_radius = 6360e3 ; // (m)
26
+ const float atmosphere_radius = 6420e3 ; // (m)
27
+
28
+ vec3 sun_dir = vec3 (0 , 1 , 0 );
29
+ const float sun_power = 30.0 ;
30
+
31
+
32
+ const int num_samples = 16 ;
33
+ const int num_samples_light = 8 ;
34
+
27
35
28
- // Shadertoy specific uniforms
29
- #define u_res resolution
30
- #define u_time time
31
36
32
37
struct ray_t {
33
38
vec3 origin;
34
39
vec3 direction;
35
40
};
36
- #define BIAS 1e-4 // small offset to avoid self-intersections
37
41
38
42
struct sphere_t {
39
43
vec3 origin;
40
44
float radius;
41
- int material;
42
45
};
43
46
44
- struct plane_t {
45
- vec3 direction;
46
- float distance ;
47
- int material;
48
- };
47
+ const sphere_t atmosphere = sphere_t(vec3 (0 , 0 , 0 ), atmosphere_radius);
49
48
50
- int check_pos(vec2 x, float size) {
51
- return int (mod (floor (x.x), size) + mod (floor (x.y), size)* size);
52
- }
53
49
54
- mat3 rotate_around_x(_in( float ) angle_degrees)
50
+ mat3 rotate_around_x(const in float angle_degrees)
55
51
{
56
52
float angle = radians (angle_degrees);
57
53
float _sin = sin (angle);
58
54
float _cos = cos (angle);
59
55
return mat3 (1 , 0 , 0 , 0 , _cos, - _sin, 0 , _sin, _cos);
60
56
}
61
57
62
-
63
- ray_t get_primary_ray(
64
- _in(vec3 ) cam_local_point,
65
- _inout(vec3 ) cam_origin,
66
- _inout(vec3 ) cam_look_at
67
- ){
68
- vec3 fwd = normalize (cam_look_at - cam_origin);
69
- vec3 up = vec3 (0 , 1 , 0 );
70
- vec3 right = cross (up, fwd);
71
- up = cross (fwd, right);
72
-
73
- ray_t r = _begin(ray_t)
74
- cam_origin,
75
- normalize (fwd + up * cam_local_point.y + right * cam_local_point.x)
76
- _end;
77
- return r;
78
- }
79
-
80
- bool isect_sphere(_in(ray_t) ray, _in(sphere_t) sphere, _inout(float ) t0, _inout(float ) t1)
58
+ bool isect_sphere(const in ray_t ray, const in sphere_t sphere, inout float t0, inout float t1)
81
59
{
82
60
vec3 rc = sphere.origin - ray.origin;
83
61
float radius2 = sphere.radius * sphere.radius;
@@ -91,15 +69,6 @@ bool isect_sphere(_in(ray_t) ray, _in(sphere_t) sphere, _inout(float) t0, _inout
91
69
return true;
92
70
}
93
71
94
- // scattering coefficients at sea level (m)
95
- const vec3 betaR = vec3 (5.5e-6 , 13.0e-6 , 22.4e-6 ); // Rayleigh
96
- const vec3 betaM = vec3 (21e-6 ); // Mie
97
-
98
- // scale height (m)
99
- // thickness of the atmosphere if its density were uniform
100
- const float hR = 7994.0 ; // Rayleigh
101
- const float hM = 1200.0 ; // Mie
102
-
103
72
float rayleigh_phase_func(float mu)
104
73
{
105
74
return
@@ -112,43 +81,19 @@ float rayleigh_phase_func(float mu)
112
81
// represents the average cosine of the scattered directions
113
82
// 0 is isotropic scattering
114
83
// > 1 is forward scattering, < 1 is backwards
115
- const float g = 0.76 ;
116
84
float henyey_greenstein_phase_func(float mu)
117
85
{
86
+ const float g = 0.76 ;
118
87
return
119
88
(1 . - g* g)
120
89
/ // ---------------------------------------------
121
90
((4 . + PI) * pow (1 . + g* g - 2 .*g* mu, 1.5 ));
122
91
}
123
92
124
- // Schlick Phase Function factor
125
- // Pharr and Humphreys [2004] equivalence to g above
126
- const float k = 1.55 * g - 0.55 * (g* g* g);
127
- float schlick_phase_func(float mu)
128
- {
129
- return
130
- (1 . - k* k)
131
- / // -------------------------------------------
132
- (4 . * PI * (1 . + k* mu) * (1 . + k* mu));
133
- }
134
-
135
- const float earth_radius = 6360e3 ; // (m)
136
- const float atmosphere_radius = 6420e3 ; // (m)
137
-
138
- vec3 sun_dir = vec3 (0 , 1 , 0 );
139
- const float sun_power = 20.0 ;
140
-
141
- const sphere_t atmosphere = _begin(sphere_t)
142
- vec3 (0 , 0 , 0 ), atmosphere_radius, 0
143
- _end;
144
-
145
- const int num_samples = 16 ;
146
- const int num_samples_light = 8 ;
147
-
148
93
bool get_sun_light(
149
- _in( ray_t) ray,
150
- _inout( float ) optical_depthR,
151
- _inout( float ) optical_depthM
94
+ const in ray_t ray,
95
+ inout float optical_depthR,
96
+ inout float optical_depthM
152
97
){
153
98
float t0 = 0.0 ;
154
99
float t1 = 0.0 ;
@@ -174,7 +119,7 @@ bool get_sun_light(
174
119
return true;
175
120
}
176
121
177
- vec3 get_incident_light(_in( ray_t) ray)
122
+ vec3 get_incident_light(const in ray_t ray)
178
123
{
179
124
// "pierce" the atmosphere with the viewing ray
180
125
float t0 = 0.0 ;
@@ -197,11 +142,7 @@ vec3 get_incident_light(_in(ray_t) ray)
197
142
// * integrates to 1 over the entire sphere of directions
198
143
float phaseR = rayleigh_phase_func(mu);
199
144
float phaseM =
200
- #if 1
201
- henyey_greenstein_phase_func(mu);
202
- #else
203
- schlick_phase_func(mu);
204
- #endif
145
+ henyey_greenstein_phase_func(mu);
205
146
206
147
// optical depth (or "average density")
207
148
// represents the accumulated extinction coefficients
@@ -226,10 +167,7 @@ vec3 get_incident_light(_in(ray_t) ray)
226
167
optical_depthM += hm;
227
168
228
169
// gather the sunlight
229
- ray_t light_ray = _begin(ray_t)
230
- s,
231
- sun_dir
232
- _end;
170
+ ray_t light_ray = ray_t(s, sun_dir);
233
171
float optical_depth_lightR = 0 .;
234
172
float optical_depth_lightM = 0 .;
235
173
bool overground = get_sun_light(
@@ -256,64 +194,38 @@ vec3 get_incident_light(_in(ray_t) ray)
256
194
sumM * phaseM * betaM);
257
195
}
258
196
259
- vec3 ACESFilm( vec3 x )
260
- {
261
- float a = 2 .51f;
262
- float b = 0 .03f;
263
- float c = 2 .43f;
264
- float d = 0 .59f;
265
- float e = 0 .14f;
266
- return clamp ((x* (a* x+ b))/ (x* (c* x+ d)+ e), 0.0 , 1.0 );
197
+ vec3 U2Tone(vec3 x) {
198
+ const float A = 0.15 ;
199
+ const float B = 0.50 ;
200
+ const float C = 0.10 ;
201
+ const float D = 0.20 ;
202
+ const float E = 0.02 ;
203
+ const float F = 0.30 ;
204
+
205
+ return ((x* (A* x+ C* B)+ D* E)/ (x* (A* x+ B)+ D* F))- E/ F;
267
206
}
268
207
269
208
void main()
270
209
{
271
- vec2 aspect_ratio = vec2 (u_res.x / u_res.y, 1 );
272
- float fov = tan (radians (45.0 ));
273
- vec2 point_ndc = gl_FragCoord .xy / u_res.xy;
274
- vec3 point_cam = vec3 ((2.0 * point_ndc - 1.0 ) * aspect_ratio * fov, 1.0 );
210
+ vec2 aspect_ratio = vec2 (resolution.x / resolution.y, 1 );
211
+ vec2 point_ndc = gl_FragCoord .xy / resolution.xy;
212
+ vec3 point_cam = vec3 ((2.0 * point_ndc - 1.0 ) * aspect_ratio, 1.0 );
275
213
276
214
vec4 worldPos = (inverse((MVPM))* vec4 (point_cam, 1.0 ));
277
215
worldPos.xyz /= worldPos.w;
278
216
point_cam = normalize (worldPos.xyz);
279
217
280
-
281
- if (check_pos(gl_FragCoord .xy/ 4.0 , 4.0 )!= check){
282
- // reprojection from http://john-chapman-graphics.blogspot.ca/2013/01/what-is-motion-blur-motion-pictures-are.html
283
- // look into running all this on cpu
284
- discard ;
285
- /*
286
- vec4 current = vec4((2.0 * point_ndc - 1.0) * aspect_ratio * fov, 1.0, 1.0);
287
- current = inverse(MVPM) * current;
288
- vec4 previous = LFMVPM * current;
289
- previous.xyz /= previous.w;
290
- previous.xy = previous.xy * 0.5 + 0.5;
291
- vec2 blurVec = previous.xy - TexCoords.xy;
292
- vec2 lookup = TexCoords.xy+blurVec;
293
- float mip = 0.0;
294
- if (lookup.x<0.0||lookup.x>1.0||lookup.y<0.0||lookup.y>1.0) {
295
- lookup = clamp(lookup, 0.0, 1.0);
296
- lookup = TexCoords.xy;
297
- mip = 1.0;
298
- }
299
- color = texture(lastFrame, lookup, mip);
300
- */
301
- } else {
302
-
303
- vec3 col = vec3 (0 );
304
- if (point_cam.y> 0.0 ) {
305
- // sun
306
- mat3 rot = rotate_around_x(- abs (sin (u_time / 20 .)) * 90 .);
218
+ vec3 col = vec3 (0.4 );
219
+ if (point_cam.y>-0.05 ) {
220
+ // sun
221
+ mat3 rot = rotate_around_x(- abs (sin (time / 20 .)) * 90 .);
307
222
sun_dir *= rot;
308
-
309
- vec3 eye = vec3 (0 , earth_radius + 1 ., 0 );
310
- vec3 look_at = vec3 (0 , earth_radius + 1.0 , - 1 );
311
223
312
- // ray_t ray = get_primary_ray(point_cam, eye, look_at);
313
224
ray_t ray = ray_t(vec3 (0.0 , earth_radius+ 1.0 , 0.0 ), point_cam);
314
225
col = get_incident_light(ray);
315
226
}
316
- col = ACESFilm(col);
227
+ col = U2Tone(col);
228
+ col /= U2Tone(vec3 (2.5 ));
229
+ col = sqrt (col);
317
230
color = vec4 (col, 1 );
318
- }
319
231
}
0 commit comments