Search Unity

I generated Voronoi data. No idea how to render it.

Discussion in 'Scripting' started by Deleted User, Jan 19, 2015.

  1. Deleted User

    Deleted User

    Guest

    Right now I'm generating a voronoi graph. (I plotted some random points, did a Delaunay Triangulation, then drew edges between the triangle circumcenters and stored them to a list of voronoi cells.)

    The whole graph is a series of float values between (0,0) and (1,1). I can render all the edges using debug lines, as seen here:

    voronoidebuglines.png

    But I realized I have NO CLUE how to render this to a plane in the world, where I fill in each cell using different terrain textures.

    Right now, each voronoi cell has its own array of edges. And each edge is stored as a pair of vectors (a and b).

    An example voronoi cell might have the following data (procedurally generated):

    e[0].a = (0.40, 0.40);
    e[0].b = (0.50, 0.45);

    e[1].a = (0.50, 0.45);
    e[1].b = (0.48, 0.50);

    e[2].a = (0.48, 0.50);
    e[2].b = (0.43, 0.48);

    e[3].a = (0.43, 0.48);
    e[3].b = (0.38, 0.44);

    e[4].a = (0.38, 0.44);
    e[4].b = (0.40, 0.40);​

    (It's 5 edges that join up to create a closed polygon.)

    I originally created a more grid-based tile map. It was too blocky for my tastes, but was more intuitive to render. I set a two dimensional array of "tile codes" for a grid of 100x100 square tiles. I then used those codes to getpixels/setpixels on a texture2D, one tile at a time. (I then applied that texture2D to a mesh.)

    The voronoi diagram isn't so straightforward.

    I can't figure out how to turn my graph of random floating point values into a texture2D. Since the voronoi polygons aren't uniform, or even square, I don't know the best way to fill in the area between the edges with some kind of texture data. I imagine I'd still use getpixels/setpixels? But don't know how to do it for more irregular shapes, starting from the edge/vector data.

    Code would be most helpful. But any advice is appreciated.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Lohoris2 likes this.
  3. Deleted User

    Deleted User

    Guest

    The plan is to draw to a texture and apply to a mesh. Is that what you mean? Or are you thinking of a better way?
     
  4. Deleted User

    Deleted User

    Guest

    No he means each point of the final Voronoi data would be a vertex of a polygon. From there you can apply colored textures to the the resulting polygons.

    I've tried a couple different methods. One being GL lines, and another GL Tris. Both work decent enough for speed.
    example pic

    I also tried out using Vector Lines from Vectrosity.
    example pic
     
  5. MakeCodeNow

    MakeCodeNow

    Joined:
    Feb 14, 2014
    Posts:
    1,246
    Basically what you are trying to do is rasterize the voroni graph. You can do some googling on that, but for you the most important thing to realize is that each voroni cell is a convex polygon. You can look up the math for checking if a point is inside or outside of a convex polygon and that'll be the key to your algorithm. In pseudocode, I think it'll look like this:

    Code (csharp):
    1.  
    2. for each pixel in the texture
    3.     use point-in-convex-poly-test to determine the cell for the picture
    4.     compute texture coordiantes for the pixel (global graph or cell based)
    5.     look up the corresponding texel in the texture for that cell
    6.     write that texel into your bake texture
    7.  
     
  6. Deleted User

    Deleted User

    Guest

    Thanks for your help, everyone. I'm creating individual meshes for each object. It seems to be working. Just have to scale it and add some additional features. Thanks a lot!
     
    ImFromTheFuture likes this.