- Home /
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:
Other side:
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();
}
}
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.)
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