Unity3D Building Damage Tutorial- Part VIII - Simple UV and Normals problem

UV


Now, inside our mesh, we have  ugly brown "texture". This is becouse the mesh.uv are not arleady set. Like in the picture.

So, we will have to add the twin info to mesh.uv 	//MeshManager class public void MakeMeshThick{ OrgTriangleCount = (mesh.triangles.Length) /3; AddTwinsToVerticleArray; AddTwinsToTriangleAlias; AddTwinsToUvArray;//here <--- TranslateFromMeshToManager; for(int i=Aliases.Count/2; i<Aliases.Count; i++){ Aliases[i].IsATwin = true; } 		IsMeshThick = true; }

AddTwinsToUvArray method
private void AddTwinsToUvArray{ Vector2[] OldUvList = mesh.uv;//what is important here is that there arleady is automaticly-generated uv. We have to ovverride them. Vector2[] TwinUv = new Vector2[OldUvList.Length/2]; //int offset = OldUvList.Length/2; for(int i=0; i<OldUvList.Length/2; i++){ TwinUv[i] = OldUvList[i]; } 		TwinUv.CopyTo(OldUvList,OldUvList.Length/2); mesh.uv = OldUvList; }



Simple. Only intresting thing is that here mesh.uv are arleady automaticly-generated (when making triangles or vertices), so we have to override second part.

Well, now inside is nice mirrored texture. On the other side, walls now are still not textured.

Normals
As you may remember, in "  Part V b - Thick Mesh in diffrent way, I assumed that all verticles tar are in one position (in one Alias), have the same normals. This is not true completly. Sometimes (in capsule or sphere), it works, but in meshes where there are more sharp angles, (like cube), it fails completely, making multiple twins. So, what we have to do is


 * 1) Count the average normals in Alias.

First, after initialising the Aliases, we count the normals. It is done in CalculateNormal method. It is called in MeshManager class public MeshManager (ref MeshFilter meshFilter){//constructor Initialise; ParentPosition = meshFilter.transform.position; filter = meshFilter; mesh = meshFilter.mesh; ManageVerticles(mesh.vertices); ManageTriangles(mesh.triangles); CalcualateMinimumHeight; CalculateNormals;//Method in MeshManager Class MakeMeshThick; //SetGroundAliases(0.5f); //CheckIfSeparated; }
 * 1) Use the same normal for moving verticles in one Alias

CalculateNormals (MeshManager Class)
public void CalculateNormals{//Counting absolute minimum and maximum height of verticle in the mesh foreach(Verticle vec in Aliases){ vec.CalculateNormal; } 	} Simply calling a method in every Alias

CalculateNormals (Verticle class)
public void CalculateNormal{//calculates the average normal Vector3 TempNormal = Vector3.zero; float HowMany = 0; foreach( int k in Aliases){ TempNormal += OwnerManager.mesh.normals[k]; } 			normal = TempNormal/(Aliases.Count); } Here, we just have to calculate the average of each verticle in Alias normal. This value is than stored in Alias, in normal Vector3.

AddTwinsToVerticleArray method (MeshManager)
private void AddTwinsToVerticleArray{ Vector3[] OldVerticleList = mesh.vertices; Vector3[] NewVerticleList = new Vector3[OldVerticleList.Length]; float Wall_Thickness = 0.005f; Vector3 TargetPosition; Vector3 NewPosition ; for(int i=0; iTargetPosition = mesh.vertices[i] - Aliases[VerticleToAliasArray[i]].normal; Instead of using the mesh.normals[i], here we check in which Alias current verticle is in, than use this Aliases normal variable. In this way, all "twined" verticles from one Alias will be in the same position. Files, as allways, are in  Github, Version 6.