From 6f8f32d8023423385d560e0a45183f124c5bb473 Mon Sep 17 00:00:00 2001 From: ksuprynowicz Date: Wed, 13 Mar 2024 22:40:17 +0100 Subject: [PATCH] Fix collisions on glTF avatars. Co-authored-by: Edgar --- .../model-serializers/src/GLTFSerializer.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/libraries/model-serializers/src/GLTFSerializer.cpp b/libraries/model-serializers/src/GLTFSerializer.cpp index 73e2bf965c..1c6076809f 100644 --- a/libraries/model-serializers/src/GLTFSerializer.cpp +++ b/libraries/model-serializers/src/GLTFSerializer.cpp @@ -30,6 +30,8 @@ #include #include +#include + #include #include #include @@ -460,6 +462,7 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& // Increment the triangle indices by the current mesh vertex count so each mesh part can all reference the same buffers within the mesh int prevMeshVerticesCount = mesh.vertices.count(); + // For each vertex (stride is WEIGHTS_PER_VERTEX), it contains index of the cluster that given weight belongs to. QVector clusterJoints; QVector clusterWeights; @@ -940,18 +943,17 @@ bool GLTFSerializer::buildGeometry(HFMModel& hfmModel, const hifi::VariantHash& } else { mesh.clusterWeights[prevMeshClusterWeightCount + j] = (uint16_t)((float)(UINT16_MAX) + ALMOST_HALF); } - for (int clusterIndex = 0; clusterIndex < mesh.clusters.size() - 1; ++clusterIndex) { + for (int k = j; k < j + WEIGHTS_PER_VERTEX; ++k) { + int clusterIndex = mesh.clusterIndices[prevMeshClusterIndexCount + k]; ShapeVertices& points = hfmModel.shapeVertices.at(clusterIndex); glm::vec3 globalMeshScale = extractScale(globalTransforms[nodeIndex]); const glm::mat4 meshToJoint = glm::scale(glm::mat4(), globalMeshScale) * jointInverseBindTransforms[clusterIndex]; - // TODO: The entire clustering is probably broken and detailed collision shapes fail to generate due to it. const uint16_t EXPANSION_WEIGHT_THRESHOLD = UINT16_MAX/4; // Equivalent of 0.25f? - if (mesh.clusterWeights[j] >= EXPANSION_WEIGHT_THRESHOLD) { - // TODO: fix transformed vertices being pushed back - auto& vertex = mesh.vertices[i]; - const glm::mat4 vertexTransform = meshToJoint * (glm::translate(glm::mat4(), vertex)); - glm::vec3 transformedVertex = hfmModel.joints[clusterIndex].translation * (extractTranslation(vertexTransform)); + if (mesh.clusterWeights[prevMeshClusterWeightCount + k] >= EXPANSION_WEIGHT_THRESHOLD) { + auto& vertex = mesh.vertices[prevMeshVerticesCount + i]; + const glm::mat4 vertexTransform = meshToJoint * glm::translate(vertex); + glm::vec3 transformedVertex = extractTranslation(vertexTransform); points.push_back(transformedVertex); } }