0

I'm trying to rasterize a conic (rational quadratic) bezier, and came across this snippet: https://www.shadertoy.com/view/MlKcDD The idea is to calculate the distance from each fragment to the bezier and use that information to calculate translucency to achieve anti-aliasing. The problem is that it needs to work for rational beziers and the example is beyond my math skills to modify to do so.

I meant this snippet in particular:

// This method provides just an approximation, and is only usable in
// the very close neighborhood of the curve. Taken and adapted from
// http://research.microsoft.com/en-us/um/people/hoppe/ravg.pdf
float sdBezier( vec2 p, vec2 v0, vec2 v1, vec2 v2 )
{
    vec2 i = v0 - v2;
    vec2 j = v2 - v1;
    vec2 k = v1 - v0;
    vec2 w = j-k;

    v0-= p; v1-= p; v2-= p;
    
    float x = cro(v0, v2);
    float y = cro(v1, v0);
    float z = cro(v2, v1);

    vec2 s = 2.0*(y*j+z*k)-x*i;

    float r =  (y*z-x*x*0.25)/dot2(s);
    float t = clamp( (0.5*x+y+r*dot(s,w))/(x+y+z),0.0,1.0);
    
    return length( v0+t*(k+k+t*w) );
}
Illari
  • 43
  • 5
  • If you set the mode to `1` in the shadertoy example, so that the code you're showing is used, things go really wrong, really fast. So you don't actually want this code, you'd want the code that is used for `METHOD=0`. However, that code is based on Cardano's algorithm for finding the roots of a cubic polynomial, and rational Beziers are by definition not polynomial. So really, you can't use any of this code. Instead, find your projection the "dumb" way by comparing your point to several known on-curve points, finding the two closest matches, then redoing the same check between those. – Mike 'Pomax' Kamermans Apr 23 '22 at 16:06
  • see [rendering cubic BEZIER in GLSL](https://stackoverflow.com/a/60113617/2521214) for some inspiration – Spektre Apr 24 '22 at 08:46

0 Answers0