From: Mikko Rasa Date: Fri, 11 Mar 2022 09:55:49 +0000 (+0200) Subject: Implement alpha cutoff for materials X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ae423e6ef278388f92802fbae34b1c7ec339292f;p=libs%2Fgl.git Implement alpha cutoff for materials Having to implement it separately in each material's shader is not optimal, but since they each define their own uniform block, no better solution is readily apparent. The occluder shader also supports alpha cutoff so that objects with such materials can cast proper shadows. --- diff --git a/shaderlib/cooktorrance.glsl b/shaderlib/cooktorrance.glsl index ff1a56c7..ad1a459d 100644 --- a/shaderlib/cooktorrance.glsl +++ b/shaderlib/cooktorrance.glsl @@ -13,6 +13,7 @@ struct PbrMaterialParameters uniform PbrMaterial { PbrMaterialParameters pbr_material; + float alpha_cutoff; }; uniform sampler2D base_color_map; @@ -29,6 +30,7 @@ layout(constant_id=auto) const bool use_occlusion_map = false; layout(constant_id=auto) const bool use_emission = false; layout(constant_id=auto) const bool use_emission_map = false; layout(constant_id=auto) const bool use_image_based_lighting = false; +layout(constant_id=auto) const bool use_alpha_cutoff = false; #pragma MSP stage(fragment) virtual vec4 get_base_color() @@ -173,10 +175,13 @@ vec3 cooktorrance_lighting(vec3 normal, vec3 look, vec3 base_color, float metaln void main() { + vec4 base_color = get_base_color(); + if(use_alpha_cutoff && base_color.a &extra_spec) const { string module_name; map spec_values; + if(alpha_cutoff>0.0f) + spec_values["use_alpha_cutoff"] = true; + fill_program_info(module_name, spec_values); for(const auto &kvp: extra_spec) @@ -48,6 +56,12 @@ const Program *Material::create_compatible_shader(const map &extra_ return shprog; } +void Material::set_alpha_cutoff(float a) +{ + alpha_cutoff = a; + shdata.uniform("alpha_cutoff", a); +} + void Material::set_debug_name(const string &name) { #ifdef DEBUG @@ -78,9 +92,15 @@ Material::Loader::Loader(Material &m, Collection &c): void Material::Loader::init_actions() { + add("alpha_cutoff", &Loader::alpha_cutoff); add("sampler", &Loader::sampler); } +void Material::Loader::alpha_cutoff(float a) +{ + obj.set_alpha_cutoff(a); +} + void Material::Loader::sampler(const string &name) { obj.sampler = &get_collection().get(name); diff --git a/source/materials/material.h b/source/materials/material.h index ff7f53c0..75ff972c 100644 --- a/source/materials/material.h +++ b/source/materials/material.h @@ -27,6 +27,7 @@ private: virtual void init_actions(); private: + void alpha_cutoff(float); void sampler(const std::string &); }; @@ -72,9 +73,10 @@ public: protected: const Sampler *sampler = 0; + float alpha_cutoff = 0.0f; ProgramData shdata; - Material() = default; + Material(); public: virtual ~Material() = default; @@ -96,6 +98,9 @@ public: virtual const Texture *get_texture(Tag) const = 0; virtual const Sampler *get_sampler(Tag) const { return sampler; } + void set_alpha_cutoff(float a); + float get_alpha_cutoff() const { return alpha_cutoff; } + void set_debug_name(const std::string &); template