• 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 Ellpower · Oct 11, 2013 at 02:34 PM · meshplaneverticestriangles

Plane: vertices and triangles

I want to create a plane with n x 1 quads (ex: 10 x 1 quads). I'm able to do so but half of the triangles are showing on one side and the other half on the other side.

First side:

alt text

Other side:

alt text

How can i make it so all triangles are showing on the same side?

Here is the code i drop on the plane showed above:

 using UnityEngine;
 using System.Collections;
 
 public class test : MonoBehaviour {
     
     public int rectanglesCount = 200;
     
     private Vector3[] _vertices;
     private int[] _triangles;
     
     private Mesh _mesh;
     private Vector3 _size;
     
     void Start ()
     {
         _vertices = new Vector3[(rectanglesCount * 2) + 2];
         _triangles = new int[(rectanglesCount * 6)];
         
         _mesh = ((MeshFilter) GetComponent(typeof(MeshFilter))).mesh;
         _size = collider.bounds.size;
         
         InitiateRectangles();
     }
     
     private void InitiateRectangles()
     {
         _mesh.Clear();
         
         // vertices
         
         /* 
            order
            0 --- 2
            |     |
            |     |
            |     |
            1 --- 3
         */
         
         float xSpace = _size.x / rectanglesCount;
         float x = 0;
         bool top = true;
         
         x -= _size.y / 2;
         
         for(int i = 0; i < _vertices.Length; i++)
         {
             _vertices[i].x = x;
             _vertices[i].z = 0.01f;
             
             if (top)
                 _vertices[i].z = _size.y / 2;
             else
             {
                 _vertices[i].z = - _size.y / 2;
                 
                 x += xSpace;
             }
                 
             top = !top;
         }
         
         top = false;
         
         // triangles
         
         /*
          triangles[0] = 0;
         _triangles[1] = 1;
         _triangles[2] = 2;
         
         _triangles[3] = 1;
         _triangles[4] = 2;
         _triangles[5] = 3;
         
         _triangles[6] = 2;
         _triangles[7] = 3;
         _triangles[8] = 4;
         */
         
         int index = 0;
         
         for(int i = 0; i < (_triangles.Length / 3); i ++)
         {
             index = i * 3;
             
             _triangles[index] = i;
             _triangles[index + 1] = i + 1;
             _triangles[index + 2] = i + 2;
         }
         
         _mesh.vertices = _vertices;
         _mesh.triangles = _triangles;
         
         _mesh.RecalculateNormals();
         _mesh.RecalculateBounds();
     }
 }
 

capture.png (31.3 kB)
capture2.png (40.7 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Graham-Dunnett · Oct 11, 2013 at 02:50 PM

The order of the vertices in the _triangle array needs to be consistent. Either they all need to be clockwise or they all need to be anti-clockwise. So, use 012 and 132. (Also, I don't understand why you have 9 indices in your comment.)

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
1

Answer by DaveA · Oct 11, 2013 at 02:52 PM

Your triangle 'winding' is wrong for half the faces. Either change your vertex order to be continuously clockwise or counterclockwise (currently cuts through the center), or change your triangle indices to make sure they are always going clockwise or counterclockwise

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

Best Way to Link Vertices into Mesh 0 Answers

Null Reference Exception Assigning Vertices to Mesh 0 Answers

Best way to optimize mesh updating through code? 1 Answer

Mesh triangles don't match wireframe view? 0 Answers

Unidirectional slicing algorithm of a mesh 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges