- Home /
How can I reverse normals on a quad?
I'm using this script to create a 2D quad in Unity: Quad Script
My problem is I have to rotate my created object 180 degrees on the Y axis to see the texture on it.
I tried to reverse the normals in the script with:
Vector3[] normals = m.normals;
for (int i = 0; i < normals.Length; i++)
normals[i] = -normals[i];
m.normals = normals;
No luck. Any idea?
Answer by Jessy · Jan 27, 2011 at 07:52 PM
Normals are only for lighting calculations. As it is, your code just tells the faces to behave, when hit by lights, to act as if they were facing the other way.
You just need to wind the triangles in the opposite order. Unity will take care of the normals automatically for you, using that script, if you do that.
Normals also used for raycasting, no? If he were to use this as a mesh collider?
As far as I can tell, only winding order matters for mesh colliders, too.
Answer by Dan 9 · Jan 28, 2011 at 01:52 PM
Thanks for the explanation; reversing the faces makes sense. I'm still having issues with reversing them.
Here is the original code that creates them (this is the code I will need to reverse).
for (float y = 0.0f; y < vCount2; y++)
{
for (float x = 0.0f; x < hCount2; x++)
{
if (orientation == Orientation.Horizontal)
{
vertices[index] = new Vector3(x * scaleX, 0.0f, y * scaleY);
}
else
{
vertices[index] = new Vector3(x * scaleX, y * scaleY, 0.0f);
}
uvs[index++] = new Vector2(x * uvFactorX, y * uvFactorY);
}
}
I've tried to reverse them with no luck. I tried to reverse the index so it decrements which did not work. Then I tried reversing the for loops so they decrement. I'm not sure why it did not work. Here is the for loop reversal code I tried.
for (float y = vCount2; y > 0.0f; y--)
{
for (float x = hCount2; x > 0.0f; x--)
{
if (orientation == Orientation.Horizontal)
{
vertices[index] = new Vector3(x * scaleX, 0.0f, y * scaleY);
}
else
{
vertices[index] = new Vector3(x * scaleX, y * scaleY, 0.0f);
}
uvs[index++] = new Vector2(x * uvFactorX, y * uvFactorY);
}
}
What am I missing?
Did some debugging and found my for loops were wrong, but I still get the same results even though they are fixed. The new loops look like this: for (float y = vCount2 - 1; y >= 0.0f; y--) Same with the x. This gives me the correct coords now and they are reversed, but it is still culling the side facing me.
Answer by Dan 9 · Jan 28, 2011 at 04:22 PM
Finally got it. I was reversing the vertices and not the triangle strip. Works now. Thanks for the help.
Can you post your solution for anyone who's interested in it?
Your answer
Follow this Question
Related Questions
Any script to 'fix' normals/winding order? 3 Answers
Is it possible to run a script in the editor so it doesnt have to run it again the runtime???? 2 Answers
Class Reference in Editor not working 1 Answer
Can't open scripts with monodevelop? 1 Answer
AssetPreview.GetMiniTypeThumbnail not working for custom scripts 0 Answers