Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Creating a clickable Voronoi diagram

Discussion in '2D' started by Tigro, Feb 1, 2017.

  1. Tigro

    Tigro

    Joined:
    Apr 25, 2014
    Posts:
    58
    What I'm currently trying to achieve is to be able to generate Voronoi diagrams in Unity but with each "cell" created clickable. So for example I'd like to be able to create something like that:


    But each cell/region to be clickable so that I can then bind some actions into them.

    How would you go about creating something like that? Cause honestly I don't really even know where to start. Is there some known library for that in the asset store? Or is it easily code-able yourself?
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Probably better of looking for an algorithm or sourcecode somewhere, most likely outside of unity. You'd then have to try to turn each region into a separate game object with a mesh/collider? Depends if you want this unique each time the thing runs, or otherwise cut out some shapes with an art program.
     
  3. Tigro

    Tigro

    Joined:
    Apr 25, 2014
    Posts:
    58
    Yep, I'd like it to generate a new randomized diagram like this every time it runs. Sounds like there's no easy way to achieve it, then :(
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    I searched "voronoi unity" and found this post with a library that can generate voronoi shapes with C#:
    https://forum.unity3d.com/threads/delaunay-voronoi-diagram-library-for-unity.248962/

    Looks like it can generate a voronoi map using a list of points and the bounds to contain them, and optionally the number of relaxation iterations. You should be able to use the generated edges to dynamically create a polygon collider for each region.
     
    Last edited: Feb 2, 2017
    Tigro likes this.
  5. Tigro

    Tigro

    Joined:
    Apr 25, 2014
    Posts:
    58
    Oh, I _did_ see this topic but discarded the idea of using it after I saw someone mentioning bugs there. Only now that you linked it I decided to have a look at the whole topic again and found out that the author has actually patched the bugs - that's great, exactly what I needed, thanks a lot :)

    The problem, though, is with forcing some interaction with the created diagram. As you mention, I do receive a list of edges but while drawing them is easy as it doesn't require any particular order, knowing what subset of them is needed for each created polygon doesn't seem that easy anymore - do you happen to have any idea how I could approach it?
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    At first glance it appears that there is a dictionary called "SitesIndexedByLocation" in Voronoi.cs which contains all the "Site" objects generated during the initialization, keyed by the original points. Each one of those Site objects contains a list "Edges" representing that site's Voronoi Region. So I would think that you could create one polygon collider for each Site using the Edges list. Grab the "LeftVertex" and "RightVertex" properties of each edge, and you should have the complete polygon.
     
    Last edited: Feb 3, 2017
    RedEarth and Tigro like this.
  7. Tigro

    Tigro

    Joined:
    Apr 25, 2014
    Posts:
    58
    Oh, didn't notice it - that's fantastic, thanks a lot for the help :)
     
    LiterallyJeff likes this.