From aad3991734ac67b1c6ced3581e732dddaf0f6db4 Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Thu, 26 Mar 2020 21:24:50 +0100 Subject: [PATCH] Optimized float parameters (now passed as dictionaries) --- addons/material_maker/engine/gen_base.gd | 9 +++++---- addons/material_maker/engine/gen_buffer.gd | 12 ++++++------ addons/material_maker/engine/gen_material.gd | 16 ++++++++-------- addons/material_maker/engine/gen_shader.gd | 1 + material_maker/graph_edit.gd | 2 +- material_maker/nodes/generic.gd | 16 ++++++---------- material_maker/preview/preview_2d.gd | 11 ++++++----- 7 files changed, 33 insertions(+), 34 deletions(-) diff --git a/addons/material_maker/engine/gen_base.gd b/addons/material_maker/engine/gen_base.gd index 77546ec..b96b5e0 100644 --- a/addons/material_maker/engine/gen_base.gd +++ b/addons/material_maker/engine/gen_base.gd @@ -90,7 +90,7 @@ func init_parameters() -> void: func set_position(p) -> void: position = p if has_randomness() and !is_seed_locked() and is_inside_tree(): - get_tree().call_group("preview", "on_float_parameter_changed", "seed_o%s" % [ str(get_instance_id()) ], get_seed()) + get_tree().call_group("preview", "on_float_parameters_changed", { "seed_o"+str(get_instance_id()): get_seed() }) func get_type() -> String: return "generic" @@ -127,7 +127,7 @@ func set_parameter(n : String, v) -> void: if parameter_def.has("type"): if parameter_def.type == "float": var parameter_name = "p_o%s_%s" % [ str(get_instance_id()), n ] - get_tree().call_group("preview", "on_float_parameter_changed", parameter_name, v) + get_tree().call_group("preview", "on_float_parameters_changed", { parameter_name:v }) return elif parameter_def.type == "gradient": if v.interpolation == old_value.interpolation && v.points.size() == old_value.points.size(): @@ -138,13 +138,14 @@ func set_parameter(n : String, v) -> void: old_value.points[i] = { pos=old.v, r=old.c.r, g=old.c.g, b=old.c.b, a=old.c.a } old_value.points.sort_custom(CustomGradientSorter, "compare") v.points.sort_custom(CustomGradientSorter, "compare") + var parameter_changes = {} for i in range(old_value.points.size()): for f in [ "pos", "r", "g", "b", "a" ]: if v.points[i][f] != old_value.points[i][f]: var parameter_name = "p_o%s_%s_%d_%s" % [ str(get_instance_id()), n, i, f ] - get_tree().call_group("preview", "on_float_parameter_changed", parameter_name, v.points[i][f]) + parameter_changes[parameter_name] = v.points[i][f] + get_tree().call_group("preview", "on_float_parameters_changed", parameter_changes) return - print("regenerating shader") source_changed(0) func notify_output_change(output_index : int) -> void: diff --git a/addons/material_maker/engine/gen_buffer.gd b/addons/material_maker/engine/gen_buffer.gd index ad20999..b28fe59 100644 --- a/addons/material_maker/engine/gen_buffer.gd +++ b/addons/material_maker/engine/gen_buffer.gd @@ -55,12 +55,12 @@ func update_shader() -> void: material.set_shader_param(k, source.textures[k]) update_buffer() -func on_float_parameter_changed(n : String, v : float) -> void: - for p in VisualServer.shader_get_param_list(material.shader.get_rid()): - if p.name == n: - material.set_shader_param(n, v) - update_buffer() - return +func on_float_parameters_changed(parameter_changes : Dictionary) -> void: + for n in parameter_changes.keys(): + for p in VisualServer.shader_get_param_list(material.shader.get_rid()): + if p.name == n: + material.set_shader_param(n, parameter_changes[n]) + break func on_texture_changed(n : String) -> void: for p in VisualServer.shader_get_param_list(material.shader.get_rid()): diff --git a/addons/material_maker/engine/gen_material.gd b/addons/material_maker/engine/gen_material.gd index e33e18a..747163d 100644 --- a/addons/material_maker/engine/gen_material.gd +++ b/addons/material_maker/engine/gen_material.gd @@ -123,16 +123,16 @@ func render_textures() -> void: texture.flags ^= ImageTexture.FLAG_FILTER need_update[t.texture] = false -func on_float_parameter_changed(n : String, v : float) -> void: +func on_float_parameters_changed(parameter_changes : Dictionary) -> void: for t in TEXTURE_LIST: if generated_textures[t.texture] != null: - for p in VisualServer.shader_get_param_list(shader_materials[t.texture].shader.get_rid()): - if p.name == n: - shader_materials[t.texture].set_shader_param(n, v) - need_render[t.texture] = true - update_textures() - break - + for n in parameter_changes.keys(): + for p in VisualServer.shader_get_param_list(shader_materials[t.texture].shader.get_rid()): + if p.name == n: + shader_materials[t.texture].set_shader_param(n, parameter_changes[n]) + need_render[t.texture] = true + update_textures() + break func on_texture_changed(n : String) -> void: for t in TEXTURE_LIST: diff --git a/addons/material_maker/engine/gen_shader.gd b/addons/material_maker/engine/gen_shader.gd index 2139812..7284f1f 100644 --- a/addons/material_maker/engine/gen_shader.gd +++ b/addons/material_maker/engine/gen_shader.gd @@ -72,6 +72,7 @@ func set_shader_model(data: Dictionary) -> void: if shader_model.has("instance"): if shader_model.instance.find("$seed") != -1 or shader_model.instance.find("$(seed)") != -1: uses_seed = true + source_changed(0) func find_keyword_call(string, keyword): var search_string = "$%s(" % keyword diff --git a/material_maker/graph_edit.gd b/material_maker/graph_edit.gd index 4c2e658..8bbdb80 100644 --- a/material_maker/graph_edit.gd +++ b/material_maker/graph_edit.gd @@ -103,7 +103,7 @@ func update_tab_title() -> void: if get_parent().has_method("set_tab_title"): get_parent().set_tab_title(get_index(), title) -func set_need_save(ns) -> void: +func set_need_save(ns = true) -> void: if ns != need_save: need_save = ns update_tab_title() diff --git a/material_maker/nodes/generic.gd b/material_maker/nodes/generic.gd index 27e0efd..51c82ef 100644 --- a/material_maker/nodes/generic.gd +++ b/material_maker/nodes/generic.gd @@ -53,7 +53,7 @@ func on_parameter_changed(p, v) -> void: o.value = gradient else: print("unsupported widget "+str(o)) - update_shaders() + get_parent().set_need_save() func initialize_properties() -> void: var parameter_names = [] @@ -82,33 +82,29 @@ func initialize_properties() -> void: else: print("unsupported widget "+str(o)) -func update_shaders() -> void: - #get_parent().send_changed_signal() - update_preview() - func _on_text_changed(new_text, variable) -> void: ignore_parameter_change = variable generator.set_parameter(variable, new_text) ignore_parameter_change = "" - update_shaders() + get_parent().set_need_save() func _on_value_changed(new_value, variable) -> void: ignore_parameter_change = variable generator.set_parameter(variable, new_value) ignore_parameter_change = "" - if ! (new_value is float): - update_shaders() + get_parent().set_need_save() func _on_color_changed(new_color, variable) -> void: ignore_parameter_change = variable generator.set_parameter(variable, new_color) ignore_parameter_change = "" - update_shaders() + get_parent().set_need_save() func _on_gradient_changed(new_gradient, variable) -> void: ignore_parameter_change = variable generator.set_parameter(variable, MMType.serialize_value(new_gradient)) ignore_parameter_change = "" + get_parent().set_need_save() func create_parameter_control(p : Dictionary) -> Control: var control = null @@ -297,7 +293,7 @@ func edit_generator() -> void: func update_generator(shader_model) -> void: generator.set_shader_model(shader_model) update_node() - update_shaders() + get_parent().set_need_save() func load_generator() -> void: var dialog = FileDialog.new() diff --git a/material_maker/preview/preview_2d.gd b/material_maker/preview/preview_2d.gd index 6f0d8ac..b62d898 100644 --- a/material_maker/preview/preview_2d.gd +++ b/material_maker/preview/preview_2d.gd @@ -51,11 +51,12 @@ func on_parameter_changed(n : String, v) -> void: _: set_generator(generator, output) -func on_float_parameter_changed(n : String, v : float) -> void: - for p in VisualServer.shader_get_param_list(material.shader.get_rid()): - if p.name == n: - material.set_shader_param(n, v) - return +func on_float_parameters_changed(parameter_changes : Dictionary) -> void: + for n in parameter_changes.keys(): + for p in VisualServer.shader_get_param_list(material.shader.get_rid()): + if p.name == n: + material.set_shader_param(n, parameter_changes[n]) + break func on_resized() -> void: material.set_shader_param("size", rect_size)