Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Wiebren-de-Haan · Nov 05, 2015 at 03:07 PM · meshproceduralprocedural meshprocedural generationprocedural-generation

C# Proceducal Mesh terrain

Hello guys,

I am trying to create a proceducal terrain. I created a script that should work (I my head, not in unity). What I want is just a lot of faces next to each other. Just like a plane in Unity.

This is the code that i have now: // Create terrain mesh Mesh mesh = GetComponent().mesh;

         for (int _x = 0; _x < chunkSize; _x++)
         {
             for (int _z= 0; _z < chunkSize; _z++)
             {
                 
                 vertices.Add(new Vector3(_x, 0, _z));
                 vertices.Add(new Vector3(_x + 1, 0, _z));
                 vertices.Add(new Vector3(_x + 1, 0, _z + 1));
                 vertices.Add(new Vector3(_x, 0, _z + 1));
 
                 triangles.AddRange
                 (
                     new int[]
                     {
                         3 + (_z * 4), 1 + (_z * 4), 0 + (_z * 4),
                         3 + (_z * 4), 2 + (_z * 4), 1 + (_z * 4)
                     }
                 );
             }
         }
 
 
         // Apply data to the mesh
         mesh.Clear();
         mesh.vertices = vertices.ToArray();
         mesh.triangles = triangles.ToArray();
         mesh.Optimize();
         mesh.RecalculateNormals();

But it only makes one line of faces: https://gyazo.com/266ffbcaa953bf711255713f94e58604

Does anyone know what the problem is?

Thanks in advance, Wiebren de Haan.

Comment
Add comment · Show 2
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 Cherno · Nov 05, 2015 at 03:59 PM 0
Share

Have you tried inseting Debug.Log lines to check the x,z values that are caluclated? The problem might lie in the way you are assigning triangles.

There's a plane generation script on the User Wiki you can use as reference:

link

avatar image Wiebren-de-Haan Cherno · Nov 05, 2015 at 05:31 PM 0
Share

You were right, the problem was with the triangles. Thanks!

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Nov 05, 2015 at 05:57 PM

Since you seem to create seperate quads which doesn't share vertices with other quads the triangle generation would simply be:

 int size = (chunkSize-1) * (chunkSize-1);
 triangles = new int[size*6];
 for (int i = 0; i < size; i++)
 {
     triangles[i*6 + 0] = i*4 + 3;
     triangles[i*6 + 1] = i*4 + 1;
     triangles[i*6 + 2] = i*4 + 0;

     triangles[i*6 + 3] = i*4 + 3;
     triangles[i*6 + 4] = i*4 + 2;
     triangles[i*6 + 5] = i*4 + 1;
 }

You don't need to use Lists in your case since you know the required size beforehand. Using Lists will just create additional garbage, especially your temp arrays when creating the triangles.

Usually you would create a plane with shared vertices. That will reduce the required vertices by quite a bit (4 times less vertices). Since Unity has a Vertex limit of 65000 vertices per mesh that's quite a difference. With single quads like in your code you can create a plane with 127x127 subquads. With shared verices about 254x254 Of course the mesh is one big shared quad so UVs and normals are continous across the mesh. If you need / want to assign individual UVs to a single subquad you can't share all vertices.

 Vector3[] vertices = new Vector3[chunkSize * chunkSize];
 Vector2[] uvs = new Vector2[vertices.Length];
 int size = (chunkSize-1) * (chunkSize-1); // sub quad count
 int[] triangles = new int[size * 6]; // 6 indices per quad
 
 for (int _z= 0; _z < chunkSize; _z++)
 {
     for (int _x = 0; _x < chunkSize; _x++)
     {
         vertices[_z*chunkSize + _x] = new Vector3(_x, 0f, _z);
         uvs[_z*chunkSize + _x] = new Vector2(_x, _z) / (chunkSize-1);
     }
 }
 for (int i = 0; i < size; i++)
 {
     triangles[i*6 + 0] = i;
     triangles[i*6 + 1] = i + chunkSize;
     triangles[i*6 + 2] = i + 1;

     triangles[i*6 + 3] = i + chunkSize;
     triangles[i*6 + 4] = i + chunkSize + 1;
     triangles[i*6 + 5] = i + 1;
 }
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 Inxhaine · Nov 06, 2015 at 02:28 PM

I will recommend taking a look on the following site:

http://jayelinda.com/

It has some excellent tutorials on procedural meshes in Unity....including terrain

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

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

36 People are following this question.

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

Related Questions

What is wrong with this mesh editing code? 0 Answers

UV problem on procedural mesh generation 1 Answer

Procedurally generate 3D mesh from 2D image 1 Answer

Procedural Mesh From Random List Of Veritces 2 Answers

Best Solution For Procedural Match-3 Board? 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