From 583415d5fb7af965646d185d63d8d10d6b9be8b2 Mon Sep 17 00:00:00 2001 From: Relintai Date: Tue, 26 Oct 2021 17:16:19 +0200 Subject: [PATCH] brightness_contrast node. --- .../mat_maker_gd/nodes/common/filter.gd | 9 +-- .../nodes/filter/brightness_contrast.gd | 64 +++++++++++++++++++ 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 game/addons/mat_maker_gd/nodes/filter/brightness_contrast.gd diff --git a/game/addons/mat_maker_gd/nodes/common/filter.gd b/game/addons/mat_maker_gd/nodes/common/filter.gd index f634cbb1..b8b5dc85 100644 --- a/game/addons/mat_maker_gd/nodes/common/filter.gd +++ b/game/addons/mat_maker_gd/nodes/common/filter.gd @@ -4256,14 +4256,15 @@ static func adjust_hsv(color : Color, hue : float, saturation : float, value : f #output: vec4(clamp($in($uv).rgb*$contrast+vec3($brightness)+0.5-$contrast*0.5, vec3(0.0), vec3(1.0)), $in($uv).a) static func brightness_contrast(color : Color, brightness : float, contrast : float) -> Color: - var bv : Vector3 = Vector3(brightness, brightness, brightness) - var cvv : Vector3 = Vector3(color.r * contrast, color.g * contrast, color.b * contrast) + #output: vec4(clamp( $in($uv).rgb*$contrast + vec3($brightness) + 0.5 - $contrast*0.5, vec3(0.0), vec3(1.0)), $in($uv).a) - var cv : Vector3 = cvv + bv + Vector3(0.5, 0.5, 0.5) - (Vector3(contrast, contrast, contrast) * 0.5) + var cvv : Vector3 = Vector3(color.r, color.g, color.b) * contrast + + var cv : Vector3 = cvv + Vector3(brightness, brightness, brightness) + Vector3(0.5, 0.5, 0.5) - Vector3(contrast, contrast, contrast) * 0.5 var v : Vector3 = Commons.clampv3(cv, Vector3(), Vector3(1, 1, 1)) - return Color(v.x, v.y, v.z, 1); + return Color(v.x, v.y, v.z, color.a); #greyscale diff --git a/game/addons/mat_maker_gd/nodes/filter/brightness_contrast.gd b/game/addons/mat_maker_gd/nodes/filter/brightness_contrast.gd new file mode 100644 index 00000000..b22db913 --- /dev/null +++ b/game/addons/mat_maker_gd/nodes/filter/brightness_contrast.gd @@ -0,0 +1,64 @@ +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 brightness : float = 0 +export(float) var contrast : 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_brightness", "set_brightness", "Brightness", 0.01) + mm_graph_node.add_slot_float("get_contrast", "set_contrast", "Contrast", 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.brightness_contrast(c, brightness, contrast) + +#brightness +func get_brightness() -> float: + return brightness + +func set_brightness(val : float) -> void: + brightness = val + + set_dirty(true) + +#contrast +func get_contrast() -> float: + return contrast + +func set_contrast(val : float) -> void: + contrast = val + + set_dirty(true)