EDIT: Question that might answer one of my questions: Is the mesh.vertices (not sure what this is called, perhaps a method of class Mesh()?) doing the initializing, and vertices is the variable that we are using to feed that (method of class Mesh?) it’s data?
I am following a mesh generation tutorial (https://youtu.be/64NblGkAabk?feature=shared), and I don’t understand how this bit of code is being used to define an area with a Vector3. As in, I understand that x * y = area of a quad, but I don’t understand how a vector3 is doing this, since my understanding is that a Vector3 is basically a position in space defined by (x, y, z).
I have read through all the comments in the video, and read through Vector3 in the Unity docs, but I can’t seem to find how this works. I am new to programming, so a more layman’s type explanation would be best. I also tried using a Debug.Log to get the values of the for loop incrementer, but it was returning numbers that just confused me more (i.e. Debug.Log(z) line 7 would go up only 1 time to Debug.Log(i) in line 11’s hundreds) with and xSize and zSize value of 20.
Declaration of new Vector3[index] variable called vertices (right?) and int[index] variable called triangles:
Vector3[] vertices;
int[] triangles;
The bit of code I don’t understand that defines our area and then generates vertices
void CreateShape()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
for (int i = 0, z = 0; z <= zSize; z++)
{
Debug.Log($"value of z is: {z}");
for (int x = 0; x <= xSize; x++)
{
Debug.Log($"Value of i is: {i}");
vertices[i] = new Vector3(x, 0, z);
i++;
}
}
triangles = new int[6];
triangles[0] = 0;
triangles[1] = xSize + 1;
triangles[2] = 1;
triangles[3] = 1;
triangles[4] = xSize + 1;
triangles[5] = xSize + 2;
}
The MeshUpdater() method
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
Specifically, I understand (maybe?) that this function is defining an area (xSize + 1) * (zSize +1), and then initializing vertices (vector3[i].vertices) left to right [0,0,0] → [xSize+1, 0, zSize+1], but I don’t understand how it knows to go to the next z position and continue initializing vertices, and I don’t understand the implementation of the area, since it doesn’t have any value for y, and it isn’t in the format of Vector3(x,y,z).
I would understand the area formatting if it was in the form of like area = Vector3(xSize+1,0,0) * Vector3(0,0,zSize+1). as that is 2 points in space being multiplied. It leaves me knowing I don’t understand what is going on.
I’m not sure that all makes sense, so let me know if I need to clarify or anything.