@@ -30,9 +30,13 @@ out vec4 frag_data[4];
3030// The fragment shader for the sky
3131// ///////////////////////////////////////////////////////////////////////
3232
33- in vec3 vary_CloudColorSun;
34- in vec3 vary_CloudColorAmbient;
35- in float vary_CloudDensity;
33+ // In
34+ in vec3 pos;
35+
36+ in vec2 vary_texcoord0;
37+ in vec2 vary_texcoord1;
38+ in vec2 vary_texcoord2;
39+ in vec2 vary_texcoord3;
3640
3741uniform sampler2D cloud_noise_texture;
3842uniform sampler2D cloud_noise_texture_next;
@@ -42,11 +46,26 @@ uniform vec3 cloud_pos_density2;
4246uniform float cloud_scale;
4347uniform float cloud_variance;
4448
45- in vec2 vary_texcoord0;
46- in vec2 vary_texcoord1;
47- in vec2 vary_texcoord2;
48- in vec2 vary_texcoord3;
49- in float altitude_blend_factor;
49+ uniform vec3 camPosLocal;
50+
51+ uniform vec3 lightnorm;
52+ uniform vec3 sunlight_color;
53+ uniform vec3 moonlight_color;
54+ uniform int sun_up_factor;
55+ uniform vec3 ambient_color;
56+ uniform vec3 blue_horizon;
57+ uniform vec3 blue_density;
58+ uniform float haze_horizon;
59+ uniform float haze_density;
60+
61+ uniform float cloud_shadow;
62+ uniform float density_multiplier;
63+ uniform float max_y;
64+
65+ uniform vec3 glow;
66+ uniform float sun_moon_glow_factor;
67+
68+ uniform vec3 cloud_color;
5069
5170vec4 cloudNoise(vec2 uv)
5271{
@@ -58,21 +77,111 @@ vec4 cloudNoise(vec2 uv)
5877
5978void main()
6079{
80+ if (cloud_scale < 0.001 )
81+ {
82+ discard ;
83+ }
84+
6185 // Set variables
6286 vec2 uv1 = vary_texcoord0.xy;
6387 vec2 uv2 = vary_texcoord1.xy;
64-
65- vec3 cloudColorSun = vary_CloudColorSun;
66- vec3 cloudColorAmbient = vary_CloudColorAmbient;
67- float cloudDensity = vary_CloudDensity;
6888 vec2 uv3 = vary_texcoord2.xy;
6989 vec2 uv4 = vary_texcoord3.xy;
7090
71- if (cloud_scale < 0.001 )
91+ // Get relative position
92+ vec3 rel_pos = pos.xyz - camPosLocal.xyz + vec3 (0 , 50 , 0 );
93+
94+ float altitude_blend_factor = clamp ((rel_pos.y + 512.0 ) / max_y, 0.0 , 1.0 );
95+
96+ // Set altitude
97+ if (rel_pos.y > 0 )
7298 {
73- discard ;
99+ rel_pos *= (max_y / rel_pos.y);
100+ }
101+ if (rel_pos.y < 0 )
102+ {
103+ altitude_blend_factor = 0 ; // SL-11589 Fix clouds drooping below horizon
104+ rel_pos *= (- 32000 . / rel_pos.y);
74105 }
75106
107+ // Can normalize then
108+ vec3 rel_pos_norm = normalize (rel_pos);
109+ float rel_pos_len = length (rel_pos);
110+
111+ // Initialize temp variables
112+ vec3 sunlight = sunlight_color;
113+ vec3 light_atten;
114+
115+ // Sunlight attenuation effect (hue and brightness) due to atmosphere
116+ // this is used later for sunlight modulation at various altitudes
117+ light_atten = (blue_density + vec3 (haze_density * 0.25 )) * (density_multiplier * max_y);
118+
119+ // Calculate relative weights
120+ vec3 combined_haze = abs (blue_density) + vec3 (abs (haze_density));
121+ vec3 blue_weight = blue_density / combined_haze;
122+ vec3 haze_weight = haze_density / combined_haze;
123+
124+ // Compute sunlight from rel_pos & lightnorm (for long rays like sky)
125+ float off_axis = 1.0 / max (1e-6 , max (0 ., rel_pos_norm.y) + lightnorm.y);
126+ sunlight *= exp (- light_atten * off_axis);
127+
128+ // Distance
129+ float density_dist = rel_pos_len * density_multiplier;
130+
131+ // Transparency (-> combined_haze)
132+ // ATI Bugfix -- can't store combined_haze*density_dist in a variable because the ati
133+ // compiler gets confused.
134+ combined_haze = exp (- combined_haze * density_dist);
135+
136+ // Compute haze glow
137+ float haze_glow = 1.0 - dot (rel_pos_norm, lightnorm.xyz);
138+ // haze_glow is 0 at the sun and increases away from sun
139+ haze_glow = max (haze_glow, .001 );
140+ // Set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
141+ haze_glow *= glow.x;
142+ // Higher glow.x gives dimmer glow (because next step is 1 / "angle")
143+ haze_glow = pow (haze_glow, glow.z);
144+ // glow.z should be negative, so we're doing a sort of (1 / "angle") function
145+
146+ haze_glow *= sun_moon_glow_factor;
147+
148+ // Add "minimum anti-solar illumination"
149+ // For sun, add to glow. For moon, remove glow entirely. SL-13768
150+ haze_glow = (sun_moon_glow_factor < 1.0 ) ? 0.0 : (haze_glow + 0.25 );
151+
152+ // Increase ambient when there are more clouds
153+ vec3 tmpAmbient = ambient_color;
154+ tmpAmbient += (1 . - tmpAmbient) * cloud_shadow * 0.5 ;
155+
156+ // Dim sunlight by cloud shadow percentage
157+ sunlight *= (1 . - cloud_shadow);
158+
159+ // Haze color below cloud
160+ vec3 additiveColorBelowCloud =
161+ (blue_horizon * blue_weight * (sunlight + tmpAmbient) + (haze_horizon * haze_weight) * (sunlight * haze_glow + tmpAmbient));
162+
163+ // CLOUDS
164+ sunlight = sunlight_color;
165+ off_axis = 1.0 / max (1e-6 , lightnorm.y * 2 .);
166+ sunlight *= exp (- light_atten * off_axis);
167+
168+ // Cloud color out
169+ vec3 cloudColorSun = (sunlight * haze_glow) * cloud_color;
170+ vec3 cloudColorAmbient = tmpAmbient * cloud_color;
171+
172+ // Attenuate cloud color by atmosphere
173+ combined_haze = sqrt (combined_haze); // less atmos opacity (more transparency) below clouds
174+ cloudColorSun *= combined_haze;
175+ cloudColorAmbient *= combined_haze;
176+ vec3 oHazeColorBelowCloud = additiveColorBelowCloud * (1 . - combined_haze);
177+
178+ // Make a nice cloud density based on the cloud_shadow value that was passed in.
179+ float cloudDensity = 2 . * (cloud_shadow - 0.25 );
180+
181+ // Combine these to minimize register use
182+ cloudColorAmbient += oHazeColorBelowCloud;
183+
184+ // Cloud Fragment
76185 vec2 disturbance = vec2 (cloudNoise(uv1 / 8 .0f).x, cloudNoise((uv3 + uv1) / 16 .0f).x) * cloud_variance * (1 .0f - cloud_scale * 0 .25f);
77186 vec2 disturbance2 = vec2 (cloudNoise((uv1 + uv3) / 4 .0f).x, cloudNoise((uv4 + uv2) / 8 .0f).x) * cloud_variance * (1 .0f - cloud_scale * 0 .25f);
78187
0 commit comments