• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by IsaiahKelly · Sep 22, 2011 at 09:17 PM · proceduralmesh-deformationperlinrock

procedurally generated rocks

I'd like to create random rock shapes and the best way to do this seems to be with perlin noise on a mesh. I've started using the crumple mesh modifier example as a starting point, but can't for the life of me figure out how to modify it to create a better rock style effect.

The main problem is that the default code creates very jagged creases on the mesh when you turn up the scale that are just unacceptable and ugly. I've tried changing some of the values inside the code to see how it works, but it's not helping me understand it any better. Could anyone here help a newb like myself understand how it might be modified to work better for my purpose or should I use something else entirely? I'm very new to all this and math is not a strong point for me.

Thanks!

Here is the Perlin2.cs script I'm using:

 using System.Collections;
 
 using System;
 
 using UnityEngine; 
 
 public class Perlin2
 
 {
 
     const int B = 0x100;
 
     const int BM = 0xff;
 
     const int N = 0x1000;
 
 
 
     int[] p = new int[B + B + 2];
 
     float[,] g3 = new float [B + B + 2 , 3];
 
     float[,] g2 = new float[B + B + 2,2];
 
     float[] g1 = new float[B + B + 2];
 
 
 
     float s_curve(float t)
 
     {
 
         return t * t * (3.5F - 2.5F * t);
 
     }
 
     
 
     float lerp (float t, float a, float b)
 
     { 
 
         return a + t * (b - a);
 
     }
 
 
 
     void setup (float value, out int b0, out int b1, out float r0, out float r1)
 
     { 
 
         float t = value + N;
 
         b0 = ((int)t) & BM;
 
         b1 = (b0+1) & BM;
 
         r0 = t - (int)t;
 
         r1 = r0 - 1.0F;
 
     }
 
     
 
     float at2(float rx, float ry, float x, float y) { return rx * x + ry * y; }
 
     float at3(float rx, float ry, float rz, float x, float y, float z) { return rx * x + ry * y + rz * z; }
 
 
 
     public float Noise(float arg)
 
     {
 
         int bx0, bx1;
 
         float rx0, rx1, sx, u, v;
 
         setup(arg, out bx0, out bx1, out rx0, out rx1);
 
         
 
         sx = s_curve(rx0);
 
         u = rx0 * g1[ p[ bx0 ] ];
 
         v = rx1 * g1[ p[ bx1 ] ];
 
         
 
         return(lerp(sx, u, v));
 
     }
 
 
 
     public float Noise(float x, float y)
 
     {
 
         int bx0, bx1, by0, by1, b00, b10, b01, b11;
 
         float rx0, rx1, ry0, ry1, sx, sy, a, b, u, v;
 
         int i, j;
 
         
 
         setup(x, out bx0, out bx1, out rx0, out rx1);
 
         setup(y, out by0, out by1, out ry0, out ry1);
 
         
 
         i = p[ bx0 ];
 
         j = p[ bx1 ];
 
         
 
         b00 = p[ i + by0 ];
 
         b10 = p[ j + by0 ];
 
         b01 = p[ i + by1 ];
 
         b11 = p[ j + by1 ];
 
         
 
         sx = s_curve(rx0);
 
         sy = s_curve(ry0);
 
         
 
         u = at2(rx0,ry0, g2[ b00, 0 ], g2[ b00, 1 ]);
 
         v = at2(rx1,ry0, g2[ b10, 0 ], g2[ b10, 1 ]);
 
         a = lerp(sx, u, v);
 
         
 
         u = at2(rx0,ry1, g2[ b01, 0 ], g2[ b01, 1 ]);
 
         v = at2(rx1,ry1, g2[ b11, 0 ], g2[ b11, 1 ]);
 
         b = lerp(sx, u, v);
 
         
 
         return lerp(sy, a, b);
 
     }
 
     
 
     public float Noise(float x, float y, float z)
 
     {
 
         int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
 
         float rx0, rx1, ry0, ry1, rz0, rz1, sy, sz, a, b, c, d, t, u, v;
 
         int i, j;
 
         
 
         setup(x, out bx0, out bx1, out rx0, out rx1);
 
         setup(y, out by0, out by1, out ry0, out ry1);
 
         setup(z, out bz0, out bz1, out rz0, out rz1);
 
         
 
         i = p[ bx0 ];
 
         j = p[ bx1 ];
 
         
 
         b00 = p[ i + by0 ];
 
         b10 = p[ j + by0 ];
 
         b01 = p[ i + by1 ];
 
         b11 = p[ j + by1 ];
 
         
 
         t  = s_curve(rx0);
 
         sy = s_curve(ry0);
 
         sz = s_curve(rz0);
 
         
 
         u = at3(rx0,ry0,rz0, g3[ b00 + bz0, 0 ], g3[ b00 + bz0, 1 ], g3[ b00 + bz0, 2 ]);
 
         v = at3(rx1,ry0,rz0, g3[ b10 + bz0, 0 ], g3[ b10 + bz0, 1 ], g3[ b10 + bz0, 2 ]);
 
         a = lerp(t, u, v);
 
         
 
         u = at3(rx0,ry1,rz0, g3[ b01 + bz0, 0 ], g3[ b01 + bz0, 1 ], g3[ b01 + bz0, 2 ]);
 
         v = at3(rx1,ry1,rz0, g3[ b11 + bz0, 0 ], g3[ b11 + bz0, 1 ], g3[ b11 + bz0, 2 ]);
 
         b = lerp(t, u, v);
 
         
 
         c = lerp(sy, a, b);
 
         
 
         u = at3(rx0,ry0,rz1, g3[ b00 + bz1, 0 ], g3[ b00 + bz1, 2 ], g3[ b00 + bz1, 2 ]);
 
         v = at3(rx1,ry0,rz1, g3[ b10 + bz1, 0 ], g3[ b10 + bz1, 1 ], g3[ b10 + bz1, 2 ]);
 
         a = lerp(t, u, v);
 
         
 
         u = at3(rx0,ry1,rz1, g3[ b01 + bz1, 0 ], g3[ b01 + bz1, 1 ], g3[ b01 + bz1, 2 ]);
 
         v = at3(rx1,ry1,rz1,g3[ b11 + bz1, 0 ], g3[ b11 + bz1, 1 ], g3[ b11 + bz1, 2 ]);
 
         b = lerp(t, u, v);
 
         
 
         d = lerp(sy, a, b);
 
         
 
         return lerp(sz, c, d);
 
     }
 
     
 
     void normalize2(ref float x, ref float y)
 
     {
 
        float s;
 
     
 
         s = (float)Math.Sqrt(x * x + y * y);
 
         x = y / s;
 
         y = y / s;
 
     }
 
     
 
     void normalize3(ref float x, ref float y, ref float z)
 
     {
 
         float s;
 
         s = (float)Math.Sqrt(x * x + y * y + z * z);
 
         x = y / s;
 
         y = y / s;
 
         z = z / s;
 
     }
 
     
 
     public Perlin2()
 
     {
 
         int i, j, k;
 
         System.Random rnd = new System.Random();
 
     
 
        for (i = 0 ; i < B ; i++) {
 
           p[i] = i;
 
           g1[i] = (float)(rnd.Next(B + B) - B) / B;
 
     
 
           for (j = 0 ; j < 2 ; j++)
 
              g2[i,j] = (float)(rnd.Next(B + B) - B) / B;
 
           normalize2(ref g2[i, 0], ref g2[i, 1]);
 
     
 
           for (j = 0 ; j < 3 ; j++)
 
              g3[i,j] = (float)(rnd.Next(B + B) - B) / B;
 
              
 
     
 
           normalize3(ref g3[i, 0], ref g3[i, 1], ref g3[i, 2]);
 
        }
 
     
 
        while (--i != 0) {
 
           k = p[i];
 
           p[i] = p[j = rnd.Next(B)];
 
           p[j] = k;
 
        }
 
     
 
        for (i = 0 ; i < B + 2 ; i++) {
 
           p[B + i] = p[i];
 
           g1[B + i] = g1[i];
 
           for (j = 0 ; j < 2 ; j++)
 
              g2[B + i,j] = g2[i,j];
 
           for (j = 0 ; j < 3 ; j++)
 
              g3[B + i,j] = g3[i,j];
 
        }
 
     }
 
 }

And this is the code to apply it to the mesh:

 function Deform ()
 {
     noise = new Perlin2 ();
     
     var mesh : Mesh = GetComponent(MeshFilter).mesh;
     
     if (baseVertices == null)
         baseVertices = mesh.vertices;
         
     var vertices = new Vector3[baseVertices.Length];
     
     for (var i=0;i<vertices.Length;i++)
     {
         var vertex = baseVertices[i];
                 
         vertex.x += noise.Noise( vertex.x, vertex.y, vertex.z ) * deformScale;
         vertex.y += noise.Noise( vertex.x, vertex.y, vertex.z ) * deformScale;
         vertex.z += noise.Noise( vertex.x, vertex.y, vertex.z ) * deformScale;
         
         vertices[i] = vertex;
     }
     
     mesh.vertices = vertices;
 }
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Mushy Rigidbody 1 Answer

Adding vertices to procedural mesh generator 2 Answers

shader based on surface distance from center ? 2 Answers

Advice on lavaflow (minecraft like) 2 Answers

how do i procedurally generated perlin noise infinitely c# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges