Added sd_shape_line node. Initial setup for curves, but they don't yet work properly with it.

This commit is contained in:
Relintai 2021-10-20 18:33:11 +02:00
parent bb7fee062c
commit 3841c50674
7 changed files with 140 additions and 153 deletions

View File

@ -3,6 +3,7 @@ extends GraphNode
var gradient_editor_scene : PackedScene = preload("res://addons/mat_maker_gd/widgets/gradient_editor/gradient_editor.tscn")
var polygon_edit_scene : PackedScene = preload("res://addons/mat_maker_gd/widgets/polygon_edit/polygon_edit.tscn")
var curve_edit_scene : PackedScene = preload("res://addons/mat_maker_gd/widgets/curve_edit/curve_edit.tscn")
var slot_colors : PoolColorArray
@ -84,6 +85,16 @@ func add_slot_polygon() -> int:
return slot_idx
func add_slot_curve() -> int:
var ge : Control = curve_edit_scene.instance()
var slot_idx : int = add_slot(MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_NONE, "", "", ge)
ge.set_value(_node)
#ge.texture = _node.call(getter, _material, slot_idx)
#properties[slot_idx].append(ge.texture)
return slot_idx
func add_slot_color(getter : String, setter : String) -> int:
var cp : ColorPickerButton = ColorPickerButton.new()

View File

@ -1,4 +1,4 @@
[gd_resource type="Resource" load_steps=64 format=2]
[gd_resource type="Resource" load_steps=67 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/noise.gd" type="Script" id=2]
@ -16,6 +16,7 @@
[ext_resource path="res://addons/mat_maker_gd/nodes/sdf2d/sd_shape_circle.gd" type="Script" id=14]
[ext_resource path="res://addons/mat_maker_gd/nodes/sdf2d/sd_shape_box.gd" type="Script" id=15]
[ext_resource path="res://addons/mat_maker_gd/nodes/sdf2d/sd_shape_polygon.gd" type="Script" id=16]
[ext_resource path="res://addons/mat_maker_gd/nodes/sdf2d/sd_shape_line.gd" type="Script" id=17]
[sub_resource type="Resource" id=3]
script = ExtResource( 5 )
@ -426,10 +427,27 @@ size = Vector2( 0.3, 0.2 )
[sub_resource type="Resource" id=63]
script = ExtResource( 16 )
graph_position = Vector2( 1060, -400 )
points = PoolVector2Array( 0.2, 0.2, 0.7, 0.4, 0.672764, 0.875339, 0.396341, 0.873984, 0.25542, 0.856369, 0.4, 0.7 )
points = PoolVector2Array( 0.2, 0.2, 0.7, 0.4, 0.858401, 0.631436, 0.672764, 0.875339, 0.396341, 0.873984, 0.25542, 0.856369, 0.143902, 0.556369 )
output = SubResource( 62 )
[sub_resource type="Resource" id=65]
script = ExtResource( 5 )
default_type = 1
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 )
[sub_resource type="Resource" id=66]
script = ExtResource( 17 )
graph_position = Vector2( 1280, -280 )
output = SubResource( 65 )
A = Vector2( -0.3, -0.3 )
B = Vector2( 0.3, 0.3 )
width = 0.1
[resource]
script = ExtResource( 1 )
image_size = Vector2( 128, 128 )
nodes = [ SubResource( 5 ), SubResource( 6 ), SubResource( 12 ), SubResource( 15 ), SubResource( 18 ), SubResource( 21 ), SubResource( 24 ), SubResource( 27 ), SubResource( 39 ), SubResource( 42 ), SubResource( 45 ), SubResource( 49 ), SubResource( 54 ), SubResource( 58 ), SubResource( 60 ), SubResource( 63 ) ]
nodes = [ SubResource( 5 ), SubResource( 6 ), SubResource( 12 ), SubResource( 15 ), SubResource( 18 ), SubResource( 21 ), SubResource( 24 ), SubResource( 27 ), SubResource( 39 ), SubResource( 42 ), SubResource( 45 ), SubResource( 49 ), SubResource( 54 ), SubResource( 58 ), SubResource( 60 ), SubResource( 63 ), SubResource( 66 ) ]

View File

