Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Random Map Generator Help

Discussion in 'Scripting' started by Shiikarii, Apr 14, 2015.

  1. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    Hey everybody my name is Alex..
    above you can see my Random Mesh, generated with a Perlin Noise.
    Unbensannt.PNG
    Finally my game should be a 2D Game and i want that every Triangle or Vertices that is on the Z axis above a Satic value to show or to delete this point.
    It should look like this, to show that i simply added a plane and put it above the Noise Mesh.
    Unbenannts.PNG
    Unbenannt.PNG
    And that should be my final Terrain.

    Should i check the Vertices or Triangles to achive this terrain?
    Sry for my bad English.
     
  2. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38
    you need to check the Vertices position, the triangles just store the index of the vertices
     
  3. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    yeah right, but what should i do when i say that Vertice is under my Borderline and should be deleted?

    What i have achived is that all the Vertices that are under 0f should be recalculated and the Y Axis has chanched -2f backwords, that looks really cool, but my questions is, who can i remove those Triangles, so that just the hills are the Mesh?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Look at it from the triangles first. Here is what I would do in pseudocode:

    make a new list of verts (empty)
    make a new list of triangle (empty)
    iterate every triangle in the original, considering each point inside the triangle:
    -- if all points making up that triangle are below your "zero line," disregard it all
    -- if any points are above the zero line:
    -- -- add all vertices from this triangle to the new list of verts
    -- -- add the triangle (with updated values indexing the new verts obviously) to the new triangle list
    update the mesh with the new verts and new tris lists.

    Hope this helps!
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Do you need the mesh? Seems to me like you could simply go straight to setting pixels on a sprite based on the value of the Perlin noise function.
     
  6. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38
    or you could set the vertex alpaha but than you need a fitting shader.

    but removing the verts is the best methode for perfomance reasons in the long run.
     
  7. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    i finally made it, but i have a new problem that i want to achive :p

    how can i get a noise that points every Mesh Triangles on the Yaxis and looks smooth to other meshes?? like chunks ^^
    pseudocode will do it, hopefully :D
    Usssnbenannt.PNG
     
  8. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    If you use for One Chunk a Noise with a resolution of 1024x1024 make a 4096x4096 Noise for 4 chunks then use it like a atlas for your terrain generation

    You can also work with seeds and move inside the Noise
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Do your chunks have to repeat? You can continue to use Perlin noise offsetting it for each tile to create a non repeating continuous set of chunks.

    Or you can use a drop off function on the edges. Corners get a bit tricky this way, but can be done.
     
  10. RabenFutter

    RabenFutter

    Joined:
    Dec 30, 2012
    Posts:
    38
    is it not possible to change the offset of the perlin noise?

    I would recommend libnoise for more complex noise effects. There you can just change the offset and seed etc.
     
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sure it is. Here is the full PerlinNoise function with all the adjustments.

    Code (CSharp):
    1. float noise = magnitude * Mathf.PerlinNoise ((x + xOffset) * xFrequency, (y + yOffset) * yFrequency) + magnitudeOffset;
     
  12. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    This is my noise function:
    Code (CSharp):
    1. public float InitNoiseY (int x, int y, float perlinHeight, float xScale, float yScale, int xOffset, int yOffset)
    2.     {
    3.         float noise = perlinHeight * Mathf.PerlinNoise(Time.time + ((x + xOffset) * xScale), Time.time + ((y + yOffset) * yScale));
    4.         return noise;
    5.     }
    This function is on every chunk:
    Code (CSharp):
    1. //Add some Noise to the Terrain
    2.         if (allowNoiseAdd)
    3.         {
    4.             for (int v = 0, y = 0; y <= resolution; y++)
    5.             {
    6.                 for (int x = 0; x <= resolution; x++, v++)
    7.                 {
    8.                     float noiseY = world.InitNoiseY(x, y, 10f, 0.05f, 0.05f, world.ChunkWidth, world.ChunkHeight);
    9.  
    10.                     if (noiseY > borderline)
    11.                     {
    12.                         vertices[v] = new Vector3(vertices[v].x, +0.1f, vertices[v].z);
    13.                     }
    14.                     else
    15.                     {
    16.                         vertices[v] = new Vector3(vertices[v].x, -0.1f, vertices[v].z);
    17.                     }
    18.                 }
    19.             }
    20.         }

    i don't get it :/
    this above is my noise function... every chunk is calling it for every vertice, but it looks strange.. how should i use the offset?

    Unbenannt.PNG
     
    Last edited: Apr 20, 2015
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The offset should be based on the x and y position of the terrain. Basically you want continuous noise from one piece of terrain to the next.