Skip to content

Commit f773945

Browse files
committed
general cleanup, started removing atmo pass
1 parent 38b2e9a commit f773945

File tree

4 files changed

+375
-230
lines changed

4 files changed

+375
-230
lines changed

assets/shaders/atmo.frag

+51-112
Original file line numberDiff line numberDiff line change
@@ -6,75 +6,56 @@ uniform mat4 MVPM;
66

77
out vec4 color;
88

9-
// ----------------------------------------------------------------------------
10-
// Rayleigh and Mie scattering atmosphere system
11-
//
12-
// implementation of the techniques described here:
13-
// http://www.scratchapixel.com/old/lessons/3d-advanced-lessons/simulating-the-colors-of-the-sky/atmospheric-scattering/
14-
// ----------------------------------------------------------------------------
15-
16-
#define _in(T) const in T
17-
#define _inout(T) inout T
18-
#define _out(T) out T
19-
#define _begin(type) type (
20-
#define _end )
21-
#define mul(a, b) (a) * (b)
9+
/*
10+
Atmospheric scattering based off of:
11+
*/
2212

2313
#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+
2435

25-
// Shadertoy specific uniforms
26-
#define u_res resolution
27-
#define u_time time
2836

2937
struct ray_t {
3038
vec3 origin;
3139
vec3 direction;
3240
};
33-
#define BIAS 1e-4 // small offset to avoid self-intersections
3441

3542
struct sphere_t {
3643
vec3 origin;
3744
float radius;
38-
int material;
3945
};
4046

41-
struct plane_t {
42-
vec3 direction;
43-
float distance;
44-
int material;
45-
};
47+
const sphere_t atmosphere = sphere_t(vec3(0, 0, 0), atmosphere_radius);
4648

47-
int check_pos(vec2 x, float size) {
48-
return int(mod(floor(x.x), size) + mod(floor(x.y), size)*size);
49-
}
5049

51-
mat3 rotate_around_x(_in(float) angle_degrees)
50+
mat3 rotate_around_x(const in float angle_degrees)
5251
{
5352
float angle = radians(angle_degrees);
5453
float _sin = sin(angle);
5554
float _cos = cos(angle);
5655
return mat3(1, 0, 0, 0, _cos, -_sin, 0, _sin, _cos);
5756
}
5857

59-
60-
ray_t get_primary_ray(
61-
_in(vec3) cam_local_point,
62-
_inout(vec3) cam_origin,
63-
_inout(vec3) cam_look_at
64-
){
65-
vec3 fwd = normalize(cam_look_at - cam_origin);
66-
vec3 up = vec3(0, 1, 0);
67-
vec3 right = cross(up, fwd);
68-
up = cross(fwd, right);
69-
70-
ray_t r = _begin(ray_t)
71-
cam_origin,
72-
normalize(fwd + up * cam_local_point.y + right * cam_local_point.x)
73-
_end;
74-
return r;
75-
}
76-
77-
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)
7859
{
7960
vec3 rc = sphere.origin - ray.origin;
8061
float radius2 = sphere.radius * sphere.radius;
@@ -88,15 +69,6 @@ bool isect_sphere(_in(ray_t) ray, _in(sphere_t) sphere, _inout(float) t0, _inout
8869
return true;
8970
}
9071

91-
// scattering coefficients at sea level (m)
92-
const vec3 betaR = vec3(5.5e-6, 13.0e-6, 22.4e-6); // Rayleigh
93-
const vec3 betaM = vec3(21e-6); // Mie
94-
95-
// scale height (m)
96-
// thickness of the atmosphere if its density were uniform
97-
const float hR = 7994.0; // Rayleigh
98-
const float hM = 1200.0; // Mie
99-
10072
float rayleigh_phase_func(float mu)
10173
{
10274
return
@@ -109,43 +81,19 @@ float rayleigh_phase_func(float mu)
10981
// represents the average cosine of the scattered directions
11082
// 0 is isotropic scattering
11183
// > 1 is forward scattering, < 1 is backwards
112-
const float g = 0.76;
11384
float henyey_greenstein_phase_func(float mu)
11485
{
86+
const float g = 0.76;
11587
return
11688
(1. - g*g)
11789
/ //---------------------------------------------
11890
((4. + PI) * pow(1. + g*g - 2.*g*mu, 1.5));
11991
}
12092

121-
// Schlick Phase Function factor
122-
// Pharr and Humphreys [2004] equivalence to g above
123-
const float k = 1.55*g - 0.55 * (g*g*g);
124-
float schlick_phase_func(float mu)
125-
{
126-
return
127-
(1. - k*k)
128-
/ //-------------------------------------------
129-
(4. * PI * (1. + k*mu) * (1. + k*mu));
130-
}
131-
132-
const float earth_radius = 6360e3; // (m)
133-
const float atmosphere_radius = 6420e3; // (m)
134-
135-
vec3 sun_dir = vec3(0, 1, 0);
136-
const float sun_power = 20.0;
137-
138-
const sphere_t atmosphere = _begin(sphere_t)
139-
vec3(0, 0, 0), atmosphere_radius, 0
140-
_end;
141-
142-
const int num_samples = 16;
143-
const int num_samples_light = 8;
144-
14593
bool get_sun_light(
146-
_in(ray_t) ray,
147-
_inout(float) optical_depthR,
148-
_inout(float) optical_depthM
94+
const in ray_t ray,
95+
inout float optical_depthR,
96+
inout float optical_depthM
14997
){
15098
float t0 = 0.0;
15199
float t1 = 0.0;
@@ -171,7 +119,7 @@ bool get_sun_light(
171119
return true;
172120
}
173121

174-
vec3 get_incident_light(_in(ray_t) ray)
122+
vec3 get_incident_light(const in ray_t ray)
175123
{
176124
// "pierce" the atmosphere with the viewing ray
177125
float t0 = 0.0;
@@ -194,11 +142,7 @@ vec3 get_incident_light(_in(ray_t) ray)
194142
// * integrates to 1 over the entire sphere of directions
195143
float phaseR = rayleigh_phase_func(mu);
196144
float phaseM =
197-
#if 1
198-
henyey_greenstein_phase_func(mu);
199-
#else
200-
schlick_phase_func(mu);
201-
#endif
145+
henyey_greenstein_phase_func(mu);
202146

203147
// optical depth (or "average density")
204148
// represents the accumulated extinction coefficients
@@ -223,10 +167,7 @@ vec3 get_incident_light(_in(ray_t) ray)
223167
optical_depthM += hm;
224168

225169
// gather the sunlight
226-
ray_t light_ray = _begin(ray_t)
227-
s,
228-
sun_dir
229-
_end;
170+
ray_t light_ray = ray_t(s, sun_dir);
230171
float optical_depth_lightR = 0.;
231172
float optical_depth_lightM = 0.;
232173
bool overground = get_sun_light(
@@ -253,40 +194,38 @@ vec3 get_incident_light(_in(ray_t) ray)
253194
sumM * phaseM * betaM);
254195
}
255196

256-
vec3 ACESFilm( vec3 x )
257-
{
258-
float a = 2.51f;
259-
float b = 0.03f;
260-
float c = 2.43f;
261-
float d = 0.59f;
262-
float e = 0.14f;
263-
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;
264206
}
265207

266208
void main()
267209
{
268-
vec2 aspect_ratio = vec2(u_res.x / u_res.y, 1);
269-
float fov = tan(radians(45.0));
270-
vec2 point_ndc = gl_FragCoord.xy / u_res.xy;
271-
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);
272213

273214
vec4 worldPos = (inverse((MVPM))*vec4(point_cam, 1.0));
274215
worldPos.xyz /= worldPos.w;
275216
point_cam = normalize(worldPos.xyz);
276217

277-
vec3 col = vec3(0);
218+
vec3 col = vec3(0.4);
278219
if (point_cam.y>-0.05) {
279220
// sun
280-
mat3 rot = rotate_around_x(-abs(sin(u_time / 20.)) * 90.);
221+
mat3 rot = rotate_around_x(-abs(sin(time / 20.)) * 90.);
281222
sun_dir *= rot;
282-
283-
vec3 eye = vec3 (0, earth_radius + 1., 0);
284-
vec3 look_at = vec3 (0, earth_radius + 1.0, -1);
285223

286-
//ray_t ray = get_primary_ray(point_cam, eye, look_at);
287224
ray_t ray = ray_t(vec3(0.0, earth_radius+1.0, 0.0), point_cam);
288225
col = get_incident_light(ray);
289226
}
290-
col = ACESFilm(col);
227+
col = U2Tone(col);
228+
col /= U2Tone(vec3(2.5));
229+
col = sqrt(col);
291230
color = vec4(col, 1);
292231
}

0 commit comments

Comments
 (0)