@ -16,10 +16,12 @@ func to_string() -> String:
var rv = PoolStringArray()
for p in points:
rv.append("("+str(p.x)+","+str(p.y)+","+str(p.ls)+","+str(p.rs)+")")
return rv.join(",")
func clear() -> void:
points.clear()
curve_changed()
func add_point(x : float, y : float, ls : float = INF, rs : float = INF) -> void:
for i in points.size():
@ -28,15 +30,20 @@ func add_point(x : float, y : float, ls : float = INF, rs : float = INF) -> void
ls == 0
if rs == INF:
rs == 0
points.insert(i, Point.new(x, y, ls, rs))
curve_changed()
return
points.append(Point.new(x, y, ls, rs))
curve_changed()
func remove_point(index : int) -> bool:
if index <= 0 or index >= points.size() - 1:
return false
else:
points.remove(index)
curve_changed()
return true
func get_point_count() -> int:
@ -44,55 +51,14 @@ func get_point_count() -> int:
func set_point(i : int, v : Point) -> void:
points[i] = v
curve_changed()
func get_shader_params(name) -> Dictionary:
var rv = {}
for i in range(points.size()):
rv["p_"+name+"_"+str(i)+"_x"] = points[i].p.x
rv["p_"+name+"_"+str(i)+"_y"] = points[i].p.y
rv["p_"+name+"_"+str(i)+"_ls"] = points[i].ls
rv["p_"+name+"_"+str(i)+"_rs"] = points[i].rs
return rv
func set_poins(v : PoolRealArray) -> void:
#points[i] = v
curve_changed()
func get_shader(name) -> String:
var shader
shader = "float "+name+"_curve_fct(float x) {\n"
for i in range(points.size()-1):
if i < points.size()-2:
shader += "if (x <= p_"+name+"_"+str(i+1)+"_x) "
shader += "{\n"
shader += "float dx = x - p_"+name+"_"+str(i)+"_x;\n"
shader += "float d = p_"+name+"_"+str(i+1)+"_x - p_"+name+"_"+str(i)+"_x;\n"
shader += "float t = dx/d;\n"
shader += "float omt = (1.0 - t);\n"
shader += "float omt2 = omt * omt;\n"
shader += "float omt3 = omt2 * omt;\n"
shader += "float t2 = t * t;\n"
shader += "float t3 = t2 * t;\n"
shader += "d /= 3.0;\n"
shader += "float y1 = p_"+name+"_"+str(i)+"_y;\n"
shader += "float yac = p_"+name+"_"+str(i)+"_y + d*p_"+name+"_"+str(i)+"_rs;\n"
shader += "float ybc = p_"+name+"_"+str(i+1)+"_y - d*p_"+name+"_"+str(i+1)+"_ls;\n"
shader += "float y2 = p_"+name+"_"+str(i+1)+"_y;\n"
shader += "return y1*omt3 + yac*omt2*t*3.0 + ybc*omt*t2*3.0 + y2*t3;\n"
shader += "}\n"
shader += "}\n"
return shader
func curve_changed() -> void:
_curve_changed()
func serialize() -> Dictionary:
var rv = []
for p in points:
rv.append({ x=p.p.x, y=p.p.y, ls=p.ls, rs=p.rs })
return { type="Curve", points=rv }
func deserialize(v) -> void:
clear()
if typeof(v) == TYPE_DICTIONARY and v.has("type") and v.type == "Curve":
for p in v.points:
add_point(p.x, p.y, p.ls, p.rs)
elif typeof(v) == TYPE_OBJECT and v.get_script() == get_script():
clear()
for p in v.points:
add_point(p.p.x, p.p.y, p.ls, p.rs)
else:
print("Cannot deserialize curve")
func _curve_changed() -> void:
emit_changed()

View File

@ -59,10 +59,12 @@ func set_points(v : PoolVector2Array) -> void:
_polygon_changed()
func polygon_changed() -> void:
_polygon_changed()
func _polygon_changed() -> void:
emit_changed()
func to_string() -> String:
var rv = PoolStringArray()
for p in points:

View File

