• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# Terrain Normals

## Recommended Posts

AN_D_K    158
I know that on a terrain mesh that each vertex has the average normal of all the triangles connected too it. All the vertices within the mesh are connected to 6 triangles and this has let me make a simple algorithm to get the values for it. What is troubling me is how to get the normals for the vertices that are on the outsides of the mesh as they are not connected to 6 triangles. How to I make an algorithm for these that will compliment my previous one?

##### Share on other sites
budipro    122
If you use triangle list and indices, then it should be easy to calculate the normals. Just loop through the indices and average the vertex normals those indices point to.

I haven''t figured out the way if you are not using list and indices. It seems quite complicated.

Here is the sample using list and indices:

for (int i = 0; i < indices.Length; i+=3)				{					normal = (CalcNormal(verts[indices[i+1]], verts[indices[i+2]], verts[indices[i]]));					verts[indices[i]].SetNormal(Vector3.Add(verts[indices[i]].GetNormal(), normal));					verts[indices[i+1]].SetNormal(Vector3.Add(verts[indices[i+1]].GetNormal(), normal));					verts[indices[i+2]].SetNormal(Vector3.Add(verts[indices[i+2]].GetNormal(), normal));					verts[indices[i]].SetNormal(Vector3.Normalize(verts[indices[i]].GetNormal()));					verts[indices[i+1]].SetNormal(Vector3.Normalize(verts[indices[i+1]].GetNormal()));					verts[indices[i+2]].SetNormal(Vector3.Normalize(verts[indices[i+2]].GetNormal()));				}

##### Share on other sites
joanusdmentia    1060
2 ways come to mind. You could assume that the normal of the non-existent triangles simply point upwards. Alternatively, just exclude them from the calculation (ie. average only the normals that you do have). How you do it is really up to you.