medfall

A super great game engine
Log | Files | Refs

commit 23a8978e84489fb13d966c2319e1c9fd22bd58a0
parent 418ed15ded78abab61c511e20dc3b4b3d3057945
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon Jul 17 22:18:58 +0300

Terrain colour dithering

Diffstat:
shaders/terrain.glsl | 15+++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/shaders/terrain.glsl b/shaders/terrain.glsl @@ -1,6 +1,7 @@ struct VSOut { vec4 view_position; vec3 world_position; + vec4 screen_position; }; #ifdef VERTEX_SHADER @@ -16,7 +17,8 @@ layout( std140 ) uniform view { void main() { v2f.world_position = position; v2f.view_position = V * vec4( position, 1.0 ); - gl_Position = P * v2f.view_position; + v2f.screen_position = P * v2f.view_position; + gl_Position = v2f.screen_position; } #else @@ -37,6 +39,11 @@ uniform sampler2D horizons; uniform samplerBuffer point_light_origins; uniform samplerBuffer point_light_colours; +vec3 noise( vec2 uv ) { + vec3 dither = vec3( dot( vec2( 171.0, 231.0 ), uv ) ); + return fract( dither / vec3( 103.0, 71.0, 97.0 ) ) - vec3( 0.5, 0.5, 0.5 ); +} + void main() { vec2 world_pos = mod( v2f.world_position.xy, textureSize( normals, 0 ) - ivec2( 1, 1 ) ); @@ -93,7 +100,11 @@ void main() { float fog_distance_bias = 100.0; float fog_t = 1.0 - exp( -fog_strength * max( ( length( v2f.view_position ) - fog_distance_bias ), 0.0 ) ); - colour = vec4( mix( c, fog_colour, fog_t ), 1.0 ); + vec2 ndc = v2f.screen_position.xy / v2f.screen_position.w; + // seems to produce good results even at other resolutions + vec3 dither_noise = noise( ndc * vec2( 1920, 1080 ) ) / 512.0; + + colour = vec4( mix( c, fog_colour, fog_t ) + dither_noise, 1.0 ); } #endif