From 77d2e64fe15c96d1c6db3e1c1841a9219d45c3c8 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 26 Oct 2021 22:51:51 +0200 Subject: [PATCH] adjust_hsv node. --- .../mat_maker_gd/nodes/common/commons.gd | 2 +- .../mat_maker_gd/nodes/common/filter.gd | 29 +++---- .../mat_maker_gd/nodes/filter/adjust_hsv.gd | 75 +++++++++++++++++++ 3 files changed, 91 insertions(+), 15 deletions(-) create mode 100644 game/addons/mat_maker_gd/nodes/filter/adjust_hsv.gd diff --git a/game/addons/mat_maker_gd/nodes/common/commons.gd b/game/addons/mat_maker_gd/nodes/common/commons.gd index a42b8999..3e490c07 100644 --- a/game/addons/mat_maker_gd/nodes/common/commons.gd +++ b/game/addons/mat_maker_gd/nodes/common/commons.gd @@ -147,7 +147,7 @@ static func absv2(v : Vector2) -> Vector2: static func absv3(v : Vector3) -> Vector3: v.x = abs(v.x) v.y = abs(v.y) - v.z = abs(v.y) + v.z = abs(v.z) return v diff --git a/game/addons/mat_maker_gd/nodes/common/filter.gd b/game/addons/mat_maker_gd/nodes/common/filter.gd index b8b5dc85..5aecec46 100644 --- a/game/addons/mat_maker_gd/nodes/common/filter.gd +++ b/game/addons/mat_maker_gd/nodes/common/filter.gd @@ -9,16 +9,6 @@ const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd") #main node methods: adjust_hsv, brightness_contrast -#vec3 rgb_to_hsv(vec3 c) { -# vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); -# vec4 p = c.g < c.b ? vec4(c.bg, K.wz) : vec4(c.gb, K.xy); -# vec4 q = c.r < p.x ? vec4(p.xyw, c.r) : vec4(c.r, p.yzx); -# -# float d = q.x - min(q.w, q.y); -# float e = 1.0e-10; -# return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -#} - #---------------------- #colorize.mmg #Remaps a greyscale image to a custom gradient @@ -4188,6 +4178,17 @@ const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd") # } # ], +#vec3 rgb_to_hsv(vec3 c) { +# vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); +# vec4 p = c.g < c.b ? vec4(c.bg, K.wz) : vec4(c.gb, K.xy); +# vec4 q = c.r < p.x ? vec4(p.xyw, c.r) : vec4(c.r, p.yzx); +# +# float d = q.x - min(q.w, q.y); +# float e = 1.0e-10; +# return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +#} + + static func rgb_to_hsv(c : Vector3) -> Vector3: var K : Color = Color(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); @@ -4240,11 +4241,11 @@ static func hsv_to_rgb(c : Vector3) -> Vector3: static func adjust_hsv(color : Color, hue : float, saturation : float, value : float) -> Color: var hsv : Vector3 = rgb_to_hsv(Vector3(color.r, color.g, color.b)); - var x : float = Commons.fract(hsv.x + hue) - var y : float = clamp(hsv.y * saturation, 0.0, 1.0) - var z : float = clamp(hsv.z * value, 0.0, 1.0) + hsv.x += hue + hsv.y = clamp(hsv.y * saturation, 0.0, 1.0) + hsv.z = clamp(hsv.z * value, 0.0, 1.0) - var h : Vector3 = hsv_to_rgb(Vector3(x, y, z)) + var h : Vector3 = hsv_to_rgb(hsv) return Color(h.x, h.y, h.z, color.a); diff --git a/game/addons/mat_maker_gd/nodes/filter/adjust_hsv.gd b/game/addons/mat_maker_gd/nodes/filter/adjust_hsv.gd new file mode 100644 index 00000000..664ff95a --- /dev/null +++ b/game/addons/mat_maker_gd/nodes/filter/adjust_hsv.gd @@ -0,0 +1,75 @@ +tool +extends MMNode + +var Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd") +var Filter = preload("res://addons/mat_maker_gd/nodes/common/filter.gd") + +export(Resource) var image : Resource +export(Resource) var input : Resource +export(float) var hue : float = 0 +export(float) var saturation : float = 1 +export(float) var value : float = 1 + +func _init_properties(): + if !input: + input = MMNodeUniversalProperty.new() + input.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_COLOR + input.set_default_value(Color(0, 0, 0, 1)) + + input.input_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_UNIVERSAL + input.slot_name = ">>> Input1 " + + if !image: + image = MMNodeUniversalProperty.new() + image.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_IMAGE + + #image.input_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_FLOAT + image.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_IMAGE + #image.force_override = true + + register_input_property(input) + register_output_property(image) + +func _register_methods(mm_graph_node) -> void: + mm_graph_node.add_slot_label_universal(input) + mm_graph_node.add_slot_texture_universal(image) + mm_graph_node.add_slot_float("get_hue", "set_hue", "Hue", 0.01) + mm_graph_node.add_slot_float("get_saturation", "set_saturation", "Saturation", 0.01) + mm_graph_node.add_slot_float("get_value", "set_value", "Value", 0.01) + +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 : Color = input.get_value(uv) + + return Filter.adjust_hsv(c, hue, saturation, value) + +#hue +func get_hue() -> float: + return hue + +func set_hue(val : float) -> void: + hue = val + + set_dirty(true) + +#saturation +func get_saturation() -> float: + return saturation + +func set_saturation(val : float) -> void: + saturation = val + + set_dirty(true) + +#value +func get_value() -> float: + return value + +func set_value(val : float) -> void: + value = val + + set_dirty(true)