File tree Expand file tree Collapse file tree 1 file changed +17
-3
lines changed
modules/jolt_physics/objects Expand file tree Collapse file tree 1 file changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -629,8 +629,13 @@ void JoltSoftBody3D::update_rendering_server(PhysicsServer3DRenderingServerHandl
629629
630630 const int physics_vertex_count = (int )physics_vertices.size ();
631631
632+ normals.clear ();
632633 normals.resize (physics_vertex_count);
633634
635+ // Compute vertex normals using smooth-shading:
636+ // Each vertex should use the average normal of all faces it is a part of.
637+ // Iterate over each face, and add the face normal to each of the face vertices.
638+ // By the end of the loop, each vertex normal will be the sum of all face normals it belongs to.
634639 for (const SoftBodyFace &physics_face : physics_faces) {
635640 // Jolt uses a different winding order, so we swap the indices to account for that.
636641
@@ -644,9 +649,18 @@ void JoltSoftBody3D::update_rendering_server(PhysicsServer3DRenderingServerHandl
644649
645650 const Vector3 normal = (v2 - v0).cross (v1 - v0).normalized ();
646651
647- normals[i0] = normal;
648- normals[i1] = normal;
649- normals[i2] = normal;
652+ normals[i0] += normal;
653+ normals[i1] += normal;
654+ normals[i2] += normal;
655+ }
656+ // Normalize the vertex normals to have length 1.0
657+ for (Vector3 &n : normals) {
658+ real_t len = n.length ();
659+ // Some normals may have length 0 if the face was degenerate,
660+ // so don't divide by zero.
661+ if (len > CMP_EPSILON) {
662+ n /= len;
663+ }
650664 }
651665
652666 const int mesh_vertex_count = shared->mesh_to_physics .size ();
You can’t perform that action at this time.
0 commit comments