From d9b98693da82233de2b7d413c3650e74bfc64f0c Mon Sep 17 00:00:00 2001 From: RodZill4 Date: Wed, 19 Sep 2018 08:21:48 +0200 Subject: [PATCH] Added a simple shape generator Can be used to create a circle, a polygon or a curved star --- addons/material_maker/common.shader | 20 +++ addons/material_maker/library/base.json | 4 + addons/material_maker/nodes/shape.gd | 17 +++ addons/material_maker/nodes/shape.tscn | 179 ++++++++++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 addons/material_maker/nodes/shape.gd create mode 100644 addons/material_maker/nodes/shape.tscn diff --git a/addons/material_maker/common.shader b/addons/material_maker/common.shader index 56dcbc8..be75386 100644 --- a/addons/material_maker/common.shader +++ b/addons/material_maker/common.shader @@ -13,6 +13,26 @@ vec3 rand3(vec2 x) { dot(x, vec2(13.254, 5.867)))) * 43758.5453); } +float circle(vec2 uv, float sides, float radius, float edge) { + uv = 2.0*uv-1.0; + float distance = length(uv); + return clamp(1.0-distance/(edge*radius), 0.0, 1.0); +} + +float polygon(vec2 uv, float sides, float radius, float edge) { + uv = 2.0*uv-1.0; + float angle = atan(uv.x, uv.y)+3.14159265359; + float slice = 6.28318530718/sides; + return clamp((radius-cos(floor(0.5+angle/slice)*slice-angle)*length(uv))/(edge*radius), 0.0, 1.0); +} + +float star(vec2 uv, float sides, float radius, float edge) { + uv = 2.0*uv-1.0; + float angle = 2.0*(atan(uv.x, uv.y)+3.14159265359); + float slice = 6.28318530718/sides; + return clamp((radius-cos(floor(0.5+0.5*angle/slice)*2.0*slice-angle)*length(uv))/(edge*radius), 0.0, 1.0); +} + float wave_constant(float x) { return 1.0; } diff --git a/addons/material_maker/library/base.json b/addons/material_maker/library/base.json index 882c6ca..4f95e00 100644 --- a/addons/material_maker/library/base.json +++ b/addons/material_maker/library/base.json @@ -15,6 +15,10 @@ "tree_item":"Generators/Uniform", "type":"uniform" }, + { + "tree_item":"Generators/Shape", + "type":"shape" + }, { "tree_item":"Generators/Image", "type":"image", diff --git a/addons/material_maker/nodes/shape.gd b/addons/material_maker/nodes/shape.gd new file mode 100644 index 0000000..2938a10 --- /dev/null +++ b/addons/material_maker/nodes/shape.gd @@ -0,0 +1,17 @@ +tool +extends "res://addons/material_maker/node_base.gd" + +var shape +var sides +var radius +var edge + +const SHAPES = [ "circle", "polygon", "star" ] + +func _ready(): + initialize_properties([ $shape, $sides, $radius, $edge ]) + +func _get_shader_code(uv): + var rv = { defs="", code="" } + rv.f = "%s(%s, %d, %f, %f)" % [ SHAPES[shape], uv, sides, radius, edge ] + return rv diff --git a/addons/material_maker/nodes/shape.tscn b/addons/material_maker/nodes/shape.tscn new file mode 100644 index 0000000..623eeb9 --- /dev/null +++ b/addons/material_maker/nodes/shape.tscn @@ -0,0 +1,179 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://addons/material_maker/nodes/shape.gd" type="Script" id=1] + +[sub_resource type="Theme" id=1] + + +[node name="Shape" type="GraphNode" index="0"] + +anchor_left = 0.0 +anchor_top = 0.0 +anchor_right = 0.0 +anchor_bottom = 0.0 +margin_left = 1.0 +margin_top = 4.0 +margin_right = 98.0 +margin_bottom = 53.0 +rect_pivot_offset = Vector2( 0, 0 ) +rect_clip_content = false +mouse_filter = 1 +mouse_default_cursor_shape = 0 +size_flags_horizontal = 1 +size_flags_vertical = 1 +theme = SubResource( 1 ) +title = "Shape" +offset = Vector2( 0, 0 ) +show_close = true +resizable = false +selected = false +comment = false +overlay = 0 +slot/0/left_enabled = false +slot/0/left_type = 0 +slot/0/left_color = Color( 0.5, 0.5, 1, 1 ) +slot/0/right_enabled = true +slot/0/right_type = 0 +slot/0/right_color = Color( 0.5, 0.5, 1, 1 ) +slot/1/left_enabled = false +slot/1/left_type = 0 +slot/1/left_color = Color( 1, 1, 1, 1 ) +slot/1/right_enabled = false +slot/1/right_type = 0 +slot/1/right_color = Color( 1, 1, 1, 1 ) +slot/2/left_enabled = false +slot/2/left_type = 0 +slot/2/left_color = Color( 1, 1, 1, 1 ) +slot/2/right_enabled = false +slot/2/right_type = 0 +slot/2/right_color = Color( 1, 1, 1, 1 ) +slot/3/left_enabled = false +slot/3/left_type = 0 +slot/3/left_color = Color( 1, 1, 1, 1 ) +slot/3/right_enabled = false +slot/3/right_type = 0 +slot/3/right_color = Color( 1, 1, 1, 1 ) +script = ExtResource( 1 ) +_sections_unfolded = [ "Theme", "slot", "slot/0" ] + +[node name="shape" type="OptionButton" parent="." index="0"] + +anchor_left = 0.0 +anchor_top = 0.0 +anchor_right = 0.0 +anchor_bottom = 0.0 +margin_left = 16.0 +margin_top = 24.0 +margin_right = 91.0 +margin_bottom = 44.0 +rect_pivot_offset = Vector2( 0, 0 ) +rect_clip_content = false +focus_mode = 2 +mouse_filter = 0 +mouse_default_cursor_shape = 0 +size_flags_horizontal = 1 +size_flags_vertical = 1 +toggle_mode = false +action_mode = 0 +enabled_focus_mode = 2 +shortcut = null +group = null +text = "Circle" +flat = false +align = 0 +items = [ "Circle", null, false, 0, null, "Polygon", null, false, 1, null, "Star", null, false, -1, null ] +selected = 0 + +[node name="sides" type="HSlider" parent="." index="1"] + +anchor_left = 0.0 +anchor_top = 0.0 +anchor_right = 0.0 +anchor_bottom = 0.0 +margin_left = 16.0 +margin_top = 44.0 +margin_right = 91.0 +margin_bottom = 60.0 +rect_pivot_offset = Vector2( 0, 0 ) +rect_clip_content = false +hint_tooltip = "Sides" +focus_mode = 2 +mouse_filter = 0 +mouse_default_cursor_shape = 0 +size_flags_horizontal = 1 +size_flags_vertical = 0 +min_value = 3.0 +max_value = 16.0 +step = 1.0 +page = 0.0 +value = 3.0 +exp_edit = false +rounded = false +editable = true +tick_count = 0 +ticks_on_borders = false +focus_mode = 2 +_sections_unfolded = [ "Hint" ] + +[node name="radius" type="HSlider" parent="." index="2"] + +anchor_left = 0.0 +anchor_top = 0.0 +anchor_right = 0.0 +anchor_bottom = 0.0 +margin_left = 16.0 +margin_top = 61.0 +margin_right = 91.0 +margin_bottom = 77.0 +rect_pivot_offset = Vector2( 0, 0 ) +rect_clip_content = false +hint_tooltip = "Radius" +focus_mode = 2 +mouse_filter = 0 +mouse_default_cursor_shape = 0 +size_flags_horizontal = 1 +size_flags_vertical = 0 +min_value = 0.0 +max_value = 1.0 +step = 0.01 +page = 0.0 +value = 1.0 +exp_edit = false +rounded = false +editable = true +tick_count = 0 +ticks_on_borders = false +focus_mode = 2 +_sections_unfolded = [ "Hint" ] + +[node name="edge" type="HSlider" parent="." index="3"] + +anchor_left = 0.0 +anchor_top = 0.0 +anchor_right = 0.0 +anchor_bottom = 0.0 +margin_left = 16.0 +margin_top = 78.0 +margin_right = 91.0 +margin_bottom = 94.0 +rect_pivot_offset = Vector2( 0, 0 ) +rect_clip_content = false +focus_mode = 2 +mouse_filter = 0 +mouse_default_cursor_shape = 0 +size_flags_horizontal = 1 +size_flags_vertical = 0 +min_value = 0.0 +max_value = 1.0 +step = 0.01 +page = 0.0 +value = 0.0 +exp_edit = false +rounded = false +editable = true +tick_count = 0 +ticks_on_borders = false +focus_mode = 2 +_sections_unfolded = [ "Hint" ] + +