From c816f0e0fe3fc7ba4b6501ba5cc59f5c22a763e9 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 9 Oct 2021 14:04:09 +0200 Subject: [PATCH] Finish up the new mat_maker_gd input/output notification api and made shape use it. Now shape's image updates whenever a property changes. --- .../mat_maker_gd/editor/MatMakerGDEditor.gd | 2 ++ game/addons/mat_maker_gd/new_resource.tres | 20 +++++++++---- game/addons/mat_maker_gd/nodes/mm_material.gd | 14 ++++++++-- game/addons/mat_maker_gd/nodes/mm_node.gd | 28 +++++++++++++------ .../addons/mat_maker_gd/nodes/noise/perlin.gd | 2 +- .../addons/mat_maker_gd/nodes/simple/shape.gd | 18 ++++++++---- 6 files changed, 61 insertions(+), 23 deletions(-) diff --git a/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd b/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd index 496a24ed..5f033327 100644 --- a/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd +++ b/game/addons/mat_maker_gd/editor/MatMakerGDEditor.gd @@ -37,6 +37,8 @@ func recreate() -> void: _graph_edit.add_child(gn) #connect them + + _material.render() func set_mmmaterial(object : MMMateial): _material = object diff --git a/game/addons/mat_maker_gd/new_resource.tres b/game/addons/mat_maker_gd/new_resource.tres index dbd7161e..a525c4a6 100644 --- a/game/addons/mat_maker_gd/new_resource.tres +++ b/game/addons/mat_maker_gd/new_resource.tres @@ -1,4 +1,4 @@ -[gd_resource type="Resource" load_steps=11 format=2] +[gd_resource type="Resource" load_steps=12 format=2] [ext_resource path="res://addons/mat_maker_gd/nodes/mm_material.gd" type="Script" id=1] [ext_resource path="res://addons/mat_maker_gd/nodes/noise/perlin.gd" type="Script" id=2] @@ -24,7 +24,16 @@ refresh = false script = ExtResource( 5 ) default_type = 1 default_int = 0 -default_float = 0.3 +default_float = 0.8 +default_vector2 = Vector2( 0, 0 ) +default_vector3 = Vector3( 0, 0, 0 ) +default_color = Color( 0, 0, 0, 1 ) + +[sub_resource type="Resource" id=6] +script = ExtResource( 5 ) +default_type = 5 +default_int = 0 +default_float = 0.0 default_vector2 = Vector2( 0, 0 ) default_vector3 = Vector3( 0, 0, 0 ) default_color = Color( 0, 0, 0, 1 ) @@ -33,7 +42,7 @@ default_color = Color( 0, 0, 0, 1 ) script = ExtResource( 5 ) default_type = 1 default_int = 0 -default_float = 0.4 +default_float = 0.45 default_vector2 = Vector2( 0, 0 ) default_vector3 = Vector3( 0, 0, 0 ) default_color = Color( 0, 0, 0, 1 ) @@ -41,8 +50,9 @@ default_color = Color( 0, 0, 0, 1 ) [sub_resource type="Resource" id=5] script = ExtResource( 4 ) graph_position = Vector2( -240, -340 ) -shape_type = 2 -sides = 6 +image = SubResource( 6 ) +shape_type = 3 +sides = 4 radius = SubResource( 4 ) edge = SubResource( 3 ) diff --git a/game/addons/mat_maker_gd/nodes/mm_material.gd b/game/addons/mat_maker_gd/nodes/mm_material.gd index 10806c59..70c0845c 100644 --- a/game/addons/mat_maker_gd/nodes/mm_material.gd +++ b/game/addons/mat_maker_gd/nodes/mm_material.gd @@ -5,9 +5,14 @@ extends Resource export(Vector2) var image_size : Vector2 = Vector2(128, 128) export(Array) var nodes : Array -func _init(): - for n in nodes: - n.connect("changed", self, "on_node_changed") +var initialized : bool = false + +func initialize(): + if !initialized: + initialized = true + + for n in nodes: + n.connect("changed", self, "on_node_changed") func add_node(node : MMNode) -> void: nodes.append(node) @@ -24,6 +29,9 @@ func remove_node(node : MMNode) -> void: emit_changed() func render() -> void: + if !initialized: + initialize() + var did_render : bool = true while did_render: diff --git a/game/addons/mat_maker_gd/nodes/mm_node.gd b/game/addons/mat_maker_gd/nodes/mm_node.gd index 916b36ef..43dc0918 100644 --- a/game/addons/mat_maker_gd/nodes/mm_node.gd +++ b/game/addons/mat_maker_gd/nodes/mm_node.gd @@ -5,6 +5,7 @@ extends Resource export(Vector2) var graph_position : Vector2 = Vector2() var input_properties : Array +var output_properties : Array var properties_initialized : bool = false @@ -21,17 +22,17 @@ func render(material) -> bool: _render(material) + dirty = false + return true #MMMateial func _render(material) -> void: pass -func render_image(material) -> ImageTexture: +func render_image(material) -> Image: 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() @@ -49,10 +50,8 @@ func render_image(material) -> ImageTexture: image.set_pixel(x, y, col) image.unlock() - - tex.create_from_image(image) - return tex + return image func get_value_for(uv : Vector2, pseed : int) -> Color: return Color() @@ -84,7 +83,8 @@ func set_graph_position(pos : Vector2) -> void: func register_input_property(prop : MMNodeUniversalProperty) -> void: prop.owner = self - prop.connect("changed", self, "on_input_property_changed") + if !prop.is_connected("changed", self, "on_input_property_changed"): + prop.connect("changed", self, "on_input_property_changed") input_properties.append(prop) @@ -92,10 +92,22 @@ func unregister_input_property(prop : MMNodeUniversalProperty) -> void: if prop.owner == self: prop.owner = null - prop.disconnect("changed", self, "on_input_property_changed") + if prop.is_connected("changed", self, "on_input_property_changed"): + prop.disconnect("changed", self, "on_input_property_changed") input_properties.erase(prop) +func register_output_property(prop : MMNodeUniversalProperty) -> void: + prop.owner = self + + output_properties.append(prop) + +func unregister_output_property(prop : MMNodeUniversalProperty) -> void: + if prop.owner == self: + prop.owner = null + + output_properties.erase(prop) + func set_dirty(val : bool) -> void: var changed : bool = val != dirty diff --git a/game/addons/mat_maker_gd/nodes/noise/perlin.gd b/game/addons/mat_maker_gd/nodes/noise/perlin.gd index 9bb7030e..eee8ddc4 100644 --- a/game/addons/mat_maker_gd/nodes/noise/perlin.gd +++ b/game/addons/mat_maker_gd/nodes/noise/perlin.gd @@ -14,7 +14,7 @@ func get_value_for(uv : Vector2, pseed : int) -> Color: return NoisePerlin.perlinc(uv, scale, iterations, persistence, pseed) func _register_methods(mm_graph_node) -> void: - mm_graph_node.add_slot_texture(MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE, "render_image", "") + #mm_graph_node.add_slot_texture(MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE, "render_image", "") mm_graph_node.add_slot_int(MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, "get_iterations", "set_iterations", "iterations")#, Vector2(1, 10)) mm_graph_node.add_slot_float(MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, "get_persistence", "set_persistence", "persistence", 0.05)#, Vector2(0, 1)) mm_graph_node.add_slot_vector2(MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, "get_scale", "set_scale", "scale", 1)#, Vector2(1, 32)) diff --git a/game/addons/mat_maker_gd/nodes/simple/shape.gd b/game/addons/mat_maker_gd/nodes/simple/shape.gd index 3eab639b..5e19ad85 100644 --- a/game/addons/mat_maker_gd/nodes/simple/shape.gd +++ b/game/addons/mat_maker_gd/nodes/simple/shape.gd @@ -41,17 +41,23 @@ func _init_properties(): edge.slot_name = "edge" edge.value_step = 0.05 - #this will end up generating the image - #maybe whis should be automaticly done whenn added into material - #emit_changed() + register_input_property(radius) + register_input_property(edge) + + register_output_property(image) func _register_methods(mm_graph_node) -> void: - mm_graph_node.add_slot_texture(MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE, "render_image", "") + mm_graph_node.add_slot_texture_universal(image) mm_graph_node.add_slot_enum(MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, "get_shape_typoe", "set_shape_typoe", "shape_type", [ "Circle", "Polygon", "Star", "Curved Star", "Rays" ]) mm_graph_node.add_slot_int(MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, "get_sides", "set_sides", "sides")#, Vector2(1, 10)) mm_graph_node.add_slot_float_universal(radius) mm_graph_node.add_slot_float_universal(edge) +func _render(material) -> void: + var img : Image = render_image(material) + + image.set_value(img) + func get_value_for(uv : Vector2, pseed : int) -> Color: var c : float = 0 @@ -74,7 +80,7 @@ func get_shape_typoe() -> int: func set_shape_typoe(val : int) -> void: shape_type = val - emit_changed() + set_dirty(true) func get_sides() -> int: return sides @@ -82,4 +88,4 @@ func get_sides() -> int: func set_sides(val : int) -> void: sides = val - emit_changed() + set_dirty(true)