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

Procedural Terrain on a Planet

Discussion in 'Scripting' started by Manmax75, Mar 12, 2012.

  1. SnotE101

    SnotE101

    Joined:
    Mar 5, 2014
    Posts:
    4
    This may seem cheap, but is anyone willing to post their code for this project?
    Thanks a bunch!
     
  2. gosaints70

    gosaints70

    Joined:
    Dec 9, 2014
    Posts:
    4
    To those who may stumble upon this in the future here is some tested (5.4.0f3) code based off of this thread. Please note that this requires CoherentNoise (https://app.assembla.com/spaces/coherentnoise/ans8N-3Gar4jxyeJe4gwI3/source) and a cube mesh with extra vertices (https://www.dropbox.com/s/3i11dpszzzbey3v/Cube.fbx) This, with the default settings, will look more like an asteroid than a planet, but if you tinker around with the noise you can get any results you want. To make this work simply drag the cube model into your scene, attach the Planet script to it, and add a mesh collider.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using CoherentNoise.Generation.Fractal;
    4.  
    5. public class Planet : MonoBehaviour
    6. {
    7.     private Mesh gameMesh;
    8.     private Vector3[] vertices;
    9.     public float radius = 1f;
    10.     public float noiseModifier = 0.1f;
    11.     private MeshCollider meshCollider;
    12.  
    13.     void Start()
    14.     {
    15.         GeneratePlanet();
    16.     }
    17.  
    18.     public void GeneratePlanet()
    19.     {
    20.         //You can change PinkNoise to any type of noise. For more information on types of noise look through the CoherentNoise documentation (http://chaoscultgames.com/products/CN/CoherentNoiseManual.pdf)
    21.         PinkNoise noise = new PinkNoise(Random.Range(-999999999, 999999999));
    22.         gameMesh = GetComponent<MeshFilter>().mesh;
    23.         meshCollider = GetComponent<MeshCollider>();
    24.         vertices = gameMesh.vertices;
    25.  
    26.         for (int i = 0; i < vertices.Length; i++)
    27.         {
    28.             vertices[i] = (vertices[i].normalized * (radius + noise.GetValue(vertices[i].normalized) * noiseModifier));
    29.         }
    30.         gameMesh.vertices = vertices;
    31.         gameMesh.RecalculateNormals();
    32.         gameMesh.RecalculateBounds();
    33.         meshCollider.sharedMesh = gameMesh;
    34.     }
    35. }
     
    Rodolinc likes this.
  3. Rodolinc

    Rodolinc

    Joined:
    Sep 23, 2013
    Posts:
    63
    Awesome, sorry for my dumb question how am I supossed to add the coherent noise solution to Unity?
     
  4. gosaints70

    gosaints70

    Joined:
    Dec 9, 2014
    Posts:
    4
    You're gonna wanna download the file from the link I provided and drag the CoherentNoise folder, located inside of the trunk folder, into your Unity project
     
  5. Rodolinc

    Rodolinc

    Joined:
    Sep 23, 2013
    Posts:
    63
    Great worked like a charm, i though I had to do some bridge class or soemthing weird.

    Thanks!

    upload_2017-7-7_18-29-28.png

    Any suggestions on what to do, to collide with the actual mesh? (currently that has a mesh collider, bu since its scale is 1000 the collider is terribly unaccurate)

    edit:
    I know mesh colliders are not very efficient, but the only solution I can think of is manually placing box colliders over the mesh...
     
    Last edited: Jul 7, 2017
  6. gosaints70

    gosaints70

    Joined:
    Dec 9, 2014
    Posts:
    4
    Well if you mean that you're scaling the actual mesh to 1000 you shouldn't be doing that. Just set the radius and noise scale to a higher value to produce the same model without messing up the collider.