I have two functions in the script that i generate the vertices the triangles normals and uv. If i call in the start function to the function Generate it will work fine and will show a mesh with two triangles. I can change the mesh size using the variables width and height.
But now i want to use the function GenerateOrigin.
So if i set for example the width and height both to 16. Then when creating the vertices array i have 298 vertices.
Then i loop over them. The problem is i don’t know how to loop over the triangles and normals and uv. Using GenerateOrigin i want first to see only 289 points when running the game. 289 vertices.
Then somehow i want to make in each 4 vertices two triangles.
But i don’t see anything when using the GenerateOrigin. Even if i set only the vertices in to the mesh: mesh.vertices = vertices; i don’t see the vertices. Why i can’t see the vertices when using the GenerateOrigin ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grid : MonoBehaviour
{
public int width;
public int height;
private MeshFilter meshf;
private Mesh mesh;
private void Start()
{
meshf = GetComponent<MeshFilter>();
mesh = new Mesh();
meshf.mesh = mesh;
Generate();
}
private void GenerateOrigin()
{
//Vertices
Vector3[] vertices = new Vector3[(width + 1) * (height + 1)];
for (int i = 0, y = 0; y <= height; y++)
{
for (int x = 0; x <= width; x++, i++)
{
vertices *= new Vector3(x, y);*
}
}
//Triangles
int[] tri = new int[6];
{
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
}
//Normals (Display object and set direction)
Vector3[] normals = new Vector3[(width + 1) * (height + 1)];
{
for (int i = 0; i < normals.Length; i++)
{
normals = Vector3.forward;
}
}
//UVs (How textue will display)
Vector2[] uv = new Vector2[(width + 1) * (height + 1)];
{
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);
}
//Assgind Arrays
mesh.vertices = vertices;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = uv;
}
private void Generate()
{
//Vertices
Vector3[] vertices = new Vector3[4];
{
vertices[0] = new Vector3(0, 0, 0);
vertices[1] = new Vector3(width, 0, 0);
vertices[2] = new Vector3(0, height, 0);
vertices[3] = new Vector3(width, height, 0);
}
//Triangles
int[] tri = new int[6];
{
tri[0] = 0;
tri[1] = 2;
tri[2] = 1;
tri[3] = 2;
tri[4] = 3;
tri[5] = 1;
}
//Normals (Display object and set direction)
Vector3[] normals = new Vector3[4];
{
normals[1] = Vector3.forward;
normals[2] = Vector3.forward;
normals[3] = Vector3.forward;
}
//UVs (How textue will display)
Vector2[] uv = new Vector2[4];
{
uv[0] = new Vector2(0, 0);
uv[1] = new Vector2(1, 0);
uv[2] = new Vector2(0, 1);
uv[3] = new Vector2(1, 1);
}
//Assgind Arrays
mesh.vertices = vertices;
mesh.triangles = tri;
mesh.normals = normals;
mesh.uv = uv;
}
}