p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Add SEGMENTED shading mode to computeNormals() for true flat shading

Open Srinidhi444 opened this issue 1 month ago • 1 comments

Increasing access

Introducing SEGMENTED would make it easier for users to create custom 3D geometry with true flat shading, without manually splitting shared vertices or calculating individual face normals.

This would be especially helpful for beginners exploring WebGL geometry, as it provides a clearer distinction between smooth shading, preserving existing vertex connections, and true per-face flat shading.

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [ ] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [x] WebGL
  • [ ] Build process
  • [ ] Unit testing
  • [ ] Internationalization
  • [ ] Friendly errors
  • [ ] Other (specify if possible)

Feature enhancement details

Problem

Currently, computeNormals() supports SMOOTH and KEEP_CONNECTIONS shading modes.

However, neither mode guarantees true flat shading when multiple faces share vertex indices.

With KEEP_CONNECTIONS, vertex normals are calculated by accumulating the normals of connected faces. When two faces share a vertex, their face normals are averaged at that vertex, which can produce smooth transitions instead of sharp edges.

As discussed in #9205, we need a separate shading mode that allows each face to have independent normals.

Proposed solution

Introduce a new SEGMENTED shading mode in computeNormals() that provides true per-face flat shading.

The proposed approach is:

  1. Calculate the normal of each face independently.
  2. Create independent vertex entries for each triangle, separating vertices shared by multiple faces.
  3. Duplicate the corresponding UV coordinates and other per-vertex attributes so they remain correctly aligned with the new vertices.
  4. Assign each triangle's face normal to its three vertices.
  5. Update the face indices to reference the corresponding independent vertices.

This would allow adjacent faces to have different normals even when their vertices occupy the same positions.

Expected behavior

  • computeNormals(SEGMENTED) produces true flat shading by assigning independent face normals.
  • Existing SMOOTH and KEEP_CONNECTIONS behavior remains unchanged.
  • UV coordinates, vertex colors, and other per-vertex attributes are preserved.
  • Face indices remain valid after vertex duplication.
  • Existing geometry operations continue to work with the resulting geometry.

Testing

Add unit tests covering:

  • Independent normals for adjacent non-coplanar faces sharing vertices.
  • Correct vertex and face index relationships after segmentation.
  • Preservation of UV coordinates and other per-vertex attributes.
  • Behavior when vertices are already independent.
  • Compatibility with existing SMOOTH and KEEP_CONNECTIONS modes.

Related work

  • #9205: Original discussion about normal calculation and shading behavior.
  • #9233: Preserve vertex attributes when calculating smooth normals.
  • #9271: Introduce KEEP_CONNECTIONS and deprecate FLAT.

This issue focuses exclusively on implementing SEGMENTED. Selective smoothing, where some edges remain sharp while others are smooth, can be explored separately once a concrete approach is established.

Srinidhi444 avatar Oct 13 '26 19:10 Srinidhi444

This issue has already been assigned to me as follow-up work discussed in #9205 . I'd appreciate it if others could avoid working on it simultaneously to prevent duplicate efforts. Thanks for understanding!

Srinidhi444 avatar Oct 13 '26 19:10 Srinidhi444