-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskybox_buffer_frag.glsl
More file actions
118 lines (94 loc) · 4.03 KB
/
skybox_buffer_frag.glsl
File metadata and controls
118 lines (94 loc) · 4.03 KB
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
#version 300 es
precision highp float;
in vec2 v_uv;
out vec4 fragColor;
uniform vec3 sun_light_direction;
uniform float sun_light_energy;
uniform vec3 sun_light_color;
uniform float rayleigh;
uniform vec3 rayleigh_color;
uniform float mie;
uniform float mie_eccentricity;
uniform vec3 mie_color;
uniform float turbidity;
uniform float sun_disk_scale;
uniform vec3 ground_color;
uniform float exposure;
// Sun constants
const float light_size = 0.00872663806;
const float SUN_ENERGY = 1000.0; //OLD 1000.0
// optical length at zenith for molecules
const float rayleigh_zenith_size = 8.4e3;
const float mie_zenith_size = 1.25e3;
const vec3 UP = vec3( 0.0, 1.0, 0.0 );
#define PI 3.14159
vec2 dir_to_skybox_uv(vec3 dir) {
vec2 uv = vec2(atan(dir.x, -dir.z), acos(dir.y));
if (uv.x < 0.0) {
uv.x += PI * 2.0;
}
return (uv / vec2(PI * 2.0, PI));
}
vec3 skybox_uv_to_dir(vec2 uv) {
uv = (uv - 0.5) * vec2(-2.0, -1.0) * PI;
vec3 r_dir = normalize( vec3( sin(uv.x), tan(uv.y), cos(uv.x) ) * cos(uv.y) );
return r_dir;
}
// cheaper than smoothstep() linear version
float linearstepc(float a, float b, float value) {
return clamp((value - a) / (b-a),0.0,1.0);
}
float henyey_greenstein(float cos_theta, float g) {
const float k = 0.0795774715459;
return k * (1.0 - g * g) / (pow(1.0 + g * g - 2.0 * g * cos_theta, 1.5));
}
float cos_theta(vec3 r_dir, vec3 light_dir) {
return dot(r_dir, normalize(light_dir));
}
vec3 atmosphere_clouds(vec3 eye_dir) {
float zenith_angle = clamp( dot( UP, normalize(sun_light_direction)), -1.0, 1.0 );
float sun_energy = max(0.0, 1.0 - exp(-((PI * 0.5) - acos(zenith_angle)))) * sun_light_energy;
float sun_fade = 1.0 - clamp(1.0 - exp(normalize(sun_light_direction).y), 0.0, 1.0);
// Rayleigh coefficients.
float rayleigh_coefficient = rayleigh - ( 1.0 * ( 1.0 - sun_fade ) );
vec3 rayleigh_beta = rayleigh_coefficient * rayleigh_color * 0.0001;
// mie coefficients from Preetham
vec3 mie_beta = turbidity * mie * mie_color * 0.000434;
// optical length
float zenith = acos(max(0.0, dot( UP, eye_dir)));
float optical_mass = 1.0 / (cos(zenith) + 0.15 * pow(93.885 - degrees(zenith), -1.253));
float rayleigh_scatter = rayleigh_zenith_size * optical_mass;
float mie_scatter = mie_zenith_size * optical_mass;
// light extinction based on thickness of atmosphere
vec3 extinction = exp(-(rayleigh_beta * rayleigh_scatter + mie_beta * mie_scatter));
// in scattering
float cos_theta = dot(eye_dir, normalize(sun_light_direction));
float rayleigh_phase = (3.0 / (16.0 * PI)) * (1.0 + pow(cos_theta * 0.5 + 0.5, 2.0));
vec3 betaRTheta = rayleigh_beta * rayleigh_phase;
float mie_phase = henyey_greenstein(cos_theta, mie_eccentricity);
vec3 betaMTheta = mie_beta * mie_phase;
vec3 Lin = pow(sun_energy * ((betaRTheta + betaMTheta) / (rayleigh_beta + mie_beta)) * (1.0 - extinction), vec3(1.5));
// Hack from https://github.com/mrdoob/three.js/blob/master/examples/jsm/objects/Sky.js
Lin *= mix(vec3(1.0), pow(sun_energy * ((betaRTheta + betaMTheta) / (rayleigh_beta + mie_beta)) * extinction, vec3(0.5)), clamp(pow(1.0 - zenith_angle, 5.0), 0.0, 1.0));
vec3 color;
// Hack in the ground color
// Lin *= mix(ground_color.rgb, vec3(1.0), smoothstep(-0.1, 0.1, dot(UP, eye_dir)));
// Lin *= mix(ground_color.rgb, vec3(1.0), linearstepc(-0.1, 0.1, dot(UP, eye_dir)));
// Modified For Clouds
// Lin *= mix(ground_color.rgb, vec3(1.0), linearstepc(-0.9, 0.1, dot(UP, eye_dir)) * 0.9 + 0.1 );
// Solar disk and out-scattering
float sunAngularDiameterCos = cos(light_size * sun_disk_scale);
float sunAngularDiameterCos2 = cos(light_size * sun_disk_scale * 0.5);
float sundisk = smoothstep(sunAngularDiameterCos, sunAngularDiameterCos2, cos_theta);
vec3 L0 = (sun_energy * extinction) * sundisk * sun_light_color.xyz;
//L0 += texture(night_sky, SKY_COORDS).xyz * extinction;
color = Lin + L0;
color = pow(color, vec3(1.0 / (1.2 + (1.2 * sun_fade) ) ) );
color *= exposure;
return color;
}
void main() {
vec3 dir = skybox_uv_to_dir(v_uv);
fragColor = vec4(atmosphere_clouds(dir), 1.0);
// fragColor = vec4(v_uv, 0.0, 1.0);
}