Debian Codegraph @

Shaders/planet-high.frag

raw
1 // -*-C++-*-
2
3 // Ambient term comes in gl_Color.rgb.
4 #version 120
5
6 varying vec4 diffuse_term;
7 varying vec3 normal;
8 varying vec3 ecViewDir;
9 varying vec3 VTangent;
10
11 uniform float visibility;
12 uniform float air_pollution;
13 uniform float moonlight;
14 uniform float sun_angle;
15 uniform bool use_clouds;
16 uniform bool use_cloud_shadows;
17 uniform bool use_overlay;
18 uniform sampler2D texture;
19 uniform sampler2D shadowtex;
20 uniform sampler2D grain_texture;
21 uniform sampler2D normal_texture;
22
23 float Noise2D(in vec2 coord, in float wavelength);
24 vec3 filter_combined (in vec3 color) ;
25 vec3 moonlight_perception (in vec3 light);
26
27 void main()
28 {
29 vec3 n;
30 float NdotL, NdotHV;
31 vec4 color = gl_Color;
32 vec3 lightDir = normalize(gl_LightSource[0].position.xyz);
33
34 vec3 halfVector = normalize(lightDir + normalize(ecViewDir));
35 vec4 texel;
36 vec4 shadowTexel;
37 vec4 grainTexel;
38 vec4 fragColor;
39 vec4 specular = vec4(0.0);
40
41 // If gl_Color.a == 0, this is a back-facing polygon and the
42 // normal should be reversed.
43 n = (2.0 * gl_Color.a - 1.0) * normal;
44 n = normalize(n);
45
46 float parallaxFactor = max(1.0 - dot(normalize(ecViewDir), n),0.0);
47
48 vec3 VBinormal;
49 VBinormal = normalize(cross(normal, VTangent));
50
51 float xOffset1 = -1.0 * dot(ecViewDir, normalize(VTangent));
52 float yOffset1 = -1.0 * dot(ecViewDir, VBinormal);
53
54 vec2 grad_dir = normalize (vec2 (xOffset1, yOffset1));
55
56 vec4 nmap = texture2D(normal_texture, gl_TexCoord[0].st);
57
58
59
60
61 float hmap = 1.0 - nmap.a;
62 nmap = texture2D(normal_texture, gl_TexCoord[0].st - 0.0005 * grad_dir * hmap * 2.0 * parallaxFactor);
63
64
65 // nmap.rgb should not be normalized, it adversely modifies N = 2 nmap - 1
66 // nmap.rgb = normalize(nmap.rgb);
67 //if (nmap.b < 0.0) {nmap.b = -nmap.b;}
68
69 vec3 N = nmap.rgb * 2.0 - 1.0;
70 N = normalize(N.x * normalize(VTangent) * 0.6 + N.y * VBinormal * 0.6 + N.z * n);
71
72
73 float xOffset = -0.005 * dot(lightDir, normalize(VTangent));
74 float yOffset = -0.005 * dot(lightDir, VBinormal);
75
76
77
78
79 if ((use_cloud_shadows)&&(use_clouds))
80 {shadowTexel = texture2D(shadowtex, vec2(gl_TexCoord[0].s-xOffset, gl_TexCoord[0].t-yOffset));}
81 else
82 {shadowTexel = vec4 (0.0,0.0,0.0,0.0);}
83
84 texel = texture2D(texture, gl_TexCoord[0].st - 0.0005 * grad_dir * hmap * 2.0 * parallaxFactor);
85 float night_light = (1.0 -texel.a);
86 texel.a = 1.0;
87 grainTexel = texture2D(grain_texture, gl_TexCoord[0].st * 40.0);
88
89 float noise = Noise2D( gl_TexCoord[0].st, 0.00005);
90 noise += Noise2D( gl_TexCoord[0].st, 0.0002);
91 noise += Noise2D( gl_TexCoord[0].st, 0.0001);
92
93 noise= noise/3.0;
94
95
96 vec3 light_specular = vec3 (1.0, 1.0, 1.0);
97 NdotL = dot(N, lightDir);
98 float NdotLraw = NdotL;
99 // due to atmosphere scattering, we should make this harder
100 NdotL = smoothstep(-0.2 ,0.2,NdotL);
101 // fog does not feel normal map
102 float NdotLfog = smoothstep(-0.2 , 0.2, dot(n, lightDir));
103
104 float intensity = length(diffuse_term);
105 vec4 dawn = intensity * normalize (vec4 (1.0,0.5,0.3,1.0));
106 vec4 diff_term = mix(dawn, diffuse_term, smoothstep(0.0, 0.3, NdotL));
107
108
109 intensity = length(light_specular);
110 light_specular = mix(dawn.rgb, light_specular, smoothstep(0.0, 0.4, NdotL));
111
112 float oceanness = smoothstep(0.0, 0.1,length(texel.rgb - vec3 (0.007,0.019, 0.078)));
113 float specular_enhancement = 4.0 * (1.0 - oceanness);
114
115 if (use_overlay) {
116 //texel.rgb = mix(texel.rgb, grainTexel.rgb, 0.4* grainTexel.a * oceanness);
117 texel.rgb = texel.rgb * (0.85 + 0.3 * noise);
118 texel.r = smoothstep(0.0, 0.95, texel.r);
119 texel.g = smoothstep(0.0, 0.95, texel.g);
120 texel.b = smoothstep(0.0, 0.95, texel.b);
121 float intensity = length(texel.rgb);
122 texel.rgb = mix(texel.rgb, intensity * vec3 (1.0,1.0,1.0), 0.3);
123 }
124
125 //texel.rgb = vec3 (0.5,0.5,0.5);
126
127
128
129 // strictly positive produces visible hard cut
130 if (NdotL >= 0.0) {
131 color += diff_term * NdotL * (1.0-shadowTexel.a);
132 NdotHV = max(dot(n, halfVector), 0.0);
133 if (gl_FrontMaterial.shininess > 0.0)
134 specular.rgb = (gl_FrontMaterial.specular.rgb * specular_enhancement
135 * light_specular * (1.0-shadowTexel.a)
136 * pow(NdotHV, gl_FrontMaterial.shininess));
137 }
138
139 vec3 moonLightColor = vec3 (0.095, 0.095, 0.15) * moonlight;
140 moonLightColor = moonlight_perception (moonLightColor);
141 color.rgb += moonLightColor;
142
143 color.a = diffuse_term.a;
144
145
146 // This shouldn't be necessary, but our lighting becomes very
147 // saturated. Clamping the color before modulating by the texture
148 // is closer to what the OpenGL fixed function pipeline does.
149 color = clamp(color, 0.0, 1.0);
150
151 fragColor = color * texel + specular;
152
153
154 float night_light_factor = night_light * (1.0 - smoothstep(-0.3, 0.0, NdotLraw));
155
156 float noise_factor = (0.4 + 0.6* smoothstep(0.7 - 0.4* night_light,0.9 - 0.4 * night_light,noise));
157 night_light_factor *= noise_factor;
158
159
160 vec3 light_color = vec3(1.0, 0.7, 0.3);
161 vec3 central_light_color = vec3 (1.0, 1.0, 1.0);
162 light_color = mix(light_color, central_light_color, smoothstep(0.3, 0.6,noise*noise * night_light));
163
164 fragColor.rgb += light_color * night_light_factor * 1.4;
165
166 float angle = dot(normalize(ecViewDir), normalize(normal));
167 float distance_through_atmosphere = min(10.0 / (abs(angle)+0.001),500.0);
168
169
170 float fogLighting = clamp(NdotLfog,0.0,1.0) * length(diff_term.rgb/1.73);
171
172 vec4 fogColor = vec4 (0.83,0.9,1.0,1.0) * fogLighting;
173 vec3 rayleighColor = vec3 (0.17, 0.52, 0.87) * fogLighting;
174
175 float heightFactor = exp(-hmap * 0.8);
176 distance_through_atmosphere *= heightFactor;
177
178 float fogFactor = exp(-distance_through_atmosphere/(visibility/1000.0));
179 float rayleighFactor = exp(-distance_through_atmosphere/(300.0 / (1.0 + 4.0 * air_pollution)) );
180
181
182
183 fragColor.rgb = mix(rayleighColor, fragColor.rgb, rayleighFactor);
184 fragColor = mix(fogColor, fragColor, fogFactor);
185
186 fragColor.rgb = filter_combined(fragColor.rgb);
187
188
189
190
191 gl_FragColor = clamp(fragColor, 0.0, 1.0);
192
193
194 }