]> git.tdb.fi Git - libs/gl.git/blobdiff - shaderlib/common.glsl
Add support for alpha to coverage
[libs/gl.git] / shaderlib / common.glsl
index 300726dc541c0f7d4cfcdfdcdba9094f4662f242..695bce3b5da4ca0d8b06c5785a8bcea6f2c1da3a 100644 (file)
@@ -1,10 +1,17 @@
 import msp_interface;
 import shadow;
 
+struct AlphaCutoffParams
+{
+       float cutoff;
+       float feather;
+};
+
 layout(set=1) uniform sampler2D normal_map;
 
 layout(constant_id=auto) const bool use_instancing = false;
 layout(constant_id=auto) const bool use_normal_map = false;
+layout(constant_id=auto) const bool use_alpha_cutoff = false;
 
 #pragma MSP stage(vertex)
 virtual vec4 get_vertex_position()
@@ -95,6 +102,19 @@ virtual IncomingLight get_incoming_light(int index, vec3 world_pos)
        return IncomingLight(rel_pos/d, light_sources[index].color*attenuation);
 }
 
+float apply_alpha_cutoff(float alpha, AlphaCutoffParams params)
+{
+       if(use_alpha_cutoff)
+       {
+               if(alpha<params.cutoff)
+                       discard;
+               float limit = min(params.cutoff+params.feather*fwidth(alpha), 1.0);
+               return smoothstep(params.cutoff, limit, alpha);
+       }
+       else
+               return alpha;
+}
+
 vec3 apply_fog(vec3 color)
 {
        float fog_value = exp(fog_coord*fog_density);