Search Unity

  1. Check out our Unite Austin 2017 YouTube playlist to catch up on what you missed. More videos coming soon.
    Dismiss Notice
  2. Unity 2017.2 is now released.
    Dismiss Notice
  3. The Unity Gear Store is here to help you look great at your next meetup, user group or conference. With all new Unity apparel, stickers and more!
    Dismiss Notice
  4. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice
  6. Unity 2017.3 beta is now available for download.
    Dismiss Notice

Procedurally deforming a subdiv sphere with perlin noise

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

  1. Wildpepper


    Dec 15, 2009
    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

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

    Uploaded with

    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.
    3. var scale = 1.0;
    4. var speed = 1.0;
    5. var recalculateNormals = false;
    7. private var baseVertices : Vector3[];
    8. private var noise : Perlin;
    10. function Start ()
    11. {
    12.     noise = new Perlin ();
    13. }
    15. function Update () {
    16.     var mesh : Mesh = GetComponent(MeshFilter).mesh;
    18.     if (baseVertices == null)
    19.         baseVertices = mesh.vertices;
    21.     var vertices = new Vector3[baseVertices.Length];
    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];
    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;
    34.         vertices[i] = vertex;
    35.     }
    37.     mesh.vertices = vertices;
    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


    Dec 15, 2009
    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


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


    Jun 8, 2013
    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.