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.

Question I am trying to make my procedural mesh move like a wave for a project but can't find anything online

Discussion in 'Scripting' started by Emarb2003, Nov 28, 2022.

  1. Emarb2003

    Emarb2003

    Joined:
    Nov 28, 2022
    Posts:
    3
    I am trying to make my procedural mesh move like a wave for a project but can't find anything online and also don't know if it possible any help is appreciated. Thanks in advance.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. [RequireComponent(typeof(MeshFilter))]
    7. public class MeshGeneorator : MonoBehaviour
    8. {
    9.  
    10.     Mesh mesh;
    11.  
    12.     Vector3[] vertices;
    13.     int[] triangles;
    14.     Color[] colors;
    15.  
    16.     public int xSize = 20;
    17.     public int zSize = 20;
    18.  
    19.     public Gradient gradient;
    20.  
    21.     float minTerrainHeight;
    22.     float maxTerrainHeigth;
    23.  
    24.  
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         mesh = new Mesh();
    30.         GetComponent<MeshFilter>().mesh = mesh;
    31.  
    32.         CreateShape();
    33.         UpdateMesh();
    34.     }
    35.  
    36.  
    37.  
    38.     void CreateShape()
    39.     {
    40.  
    41.         vertices = new Vector3[(xSize + 1) * (zSize + 1)];
    42.  
    43.         for(int i = 0, z = 0; z <= zSize; z++)
    44.         {
    45.             for (int x = 0; x <= xSize; x++)
    46.             {
    47.                 float y = Mathf.PerlinNoise(x * .3f, z * .3f) * 2f;
    48.                 vertices[i] = new Vector3(x, y, z);
    49.  
    50.                 if (y > maxTerrainHeigth)
    51.                     maxTerrainHeigth = y;
    52.                 if (y < minTerrainHeight)
    53.                     minTerrainHeight = y;
    54.  
    55.                 i++;
    56.             }
    57.         }
    58.  
    59.         triangles = new int[xSize * zSize * 6];
    60.  
    61.         int vert = 0;
    62.         int tris = 0;
    63.  
    64.         for (int z = 0; z < zSize; z++)
    65.         {
    66.             for (int x = 0; x < xSize; x++)
    67.             {
    68.                 triangles[tris + 0] = vert + 0;
    69.                 triangles[tris + 1] = vert + xSize + 1;
    70.                 triangles[tris + 2] = vert + 1;
    71.                 triangles[tris + 3] = vert + 1;
    72.                 triangles[tris + 4] = vert + xSize + 1;
    73.                 triangles[tris + 5] = vert + xSize + 2;
    74.  
    75.  
    76.  
    77.                 vert++;
    78.                 tris += 6;
    79.             }
    80.             vert++;
    81.  
    82.         }
    83.  
    84.         colors = new Color[vertices.Length];
    85.  
    86.         for (int i = 0, z = 0; z <= zSize; z++)
    87.         {
    88.             for (int x = 0; x <= xSize; x++)
    89.             {
    90.                 float height = Mathf.InverseLerp(minTerrainHeight, maxTerrainHeigth, vertices[i].y);
    91.                 colors[i] = gradient.Evaluate(height);
    92.                 i++;
    93.             }
    94.         }
    95.  
    96.     }
    97.  
    98.  
    99.     void UpdateMesh()
    100.     {
    101.         mesh.Clear();
    102.  
    103.         mesh.vertices = vertices;
    104.         mesh.triangles = triangles;
    105.         mesh.colors = colors;
    106.  
    107.         mesh.RecalculateNormals();
    108.     }
    109.  
    110.  
    111.  
    112.  
    113.  
    114. }
    115.  
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,246
    do you mean like a waving flag?
    or sea surface waves?
     
  3. Emarb2003

    Emarb2003

    Joined:
    Nov 28, 2022
    Posts:
    3
    like sea surface waves? I've made a terrain with a water texture if that helps
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,246
    unity terrain object wont help there (it would be too slow to animate),
    its mostly done on shaders, but check some of these resources and play with them:

    tutorial & theory: https://catlikecoding.com/unity/tutorials/flow/waves/

    all kind of ocean solutions,
    https://github.com/JakubSzark/unity-water-shader
    https://github.com/ScrambledRK/ocean-waves
    https://github.com/leinlin/crest-oceanrender
    https://github.com/douduck08/UnityVFX-GerstnerWave
    simple shader, assign to high resolution mesh plane https://gist.github.com/unitycoder/7b501f5d6af03dbabd48c16c08767fe3

    can find more seaching with: unity sea waves / unity ocean shader etc.
     
  5. Emarb2003

    Emarb2003

    Joined:
    Nov 28, 2022
    Posts:
    3
    Ok thanks alot man big help! :)
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,246