rj-action-library/Runtime/Shading/Library/SDF.gdshaderinc

109 lines
2.5 KiB
Plaintext
Raw Normal View History

2025-05-18 17:11:31 +00:00
// #include "res://addons/rokojori_action_library/Runtime/Shading/Library/SDF.gdshaderinc"
2025-08-31 06:05:39 +00:00
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Colors.gdshaderinc"
2025-05-18 17:11:31 +00:00
void computeRectangleBounds( float in_offset, float in_radius, float in_maxRadius, vec2 size, out vec2 minP, out vec2 maxP, out float out_radius )
{
out_radius = min( in_radius, in_maxRadius );
float borderSize = out_radius + in_offset;
minP = vec2( borderSize, borderSize );
maxP = size - vec2( borderSize, borderSize );
}
float roundedRectangleDistance( vec2 minP, vec2 maxP, vec2 pos)
{
vec2 leftTop = minP - pos;
vec2 bottomRight = pos - maxP;
vec2 maximum = max( leftTop, bottomRight );
maximum = max( maximum, vec2(0,0) );
return length( maximum );
}
float roundedRectangle( vec2 minP, vec2 maxP, vec2 point, float borderSize, float sharp )
{
float rectangleDistance = roundedRectangleDistance( minP, maxP, point );
rectangleDistance -= borderSize;
rectangleDistance *= sharp;
rectangleDistance = clamp( rectangleDistance, 0.0, 1.0 );
return 1.0 - rectangleDistance;
2025-08-31 06:05:39 +00:00
}
float sdf_fill( float sd )
{
return 1.0 - clamp( sd, 0, 1 );
}
float sdf_stroke( float sd, float strokeWidth )
{
return sdf_fill( abs( sd ) - strokeWidth );
}
vec2 sdf_shape( float sd, float strokeWidth )
{
float fill = sdf_fill( sd + strokeWidth );
float stroke = sdf_stroke( sd, strokeWidth );
return vec2( fill, stroke );
}
vec4 sdf_coloredShape( float _sd, float _strokeWidth, vec4 _fillColor, vec4 _strokeColor )
{
vec2 _sdf_shape = sdf_shape( _sd, _strokeWidth );
vec4 _fill = fade( _fillColor, _sdf_shape.x );
vec4 _stroke = fade( _strokeColor, _sdf_shape.y );
return blendMode_alpha( _fill, _stroke );
}
/*
TAKEN FROM
[ Inigo Quilez ]
https://iquilezles.org/articles/distfunctions2d/
*/
float sdCircle( vec2 p, float r )
{
return length(p) - r;
}
float sdRoundedBox( in vec2 p, in vec2 b, in vec4 r )
{
r.xy = (p.x>0.0)?r.xy : r.zw;
r.x = (p.y>0.0)?r.x : r.y;
vec2 q = abs(p)-b+r.x;
return min(max(q.x,q.y),0.0) + length(max(q,0.0)) - r.x;
}
float sdChamferBox( in vec2 p, in vec2 b, in float chamfer )
{
p = abs(p)-b;
p = (p.y>p.x) ? p.yx : p.xy;
p.y += chamfer;
const float k = 1.0-sqrt(2.0);
if( p.y<0.0 && p.y+p.x*k<0.0 )
return p.x;
if( p.x<p.y )
return (p.x+p.y)*sqrt(0.5);
return length(p);
}
float sdBox( in vec2 p, in vec2 b )
{
vec2 d = abs(p)-b;
return length(max(d,0.0)) + min(max(d.x,d.y),0.0);
}