Search Unity

PerlinNoise on a mesh

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

  1. crasse

    crasse

    Joined:
    Mar 28, 2014
    Posts:
    23
    Hi there ! :)

    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


    edit : oops i made a mistake, i should have posted it in the script forum not the support one i think, sorry about that, is there a way i can move it there ?
     
    Last edited: Apr 23, 2014