- Home /
Question by
$$anonymous$$ · Oct 30, 2018 at 07:14 PM ·
c#scripting problemmeshmesh verticesmesh-deformation
how do i fix how i make triangels in my mesh
[RequireComponent(typeof(MeshFilter))]
public class MeshGenerator : MonoBehaviour
{
Mesh mesh;
//int[] triangles;
//Vector3[] vertices;
List<Vector3> vertices;
List<int> triangles;
public int y;
public int x;
public int z;
int totalVertices;
int currentY;
int currentX;
int currentZ;
int point;
public bool updateMesh;
void Start ()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
totalVertices = y * x;
CreateShape();
UpdateMesh();
}
void CreateShape()
{
vertices = new List<Vector3>(totalVertices);
for (int i = 0; i < totalVertices; i++)
{
//currentZ = Random.Range(0, 10);
vertices.Add(new Vector3(currentX, currentZ, currentY));
if(currentX < x - 1)
{
currentX++;
}else
if(currentY < y - 1)
{
currentY++;
currentX = 0;
}
}
triangles = new List<int>(0);
for (int i = 0; i < totalVertices - x; i++)
{
triangles.Add(i);
triangles.Add(i + x);
triangles.Add(i + 1);
}
for (int i = 0; i < totalVertices - x; i++)
{
triangles.Add(i);
triangles.Add(i + x - 1);
triangles.Add(i + x);
}
}
void UpdateMesh()
{
mesh.Clear();
Vector3[] verticesArray;
verticesArray = vertices.ToArray();
int[] trianglesArray;
trianglesArray = triangles.ToArray();
mesh.vertices = verticesArray;
mesh.triangles = trianglesArray;
mesh.RecalculateNormals();
}
}
there is something wrong with how i make the triangles but i dont know how to fix it
Comment
This page explains a quad, maybe that helps you finding out what's wrong with your triangels:
https://docs.unity3d.com/$$anonymous$$anual/Example-CreatingaBillboardPlane.html
Your answer
Follow this Question
Related Questions
Splitting shared vertices in a plane 0 Answers
How can i create a Plane using this Mesh generator script ? 0 Answers
Why is my vertex array empty using GetComponent().mesh.vertices? 3 Answers
Why is my job running slowly the first few frames then speeding up? 1 Answer
How to detect when two colliding objects create a corner? 1 Answer