From 29048fc307c8d9020d69140a420806f4b5145868 Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Fri, 27 Mar 2020 23:35:33 +0100 Subject: [PATCH] Optimized tones UI and added histogram panel --- material_maker/main_window.gd | 4 ++ material_maker/main_window_layout.gd | 1 + material_maker/nodes/tones.gd | 11 +--- material_maker/widgets/histogram/histogram.gd | 61 ++++++++++++++++++- .../widgets/histogram/histogram.tscn | 4 +- 5 files changed, 68 insertions(+), 13 deletions(-) diff --git a/material_maker/main_window.gd b/material_maker/main_window.gd index 9b2e293..8b8c87e 100644 --- a/material_maker/main_window.gd +++ b/material_maker/main_window.gd @@ -15,6 +15,7 @@ onready var projects = $VBoxContainer/Layout/SplitRight/ProjectsPane/Projects onready var layout = $VBoxContainer/Layout var library var preview_2d +var histogram var preview_3d var hierarchy @@ -131,6 +132,7 @@ func _ready() -> void: layout.load_panes(config_cache) library = layout.get_pane("Library") preview_2d = layout.get_pane("Preview2D") + histogram = layout.get_pane("Histogram") preview_3d = layout.get_pane("Preview3D") preview_3d.connect("need_update", self, "update_preview_3d") hierarchy = layout.get_pane("Hierarchy") @@ -585,9 +587,11 @@ func update_preview_2d(node = null) -> void: break if node != null: preview_2d.set_generator(node.generator) + histogram.set_generator(node.generator) preview_2d_background.set_generator(node.generator) else: preview_2d.set_generator(null) + histogram.set_generator(null) preview_2d_background.set_generator(null) func update_preview_3d(previews : Array) -> void: diff --git a/material_maker/main_window_layout.gd b/material_maker/main_window_layout.gd index 30b268c..60391cc 100644 --- a/material_maker/main_window_layout.gd +++ b/material_maker/main_window_layout.gd @@ -10,6 +10,7 @@ const PANES = [ { name="Library", scene=preload("res://material_maker/library.tscn"), position="TopLeft" }, { name="Preview2D", scene=preload("res://material_maker/preview/preview_2d_panel.tscn"), position="BottomLeft" }, { name="Preview3D", scene=preload("res://material_maker/preview/preview_3d_panel.tscn"), position="BottomLeft" }, + { name="Histogram", scene=preload("res://material_maker/widgets/histogram/histogram.tscn"), position="BottomLeft" }, { name="Hierarchy", scene=preload("res://material_maker/widgets/graph_tree/hierarchy_pane.tscn"), position="TopRight" } ] diff --git a/material_maker/nodes/tones.gd b/material_maker/nodes/tones.gd index aff0e33..88e0b0d 100644 --- a/material_maker/nodes/tones.gd +++ b/material_maker/nodes/tones.gd @@ -69,19 +69,10 @@ func _ready() -> void: cursor_out_max = Cursor.new(Color(1.0, 1.0, 1.0), 1.0, false) $Histogram.add_child(cursor_out_max) -func set_generator(g) -> void: - .set_generator(g) - generator.connect("parameter_changed", self, "on_parameter_changed") - _on_Mode_item_selected(0) - func on_parameter_changed(p, v) -> void: if p == "__input_changed__": var source = generator.get_source(0) - var result = source.generator.render(source.output_index, 128, true) - while result is GDScriptFunctionState: - result = yield(result, "completed") - result.copy_to_texture($Histogram.get_image_texture()) - result.release() + $Histogram.set_generator(source.generator, source.output_index) func get_parameter(n : String) -> float: var value = generator.get_parameter(n) diff --git a/material_maker/widgets/histogram/histogram.gd b/material_maker/widgets/histogram/histogram.gd index d2ae909..8ff3fd8 100644 --- a/material_maker/widgets/histogram/histogram.gd +++ b/material_maker/widgets/histogram/histogram.gd @@ -1,10 +1,67 @@ extends Control -func update_histogram() -> void: - pass +var generator : MMGenBase = null +var output : int = 0 + +onready var image : ColorRect = $ViewportImage/ColorRect func get_image_texture() -> ImageTexture: return $ViewportImage/ColorRect.material.get_shader_param("tex") func get_histogram_texture() -> ImageTexture: return $Control.material.get_shader_param("tex") + +func set_generator(g : MMGenBase, o : int = 0) -> void: + if is_instance_valid(generator): + generator.disconnect("parameter_changed", self, "on_parameter_changed") + var source = { defs="", code="", textures={}, type="f", f="1.0" } + if is_instance_valid(g): + generator = g + output = o + generator.connect("parameter_changed", self, "on_parameter_changed") + var param_defs : Array = generator.get_parameter_defs() + var gen_output_defs = generator.get_output_defs() + if ! gen_output_defs.empty(): + var context : MMGenContext = MMGenContext.new() + source = generator.get_shader_code("uv", output, context) + while source is GDScriptFunctionState: + source = yield(source, "completed") + if source.empty(): + source = { defs="", code="", textures={}, type="f", f="1.0" } + # Update shader + image.material.shader.code = MMGenBase.generate_preview_shader(source, source.type, "uniform vec2 size;void fragment() {COLOR = preview_2d(UV);}") + # Get parameter values from the shader code + var regex = RegEx.new() + regex.compile("uniform\\s+(\\w+)\\s+([\\w_\\d]+)\\s*=\\s*([^;]+);") + for p in regex.search_all(image.material.shader.code): + image.material.set_shader_param(p.strings[2], float(p.strings[3])) + # Set texture params + if source.has("textures"): + for k in source.textures.keys(): + image.material.set_shader_param(k, source.textures[k]) + update_histogram() + +func on_parameter_changed(n : String, v) -> void: + if n == "__input_changed__": + set_generator(generator, output) + var p = generator.get_parameter_def(n) + if p.has("type"): + match p.type: + "float", "color", "gradient": + pass + _: + set_generator(generator, output) + +func on_float_parameters_changed(parameter_changes : Dictionary) -> void: + var need_update : bool = false + for n in parameter_changes.keys(): + for p in VisualServer.shader_get_param_list(image.material.shader.get_rid()): + if p.name == n: + image.material.set_shader_param(n, parameter_changes[n]) + need_update = true + break + if need_update: + update_histogram() + +func update_histogram() -> void: + pass diff --git a/material_maker/widgets/histogram/histogram.tscn b/material_maker/widgets/histogram/histogram.tscn index 02670ed..063f52c 100644 --- a/material_maker/widgets/histogram/histogram.tscn +++ b/material_maker/widgets/histogram/histogram.tscn @@ -96,7 +96,9 @@ resource_local_to_scene = true shader = SubResource( 10 ) shader_param/tex = SubResource( 11 ) -[node name="Histogram" type="Control"] +[node name="Histogram" type="Control" groups=[ +"preview", +]] margin_right = 64.0 margin_bottom = 64.0 script = ExtResource( 1 )