Moved sdf2d stuff to a new sdf2d script.

This commit is contained in:
Relintai 2021-10-03 01:07:39 +02:00
parent 743e92ad55
commit 2f521f2998
3 changed files with 215 additions and 181 deletions

View File

@ -246,162 +246,6 @@ static func ThickLine(uv : Vector2, posA : Vector2, posB : Vector2, radiusInv :
return finalGray;
static func sdr_ndot(a : Vector2, b : Vector2) -> float:
return a.x * b.x - a.y * b.y;
static func sdRhombus(p : Vector2, b : Vector2) -> float:
var q : Vector2 = absv2(p);
var h : float = clamp((-2.0 * sdr_ndot(q,b) + sdr_ndot(b,b)) / b.dot(b), -1.0, 1.0);
var d : float = ( q - 0.5*b * Vector2(1.0-h, 1.0+h)).length()
return d * sign(q.x*b.y + q.y*b.x - b.x*b.y)
static func sdArc(p : Vector2, a1 : float, a2 : float, ra : float, rb : float) -> float:
var amid : float = 0.5*(a1+a2)+1.6+3.14 * step(a1, a2);
var alength : float = 0.5*(a1-a2)-1.6+3.14 * step(a1, a2);
var sca : Vector2 = Vector2(cos(amid), sin(amid));
var scb : Vector2 = Vector2(cos(alength), sin(alength));
#p *= Matrix(Vector2(sca.x , sca.y), Vector2(-sca.y, sca.x));
var pt : Vector2 = p
p.x = pt.x * sca.x + pt.y * sca.y
p.y = pt.x * -sca.y + pt.y * sca.x
p.x = abs(p.x);
var k : float
if (scb.y * p.x > scb.x * p.y):
k = p.dot(scb)
else:
k = p.length();
return sqrt( p.dot(p) + ra * ra - 2.0 * ra * k ) - rb;
static func sdf_boolean_union(a : float, b : float) -> float:
return min(a, b)
static func sdf_boolean_substraction(a : float, b : float) -> float:
return max(-a, b)
static func sdf_boolean_intersection(a : float, b : float) -> float:
return max(a, b)
static func sdf_smooth_boolean_union(d1 : float, d2 : float, k : float) -> float:
var h : float = clamp( 0.5 + 0.5 * (d2 - d1) / k, 0.0, 1.0)
return lerp(d2, d1, h) - k * h * (1.0 - h)
static func sdf_smooth_boolean_substraction(d1 : float, d2 : float, k : float) -> float:
var h : float = clamp( 0.5 - 0.5 * (d2 + d1) / k, 0.0, 1.0)
return lerp(d2, -d1, h) + k * h * (1.0 - h)
static func sdf_smooth_boolean_intersection(d1 : float, d2 : float, k : float) -> float:
var h : float = clamp( 0.5 - 0.5 * (d2 - d1) / k, 0.0, 1.0)
return lerp(d2, d1, h) + k * h * (1.0 - h)
static func sdf_rounded_shape(a : float, r : float) -> float:
return a - r
static func sdf_annular_shape(a : float, r : float) -> float:
return abs(a) - r
static func sdf_morph(a : float, b : float, amount : float) -> float:
return lerp(a, b, amount)
static func sdLine(p : Vector2, a : Vector2, b : Vector2) -> float:
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()
#Needs thought
#func sdf_translate(a : float, x : float, y : float) -> float:
# return lerp(a, b, amount)
static func sdf2d_rotate(uv : Vector2, a : float) -> Vector2:
var rv : Vector2;
var c : float = cos(a);
var s : float = sin(a);
uv -= Vector2(0.5, 0.5);
rv.x = uv.x*c+uv.y*s;
rv.y = -uv.x*s+uv.y*c;
return rv+Vector2(0.5, 0.5);
# signed distance to a quadratic bezier
static func sdBezier(pos : Vector2, A : Vector2, B : Vector2, C : Vector2) -> Vector2:
var a : Vector2 = B - A;
var b : Vector2 = A - 2.0*B + C;
var c : Vector2 = a * 2.0;
var d : Vector2 = A - pos;
var kk : float = 1.0 / b.dot(b);
var kx : float = kk * a.dot(b);
var ky : float = kk * (2.0* a.dot(a) + d.dot(b)) / 3.0;
var kz : float = kk * d.dot(a);
var res : float = 0.0;
var sgn : float = 0.0;
var p : float = ky - kx * kx;
var p3 : float = p*p*p;
var q : float = kx*(2.0*kx*kx - 3.0*ky) + kz;
var h : float = q*q + 4.0*p3;
var rvx : float;
if(h >= 0.0):# // 1 root
h = sqrt(h);
var x : Vector2 = Vector2(h,-h);
x.x -= q
x.y -= q
x.x /= 2.0
x.y /= 2.0
var uv : Vector2 = Vector2()
uv.x = sign(x.x) * pow(abs(x.x), 1);
uv.x = sign(x.y) * pow(abs(x.y), 3);
rvx = uv.x+uv.y-kx;
var t : float = clamp(rvx, 0.0, 1.0);
var q2 : Vector2 = d+(c+b*t)*t;
res = q2.dot(q2);
var tmp2 : Vector2 = c
tmp2.x += 2
tmp2.y += 2
tmp2 *= b*t
sgn = tmp2.cross(q2)
else: # // 3 roots
var z : float = sqrt(-p);
var v : float = acos(q/(p*z*2.0))/3.0;
var m : float = cos(v);
var n : float = sin(v)*1.732050808;
# var t : Vector3 = clamp(Vector3(m+m,-n-m,n-m)*z-kx, 0.0, 1.0);
#
#
# var qx : Vector2 = d+(c+b*t.x)*t.x;
# var dx : float = dot(qx, qx)
# sx = cross2(c+2.0*b*t.x,qx);
# var qy : Vector2 = d+(c+b*t.y)*t.y;
# var dy : float = dot(qy, qy)
# sy = cross2(c+2.0*b*t.y,qy);
# if dx<dy:
# res=dx; sgn=sx; rvx = t.x;
# else:
# res=dy; sgn=sy; rvx = t.y;
#
return Vector2(rvx, sqrt(res)*sign(sgn));
static func shape_circle(uv : Vector2, sides : float, size : float, edge : float) -> float:
uv.x = 2.0 * uv.x - 1.0

View File

@ -0,0 +1,198 @@
extends Reference
const Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.gd")
static func sdf_show(val : float, bevel : float) -> Color:
var f : float = clamp(-val / max(bevel, 0.00001), 0.0, 1.0);
return Color(f, f, f, 1)
static func sdf_circle(uv : Vector2, c : Vector2, r : float) -> float:
c.x += 0.5
c.y += 0.5
return (uv - c).length() - r;
static func sdf_box(uv : Vector2, c : Vector2, wh : Vector2) -> float:
c.x += 0.5
c.y += 0.5
var d : Vector2 = Commons.absv2(uv - c) - wh
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:
a.x += 0.5
a.y += 0.5
b.x += 0.5
b.y += 0.5
return sdLine(uv, a, b) - r
static func sdf_rhombus(uv : Vector2, c : Vector2, wh : Vector2) -> float:
c.x += 0.5
c.y += 0.5
return sdRhombus(uv - c, wh);
static func sdf_arc(uv : Vector2, a : Vector2, r : Vector2) -> float:
return sdArc(uv - Vector2(0.5, 0.5), Commons.modf(a.x, 360.0) * 0.01745329251, Commons.modf(a.y, 360.0)*0.01745329251, r.x, r.y)
static func sdr_ndot(a : Vector2, b : Vector2) -> float:
return a.x * b.x - a.y * b.y;
static func sdRhombus(p : Vector2, b : Vector2) -> float:
var q : Vector2 = Commons.absv2(p);
var h : float = clamp((-2.0 * sdr_ndot(q,b) + sdr_ndot(b,b)) / b.dot(b), -1.0, 1.0);
var d : float = ( q - 0.5*b * Vector2(1.0-h, 1.0+h)).length()
return d * sign(q.x*b.y + q.y*b.x - b.x*b.y)
static func sdArc(p : Vector2, a1 : float, a2 : float, ra : float, rb : float) -> float:
var amid : float = 0.5*(a1+a2)+1.6+3.14 * Commons.step(a1, a2);
var alength : float = 0.5*(a1-a2)-1.6+3.14 * Commons.step(a1, a2);
var sca : Vector2 = Vector2(cos(amid), sin(amid));
var scb : Vector2 = Vector2(cos(alength), sin(alength));
#p *= Matrix(Vector2(sca.x , sca.y), Vector2(-sca.y, sca.x));
var pt : Vector2 = p
p.x = pt.x * sca.x + pt.y * sca.y
p.y = pt.x * -sca.y + pt.y * sca.x
p.x = abs(p.x);
var k : float
if (scb.y * p.x > scb.x * p.y):
k = p.dot(scb)
else:
k = p.length();
return sqrt( p.dot(p) + ra * ra - 2.0 * ra * k ) - rb;
static func sdf_boolean_union(a : float, b : float) -> float:
return min(a, b)
static func sdf_boolean_substraction(a : float, b : float) -> float:
return max(-a, b)
static func sdf_boolean_intersection(a : float, b : float) -> float:
return max(a, b)
static func sdf_smooth_boolean_union(d1 : float, d2 : float, k : float) -> float:
var h : float = clamp( 0.5 + 0.5 * (d2 - d1) / k, 0.0, 1.0)
return lerp(d2, d1, h) - k * h * (1.0 - h)
static func sdf_smooth_boolean_substraction(d1 : float, d2 : float, k : float) -> float:
var h : float = clamp( 0.5 - 0.5 * (d2 + d1) / k, 0.0, 1.0)
return lerp(d2, -d1, h) + k * h * (1.0 - h)
static func sdf_smooth_boolean_intersection(d1 : float, d2 : float, k : float) -> float:
var h : float = clamp( 0.5 - 0.5 * (d2 - d1) / k, 0.0, 1.0)
return lerp(d2, d1, h) + k * h * (1.0 - h)
static func sdf_rounded_shape(a : float, r : float) -> float:
return a - r
static func sdf_annular_shape(a : float, r : float) -> float:
return abs(a) - r
static func sdf_morph(a : float, b : float, amount : float) -> float:
return lerp(a, b, amount)
static func sdLine(p : Vector2, a : Vector2, b : Vector2) -> float:
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()
#Needs thought
#func sdf_translate(a : float, x : float, y : float) -> float:
# return lerp(a, b, amount)
static func sdf2d_rotate(uv : Vector2, a : float) -> Vector2:
var rv : Vector2;
var c : float = cos(a);
var s : float = sin(a);
uv -= Vector2(0.5, 0.5);
rv.x = uv.x*c+uv.y*s;
rv.y = -uv.x*s+uv.y*c;
return rv+Vector2(0.5, 0.5);
# signed distance to a quadratic bezier
static func sdBezier(pos : Vector2, A : Vector2, B : Vector2, C : Vector2) -> Vector2:
var a : Vector2 = B - A;
var b : Vector2 = A - 2.0*B + C;
var c : Vector2 = a * 2.0;
var d : Vector2 = A - pos;
var kk : float = 1.0 / b.dot(b);
var kx : float = kk * a.dot(b);
var ky : float = kk * (2.0* a.dot(a) + d.dot(b)) / 3.0;
var kz : float = kk * d.dot(a);
var res : float = 0.0;
var sgn : float = 0.0;
var p : float = ky - kx * kx;
var p3 : float = p*p*p;
var q : float = kx*(2.0*kx*kx - 3.0*ky) + kz;
var h : float = q*q + 4.0*p3;
var rvx : float;
if(h >= 0.0):# // 1 root
h = sqrt(h);
var x : Vector2 = Vector2(h,-h);
x.x -= q
x.y -= q
x.x /= 2.0
x.y /= 2.0
var uv : Vector2 = Vector2()
uv.x = sign(x.x) * pow(abs(x.x), 1);
uv.x = sign(x.y) * pow(abs(x.y), 3);
rvx = uv.x+uv.y-kx;
var t : float = clamp(rvx, 0.0, 1.0);
var q2 : Vector2 = d+(c+b*t)*t;
res = q2.dot(q2);
var tmp2 : Vector2 = c
tmp2.x += 2
tmp2.y += 2
tmp2 *= b*t
sgn = tmp2.cross(q2)
else: # // 3 roots
var z : float = sqrt(-p);
var v : float = acos(q/(p*z*2.0))/3.0;
var m : float = cos(v);
var n : float = sin(v)*1.732050808;
# var t : Vector3 = clamp(Vector3(m+m,-n-m,n-m)*z-kx, 0.0, 1.0);
#
#
# var qx : Vector2 = d+(c+b*t.x)*t.x;
# var dx : float = dot(qx, qx)
# sx = cross2(c+2.0*b*t.x,qx);
# var qy : Vector2 = d+(c+b*t.y)*t.y;
# var dy : float = dot(qy, qy)
# sy = cross2(c+2.0*b*t.y,qy);
# if dx<dy:
# res=dx; sgn=sx; rvx = t.x;
# else:
# res=dy; sgn=sy; rvx = t.y;
#
return Vector2(rvx, sqrt(res)*sign(sgn));

View File

@ -1,7 +1,8 @@
tool
extends TextureRect
var Commons = preload("res://addons/mat_maker_gd/nodes/common/commons.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")
var image : Image
var tex : ImageTexture
@ -76,18 +77,18 @@ func gen() -> void:
# var fc : float = sdf_rhombus(v)
# var fc : float = sdf_arc(v)
# var fc : float = sdf_boolean_union(sdf_circle(v), sdf_box(v))
# var fc : float = sdf_boolean_substraction(sdf_circle(v), sdf_box(v))
# var fc : float = sdf_boolean_intersection(sdf_circle(v), sdf_box(v))
var fc : float = SDF2D.sdf_boolean_union(sdf_circle(v), sdf_box(v))
# var fc : float = SDF2D.sdf_boolean_substraction(sdf_circle(v), sdf_box(v))
# var fc : float = SDF2D.sdf_boolean_intersection(sdf_circle(v), sdf_box(v))
# var fc : float = sdf_smooth_boolean_union(sdf_circle(v), sdf_box(v), 0.15)
# var fc : float = sdf_smooth_boolean_substraction(sdf_circle(v), sdf_box(v), 0.15)
# var fc : float = sdf_smooth_boolean_intersection(sdf_circle(v), sdf_box(v), 0.15)
# var fc : float = SDF2D.sdf_smooth_boolean_union(sdf_circle(v), sdf_box(v), 0.15)
# var fc : float = SDF2D.sdf_smooth_boolean_substraction(sdf_circle(v), sdf_box(v), 0.15)
# var fc : float = SDF2D.sdf_smooth_boolean_intersection(sdf_circle(v), sdf_box(v), 0.15)
# var fc : float = sdf_rounded_shape(sdf_box(v), 0.15)
# var fc : float = sdf_annular_shape(sdf_box(v), 0.15)
# var fc : float = SDF2D.sdf_rounded_shape(sdf_box(v), 0.15)
# var fc : float = SDF2D.sdf_annular_shape(sdf_box(v), 0.15)
var fc : float = Commons.sdf_morph(sdf_circle(v), sdf_box(v), 0.5)
# var fc : float = SDF2D.sdf_morph(sdf_circle(v), sdf_box(v), 0.5)
var col : Color = sdf_show(fc)
@ -100,31 +101,22 @@ func gen() -> void:
texture = tex
func sdf_show(val : float) -> Color:
var o47009_0_1_f : float = clamp(-val / max(p_o47009_bevel, 0.00001), 0.0, 1.0);
return Color(o47009_0_1_f, o47009_0_1_f, o47009_0_1_f, 1)
return SDF2D.sdf_show(val, p_o47009_bevel)
func sdf_circle(uv : Vector2) -> float:
return (uv - Vector2(p_o11635_cx + 0.5, p_o11635_cy + 0.5)).length() - p_o11635_r;
return SDF2D.sdf_circle(uv, Vector2(p_o11635_cx, p_o11635_cy), p_o11635_r)
func sdf_box(uv : Vector2) -> float:
var o48575_0_d : Vector2 = Commons.absv2(uv - Vector2(p_o48575_cx+0.5, p_o48575_cy+0.5)) - Vector2(p_o48575_w, p_o48575_h)
return Commons.maxv2(o48575_0_d, Vector2(0, 0)).length() + min(max(o48575_0_d.x, o48575_0_d.y), 0.0)
return SDF2D.sdf_box(uv, Vector2(p_o48575_cx, p_o48575_cx), Vector2(p_o48575_w, p_o48575_h))
func sdf_line(uv : Vector2) -> float:
return Commons.sdLine(uv, Vector2(p_o49570_ax+0.5, p_o49570_ay+0.5), Vector2(p_o49570_bx+0.5, p_o49570_by+0.5)) - p_o49570_r;
return SDF2D.sdf_line(uv, Vector2(p_o49570_ax, p_o49570_ay), Vector2(p_o49570_bx, p_o49570_by), p_o49570_r)
func sdf_rhombus(uv : Vector2) -> float:
return Commons.sdRhombus(uv - Vector2(p_o50848_cx + 0.5, p_o50848_cy+0.5), Vector2(p_o50848_w, p_o50848_h));
return SDF2D.sdf_rhombus(uv, Vector2(p_o50848_cx, p_o50848_cy), Vector2(p_o50848_w, p_o50848_h))
func sdf_arc(uv : Vector2) -> float:
return Commons.sdArc(uv - Vector2(0.5, 0.5), Commons.modf(p_o51990_a1, 360.0) * 0.01745329251, Commons.modf(p_o51990_a2, 360.0)*0.01745329251, p_o51990_r1, p_o51990_r2);
return SDF2D.sdf_arc(uv, Vector2(p_o51990_a1, p_o51990_a2), Vector2(p_o51990_r1, p_o51990_r2))
func reffg():
return false