commit ee467f003cf4bb64c6e2eb619ec875b507f08cfb
parent 850e1d0904f8d681c4a1923c1074d0db5eee05af
Author: Michael Savage <mikejsavage@gmail.com>
Date: Tue, 31 Oct 2017 23:27:15 +0200
Shader refactoring
Diffstat:
6 files changed, 20 insertions(+), 15 deletions(-)
diff --git a/hm.cc b/hm.cc
@@ -314,6 +314,7 @@ GAME_FRAME( game_frame ) {
render_state.shader = get_shader( SHADER_TREE );
render_state.uniforms[ UNIFORMS_VIEW ] = view_uniforms;
render_state.uniforms[ UNIFORMS_SUN ] = renderer_uniforms( sun_dir );
+ render_state.textures[ 0 ] = renderer_blue_noise();
renderer_draw_instances( tree_mesh, render_state, num_trees, instance_data );
}
diff --git a/shaders.cc b/shaders.cc
@@ -50,6 +50,7 @@ void shaders_init() {
shaders[ SHADER_UI ].path = "shaders/ui.glsl";
shaders[ SHADER_TREE ].path = "shaders/tree.glsl";
+ shaders[ SHADER_TREE ].texture_uniform_names[ 0 ] = "blue_noise";
shaders[ SHADER_TERRAIN ].path = "shaders/terrain.glsl";
shaders[ SHADER_TERRAIN ].texture_uniform_names[ 0 ] = "normals";
diff --git a/shaders/common.glsl b/shaders/common.glsl
@@ -31,6 +31,8 @@ vec3 reinhard_tonemap( vec3 c ) {
}
float linear_to_srgb( float linear ) {
+ linear = saturate( linear );
+
if( linear <= 0.0031308 )
return 12.92 * linear;
return 1.055 * pow( linear, 1.0 / 2.4 ) - 0.055;
@@ -43,3 +45,12 @@ vec3 linear_to_srgb( vec3 linear ) {
vec4 linear_to_srgb( vec4 linear ) {
return vec4( linear_to_srgb( linear.rgb ), linear.a );
}
+
+vec3 apply_fog( vec3 c, float dist ) {
+ vec3 fog_colour = vec3( 0.5, 0.6, 0.7 );
+ float fog_strength = 0.0003;
+ float fog_amount = 1.0 - exp( -fog_strength * max( dist, 0.0 ) );
+ return mix( c, fog_colour, fog_amount );
+}
+
+#define get_dither_noise( tex ) ( ( texture( blue_noise, gl_FragCoord.xy / textureSize( blue_noise, 0 ) ).xxx - vec3( 0.5 ) ) / 128.0 )
diff --git a/shaders/skybox.glsl b/shaders/skybox.glsl
@@ -40,10 +40,7 @@ void main() {
vec3 R = Z * perez_ext( cos_theta, gamma, cos_gamma );
- vec3 dither_noise = texture( blue_noise, gl_FragCoord.xy / textureSize( blue_noise, 0 ) ).xxx;
- dither_noise = ( dither_noise - vec3( 0.5 ) ) / 128.0;
-
- colour = vec4( linear_to_srgb( saturate( R + dither_noise ) ), 1.0 );
+ colour = vec4( linear_to_srgb( R + get_dither_noise( blue_noise ) ), 1.0 );
}
#endif
diff --git a/shaders/terrain.glsl b/shaders/terrain.glsl
@@ -88,14 +88,9 @@ void main() {
}
// fog
- vec3 fog_colour = vec3( 0.5, 0.6, 0.7 );
- float fog_strength = 0.0003;
- float fog_t = 1.0 - exp( -fog_strength * max( ( length( v2f.view_position ) ), 0.0 ) );
+ c = apply_fog( c, length( v2f.view_position ) );
- vec3 dither_noise = texture( blue_noise, gl_FragCoord.xy / textureSize( blue_noise, 0 ) ).xxx;
- dither_noise = ( dither_noise - vec3( 0.5 ) ) / 128.0;
-
- colour = vec4( linear_to_srgb( mix( c, fog_colour, fog_t ) + dither_noise ), 1.0 );
+ colour = vec4( linear_to_srgb( c + get_dither_noise( blue_noise ) ), 1.0 );
}
#endif
diff --git a/shaders/tree.glsl b/shaders/tree.glsl
@@ -13,6 +13,8 @@ struct VSOut {
vec3 colour;
};
+uniform sampler2D blue_noise;
+
#ifdef VERTEX_SHADER
in vec3 position;
@@ -45,11 +47,9 @@ void main() {
vec3 c = v2f.colour * half_lambert;
// fog
- vec3 fog_colour = vec3( 0.5, 0.6, 0.7 );
- float fog_strength = 0.0003;
- float fog_t = 1.0 - exp( -fog_strength * max( ( length( v2f.view_position ) ), 0.0 ) );
+ c = apply_fog( c, length( v2f.view_position ) );
- screen_colour = vec4( linear_to_srgb( mix( c, fog_colour, fog_t ) ), 1.0 );
+ screen_colour = vec4( linear_to_srgb( c + get_dither_noise( blue_noise ) ), 1.0 );
}
#endif