圆角shader

发布时间 2023-04-30 10:56:15作者: MrZivChu
// unsigned round box
float udRoundBox( vec3 p, vec3 b, float r )
{
  return length(max(abs(p)-b,0.0))-r;
}

// substracts shape d1 from shape d2
float opS( float d1, float d2 )
{
    return max(-d1,d2);
}

// to get the border of a udRoundBox, simply substract a smaller udRoundBox !
float udRoundBoxBorder( vec3 p, vec3 b, float r, float borderPercent )
{
  return opS(udRoundBox(p, b*borderPercent, r), udRoundBox(p, b, r));
}

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
    // get screen pos in [0-1]
    // we want a 1:1 aspect ratio, this is why we use iResolution.yy
    vec2 uv = fragCoord.xy / iResolution.yy;
    
    // box setup
    vec3 boxPosition = vec3(0.5,0.5,0.0);
    vec3 boxSize = vec3(0.1,0.1,0.0);
    float boxRounding = 0.05;
    
    // render the box
    vec3 curPosition = vec3(uv, 0.0);    
    float dist = udRoundBoxBorder(curPosition - boxPosition, boxSize, boxRounding, 0.9);    
    float THRESHOLD = 0.2;
    if (dist <= THRESHOLD)
        fragColor.r += 1.0;
}