Primitive Mesh Creation (square)

So, what I learned since a few days ago is a simple creation of square, which consists of only 4 vertices. Another understanding I could come out with is, how a texturable mesh could simply be product of vertices, triangles, normals, and uv information. At least this is what unity UnityEngine.Mesh really is.

So, i'll try to in detail lay the explanation upon all these components. The example with be made with

Vertices

The coordinate(s) of the mesh's vertices in 3d space.

Vector3 center = transform.position;
Vector3 topLeft = new Vector3(center.x - Width / 2, center.y + Height / 2);
Vector3 topRight = new Vector3(center.x + Width / 2, center.y + Height / 2);
Vector3 bottomLeft = new Vector3(center.x - Width / 2, center.y - Height / 2);
Vector3 bottomRight = new Vector3(center.x + Width / 2, center.y - Height / 2);

mesh.vertices = new Vector3[4] {
    topLeft, topRight, bottomLeft, bottomRight
};

The only information we could know in order to make up a square, is the position of where it'll be placed (using the object transform information the script is attached to), and the width and height that we define for the square.


In unity, this information is stored in an array of Vector3, under mesh.vertices.

The vertices information alone isn't enough to have them properly rendered. Like, a 3d object need to have a 'face' to explain it shape. And, of what I learn, a game engine usually works with triangle(s).

Triangles

The information about triangles of the mesh. So, in unity this information is stored in an array of indices of the vertices. Since a triangle is simply made up of 3 vertices. The length of this array would be triple (3X) of how many triangles you want it to be for a mesh. If there're only 2 triangles, the length will be 6.

int[] triangleVertexIndices = new int[6];

// first triangle
triangleVertexIndices[0] = 0; // topLeft
triangleVertexIndices[1] = 1; // topRight
triangleVertexIndices[2] = 2; // bottomLeft

// second triangle
triangleVertexIndices[3] = 1; // topLeft
triangleVertexIndices[4] = 3; // bottomRight
triangleVertexIndices[5] = 2; // bottomLeft

mesh.triangles = triangleVertexIndices;

Take note that the order of the vertices must be in clockwise.

Next information a mesh needed to properly be displayed by a game engine is the normals.

Normals

So, normals is basically the perpendicular information needed for the lightning component to shadefully display a mesh. For more information wikipedia source itself. So, unity stores the information in an array of directions (Vector3).

The size of this array would equal to the size of the vertices.

mesh.normals = new Vector3[4] {
    -Vector3.forward, -Vector3.forward, -Vector3.forward, -Vector3.forward
};

So, the last one will be the uv information of a mesh.

UV

It's an information of how an texture or image should be projected to the mesh, or how to map the texture to the vertices of the mesh. And in unity, the information is stored as an array of uv coordinates (Vector2), with the size equal to the size of the vertices.

mesh.uv = new Vector2[4] {
    new Vector2(0, 1),
    new Vector2(1, 1),
    new Vector2(0, 0),
    new Vector2(1, 0)
};

Imagine U as the X, and V as the Y axis of a 2D plane, except that the scalar value here represent the rate for how much of the texture should be projected at that point of vertex, with 1 being 100% and 0 being 0%.

So, I think that's all i think we need for a very simple mesh, which I believe is the same of how they all work.

More readings :
http://schemingdeveloper.com/2014/10/17/better-method-recalculate-normals-unity/
https://www.youtube.com/watch?v=R_kV3YiJqEw&t=623s
https://docs.unity3d.com/Manual/AnatomyofaMesh.html
https://docs.unity3d.com/Manual/PrimitiveObjects.html

Comments

Popular posts from this blog

Mesh Deformation / (Re-)Construction

Project Vector