Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to cut a polygon at terrain height intersection?

Discussion in 'World Building' started by Rowlan, Jan 3, 2020.

  1. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,199
    I've been implementing a biome creator to populate huge terrains automatically. I'd like to cut off the biome polygon at the terrain intersection. The question is how to do that efficiently.

    A biome has a position and expands in all directions until either a terrain bound or a maximum length are reached.

    The simplest use case is e. g. a lake. I have a terrain (green) and a center point (center of the plane) and would like to get the bounds (of the blue area) as a polygon.

    lake-test.jpg

    Can anyone share their expertise about how to do that efficiently?

    The end result should be a polygon like this:

    lake.jpg
     
  2. wyattt_

    wyattt_

    Unity Technologies

    Joined:
    May 9, 2018
    Posts:
    424
    Is it always going to be a plane?
     
  3. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,199
    Yes, my current purpose is just for a lake, i. e. basically a plane.
     
  4. Devalidating

    Devalidating

    Joined:
    Jan 8, 2020
    Posts:
    1
    The way I would go about it would be:

    get the terrain height map texture,

    iterate through every n-th pixel (we don't need it to be a pixel perfect mesh, I don't think)

    find if those pixels are within the bounds of the plane and lower than the height of the plane

    if so, record the first and last time that happens per row (we don't care about the middle, only the edges
    *alternatively, you could record every change from above to below the mask plane if you want islands

    now you have a list of points around the border. Connect the dots and you should have a polygon.

    That's the first method that came to mind.
     
  5. Rowlan

    Rowlan

    Joined:
    Aug 4, 2016
    Posts:
    4,199
    Oh, awesome, just stumbled upon this. You guys should make use of this :) Is it possible to only have the one polygon affect the terrain under which the mouse cursor is, not the entire brush? Take e. g. the big large one in the center of the brush. When I click to paint, everything should be flattened to the minimum height. Better yet would be to have that polygon outline extracted. Then I can Delaunay Triangulate myself and have my lake.

    lake.jpg