[cairo] Level sets for shaders?
spitzak at d2.com
Mon Mar 1 13:37:52 PST 2004
Seeing the questions about radial shaders, I was wondering if in fact Cairo
needs the ability to fill an arbitrary shape with some kind of gradient
defined by the shape.
We are using "Level Sets" in special effects quite a bit. This is a 2-D array
of single values that describe a 2D boundary (the idea extends to N
dimensions and most literature will describe 3D). The values are the distance
to the nearest point on the boundary except the numbers are positive "inside"
the shape and negative "outside" the shape. The boundary does not necessarily
lie on the pixel boundaries, it can be accurately described even if it goes
through the middle of a pixel. Frequencies higher than 2 (1?) pixels in the
boundary shape are lost, however.
The primary use of level sets is that it is very easy to calculate related
shapes from them (such as grow and shrink), to intersect and union them, and
to change their shape without changing their volume. They are also very
useful for drawing the shape anti-aliased (it is accurate as long as two
opposite boundaries are more than 1 pixel apart).
A cairo shader could be a level set built from a Cairo path. This would allow
the full use of the path construction interface for defining the shader
shape. A simple lookup from the level set to a color ramp (which itself could
be defined by 2 colors and a gamma value) will allow a lot of interesting
shaders to be made. If you also can define a contribution to this lookup from
the horizontal and vertical partial derivatives of the level set, you can get
a whole lot more (such as moving the brightest point in a circle to somewhere
other than the center). For complicated shapes the resulting shading is
suprisingly nice, and this is apparently by far the fastest way to calculate
such shading, and is easy to support in hardware.
There is a problem, though. From what I have heard, conversion of a path *to*
a level set is an expensive calculation. (Conversion the other way is
cheap). I suspect (but don't know for sure) that the complexity is equivalent
to that needed to convert a path to an anti-aliased set of pixels shading
values, so this may fit well into Cairo.
In any case if there is desire for more than linear ramps and circles and
ellipses for shading, this certainly should be looked into.
,~,~,~,~ ~ ~ ~ ~
/\_ _|_========___ Bill Spitzak
~~~/\/\\~~~~~~\____________/~~~~~~~~ spitzak at d2.com
More information about the cairo