Search Unity

Find shortest distance to a pixel of another color

Discussion in 'Shaders' started by AlexTorbin, Dec 15, 2020.

  1. AlexTorbin

    AlexTorbin

    Joined:
    Dec 12, 2019
    Posts:
    48
    Have been racking my brains all day, how would you approach this? Lets assume there is a texture filled with patches of different color. They are of arbitrary shape, each region has a certain value of r channel, no smooth transitions. The challenge is: for each pixel on this texture find shortest distance to another region (in a limited search range). Distance and range measured in screen space. Of course, first thing that comes to mind is to start sampling everything in expanding circle and return dist of the first hit. But is there an efficient way? May be some smart usage of filtering or another trick. Not very experienced in shaders, so I hope for your advice.
     
    Last edited: Jan 6, 2021
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,348
    You want a voronoi diagram, and you’ll want to use the jump flood algorithm (JFA) to calculate it.

    I did a write up on doing wide post process outlines, which while at first may sound unrelated, is about tackling the issue of doing an exhaustive search of all pixels within some radius to find an edge, which is exactly what you’re looking to do.
    https://bgolus.medium.com/the-quest-for-very-wide-outlines-ba82ed442cd9
     
    AlexTorbin and Invertex like this.
  3. AlexTorbin

    AlexTorbin

    Joined:
    Dec 12, 2019
    Posts:
    48
    This looks incredibly helpful, thanks a lot! I'm gonna read this with all my might.