From 7a5d84b06d0217c5edad5d9f4ef51404de0cb911 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 25 Sep 2022 19:45:34 +0300 Subject: [PATCH] Add a tint property for PBR materials --- blender/io_mspgl/material.py | 18 ++++++++++++++++++ shaderlib/cooktorrance.glsl | 5 +++-- source/materials/pbrmaterial.cpp | 7 +++++++ source/materials/pbrmaterial.h | 2 ++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/blender/io_mspgl/material.py b/blender/io_mspgl/material.py index ccbf0083..3e2bce00 100644 --- a/blender/io_mspgl/material.py +++ b/blender/io_mspgl/material.py @@ -19,6 +19,7 @@ class PropertyNode: checks = [self.check_group, self.check_scale, + self.check_tint, self.check_gray, self.check_extract, self.check_normal, @@ -63,6 +64,17 @@ class PropertyNode: self.data = self.node.inputs[i].default_value return self.set_input_from_linked(self.node.inputs[1-i]) + def check_tint(self): + if self.node.type!='MIX_RGB': + return + + if self.node.blend_type=='MULTIPLY': + for i in range(2): + if not self.node.inputs[1+i].is_linked: + self.type = 'TINT' + self.data = self.node.inputs[1+i].default_value[:] + return self.set_input_from_linked(self.node.inputs[2-i]) + def check_gray(self): if self.node.type=='RGBTOBW': self.type = 'GRAY' @@ -152,6 +164,7 @@ class MaterialProperty: self.texture = None self.tex_channels = None self.scale = 1.0 + self.tint = None def set_from_input(self, node_tree, input_socket, alpha_socket=None): if self.keyword: @@ -183,6 +196,8 @@ class MaterialProperty: channels = ['~'+c if c in n.data else c for c in channels] elif n.type=='SCALE': self.scale = n.data + elif n.type=='TINT': + self.tint = n.data elif n.type=='TEXTURE': self.texture = n.node n = n.input @@ -253,12 +268,15 @@ class Material: self.type = "pbr" base_color = self.create_property("base_color", (0.8, 0.8, 0.8, 1.0)) + tint = self.create_property("tint", (1.0, 1.0, 1.0, 1.0)) metalness = self.create_property("metalness", 0.0) roughness = self.create_property("roughness", 0.5) normal = self.create_property("normal_map") emission = self.create_property("emission", (0.0, 0.0, 0.0)) base_color.set_from_input(material.node_tree, from_node.inputs["Base Color"], from_node.inputs["Alpha"]) + if base_color.tint: + tint.value = base_color.tint metalness.set_from_input(material.node_tree, from_node.inputs["Metallic"]) roughness.set_from_input(material.node_tree, from_node.inputs["Roughness"]) normal.set_from_input(material.node_tree, from_node.inputs["Normal"]) diff --git a/shaderlib/cooktorrance.glsl b/shaderlib/cooktorrance.glsl index 1c564a04..8a69d473 100644 --- a/shaderlib/cooktorrance.glsl +++ b/shaderlib/cooktorrance.glsl @@ -6,6 +6,7 @@ import shadow; struct PbrMaterialParameters { vec4 base_color; + vec4 tint; vec4 emission; float metalness; float roughness; @@ -36,9 +37,9 @@ layout(constant_id=auto) const bool use_image_based_lighting = false; virtual vec4 get_base_color() { if(use_base_color_map) - return texture(base_color_map, texcoord.xy); + return texture(base_color_map, texcoord.xy)*pbr_material.tint; else - return pbr_material.base_color; + return pbr_material.base_color*pbr_material.tint; } virtual float get_metalness_value() diff --git a/source/materials/pbrmaterial.cpp b/source/materials/pbrmaterial.cpp index ebd3321f..c5d849c1 100644 --- a/source/materials/pbrmaterial.cpp +++ b/source/materials/pbrmaterial.cpp @@ -116,6 +116,12 @@ void PbrMaterial::set_base_color_map(const Texture *tex) base_color.texture = tex; } +void PbrMaterial::set_tint(const Color &color) +{ + tint.value = color; + shdata.uniform("pbr_material.tint", color); +} + void PbrMaterial::set_normal_map(const Texture *tex) { normal.texture = tex; @@ -177,6 +183,7 @@ void PbrMaterial::Loader::init_actions() add_property("roughness", &PbrMaterial::set_roughness, &PbrMaterial::set_roughness_map); add_property("occlusion", &PbrMaterial::set_occlusion_map); add_property("emission", &PbrMaterial::set_emission, &PbrMaterial::set_emission_map, false); + add_property("tint", &PbrMaterial::set_tint, 0, true); } } // namespace GL diff --git a/source/materials/pbrmaterial.h b/source/materials/pbrmaterial.h index 18719bd4..e863ca64 100644 --- a/source/materials/pbrmaterial.h +++ b/source/materials/pbrmaterial.h @@ -34,6 +34,7 @@ public: private: Property base_color; + Property tint; Property normal; Property metalness; Property roughness; @@ -60,6 +61,7 @@ public: void set_base_color(const Color &); void set_base_color_map(const Texture *); + void set_tint(const Color &); void set_normal_map(const Texture *); void set_metalness(float); void set_metalness_map(const Texture *); -- 2.43.0