psychopy.tools.gltools.calculateVertexNormals

psychopy.tools.gltools.calculateVertexNormals(vertices, faces, shading='smooth')[source]

Calculate vertex normals given vertices and triangle faces.

Finds all faces sharing a vertex index and sets its normal to either the face normal if shading=’flat’ or the average normals of adjacent faces if shading=’smooth’. Flat shading only works correctly if each vertex belongs to exactly one face.

The direction of the normals are determined by the winding order of triangles, assumed counter clock-wise (OpenGL default). Most model editing software exports using this convention. If not, winding orders can be reversed by calling:

faces = np.fliplr(faces)

In some case, creases may appear if vertices are at the same location, but do not share the same index.

Parameters:
  • vertices (array_like) – Nx3 vertex positions.

  • faces (array_like) – Nx3 vertex indices.

  • shading (str, optional) – Shading mode. Options are ‘smooth’ and ‘flat’. Flat only works with meshes where no vertex index is shared across faces.

Returns:

Vertex normals array with the shame shape as vertices. Computed normals are normalized.

Return type:

ndarray

Examples

Recomputing vertex normals for a UV sphere:

# create a sphere and discard normals
vertices, textureCoords, _, faces = gltools.createUVSphere()
normals = gltools.calculateVertexNormals(vertices, faces)

Back to top