From 4b334a6ad6462620e297a424222dfe00cd3c9d8d Mon Sep 17 00:00:00 2001 From: Relintai Date: Mon, 4 Oct 2021 13:09:43 +0200 Subject: [PATCH] Initial slot api for MMGraphNode. Already supports images. Also implemented the image generation helper methods for MMMaterial and MMNode. --- .../mat_maker_gd/editor/MatMakerGDEditor.gd | 7 +- .../mat_maker_gd/editor/mm_graph_node.gd | 87 ++++++++++++++++++- game/addons/mat_maker_gd/nodes/mm_material.gd | 5 ++ game/addons/mat_maker_gd/nodes/mm_node.gd | 41 ++++++++- .../addons/mat_maker_gd/nodes/noise/perlin.gd | 52 +---------- 5 files changed, 137 insertions(+), 55 deletions(-) diff --git a/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd b/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd index 666c195d..9568cc66 100644 --- a/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd +++ b/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd @@ -30,7 +30,7 @@ func recreate() -> void: for n in _material.nodes: var gn : GraphNode = MMGraphNode.new() - gn.set_node(n) + gn.set_node(_material, n) _graph_edit.add_child(gn) #connect them @@ -56,8 +56,9 @@ func _on_AddPopup_ok_pressed(script_path : String): print("_on_AddPopup_ok_pressed: Error !nnode! script: " + script_path) return - _material.nodes.append(nnode) + _material.add_node(nnode) var gn : GraphNode = MMGraphNode.new() - gn.set_node(nnode) + gn.set_node(_material, nnode) _graph_edit.add_child(gn) + diff --git a/game/addons/mat_maker_gd/editor/mm_graph_node.gd b/game/addons/mat_maker_gd/editor/mm_graph_node.gd index 4b33e7cc..a42c450a 100644 --- a/game/addons/mat_maker_gd/editor/mm_graph_node.gd +++ b/game/addons/mat_maker_gd/editor/mm_graph_node.gd @@ -1,7 +1,90 @@ tool extends GraphNode -var _node : MMNode = null +export(PoolColorArray) var slot_colors : PoolColorArray -func set_node(node : MMNode) -> void: +var _material : MMMateial = null +var _node : MMNode = null +var properties : Array = Array() + +func _init(): + show_close = true + connect("offset_changed", self, "on_offset_changed") + +func add_slot_texture(input_type : int, output_type : int, getter : String, setter : String) -> void: + var t : TextureRect = TextureRect.new() + + var slot_idx : int = add_slot(input_type, output_type, getter, setter, t) + + t.texture = _node.call(getter, _material, slot_idx) + properties[slot_idx].append(t.texture) + +func add_slot_label(input_type : int, output_type : int, getter : String, setter : String, slot_name : String) -> void: + var l : Label = Label.new() + + l.text = slot_name + + add_slot(input_type, output_type, getter, setter, l) + +func add_slot(input_type : int, output_type : int, getter : String, setter : String, control : Control) -> int: + add_child(control) + var slot_idx : int = get_child_count() - 1 + + var arr : Array = Array() + + arr.append(slot_idx) + arr.append(input_type) + arr.append(output_type) + arr.append(getter) + arr.append(setter) + arr.append(control) + + properties.append(arr) + + set_slot_enabled_left(slot_idx, input_type != -1) + set_slot_enabled_right(slot_idx, output_type != -1) + + if input_type != -1: + set_slot_type_left(slot_idx, input_type) + + if output_type != -1: + set_slot_type_left(slot_idx, output_type) + + if slot_colors.size() > input_type: + set_slot_color_left(slot_idx, slot_colors[input_type]) + + if slot_colors.size() > output_type: + set_slot_color_left(slot_idx, slot_colors[output_type]) + + return slot_idx + +func set_node(material : MMMateial, node : MMNode) -> void: _node = node + _material = material + + if !_node: + return + + title = _node.get_class() + + if _node.get_script(): + title = _node.get_script().resource_path.get_file().get_basename() + + _node.register_methods(self) + + offset = _node.get_graph_position() + + _node.connect("changed", self, "on_node_changed") + +func propagate_node_change() -> void: + pass + +func on_offset_changed(): + if _node: + _node.set_graph_position(offset) + +func on_node_changed(): + #get all properties again + #_node.recalculate_image(_material) + + propagate_node_change() diff --git a/game/addons/mat_maker_gd/nodes/mm_material.gd b/game/addons/mat_maker_gd/nodes/mm_material.gd index 2b2c0151..1150bdaa 100644 --- a/game/addons/mat_maker_gd/nodes/mm_material.gd +++ b/game/addons/mat_maker_gd/nodes/mm_material.gd @@ -4,4 +4,9 @@ extends Resource var MMMNode = preload("res://addons/mat_maker_gd/nodes/mm_node.gd") +export(Vector2) var image_size : Vector2 = Vector2(128, 128) export(Array) var nodes : Array + +func add_node(node : MMNode) -> void: + nodes.append(node) + emit_changed() diff --git a/game/addons/mat_maker_gd/nodes/mm_node.gd b/game/addons/mat_maker_gd/nodes/mm_node.gd index 68feea35..ce9327de 100644 --- a/game/addons/mat_maker_gd/nodes/mm_node.gd +++ b/game/addons/mat_maker_gd/nodes/mm_node.gd @@ -2,6 +2,45 @@ tool class_name MMNode extends Resource +export(Vector2) var graph_position : Vector2 = Vector2() -var texture : ImageTexture +func recalculate_image(material, slot_idx : int) -> ImageTexture: + var image : Image = Image.new() + image.create(material.image_size.x, material.image_size.y, false, Image.FORMAT_RGBA8) + + var tex : ImageTexture = ImageTexture.new() + image.lock() + + var w : float = image.get_width() + var h : float = image.get_width() + + var pseed : float = randf() + randi() + + for x in range(image.get_width()): + for y in range(image.get_height()): + var v : Vector2 = Vector2(x / w, y / h) + + var col : Color = get_value_for(v) + + image.set_pixel(x, y, col) + + image.unlock() + + tex.create_from_image(image) + + return tex + +func get_value_for(uv : Vector2) -> Color: + return Color() + +func register_methods(mm_graph_node) -> void: + pass + +func get_graph_position() -> Vector2: + return graph_position + +func set_graph_position(pos : Vector2) -> void: + graph_position = pos + + emit_changed() diff --git a/game/addons/mat_maker_gd/nodes/noise/perlin.gd b/game/addons/mat_maker_gd/nodes/noise/perlin.gd index a39a2543..ca0e94e2 100644 --- a/game/addons/mat_maker_gd/nodes/noise/perlin.gd +++ b/game/addons/mat_maker_gd/nodes/noise/perlin.gd @@ -9,62 +9,16 @@ var tex : ImageTexture export(Vector2) var bmin : Vector2 = Vector2(0.1, 0.1) export(Vector2) var bmax : Vector2 = Vector2(1, 1) -export(bool) var refresh setget reff,reffg - -func _ready(): - if !Engine.editor_hint: - gen() - -func gen() -> void: - if !image: - image = Image.new() - image.create(300, 300, false, Image.FORMAT_RGBA8) - - if !tex: - tex = ImageTexture.new() - -# var bmin : Vector2 = Vector2(0.1, 0.1) -# var bmax : Vector2 = Vector2(1, 1) - - image.lock() - - var w : float = image.get_width() - var h : float = image.get_width() - - var pseed : float = randf() + randi() - - for x in range(image.get_width()): - for y in range(image.get_height()): - var v : Vector2 = Vector2(x / w, y / h) - -# var f : float = pattern(v, 4, 4, CombinerType.MULTIPLY, CombinerAxisType.SINE, CombinerAxisType.SINE) - - var col : Color = perlin(v) -# var col : Color = beehive_2_col(v) -# var col : Color = beehive_3_col(v) - - image.set_pixel(x, y, col) - - - image.unlock() - - tex.create_from_image(image) - texture = tex - var seed_o12297 = -26656; var p_o12297_scale_x = 4.000000000; var p_o12297_scale_y = 4.000000000; var p_o12297_iterations = 3.000000000; var p_o12297_persistence = 0.500000000; -func perlin(uv : Vector2) -> Color: +func get_value_for(uv : Vector2) -> Color: var a = NoisePerlin.perlinc(uv, Vector2(p_o12297_scale_x, p_o12297_scale_y), int(p_o12297_iterations), p_o12297_persistence, seed_o12297) return a -func reffg(): - return false - -func reff(bb): - if bb: - gen() +func register_methods(mm_graph_node) -> void: + mm_graph_node.add_slot_texture(0, 0, "recalculate_image", "")