Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by TajamSoft · Jun 08, 2015 at 07:28 AM · meshrenderingprocedural

Generating unique vertices for a procedural mesh

I'm having an issue with a procedurally generated terrain (made from a pre-made plane), the vertices are shared, and I don't really know how to fix it...

As the vertices are shared it gives a smoothing effect, which looks really bad.

Here is the simple code for generating the terrain:

 void Start()
     {
         Mesh mesh = GetComponent<MeshFilter>().mesh;
         Vector3[] vertices = mesh.vertices;
         Vector3[] normals = mesh.normals;
         int i = 0;
         while (i < vertices.Length)
         {
             vertices[i] += normals[i] * Mathf.PerlinNoise(vertices[i].x, vertices[i].z);
             i++;
         }
         mesh.vertices = vertices;
     }

Thanks in advance!

Comment
Add comment · Show 1
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
avatar image Luiz_Thiago · Jan 20, 2016 at 12:48 PM 0
Share

Same question here... Do you resolve it?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Enum · Jun 08, 2015 at 09:31 AM

As the predefined plain mesh is not intended for such a usage, I think you will need to generate your own mesh. This will mean not much more effort for you, because you are already manipulating the vertices yourself. As long as one triangle has different normals at its corners, they will be interpolated per fragment. That's how the pipeline works.

Here is the link from Unity Manual that you might already know: GeneratingMeshGeometryProcedurally

It is really simple: Create vertices, create triangles by giving the indices of 3 vertices each and creating the normals.

Comment
Add comment · Share
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
avatar image
0

Answer by cjdev · Jan 21, 2016 at 03:54 AM

Even though this thread was resurrected a bit late I'll leave this here for anyone interested. To create a procedural plane useful for terrain generation it's easiest to iterate over the vertices and create quads of two tris each for every vertex. One way looks like this:

 public void CreateMesh(int size)
     {
         List<Vector3> verts = new List<Vector3>(); // Index used in tri list
         List<int> tris = new List<int>(); // Every 3 ints represents a triangle
         List<Vector2> uvs = new List<Vector2>(); // Vertex in 0-1 UV space
         for (int i = 0; i < size; i++)
         {
             for (int j = 0; j < size; j++)
             {
                 verts.Add(new Vector3(i, 0, j)); // Add noise function into y
                 uvs.Add(new Vector2((float)i / size, (float)j / size));
                 if (i == 0 || j == 0) continue; // First bottom and left skipped
                 tris.Add(size * i + j); //Top right
                 tris.Add(size * i + (j - 1)); //Bottom right
                 tris.Add(size * (i - 1) + (j - 1)); //Bottom left - First triangle
                 tris.Add(size * (i - 1) + (j - 1)); //Bottom left 
                 tris.Add(size * (i - 1) + j); //Top left
                 tris.Add(size * i + j); //Top right - Second triangle
             }
         }
         Mesh mesh = new Mesh();
         mesh.vertices = verts.ToArray();
         mesh.uv = uvs.ToArray();
         mesh.triangles = tris.ToArray();
         mesh.RecalculateNormals();
         
         GameObject terrain = new GameObject("Terrain");
         terrain.AddComponent<MeshFilter>();
         terrain.AddComponent<MeshRenderer>();
         terrain.GetComponent<MeshFilter>().mesh = mesh;
         // Load a material named "TerrainMat" from a folder named "Resources"
         Material terrainMat = Resources.Load<Material>("TerrainMat");
         terrain.GetComponent<Renderer>().material = terrainMat;
     }

The thing to remember about generating meshes procedurally is that the order of the vertices matters and the triangles have to be made clockwise. If you're interested in other applications of procedural meshes you can still use this method but switch out the i and j variables in the for loops or in the vertex declaration. Besides the terrain method using noise you can also get things like curves, circles, and even spheres.

Comment
Add comment · Show 2 · Share
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
avatar image Bunny83 · Jan 21, 2016 at 04:30 AM 0
Share

Uhm, you also generate shared vertices and not unique vertices per quad / triangle which was the question.

avatar image cjdev · Jan 21, 2016 at 05:26 AM 0
Share

If you needed the individual vertices per quad you'd only have to build the mesh with a size of 1 and loop over that, my point was just to show how to make the basic mesh.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shadow artifects on mesh with Standard shader 1 Answer

Seam on Procedural Mesh 1 Answer

how to make an horizontal mesh? c# 0 Answers

How to overlap tiles in procedural meshes? 1 Answer

Procedural model turns black up close 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