<p>A texture that stores distance from the edge of the shape, like you described, is called a &quot;distance field&quot; (you'll find lots of results if you google that phrase). Distance fields can be calculated efficiently on the GPU using the <a href="https://www.comp.nus.edu.sg/%7Etants/jfa.html" rel="noreferrer">jump flooding algorithm</a> (JFA).</p>
<p>This is a multi-pass algorithm that works by sampling pixels at a distance of first ½ the image size, then ¼, then ⅛, and so on down to single pixels. At the end it comes out with a texture with the shortest distance from each pixel to any of the initial &quot;seed&quot; pixels you put in (which would be along the edge of your shape). For more details see the paper, or also check out <a href="https://blog.demofox.org/2016/02/29/fast-voronoi-diagrams-and-distance-dield-textures-on-the-gpu-with-the-jump-flooding-algorithm/" rel="noreferrer">this blog post by Alan Wolfe</a> which also has some shadertoys with example implementations.</p>
<p>Another article which may interest you is <a href="https://medium.com/@bgolus/the-quest-for-very-wide-outlines-ba82ed442cd9" rel="noreferrer">The Quest For Very Wide Outlines</a> by Ben Golus, which addresses this exact issue, discusses a wide variety of GPU implementations (and settles on JFA), and addresses some of the implementation details such as using JFA on an antialiased edge.</p>

CC BY-SA 4.0
2020-09-29T18:15:45.393Z
2020-09-29T18:15:45.393Z
Nathan Reed
<p>I'm trying to implement something like an &quot;inner glow&quot;.</p>
<p>That is, I have an image which defines the shape of an object, and then
I want to create overlay that is a gradient that goes from the edge of the shape
and into the shape itself.
In programs like Photoshop and Krita, this is called an inner glow.</p>
<p>If I could first produce a texture which contained values that represented distance
along the inward travel path from the edge, the rest would be easy.</p>
<p>I want this to happen in a shader, but any general insights about the problem would be helpful, I guess.
One idea that I was working with, was to just blur the outline, and call the result shades around the edges the &quot;distance&quot;, but this might be an unnecessarily expensive operation on the GPU, maybe not, I don't know.</p>
<p><a href="https://i.sstatic.net/MjB2K.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/MjB2K.png" alt="image outline" /></a></p>
<p>A naive approach would be to check all distances to all other pixels, which comes out to something like 256⁴ square root calculations for a 256x256 pixel image. I believe that's not optimal. Also I don't know if that's even possible in a shader.</p>

<shader><texture><algorithm>
How do I efficiently calculate the distance to the edge of a shape?
Steinbitglis
# How do I efficiently calculate the distance to the edge of a shape?

(Asked by: username_0 on 2020-09-29)

I'm trying to implement something like an "inner glow".

That is, I have an image which defines the shape of an object, and then
I want to create overlay that is a gradient that goes from the edge of the shape
and into the shape itself.
In programs like Photoshop and Krita, this is called an inner glow.

If I could first produce a texture which contained values that represented distance
along the inward travel path from the edge, the rest would be easy.

I want this to happen in a shader, but any general insights about the problem would be helpful, I guess.
One idea that I was working with, was to just blur the outline, and call the result shades around the edges the "distance", but this might be an unnecessarily expensive operation on the GPU, maybe not, I don't know.

[![image outline](https://i.sstatic.net/MjB2K.png)](https://i.sstatic.net/MjB2K.png)

A naive approach would be to check all distances to all other pixels, which comes out to something like 256⁴ square root calculations for a 256x256 pixel image. I believe that's not optimal. Also I don't know if that's even possible in a shader.

**Tags:** shader, texture, algorithm


---

## 1 answers

### Answer by username_1 on 2020-09-29. Score: 5

A texture that stores distance from the edge of the shape, like you described, is called a "distance field" (you'll find lots of results if you google that phrase). Distance fields can be calculated efficiently on the GPU using the [jump flooding algorithm](https://www.comp.nus.edu.sg/%7Etants/jfa.html) (JFA).

This is a multi-pass algorithm that works by sampling pixels at a distance of first ½ the image size, then ¼, then ⅛, and so on down to single pixels. At the end it comes out with a texture with the shortest distance from each pixel to any of the initial "seed" pixels you put in (which would be along the edge of your shape). For more details see the paper, or also check out [this blog post by Alan Wolfe](https://blog.demofox.org/2016/02/29/fast-voronoi-diagrams-and-distance-dield-textures-on-the-gpu-with-the-jump-flooding-algorithm/) which also has some shadertoys with example implementations.

Another article which may interest you is [The Quest For Very Wide Outlines](https://medium.com/@bgolus/the-quest-for-very-wide-outlines-ba82ed442cd9) by Ben Golus, which addresses this exact issue, discusses a wide variety of GPU implementations (and settles on JFA), and addresses some of the implementation details such as using JFA on an antialiased edge.


---

