for (int i = 0, y = 0; y <= ySize; y++) { for (int x = 0; x <= xSize; x++, i++) { vertices [i] = new Vector3 (x, y); yieldreturn wait; } } mesh.vertices = vertices;
List<Vector3> grids = new List<Vector3> (); List<int> triangles = new List<int> ();
for (int y = 0; y <= ySize; y++) { for (int x = 0; x <= xSize; x++) { grids.Add (new Vector3 (x, y, 0)); } }
mesh = new Mesh (); mesh.vertices = grids.ToArray ();
for (int y = 0; y < ySize; y++) { for (int x = 0; x < xSize; x++) { int [] tri = newint [6];
tri [0] = grids.IndexOf (new Vector3 (x, y)); tri [1] = tri [4] = grids.IndexOf (new Vector3 (x, y + 1)); tri [2] = tri [3] = grids.IndexOf (new Vector3 (x + 1, y)); tri [5] = grids.IndexOf (new Vector3 (x + 1, y + 1));
for (int i = 0; i < tri.Length; i++) { triangles.Add (tri [i]); } } }
Normal maps are defined in tangent space. This is a 3D space that flows around the surface of an object. This approach allows us to apply the same normal map in different places and orientations.
The surface normal represents upward in this space, but which way is right? That’s defined by the tangent. Ideally, the angle between these two vectors is 90°. The cross product of them yields the third direction needed to define 3D space. In reality the angle is often not 90° but the results are still good enough.
So a tangent is a 3D vector, but Unity actually uses a 4D vector. Its fourth component is always either −1 or 1, which is used to control the direction of the third tangent space dimension – either forward or backward. This facilitates mirroring of normal maps, which is often used in 3D models of things with bilateral symmetry, like people. The way Unity’s shaders perform this calculation requires us to use −1.