- Home /
Is vertices.normals face- or vertex-normals?
Hey, guys! Merry Christmas!
I'm working on a little holidays-project for a prototype of my game. And now I'm digging down into the nitty-gritty; making shaders! I'm not VERY familiar with all the components of a 3D mesh (or applying the correct math to the algorithm), so I need a little guidance.
I am making a deform vertex-shader for a sphere where i need to work with normals. Just to make sure that I'm on the right track, I'm trying to use a script to draw debug-rays from the vertices in the direction of each vertices normal.
private Mesh mesh;
public float normalLength = 1;
// Use this for initialization
void Start () {
mesh = GetComponent<MeshFilter>().mesh;
}
// Update is called once per frame
void Update () {
foreach (Vector3 v3 in mesh.normals)
{
Debug.DrawRay(transform.position + v3, v3 * normalLength, Color.red);
}
}
However... This seems only to give me the face-normals. Am I doing something wrong in the script? What the hell is going on?!?!
Your drawRay is shooting from the center of the object (yes, the first v3 is invisible, but the 2nd visible part is still the same line.) So it "randomly" goes through a face or any vert.
As aldonaletto has in the code below, start each normal at it's vertex.
Answer by aldonaletto · Dec 22, 2013 at 06:18 PM
mesh.normals are the normals associated to the vertices: usually the shader calculates each pixel's normal by interpolating among the normals. Flat surfaces have equal normals in their vertices (like if they were surface normals), but curved surfaces have different normals associated to each vertex.
Anyway, keep in mind that all data in a mesh is in local space, which doesn't take the transform settings into account (rotation, scale, position). You should convert each vertex and its normal to world space in order to correctly display them:
void Update () {
for (int i=0; i<mesh.vertices.Length; i++){
Vector3 norm = transform.TransformDirection(mesh.normals[i]);
Vector3 vert = transform.TransformPoint(mesh.vertices[i]);
Debug.DrawRay(vert, norm * normalLength, Color.red);
}
}
Your answer
Follow this Question
Related Questions
Change vertex colours based on steepness 1 Answer
How to set mesh normals per face? 2 Answers
Double sided geometry. 3 Answers
How can I calculate the normals of the edges of a 2D plane? 0 Answers
Uncorrect normals tri-planer shader 0 Answers