From cc606137b38e6742f5f4038b0de3887d24cac351 Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Thu, 26 Mar 2020 21:58:26 +0100 Subject: [PATCH] Added optimization for color parameters --- addons/material_maker/engine/gen_base.gd | 9 ++++++++- addons/material_maker/engine/gen_shader.gd | 7 ++++++- material_maker/preview/preview_2d.gd | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/addons/material_maker/engine/gen_base.gd b/addons/material_maker/engine/gen_base.gd index b96b5e0..980e413 100644 --- a/addons/material_maker/engine/gen_base.gd +++ b/addons/material_maker/engine/gen_base.gd @@ -126,9 +126,16 @@ func set_parameter(n : String, v) -> void: var parameter_def : Dictionary = get_parameter_def(n) if parameter_def.has("type"): if parameter_def.type == "float": - var parameter_name = "p_o%s_%s" % [ str(get_instance_id()), n ] + var parameter_name = "p_o"+str(get_instance_id())+"_"+n get_tree().call_group("preview", "on_float_parameters_changed", { parameter_name:v }) return + elif parameter_def.type == "color": + var parameter_changes = {} + for f in [ "r", "g", "b", "a" ]: + var parameter_name = "p_o"+str(get_instance_id())+"_"+n+"_"+f + parameter_changes[parameter_name] = v[f] + get_tree().call_group("preview", "on_float_parameters_changed", parameter_changes) + return elif parameter_def.type == "gradient": if v.interpolation == old_value.interpolation && v.points.size() == old_value.points.size(): # convert from old format diff --git a/addons/material_maker/engine/gen_shader.gd b/addons/material_maker/engine/gen_shader.gd index 7284f1f..6728964 100644 --- a/addons/material_maker/engine/gen_shader.gd +++ b/addons/material_maker/engine/gen_shader.gd @@ -199,7 +199,7 @@ func subst(string : String, context : MMGenContext, uv : String = "") -> Diction value = 0 value_string = p.values[value].value elif p.type == "color": - value_string = "vec4(%.9f, %.9f, %.9f, %.9f)" % [ value.r, value.g, value.b, value.a ] + value_string = "vec4(p_%s_%s_r, p_%s_%s_g, p_%s_%s_b, p_%s_%s_a)" % [ genname, p.name, genname, p.name, genname, p.name, genname, p.name ] elif p.type == "gradient": value_string = genname+"_"+p.name+"_gradient_fct" elif p.type == "boolean": @@ -254,6 +254,11 @@ func _get_shader_code(uv : String, output_index : int, context : MMGenContext) - for p in shader_model.parameters: if p.type == "float": rv.defs += "uniform float p_%s_%s = %.9f;\n" % [ genname, p.name, parameters[p.name] ] + elif p.type == "color": + rv.defs += "uniform float p_%s_%s_r = %.9f;\n" % [ genname, p.name, parameters[p.name].r ] + rv.defs += "uniform float p_%s_%s_g = %.9f;\n" % [ genname, p.name, parameters[p.name].g ] + rv.defs += "uniform float p_%s_%s_b = %.9f;\n" % [ genname, p.name, parameters[p.name].b ] + rv.defs += "uniform float p_%s_%s_a = %.9f;\n" % [ genname, p.name, parameters[p.name].a ] elif p.type == "gradient": var g = parameters[p.name] if !(g is MMGradient): diff --git a/material_maker/preview/preview_2d.gd b/material_maker/preview/preview_2d.gd index b62d898..7249611 100644 --- a/material_maker/preview/preview_2d.gd +++ b/material_maker/preview/preview_2d.gd @@ -46,7 +46,7 @@ func on_parameter_changed(n : String, v) -> void: var p = generator.get_parameter_def(n) if p.has("type"): match p.type: - "float", "gradient": + "float", "color", "gradient": pass _: set_generator(generator, output)