• 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 $$anonymous$$ · Mar 29, 2014 at 02:39 PM · c#meshlistproceduralgenerated

Procedural generated mesh problem

I'm trying to generate at runtime a simple mesh that is composed by many quads. The problem I'm facing is to assign the triangles for each quad. Note that I'm trying to make the mesh as one unique mesh and not many separated quads. There is no problem in generating the vertices and uvs, but I get an out of bounds triangle error. This is the full script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class TestMesh : MonoBehaviour {
 
     private List<Vector3> verts = new List<Vector3>();
     private List<Vector2> uvs = new List<Vector2>();
     private List<int> tris = new List<int>();
 
     public int worldWidth = 10;
     public int worldHeight = 10;
 
     // Template map
     private int[,] worldMap = new int[,]
     {
         {1,1,1,1,1,1,1,1,1,1},
         {1,0,0,0,0,0,0,0,0,1},
         {1,0,0,0,0,0,0,0,0,1},
         {1,0,0,0,0,0,0,0,0,1},
         {1,0,0,0,0,0,0,0,0,1},
         {1,0,0,0,0,0,0,0,0,1},
         {1,0,0,0,0,0,0,0,0,1},
         {1,0,0,0,0,0,0,0,0,1},
         {1,0,0,0,0,0,0,0,0,1},
         {1,0,0,0,0,0,0,0,0,1}
     };
 
     // Use this for initialization
     void Start () {
         CreateWorld ();
     }
     
     // Assign a new gameobject as mesh container and prep the mesh
     void CreateWorld () {
         verts.Clear ();
         uvs.Clear ();
         tris.Clear ();
 
         GameObject o = new GameObject ("WORLD_MESH");
         
         Mesh mesh = new Mesh ();
         
         if (!o.GetComponent<MeshFilter> ())
             o.AddComponent<MeshFilter> ();
         
         mesh = o.GetComponent<MeshFilter> ().mesh;
 
         Vector3 p = Vector3.zero;
         int i = 0;
 
         for(int x = 0; x < worldWidth - 1; x++) {
             for(int y = 0; y < worldHeight - 1; y++) {
                 p = new Vector3(x * 1,y * 1, 0);
 
                 if(worldMap[x, y] != 0)
                     AddMeshElements(p, i);
 
                 i += 6;
             }
         }
 
         Vector3[] vertices = verts.ToArray ();
         Vector2[] uv = uvs.ToArray ();
         int[] triangles = tris.ToArray ();
 
         mesh.vertices = vertices;
         mesh.uv = uv;
         mesh.triangles = triangles;
         
         if (!o.GetComponent<MeshRenderer> ())
             o.AddComponent<MeshRenderer> ();
         
         MeshRenderer mr = o.GetComponent<MeshRenderer> ();
         mr.castShadows = mr.receiveShadows = false;
 
         mesh.RecalculateBounds ();
     }
 
     // Add mesh elements to the Lists
     void AddMeshElements (Vector3 p, int i) {
 
         verts.Add (new Vector3 (0.5f + p.x, 0.5f + p.y, p.z));
         verts.Add (new Vector3( 0.5f + p.x, -0.5f + p.y, p.z));
         verts.Add (new Vector3(-0.5f + p.x, 0.5f + p.y,  p.z));
         verts.Add (new Vector3(-0.5f + p.x, -0.5f + p.y, p.z));
 
         uvs.Add (new Vector2 (1, 1));
         uvs.Add (new Vector2 (1, 0));
         uvs.Add (new Vector2 (0, 1));
         uvs.Add (new Vector2 (0, 0));
 
         tris.Add (i);
         tris.Add (i + 1);
         tris.Add (i + 2);
         tris.Add (i + 2);
         tris.Add (i + 1);
         tris.Add (i + 3);
     }
 }

As you can see I try to generate quads from a 2d array skipping the values equal to 0. No problem here, the problem I think is on the number of triangles, but I tried to debug it by checking the tris List as public and everything seemed ok.

I'm skipping something simple for sure, but can't see the problem. Any help is appreciated.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by pako · Mar 30, 2014 at 11:21 AM

The problem is in line 59: i += 6; Change it to i += 4;

With i += 6, The first two triangles corresponding to the first quad, include vertices [0, 1, 2, 2, 1, 3]. So far. so good, because your vertices have indices 0 to 3. However, when you do the second quad, the two triangles corresponding to that (with i = 6 now) are [6, 7, 8, 8, 7, 9], oops! out of bounds error, because you only have 8 vertices with indices 0 to 7. There's no vertex with index 8 and 9 that you are referencing in your triangles array. Changing line 59 to i += 4 should fix that.

Comment
$$anonymous$$
giga1992

People who like this

2 Show 1 · 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 $$anonymous$$ · Mar 30, 2014 at 12:19 PM 0
Share

I feel like a dumbass, I knew it was something simple but not that much ahah. Thanks for your help.

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

21 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

Related Questions

How can I add complex mesh data directly to a list through code? 1 Answer

A node in a childnode? 1 Answer

Procedural array of meshes problem / solved 1 Answer

Do I need to split my mesh up to properly UV texture it? 1 Answer

Distribute terrain in zones 3 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