@ -318,99 +318,17 @@ const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd")
#sdline.mmg
#A line or a capsule shape described as a signed distance function
# "code": "vec2 $(name_uv)_sdl = sdLine($uv, vec2($ax+0.5, $ay+0.5), vec2($bx+0.5, $by+0.5));",
# "outputs": [
# {
# "longdesc": "The shape as signed distance function",
# "sdf2d": "$(name_uv)_sdl.x-$r*$profile($(name_uv)_sdl.y)",
# "shortdesc": "Output",
# "type": "sdf2d"
# }
# ],
# "parameters": [
# {
# "control": "P1.x",
# "default": 0,
# "label": "A X",
# "longdesc": "The position on the X axis of the first point of the line",
# "max": 1,
# "min": -1,
# "name": "ax",
# "shortdesc": "A.x",
# "step": 0.01,
# "type": "float"
# },
# {
# "control": "P1.y",
# "default": 0,
# "label": "A Y",
# "longdesc": "The position on the Y axis of the first point of the line",
# "max": 1,
# "min": -1,
# "name": "ay",
# "shortdesc": "A.y",
# "step": 0.01,
# "type": "float"
# },
# {
# "control": "P2.x",
# "default": 1,
# "label": "B X",
# "longdesc": "The position on the X axis of the second point of the line",
# "max": 1,
# "min": -1,
# "name": "bx",
# "shortdesc": "B.x",
# "step": 0.01,
# "type": "float"
# },
# {
# "control": "P2.y",
# "default": 1,
# "label": "B Y",
# "longdesc": "The position on the Y axis of the second point of the line",
# "max": 1,
# "min": -1,
# "name": "by",
# "shortdesc": "B.y",
# "step": 0.01,
# "type": "float"
# },
# {
# "control": "Radius1.r",
# "default": 0,
# "label": "Width",
# "longdesc": "The width of the capsule shape around the line",
# "max": 1,
# "min": 0,
# "name": "r",
# "shortdesc": "Width",
# "step": 0.01,
# "type": "float"
# },
# {
# "default": {
# "points": [
# {
# "ls": 0,
# "rs": 0,
# "x": 0,
# "y": 1
# },
# {
# "ls": 0,
# "rs": 0,
# "x": 1,
# "y": 1
# }
# ],
# "type": "Curve"
# },
# "label": "Profile",
# "name": "profile",
# "type": "curve"
# }
# ],
#vec2 $(name_uv)_sdl = sdLine($uv, vec2($ax+0.5, $ay+0.5), vec2($bx+0.5, $by+0.5));
#Outputs
#output, sdf2d (float), (output property)
#$(name_uv)_sdl.x-$r*$profile($(name_uv)_sdl.y)
#Inputs
#A, Vector2, min: -1, max: 1, step: 0.01, default: (-0.3, -0.3)
#B, Vector2, min: -1, max: 1, step: 0.01, default: (0.3, 0.3)
#width, float, min: 0, max: 1, step: 0.01, default: 0.1
#points (curve), default: 0, 0, 0, 1, 0, 0, 1, 1
#----------------------
#sdmorph.mmg
@ -2247,14 +2165,16 @@ static func sdf_box(uv : Vector2, c : Vector2, wh : Vector2) -> float:
return Commons.maxv2(d, Vector2(0, 0)).length() + min(max(d.x, d.y), 0.0)
static func sdf_line(uv : Vector2, a : Vector2, b : Vector2, r : float) -> float:
#vec2 $(name_uv)_sdl = sdLine($uv, vec2($ax+0.5, $ay+0.5), vec2($bx+0.5, $by+0.5));
static func sdf_line(uv : Vector2, a : Vector2, b : Vector2, r : float) -> Vector2:
a.x += 0.5
a.y += 0.5
b.x += 0.5
b.y += 0.5
return sdLine(uv, a, b) - r
return sdLine(uv, a, b)
static func sdf_rhombus(uv : Vector2, c : Vector2, wh : Vector2) -> float:
@ -2361,13 +2281,13 @@ static func sdf_morph(a : float, b : float, amount : float) -> float:
# return vec2(length(pa-ba*h), h);
#}
static func sdLine(p : Vector2, a : Vector2, b : Vector2) -> float:
static func sdLine(p : Vector2, a : Vector2, b : Vector2) -> Vector2:
var pa : Vector2 = p - a
var ba : Vector2 = b - a
var h : float = clamp(pa.dot(ba) / ba.dot(ba), 0.0, 1.0);
return (pa - (ba * h)).length()
return Vector2((pa - (ba * h)).length(), h)
#Needs thought

View File

@ -0,0 +1,73 @@
tool
extends "res://addons/mat_maker_gd/nodes/bases/curve_base.gd"
const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd")
var SDF2D = preload("res://addons/mat_maker_gd/nodes/common/sdf2d.gd")
export(Resource) var output : Resource
export(Vector2) var A : Vector2 = Vector2(-0.3, -0.3)
export(Vector2) var B : Vector2 = Vector2(0.3, 0.3)
export(float) var width : float = 0.1
func _init_properties():
if !output:
output = MMNodeUniversalProperty.new()
output.default_type = MMNodeUniversalProperty.MMNodeUniversalPropertyDefaultType.DEFAULT_TYPE_FLOAT
output.output_slot_type = MMNodeUniversalProperty.SlotTypes.SLOT_TYPE_FLOAT
output.slot_name = ">>> Output >>>"
output.get_value_from_owner = true
register_output_property(output)
func _register_methods(mm_graph_node) -> void:
mm_graph_node.add_slot_label_universal(output)
mm_graph_node.add_slot_vector2("get_a", "set_a", "A", 0.01)
mm_graph_node.add_slot_vector2("get_b", "set_b", "B", 0.01)
mm_graph_node.add_slot_float("get_width", "set_width", "Width", 0.01)
mm_graph_node.add_slot_curve()
func get_property_value(uv : Vector2) -> float:
var line : Vector2 = SDF2D.sdf_line(uv, A, B, width)
#$(name_uv)_sdl.x - $r * $profile($(name_uv)_sdl.y)
return line.x
#a
func get_a() -> Vector2:
return A
func set_a(val : Vector2) -> void:
A = val
emit_changed()
output.emit_changed()
#b
func get_b() -> Vector2:
return B
func set_b(val : Vector2) -> void:
B = val
emit_changed()
output.emit_changed()
#width
func get_width() -> float:
return width
func set_width(val : float) -> void:
width = val
emit_changed()
output.emit_changed()
func polygon_changed() -> void:
_polygon_changed()
func _polygon_changed() -> void:
emit_changed()
output.emit_changed()

View File

@ -24,9 +24,6 @@ func _register_methods(mm_graph_node) -> void:
func get_property_value(uv : Vector2) -> float:
return SDF2D.sdPolygon(uv, points)
func polygon_changed() -> void:
_polygon_changed()
func _polygon_changed() -> void:
emit_changed()
output.emit_changed()