Unity3D Building Damage Tutorial- Part I - Mehes in Unity3D

Theory
Unity Script Reference - Mesh

Mesh (or reference to it) is stored in MeshFilter Component. I belive, but i am not sure that MeshFilter store one Meh at the time. An big objct, or prefab usually consists of several smaller child objects, of which every one has its distinct MeshFilter  and mesh.

Ok, a mesh object in Unity has three important variables: triangles, verticles and uv. Now, i'm going to center on first two, becoue i'm not implementing any texture for now. But before, I am going to make something clear. For example, lets take a simple primitive - a cube:



A fairly normal cube would have 8 verticles and 6 faces, so 12 triangles. But not in Unity. Here, the number of verticles is 24, three times the number of corners. In the same time, there is just 12 triangles. Why? Becouse, apparently unity3d is trying to assign 4 verticles to every face, and so 6*4 = 24 verticles. I belive this for making shadows, but i am not really sure. Anyway, the problem why Unity is doing that is not so important. The problem is that we should regard the 3 verticles in corner as one, but Unity is treating them like 3 verticles in the same position.

Now, lets look at mesh.verticles array. Here, it consists of 24 Vector3 objects. Each Vector3 is an verticle, and in Vector3.x ,Vector3.y and Vector3.z there are local position of variables. What mean that they are local? The coordinates are assigned accoring to mesh position. To count the overall position of verticle in global coordinates, you should add the Verticle3 to MeshFilter.transform.parent. Moreover, you have to bear in mind that, when the cube will be rotated, the verticles positions in this array will not change! Then, to get global position MeshFilter.transform.rotation should be used.

Ok, now lets look at mesh.triangles. It consists of 36 ints. It is not a random value. 36/3 = 12. The number of Triangles! Every three int represents one triangle. The int are numbers of verticles from mesh.verticles array. So lets say that number 3 triangle consists of verticles number 6, 7 & 8. U can now see, that the format Unity is storing the data is at least uncomfortable.

My Plan
To make programmer -> Unity communication easier, i decided to make an MeshManager Class. Among other things, it will consist of Aliases in form of Verticle Class.

My idea is that the Aliases will be the "virtual" verticles, in the way that in a cube there will be 8 Aliases, instead of 24 Verticles. Becouse in every corner there are 3 verticles, i will treat them as one Alias. When this alias will be moved, this 3 verticles will be moved etc.

Oki, lets go to Coding: Part II - MeshManager and Verticle Class