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

"tileable" perlinnoise

Discussion in 'Scripting' started by joshcamas, Jun 16, 2018.

  1. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    I have a many tiles of water meshes, with this low poly water effect:

    Code (CSharp):
    1. public class Water : MonoBehaviour
    2. {
    3.     public float scale = 7.0f;
    4.     public float heightScale = 1.0f;
    5.     public float speed = 1;
    6.  
    7.     // Update is called once per frame
    8.     void Update ()
    9.     {
    10.         MeshFilter mf = GetComponent<MeshFilter>();
    11.         Vector3[] vertices = mf.mesh.vertices;
    12.  
    13.         for (int i = 0; i < vertices.Length; i++)
    14.         {
    15.             vertices[i].y = heightScale * Mathf.PerlinNoise(Time.time * speed + (vertices[i].x * scale), Time.time* speed + (vertices[i].z* scale));
    16.         }
    17.         mf.mesh.vertices = vertices;
    18.     }
    19. }
    Problem is, the edges of the tiles do not match up with each other. Is there a way to make perlin noise tiled? Thanks!
     
  2. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    The problem is that you're using vert coordinates which are in local space, so when you cross from one tile to the next you're essentially resetting the the values you're using for noise.

    Add each tile's world position to the vert coordinates.
     
    joshcamas likes this.
  3. joshcamas

    joshcamas

    Joined:
    Jun 16, 2017
    Posts:
    1,276
    Yup, that worked! I had tried that in the past but didn't realize the mesh was scaled... after implementing mesh scale, it works!
     
    Antony-Blackett likes this.