Search Unity

Procedurally deforming a subdiv sphere with perlin noise

Discussion in 'Scripting' started by Wildpepper, Mar 19, 2012.

  1. Wildpepper

    Wildpepper

    Joined:
    Dec 15, 2009
    Posts:
    30
    Hi !
    I've been playing around with the procedural examples, eagerly searching for something to read along with the examples.

    So, after reading the CrumpleMesh.js which appears to be quite straightforward, I tried to apply it to a more detailed subdiv mesh i modeled, and the strange thing, is that it wobbles around the screen randomly.
    would look like some kind of pivot misplacement, but I also tried resetting it, and transformations, both in my modelling program, and in unity, without luck.

    Here's visually what happens, sphere on the left, is the example one, the one on the right is the one i modeled, and to which I applied the same script.


    Uploaded with ImageShack.us

    And here's what happens when i hit play: the second sphere is squashed, and floating around, while the first correctly deforms.


    Uploaded with ImageShack.us

    Here's the code i'm using
    Code (csharp):
    1. // This script is placed in public domain. The author takes no responsibility for any possible harm.
    2.  
    3. var scale = 1.0;
    4. var speed = 1.0;
    5. var recalculateNormals = false;
    6.  
    7. private var baseVertices : Vector3[];
    8. private var noise : Perlin;
    9.  
    10. function Start ()
    11. {
    12.     noise = new Perlin ();
    13. }
    14.  
    15. function Update () {
    16.     var mesh : Mesh = GetComponent(MeshFilter).mesh;
    17.    
    18.     if (baseVertices == null)
    19.         baseVertices = mesh.vertices;
    20.        
    21.     var vertices = new Vector3[baseVertices.Length];
    22.    
    23.     var timex = Time.time * speed + 0.1365143;
    24.     var timey = Time.time * speed + 1.21688;
    25.     var timez = Time.time * speed + 2.5564;
    26.     for (var i=0;i<vertices.Length;i++)
    27.     {
    28.         var vertex = baseVertices[i];
    29.                
    30.         vertex.x += noise.Noise(timex + vertex.x, timex + vertex.y, timex + vertex.z) * scale;
    31.         vertex.y += noise.Noise(timey + vertex.x, timey + vertex.y, timey + vertex.z) * scale;
    32.         vertex.z += noise.Noise(timez + vertex.x, timez + vertex.y, timez + vertex.z) * scale;
    33.        
    34.         vertices[i] = vertex;
    35.     }
    36.    
    37.     mesh.vertices = vertices;
    38.    
    39.     if (recalculateNormals)
    40.         mesh.RecalculateNormals();
    41.     mesh.RecalculateBounds();
    42. }
    Thank you for your time, if anybody can point me to a simpler approach to perlin noise deformation on an arbitrary mesh, i would really appreciate that.

    have a good day !
     
  2. Wildpepper

    Wildpepper

    Joined:
    Dec 15, 2009
    Posts:
    30
    silly me :)

    appearently, the problem solved itself, when I set the scale factor, in the import options of my model, to 1 instead of 0.1
    hope this helps people with my same issue, but post is still open for suggestions on where to read something more about procedural approach to unity.
    have a good day !
     
  3. Marcelo-Viana

    Marcelo-Viana

    Joined:
    Jun 29, 2013
    Posts:
    11
    I was having the same issue, thanks for sharing a solution!
     
  4. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Sorry to wake up an old thread, But I am looking to do something similar, only I wish to make one offset (for spherical terrain generation) then be done with it. how would I go about doing this sort of thing? I also wish to have this done with a texture, so I can feed said texture into a shader for some nice special fx work. Oh, and I am doing this so that the mesh collider can be then updated with the new mesh.