Search Unity

Procedural Planet Generation

Discussion in 'Scripting' started by ashjack, Apr 6, 2014.

  1. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    Hi everyone. I know this question has been asked many times before, but the answers are always similar, a link to something that generates the texture and size of planets. What I want is something that can actually create planetary terrain. I read about perlin noise being applied to meshes, spheres and even cubes, but I have no idea how to even start using perlin noise.

    I saw a few things that could help:

    https://www.youtube.com/watch?v=Cngr6JUfkD4

    http://www.nullpointer.co.uk/content/procedural-planets/

    https://www.youtube.com/watch?v=CZeUpF6r5FY

    Sorry if I'm being very vague, if needed, please ask for more info.

    Thanks,

    AshJack
     
    Last edited: Apr 6, 2014
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    What is your question?

    If this topic is too high for your understanding (plenty of resources available) you may want to start with something smaller until you have enough experience to takle a task of this format.
    or go the easy way and purchase one of the packages available in the asset store.
     
  3. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    Updated the question.

    I have been doing lots of Googling, and have found a partial solution. I found some code which generates Noise, but it looks very artificial and repeated.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GenerationPerlin : MonoBehaviour
    5. {
    6.     private Vector3[] vertices;
    7.  
    8.     void Start ()
    9.     {
    10.         Mesh mesh = GetComponent<MeshFilter>().mesh;
    11.        Vector3[] vertices = mesh.vertices;
    12.  
    13.         for (var i=0; i < vertices.Length; i++)
    14.         {
    15.                vertices[i] = vertices[i] * Random.Range(0.9f, 1.1f);
    16.         }
    17.         mesh.vertices = vertices;
    18.        mesh.RecalculateBounds();
    19.         mesh.RecalculateNormals();
    20.     }
    21. }
     
  4. ashjack

    ashjack

    Joined:
    Jun 9, 2013
    Posts:
    44
    Ok, after yet more googling, I have found a proper Perlin Noise generator, but the result is always the exact same shape.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using LibNoise.Unity.Generator;
    4.  
    5. public class GenerationPerlin : MonoBehaviour
    6. {
    7. public float scale= 1.0f;
    8.  
    9. public float speed= 1.0f;
    10.  
    11. public bool recalculateNormals= false;
    12.  
    13.  
    14.  
    15. private Vector3[] baseVertices;
    16.  
    17. private ImprovedPerlin noise;
    18.  
    19.  
    20.  
    21.  
    22.  
    23.    
    24.  
    25.  
    26.  
    27. void  Start(){//Changed Update to start as mesh was constantly changing - is this why the mesh generates the same?
    28.     noise = new ImprovedPerlin ();
    29.     Mesh mesh = GetComponent<MeshFilter>().mesh;
    30.  
    31.    
    32.  
    33.     if (baseVertices == null)
    34.  
    35.         baseVertices = mesh.vertices;
    36.  
    37.        
    38.  
    39.     Vector3[] vertices= new Vector3[baseVertices.Length];
    40.  
    41.    
    42.  
    43.     float timex= Time.time * speed + 0.1365143f;
    44.  
    45.     float timey= Time.time * speed + 1.21688f;
    46.  
    47.     float timez= Time.time * speed + 2.5564f;
    48.  
    49.     for (int i=0;i<vertices.Length;i++)
    50.  
    51.     {
    52.  
    53.         Vector3 vertex= baseVertices[i];
    54.  
    55.                
    56.  
    57.         vertex.x += noise.Noise(timex + vertex.x, timex + vertex.y, timex + vertex.z) * scale;
    58.  
    59.         vertex.y += noise.Noise(timey + vertex.x, timey + vertex.y, timey + vertex.z) * scale;
    60.  
    61.         vertex.z += noise.Noise(timez + vertex.x, timez + vertex.y, timez + vertex.z) * scale;
    62.  
    63.        
    64.  
    65.         vertices[i] = vertex;
    66.  
    67.     }
    68.  
    69.    
    70.  
    71.     mesh.vertices = vertices;
    72.  
    73.    
    74.  
    75.     if (recalculateNormals)
    76.  
    77.         mesh.RecalculateNormals();
    78.  
    79.     mesh.RecalculateBounds();
    80.  
    81. }
    82. }