Debian Codegraph @

Shaders/birds-ALS.frag

raw
1 // -*-C++-*-
2 #version 120
3
4 uniform sampler2D texture;
5
6 uniform float color_base_r;
7 uniform float color_base_g;
8 uniform float color_base_b;
9
10 uniform float color_alt_r;
11 uniform float color_alt_g;
12 uniform float color_alt_b;
13
14 uniform float visibility;
15 uniform float avisibility;
16 uniform float hazeLayerAltitude;
17 uniform float eye_alt;
18 uniform float terminator;
19 uniform float scattering;
20
21 uniform float osg_SimulationTime;
22
23
24 varying vec3 vertex;
25 varying vec3 relPos;
26 varying vec3 normal;
27
28 const float terminator_width = 200000.0;
29
30 float Noise2D(in vec2 coord, in float wavelength);
31 float VoronoiNoise2D(in vec2 coord, in float wavelength, in float xrand, in float yrand);
32 float fog_func (in float targ, in float alt);
33
34 float light_func (in float x, in float a, in float b, in float c, in float d, in float e)
35 {
36 //x = x - 0.5;
37
38 // use the asymptotics to shorten computations
39 if (x < -15.0) {return 0.0;}
40
41 return e / pow((1.0 + a * exp(-b * (x-c)) ),(1.0/d));
42 }
43
44
45 void main()
46 {
47
48 float noise = 0.0;
49
50
51 vec3 color_base = vec3 (color_base_r, color_base_g, color_base_b);
52 vec3 color_alt = vec3 (color_alt_r, color_alt_g, color_alt_b);
53 vec3 shadedFogColor = vec3(0.55, 0.67, 0.88);
54
55
56 vec3 viewDir = normalize(relPos);
57
58
59 vec2 lookup_coords = vertex.yz;
60 lookup_coords.x += 0.03* osg_SimulationTime;
61
62 float domain_size = 0.05;
63 float r = length(vertex);
64
65 float domain_noise = VoronoiNoise2D(lookup_coords, domain_size, 0.0, 0.0);
66 domain_noise = domain_noise * (1.0- smoothstep(0.5, 1.0, r));
67
68 if (domain_noise < 0.9) {discard;}
69
70
71
72 // fogging
73
74 float dist = length(relPos);
75 float delta_z = hazeLayerAltitude - eye_alt;
76 float transmission;
77 float vAltitude;
78 float delta_zv;
79 float H;
80 float distance_in_layer;
81 float transmission_arg;
82
83 // angle with horizon
84 float ct = dot(vec3(0.0, 0.0, 1.0), relPos)/dist;
85
86
87 if (delta_z > 0.0) // we're inside the layer
88 {
89 if (ct < 0.0) // we look down
90 {
91 distance_in_layer = dist;
92 vAltitude = min(distance_in_layer,min(visibility, avisibility)) * ct;
93 delta_zv = delta_z - vAltitude;
94 }
95 else // we may look through upper layer edge
96 {
97 H = dist * ct;
98 if (H > delta_z) {distance_in_layer = dist/H * delta_z;}
99 else {distance_in_layer = dist;}
100 vAltitude = min(distance_in_layer,visibility) * ct;
101 delta_zv = delta_z - vAltitude;
102 }
103 }
104 else // we see the layer from above, delta_z < 0.0
105 {
106 H = dist * -ct;
107 if (H < (-delta_z))
108 {
109 distance_in_layer = 0.0;
110 delta_zv = 0.0;
111 }
112 else
113 {
114 vAltitude = H + delta_z;
115 distance_in_layer = vAltitude/H * dist;
116 vAltitude = min(distance_in_layer,visibility) * (-ct);
117 delta_zv = vAltitude;
118 }
119 }
120
121
122
123 transmission_arg = (dist-distance_in_layer)/avisibility;
124 if (visibility < avisibility)
125 {
126 transmission_arg = transmission_arg + (distance_in_layer/visibility);
127 }
128 else
129 {
130 transmission_arg = transmission_arg + (distance_in_layer/avisibility);
131 }
132
133
134
135 transmission = fog_func(transmission_arg, 0.0);
136 float lightArg = terminator/100000.0;
137 float earthShade = 0.6 * (1.0 - smoothstep(-terminator_width+ terminator, terminator_width + terminator, 0.0)) + 0.4;
138
139 vec4 light_diffuse;
140
141
142 light_diffuse.b = light_func(lightArg , 1.330e-05, 0.264, 2.227, 1.08e-05, 1.0);
143 light_diffuse.g = light_func(lightArg , 3.931e-06, 0.264, 3.827, 7.93e-06, 1.0);
144 light_diffuse.r = light_func(lightArg, 8.305e-06, 0.161, 3.827, 3.04e-05, 1.0);
145 light_diffuse.a = 1.0;
146
147 light_diffuse *=scattering;
148
149
150 float intensity = length(light_diffuse.rgb);
151 light_diffuse.rgb = intensity * normalize(mix(light_diffuse.rgb, shadedFogColor, 1.0 -smoothstep(0.4, 0.7,earthShade) ));
152
153
154 vec4 domainTexel;
155
156
157 if (domain_noise > 0.9) {domainTexel = vec4 (1.0, 1.0, 1.0, 1.0);}
158 else {domainTexel = vec4 (0.0, 0.0, 0.0, 0.0);}
159
160
161
162 vec2 coords_raw = lookup_coords/domain_size;
163 coords_raw += vec2(0.5,0.5);
164 float coord_int_x = coords_raw.x - fract(coords_raw.x) ;
165 float coord_int_y = coords_raw.y - fract(coords_raw.y) ;
166
167 vec2 domain_coords = vec2 (coords_raw.x - coord_int_x, coords_raw.y - coord_int_y);
168
169 float domain_x = coords_raw.x - coord_int_x;
170
171 domain_coords.y = clamp(domain_coords.y, 0.05, 0.95);
172
173 domain_coords.x *=0.25;
174
175 float shape_select = 0.0;
176
177 if (domain_noise > 0.975) {shape_select = 0.25;}
178 else if (domain_noise > 0.95) {shape_select = 0.5;}
179 else if (domain_noise > 0.925) {shape_select = 0.75;}
180
181 float t_fact = fract(osg_SimulationTime);
182
183 if (t_fact > 0.75) {shape_select +=0.75;}
184 else if (t_fact > 0.5) {shape_select +=0.5;}
185 else if (t_fact > 0.25) {shape_select +=0.25;}
186
187
188 domain_coords.x += shape_select;
189
190 vec4 shapeTexel = texture2D(texture, domain_coords);
191
192 color_base.rgb = mix(color_alt.rgb, color_base.rgb, length(shapeTexel.rgb)/1.73);
193
194 if ((domain_coords.y < 0.1) || (domain_coords.y > 0.9)) {shapeTexel.a = 0.0;}
195 if ((domain_x < 0.1) || (domain_x > 0.9)) {shapeTexel.a = 0.0;}
196
197 domainTexel.rgb *= color_base.rgb;
198
199 vec4 birdTexel;
200 birdTexel.rgb = domainTexel.rgb * light_diffuse.rgb;
201 birdTexel.a = domainTexel.a * shapeTexel.a * transmission;
202
203
204 gl_FragColor = birdTexel;
205
206
207 }