Improved procedural masks

This commit is contained in:
Bruno
2020-12-27 21:43:33 +01:00
parent 34c24d99df
commit a073ab41dd
7 changed files with 69 additions and 42 deletions

View File

@@ -15,33 +15,6 @@ uniform vec4 uv;
uniform vec2 size;
uniform float blur;
// See: http://www.iquilezles.org/www/articles/ellipsoids/ellipsoids.htm
float sdEllipse( in vec2 p, in vec2 e )
{
p = abs( p );
if( e.x<e.y ) { p = p.yx; e = e.yx; }
vec2 r = e*e;
vec2 z = p/e;
vec2 n = r*z;
float g = dot(z,z) - 1.0;
float s0 = z.y - 1.0;
float s1 = (g<0.0) ? 0.0 : length( n )/r.y - 1.0;
float s = 0.0;
for( int i=0; i<64; i++ )
{
s = 0.5*(s0+s1);
vec2 ratio = n/(r.y*s+r);
g = dot(ratio,ratio) - 1.0;
if( g>0.0 ) s0=s; else s1=s;
}
vec2 q = p*r/(r.y*s+r);
return length( p-q ) * (((p.y-q.y)<0.0)?-1.0:1.0);
}
float sdRoundBox( in vec2 p, in vec2 b, in float r )
{
vec2 q = abs(p)-b+r;
@@ -52,11 +25,10 @@ void main()
{
vec2 uv = -1.0 + 2.0 * gl_FragCoord.xy / iResolution.xy;
uv.x *= iResolution.x / iResolution.y;
// float d = sdEllipse( uv, vec2(size.x * iResolution.x/iResolution.y, size.y) );
float d = sdRoundBox( uv, vec2(size.x * iResolution.x/iResolution.y, size.y), blur * 0.5 );
vec3 col = vec3(1.0- sign(d));
vec3 col = vec3(1.0 - sign(d));
col *= 1.0 - exp( -60.0/ (blur * 100.f + 1.0) * abs(d));
FragColor = vec4( col, 1.0 );

View File

@@ -16,6 +16,13 @@ uniform vec2 size;
uniform float blur;
// See: http://www.iquilezles.org/www/articles/ellipsoids/ellipsoids.htm
float sdEllipsoid( in vec2 p, in vec2 r )
{
float k0 = length(p/r);
float k1 = length(p/(r*r));
return k0*(k0-1.0)/k1;
}
float sdEllipse( in vec2 p, in vec2 e )
{
p = abs( p );
@@ -46,6 +53,7 @@ void main()
{
vec2 uv = -1.0 + 2.0 * gl_FragCoord.xy / iResolution.xy;
uv.x *= iResolution.x / iResolution.y;
float d = sdEllipse( uv, vec2(size.x * iResolution.x/iResolution.y, size.y) );
vec3 col = vec3(1.0- sign(d));

55
rsc/shaders/mask_round.fs Normal file
View File

@@ -0,0 +1,55 @@
#version 330 core
out vec4 FragColor;
in vec4 vertexColor;
in vec2 vertexUV;
// from General Shader
uniform vec3 iResolution; // viewport resolution (in pixels)
uniform mat4 iTransform; // image transformation
uniform vec4 color; // drawing color
uniform vec4 uv;
// Mask Shader
uniform vec2 size;
uniform float blur;
uniform float invert;
float udSegment( in vec2 p, in vec2 a, in vec2 b )
{
vec2 ba = b-a;
vec2 pa = p-a;
float h =clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 );
return length(pa-h*ba);
}
void main()
{
float ar = iResolution.x / iResolution.y;
vec2 uv = -1.0 + 2.0 * gl_FragCoord.xy / iResolution.xy;
uv.x *= ar;
float th = 0.0;
vec2 v1;
vec2 v2;
vec2 rect = size;
rect.x *= ar;
if(rect.x < rect.y) {
th = rect.x;
v1 = vec2(0.0, -rect.y + th) ;
v2 = vec2(0.0, rect.y - th) ;
}
else {
th = rect.y;
v1 = vec2(-rect.x + th, 0.0) ;
v2 = vec2( rect.x - th, 0.0) ;
}
float d = udSegment( uv, v1, v2 )- th;
vec3 col = vec3(1.0- sign(d));
col *= 1.0 - exp( -60.0/ (blur * 100.0 + 1.0) * abs(d));
FragColor = vec4( col, 1.0 );
}