- Home /
Procedural-mesh normals reversed, but only when lit from the left
I am creating a procedural cylinder mesh using code from the Jayelinda's Web: Modelling by numbers tutorial, and ending up with some odd results.
The images below show the default unity cylinder side-by-side with the procedural mesh.
As you can see, when lit from the right both objects more or less look the same, however when lit from the left, the procedural mesh has normals reversed, i.e. what should be protruding is instead recessed and vice-versa.
When lit from above, the procedural mesh still looks like it is being lit from the side.
Rotating the object does not change what the lighting looks like, it seems purely dependent on the direction of the light.
Below is the code where most of the action happens, BuildRing is called a few times at increasing heights in order to create a cylinder.
public static void BuildRing (MeshBuilder meshBuilder, int segmentCount,
Vector3 centre, float radius,
float v, bool buildTriangles)
{
float angleInc = (Mathf.PI * 2.0f) / segmentCount;
for (int i = 0; i <= segmentCount; i++)
{
float angle = angleInc * i;
Vector3 unitPosition = Vector3.zero;
unitPosition.x = Mathf.Cos (angle);
unitPosition.z = Mathf.Sin (angle);
meshBuilder.Vertices.Add (centre + unitPosition * radius);
meshBuilder.Normals.Add (unitPosition);
meshBuilder.UVs.Add (new Vector2 ((float)i / segmentCount, v));
if (i > 0 && buildTriangles)
{
int baseIndex = meshBuilder.Vertices.Count - 1;
int vertsPerRow = segmentCount + 1;
int index0 = baseIndex;
int index1 = baseIndex - 1;
int index2 = baseIndex - vertsPerRow;
int index3 = baseIndex - vertsPerRow - 1;
meshBuilder.AddTriangle (index0, index2, index1);
meshBuilder.AddTriangle (index2, index3, index1);
}
}
}
Do you have any ideas what I might be doing wrong? I'm at a total loss, appreciate any advice!
Answer by mikeInside · Jun 23, 2015 at 05:43 PM
Mesh needs tangents. Details here: http://forum.unity3d.com/threads/procedural-mesh-normals-reversed-but-only-when-lit-from-the-left.325601/