-
Notifications
You must be signed in to change notification settings - Fork 6
/
Atmospheric_Scattering_Sample_lslXDr.fs
174 lines (133 loc) · 3.66 KB
/
Atmospheric_Scattering_Sample_lslXDr.fs
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
/*
{
"IMPORTED" : [
],
"CATEGORIES" : [
"scatter",
"Automatically Converted"
],
"DESCRIPTION" : "Automatically converted from https:\/\/www.shadertoy.com\/view\/lslXDr by gltracy. sample codes of atmosphere scattering based on the article in GPU Gems2 : http:\/\/http.developer.nvidia.com\/GPUGems2\/gpugems2_chapter16.html",
"INPUTS" : [
]
}
*/
// Written by GLtracy
// math const
const float PI = 3.14159265359;
const float DEG_TO_RAD = PI / 180.0;
const float MAX = 10000.0;
// scatter const
const float K_R = 0.166;
const float K_M = 0.0025;
const float E = 14.3; // light intensity
const vec3 C_R = vec3( 0.3, 0.7, 1.0 ); // 1 / wavelength ^ 4
const float G_M = -0.85; // Mie g
const float R = 1.0;
const float R_INNER = 0.7;
const float SCALE_H = 4.0 / ( R - R_INNER );
const float SCALE_L = 1.0 / ( R - R_INNER );
const int NUM_OUT_SCATTER = 10;
const float FNUM_OUT_SCATTER = 10.0;
const int NUM_IN_SCATTER = 10;
const float FNUM_IN_SCATTER = 10.0;
// angle : pitch, yaw
mat3 rot3xy( vec2 angle ) {
vec2 c = cos( angle );
vec2 s = sin( angle );
return mat3(
c.y , 0.0, -s.y,
s.y * s.x, c.x, c.y * s.x,
s.y * c.x, -s.x, c.y * c.x
);
}
// ray direction
vec3 ray_dir( float fov, vec2 size, vec2 pos ) {
vec2 xy = pos - size * 0.5;
float cot_half_fov = tan( ( 90.0 - fov * 0.5 ) * DEG_TO_RAD );
float z = size.y * 0.5 * cot_half_fov;
return normalize( vec3( xy, -z ) );
}
// ray intersects sphere
// e = -b +/- sqrt( b^2 - c )
vec2 ray_vs_sphere( vec3 p, vec3 dir, float r ) {
float b = dot( p, dir );
float c = dot( p, p ) - r * r;
float d = b * b - c;
if ( d < 0.0 ) {
return vec2( MAX, -MAX );
}
d = sqrt( d );
return vec2( -b - d, -b + d );
}
// Mie
// g : ( -0.75, -0.999 )
// 3 * ( 1 - g^2 ) 1 + c^2
// F = ----------------- * -------------------------------
// 2 * ( 2 + g^2 ) ( 1 + g^2 - 2 * g * c )^(3/2)
float phase_mie( float g, float c, float cc ) {
float gg = g * g;
float a = ( 1.0 - gg ) * ( 1.0 + cc );
float b = 1.0 + gg - 2.0 * g * c;
b *= sqrt( b );
b *= 2.0 + gg;
return 1.5 * a / b;
}
// Reyleigh
// g : 0
// F = 3/4 * ( 1 + c^2 )
float phase_reyleigh( float cc ) {
return 0.75 * ( 1.0 + cc );
}
float density( vec3 p ){
return exp( -( length( p ) - R_INNER ) * SCALE_H );
}
float optic( vec3 p, vec3 q ) {
vec3 step = ( q - p ) / FNUM_OUT_SCATTER;
vec3 v = p + step * 0.5;
float sum = 0.0;
for ( int i = 0; i < NUM_OUT_SCATTER; i++ ) {
sum += density( v );
v += step;
}
sum *= length( step ) * SCALE_L;
return sum;
}
vec3 in_scatter( vec3 o, vec3 dir, vec2 e, vec3 l ) {
float len = ( e.y - e.x ) / FNUM_IN_SCATTER;
vec3 step = dir * len;
vec3 p = o + dir * e.x;
vec3 v = p + dir * ( len * 0.5 );
vec3 sum = vec3( 0.0 );
for ( int i = 0; i < NUM_IN_SCATTER; i++ ) {
vec2 f = ray_vs_sphere( v, l, R );
vec3 u = v + l * f.y;
float n = ( optic( p, v ) + optic( v, u ) ) * ( PI * 4.0 );
sum += density( v ) * exp( -n * ( K_R * C_R + K_M ) );
v += step;
}
sum *= len * SCALE_L;
float c = dot( dir, -l );
float cc = c * c;
return sum * ( K_R * C_R * phase_reyleigh( cc ) + K_M * phase_mie( G_M, c, cc ) ) * E;
}
void main()
{
// default ray dir
vec3 dir = ray_dir( 45.0, RENDERSIZE.xy, gl_FragCoord.xy );
// default ray origin
vec3 eye = vec3( 0.0, 0.0, 2.4 );
// rotate camera
mat3 rot = rot3xy( vec2( 0.0, TIME * 0.5 ) );
dir = rot * dir;
eye = rot * eye;
// sun light dir
vec3 l = vec3( 0, 0, 1 );
vec2 e = ray_vs_sphere( eye, dir, R );
if ( e.x > e.y ) {
discard;
}
vec2 f = ray_vs_sphere( eye, dir, R_INNER );
e.y = min( e.y, f.x );
vec3 I = in_scatter( eye, dir, e, l );
gl_FragColor = vec4( I, 1.0 );
}