This post is an addendum to the last post where I say that you can make distance field textures with JFA but don’t fully explain how to make SIGNED distance field textures, which is what you really want.
If you want to go straight to a working demo with webgl pixel shader source code, here is the shadertoy: Shadertoy: JFA SDF Texture
If you naively use a distance transform to make a distance field texture, you’ll get an UNSIGNED distance field texture, where you only have the distance to the surface of the object from the outside, but won’t have the distance to the surface of the object from the inside.
This is important because signed distance field textures have both, and use bilinear interpolation of distance on each side of the shape surface to make a nice smooth line. Below is what happens when you try to use an unsigned distance field texture (aka the distance transform of the image, using JFA / Voronoi information), using the zero distance line as the surface of the object:
It looks ok (if not fairly pixelated), but you can really see it break down when you zoom in:
So you might say to yourself, maybe i need to keep the surface line at distance 0.5 instead of 0.0 so that there is distance information to interpolate? If you do that, the first thing you might notice is that the objects get fatter:
But it does look better when you zoom in, which is a plus:
The real issue is that you really just need the distance from each pixel to the surface of the object from both the inside and the outside. In our case, our Voronoi diagram we make with JFA only gives the distance from the outside. So what is the solution? At first I was thinking maybe you can get the gradient of this data at the point of each pixel and “push the zero line in” a little bit to give at least one pixel layer worth of inside data. However, a brilliant friend of mine came up with the actual solution: You invert your source data so empty space becomes seed, and seed becomes empty space, and you run JFA again to get the distance from the inside!
That actually works very well. It’s also very easy to combine them. You make a pixel shader that reads the data from the outside Voronoi diagram and the inside Voronoi diagram, calculate the output distance (0.5 + outsideDistance * 0.5 – insideDistance * 0.5), and output that 0 to 1 distance value in one or more of the color channels.
Here’s a glsl excerpt below, note that we divide the distance by 8 and clamp between 0 and 1 so that the data is suitable for a normalized color image (normalized as in the color channels can store values between 0 and 1):
// calculate distances from seed coordinates float outsideDist = clamp(length(outsideSeedCoord-fragCoord) / 8.0, 0.0, 1.0); float insideDist = clamp(length(insideSeedCoord-fFragCoord) / 8.0, 0.0, 1.0); // calculate output distance float signedDistance = 0.5 + outsideDist * 0.5 - insideDist * 0.5; // set the color based on that distance fragColor = vec4(signedDistance);
It actually looks a lot like the first image where we use the zero distance line of the unsigned distance field texture, so we still aren’t quite there:
When you zoom in, it looks a little better, but something still seems a bit off:
The final step to making this look good is to realize that the power of signed distance textures is in their ability to interpolate distance information well. When we have a full resolution texture, there is no interpolation going on. We actually need to decrease the size of our distance field texture to make it look better. If only all problems were solved by making textures smaller!
Here is the resulting image when making the distance field texture 1/4 as large on each axis (1/16th as big total):
And zooming in you can see that it scales very well. The zoom is a 20x magnification, on top of the magnification we already get from it being a larger texture:
And just to show the intermediary textures, here is the outside distance Voronoi diagram:
And the inside distance Voronoi diagram (The seed is in bright green, the dim green is the empty space that has distance information):
And here is the final distance field texture used to render the final result I showed above.
Zoomed in to show just how low resolution it is! This is the thing that looks like a + or a sword just left of middle.
Again, here is the shadertoy that does this technique, generating a signed distance field texture on the fly for randomly placed objects, and then using that signed distance field to render a larger image that you can further zoom in to:
Shadertoy: JFA SDF Texture