Generate normals automatically for glTF 2.0 models
From the glTF spec
Implementation note: When normals are not specified, client implementations should calculate flat normals.
This would replace the stopgap solution in https://github.com/AnalyticalGraphicsInc/cesium/pull/6501.
Note: Make sure to not calculate normals for materials where the unlit extension is in use (#6977)
Related issue: https://github.com/CesiumGS/cesium/issues/10465
The implementation for this will be quite similar to what I did for outlines in #10506. This can be done by:
- Create a class (perhaps
FlatNormalGenerator) that takes typed arrays for indices/attributes and generates the flat normal attribute. Vertices will need to be duplicated in most cases since the normal is different for each adjacent face. SeePrimitiveOutlineGeneratorfor an example of how I did the indices/attribute processing - In
PrimitiveLoadPlan:- Add a
needsFlatNormalsflag (similar to theneedsOutlinesflag) - update
postProcess()to generate flat normals (usingFlatNormalGenerator) if needed. - I think normals should be processed first because it's more likely to duplicate vertices. The more the vertices are duplicated, the less work the outline stage needs to do to generate outline coordinates.
- Add a
- Update
GltfLoaderto check for primitives without normals. if so, update details in theloadPlanso the attributes load asattribute.packedTypedArrayand then are post-processed.
This came up on the forum: https://community.cesium.com/t/lightning-or-normals-problem-with-3d-tiles/19985
Came up again here: https://community.cesium.com/t/how-to-create-detail-texture/24165/4
Came up again in https://community.cesium.com/t/3d-model-does-not-display-shades/30269/11
EDIT: And for those stumbling over this issue, in general, or via one of the linked threads: The post at https://community.cesium.com/t/3d-model-does-not-display-shades/30269/11 also contains short instructions for how to add normals to existing models, as a workaround until this issue is fixed.