From d024688511c0ebfecb1c88924881638bd8d0d071 Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Sun, 6 Oct 2019 10:02:09 +0200 Subject: [PATCH] Added a runes generator and a detailed edit for gradients --- addons/material_maker/nodes/runes.mmg | 1 + .../material_maker/widgets/gradient_editor.gd | 21 +++++++++---- .../widgets/gradient_editor.tscn | 8 ++--- .../material_maker/widgets/gradient_popup.gd | 15 ++++++++++ .../widgets/gradient_popup.tscn | 30 +++++++++++++++++++ 5 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 addons/material_maker/nodes/runes.mmg create mode 100644 addons/material_maker/widgets/gradient_popup.gd create mode 100644 addons/material_maker/widgets/gradient_popup.tscn diff --git a/addons/material_maker/nodes/runes.mmg b/addons/material_maker/nodes/runes.mmg new file mode 100644 index 0000000..f3d7df9 --- /dev/null +++ b/addons/material_maker/nodes/runes.mmg @@ -0,0 +1 @@ +{"name":"runes","node_position":{"x":0,"y":0},"parameters":{"columns":8,"rows":8},"shader_model":{"global":"float ThickLine(vec2 uv, vec2 posA, vec2 posB, float radiusInv)\n{\n\tvec2 dir = posA - posB;\n\tfloat dirLen = length(dir);\n\tvec2 dirN = normalize(dir);\n\tfloat dotTemp = clamp(dot(uv - posB, dirN), 0.0, dirLen);\n\tvec2 proj = dotTemp * dirN + posB;\n\tfloat d1 = length(uv - proj);\n\tfloat finalGray = clamp(1.0 - d1 * radiusInv, 0.0, 1.0);\n\treturn finalGray;\n}\n\n// makes a rune in the 0..1 uv space. Seed is which rune to draw.\n// passes back gray in x and derivates for lighting in yz\nfloat Rune(vec2 uv) {\n\tfloat finalLine = 0.0;\n\tvec2 seed = floor(uv)-0.41;\n\tuv = fract(uv);\n\tfor (int i = 0; i < 4; i++)\t// number of strokes\n\t{\n\t\tvec2 posA = rand2(floor(seed+0.5));\n\t\tvec2 posB = rand2(floor(seed+1.5));\n\t\tseed += 2.0;\n\t\t// expand the range and mod it to get a nicely distributed random number - hopefully. :)\n\t\tposA = fract(posA * 128.0);\n\t\tposB = fract(posB * 128.0);\n\t\t// each rune touches the edge of its box on all 4 sides\n\t\tif (i == 0) posA.y = 0.0;\n\t\tif (i == 1) posA.x = 0.999;\n\t\tif (i == 2) posA.x = 0.0;\n\t\tif (i == 3) posA.y = 0.999;\n\t\t// snap the random line endpoints to a grid 2x3\n\t\tvec2 snaps = vec2(2.0, 3.0);\n\t\tposA = (floor(posA * snaps) + 0.5) / snaps;\t// + 0.5 to center it in a grid cell\n\t\tposB = (floor(posB * snaps) + 0.5) / snaps;\n\t\t//if (distance(posA, posB) < 0.0001) continue;\t// eliminate dots.\n\t\t// Dots (degenerate lines) are not cross-GPU safe without adding 0.001 - divide by 0 error.\n\t\tfinalLine = max(finalLine, ThickLine(uv, posA, posB + 0.001, 20.0));\n\t}\n\treturn finalLine;\n}\n\n\n","inputs":[],"instance":"","name":"Runes","outputs":[{"f":"Rune(vec2($columns, $rows)*$uv)","type":"f"}],"parameters":[{"default":0,"label":"","max":32,"min":2,"name":"columns","step":1,"type":"float","widget":"spinbox"},{"default":0,"label":"","max":32,"min":2,"name":"rows","step":1,"type":"float","widget":"spinbox"}]},"type":"shader"} \ No newline at end of file diff --git a/addons/material_maker/widgets/gradient_editor.gd b/addons/material_maker/widgets/gradient_editor.gd index 1f1da1e..91e457f 100644 --- a/addons/material_maker/widgets/gradient_editor.gd +++ b/addons/material_maker/widgets/gradient_editor.gd @@ -2,6 +2,7 @@ tool extends Control class_name MMGradientEditor + class GradientCursor: extends ColorRect @@ -43,6 +44,7 @@ class GradientCursor: draw_rect(Rect2(0, 0, rect_size.x, rect_size.y), c, false) var value = null setget set_value +export var embedded : bool = true signal updated(value) @@ -53,7 +55,7 @@ func _ready(): func set_value(v): value = v for c in get_children(): - if c != $Gradient and c != $Background: + if c is GradientCursor: remove_child(c) c.free() for p in value.points: @@ -74,10 +76,19 @@ func add_cursor(x, color): cursor.color = color func _gui_input(ev): - if ev is InputEventMouseButton && ev.button_index == 1 && ev.doubleclick && ev.position.y > 15: - var p = max(0, min(ev.position.x, rect_size.x-GradientCursor.WIDTH)) - add_cursor(p, get_gradient_color(p)) - update_value() + if ev is InputEventMouseButton && ev.button_index == 1 && ev.doubleclick: + if ev.position.y > 15: + var p = max(0, min(ev.position.x, rect_size.x-GradientCursor.WIDTH)) + add_cursor(p, get_gradient_color(p)) + update_value() + elif embedded: + var popup = load("res://addons/material_maker/widgets/gradient_popup.tscn").instance() + add_child(popup) + var popup_size = popup.rect_size + popup.popup(Rect2(ev.global_position, Vector2(0, 0))) + popup.set_global_position(ev.global_position-Vector2(popup_size.x / 2, popup_size.y)) + popup.init(value) + popup.connect("updated", self, "set_value") # Showing a color picker popup to change a cursor's color diff --git a/addons/material_maker/widgets/gradient_editor.tscn b/addons/material_maker/widgets/gradient_editor.tscn index 01e47ec..b284de6 100644 --- a/addons/material_maker/widgets/gradient_editor.tscn +++ b/addons/material_maker/widgets/gradient_editor.tscn @@ -6,7 +6,7 @@ code = "shader_type canvas_item; void fragment() { - COLOR = vec4(vec3(2.0*fract(0.5*(floor(10.0*UV.x)+floor(2.0*UV.y)))), 1.0); + COLOR = vec4(vec3(2.0*fract(0.5*(floor(0.12*FRAGCOORD.x)+floor(0.125*FRAGCOORD.y)))), 1.0); }" [sub_resource type="ShaderMaterial" id=2] @@ -37,15 +37,13 @@ script = ExtResource( 1 ) [node name="Background" type="ColorRect" parent="."] material = SubResource( 2 ) -margin_right = 120.0 -margin_bottom = 20.0 +anchor_right = 1.0 rect_min_size = Vector2( 120, 20 ) mouse_filter = 2 [node name="Gradient" type="ColorRect" parent="."] material = SubResource( 4 ) -margin_right = 120.0 -margin_bottom = 20.0 +anchor_right = 1.0 rect_min_size = Vector2( 120, 20 ) mouse_filter = 2 theme = SubResource( 5 ) diff --git a/addons/material_maker/widgets/gradient_popup.gd b/addons/material_maker/widgets/gradient_popup.gd new file mode 100644 index 0000000..5590a8e --- /dev/null +++ b/addons/material_maker/widgets/gradient_popup.gd @@ -0,0 +1,15 @@ +extends Popup + +signal updated(value) + +func init(value): + $Panel/Control.set_value(value) + + + +func _on_Control_updated(value): + emit_signal("updated", value) + + +func _on_GradientPopup_popup_hide(): + queue_free() diff --git a/addons/material_maker/widgets/gradient_popup.tscn b/addons/material_maker/widgets/gradient_popup.tscn new file mode 100644 index 0000000..fe76a53 --- /dev/null +++ b/addons/material_maker/widgets/gradient_popup.tscn @@ -0,0 +1,30 @@ +[gd_scene load_steps=4 format=2] + +[ext_resource path="res://addons/material_maker/widgets/gradient_popup.gd" type="Script" id=1] +[ext_resource path="res://addons/material_maker/widgets/gradient_editor.tscn" type="PackedScene" id=2] + +[sub_resource type="StyleBoxFlat" id=2] +bg_color = Color( 0, 0, 0.25098, 0.752941 ) + +[node name="GradientPopup" type="Popup"] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_right = -648.0 +margin_bottom = -671.0 +script = ExtResource( 1 ) + +[node name="Panel" type="Panel" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +custom_styles/panel = SubResource( 2 ) + +[node name="Control" parent="Panel" instance=ExtResource( 2 )] +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_left = 10.0 +margin_top = 10.0 +margin_right = -10.0 +margin_bottom = -10.0 +embedded = false +[connection signal="popup_hide" from="." to="." method="_on_GradientPopup_popup_hide"] +[connection signal="updated" from="Panel/Control" to="." method="_on_Control_updated"]