Search Unity

Perlin Noise on mesh vertices

Discussion in 'Scripting' started by crasse, Apr 23, 2014.

  1. crasse

    crasse

    Joined:
    Mar 28, 2014
    Posts:
    23
    Hi there ! :) (sorry for double post in support forum, i realised that i posted in the wrong place but didn't find out how to remove my previous post)

    I'm trying to animate the surface of a mesh with perlin noise, i'm not trying to simulate water on anything, for the moment i'm just trying to animate vertices of the mesh so the mesh will be animated as it would be with the Deformer filter in C4D.

    for the moment i'm trying only on the x dimension of my mesh vertices, but the problem is that the PerlinNoise works but give to all my mesh vertices the same x position, they didn't keep their respective x position and my mesh is turned completly flat.

    is there a way to keep each respective position of my vertices while adding perlinNoise on them ?

    here is my code :

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Daniel : MonoBehaviour {
    5.  
    6.     public float scaleX = 1f;
    7.     public float scaleY = 1f;
    8.  
    9.     private Perlin noise;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void FixedUpdate () {
    18.    
    19.         GameObject visage = GameObject.Find ("daniel tete/Sph__re_1_3");
    20.         Mesh mesh = visage.GetComponent<MeshFilter> ().mesh;
    21.         Vector3[] vertices = mesh.vertices;
    22.  
    23.         float invertscaleX =1-(scaleX-1);
    24.         float invertscaleY =1-(scaleY-1);
    25.  
    26.  
    27.         int i;
    28.  
    29.         for (i=0; i<vertices.Length; i++) {
    30.  
    31.  
    32.         vertices[i].x = vertices[i].x * (Mathf.PerlinNoise(Time.deltaTime+(vertices[i].x*scaleX), Time.deltaTime+(vertices[i].y*scaleY))) - (Mathf.PerlinNoise(Time.deltaTime+(vertices[i].x*invertscaleX), Time.deltaTime+(vertices[i].y*invertscaleY)));
    33.  
    34.                 }
    35.  
    36.  
    37.         mesh.vertices = vertices;
    38.         mesh.RecalculateBounds ();
    39.         mesh.RecalculateNormals ();
    40.  
    41.  
    42.     }
    43. }
    44.  

    thanks for help
     
    WorldWideGlide likes this.
  2. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    429
    Store the original positions in Start() then add in update

    Code (csharp):
    1. Vector3[] vertPositions;
    2. void Start()
    3. {
    4.     GameObject visage = GameObject.Find ("daniel tete/Sph__re_1_3");
    5.     vertPositions = visage.GetComponent<MeshFilter> ().mesh.vertices;  
    6. }
    7.  
    8. void Update()
    9. {
    10.     for (i=0; i<vertices.Length; i++)
    11.     {
    12.         vertices[i].x = vertPositions[i].x + (Mathf.PerlinNoise(Time.deltaTime+(vertPositions[i].x*scaleX), Time.deltaTime+(vertPositions[i].y*scaleY))) - (Mathf.PerlinNoise(Time.deltaTime+(vertPositions[i].x*invertscaleX), Time.deltaTime+(vertPositions[i].y*invertscaleY)));
    13.     }
    14. }
     
  3. crasse

    crasse

    Joined:
    Mar 28, 2014
    Posts:
    23
    perfect ! that works, thanks you !