Skip to content

Commit

Permalink
Merge pull request #867 from overte-org/fix/gltf_collisions
Browse files Browse the repository at this point in the history
Fix collisions on glTF avatars
  • Loading branch information
ksuprynowicz committed Mar 14, 2024
2 parents 58a7819 + 6f8f32d commit 38caf61
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions libraries/model-serializers/src/GLTFSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include <qfile.h>
#include <qfileinfo.h>

#include <glm/gtx/transform.hpp>

#include <shared/NsightHelpers.h>
#include <NetworkAccessManager.h>
#include <ResourceManager.h>
Expand Down Expand Up @@ -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<uint16_t> clusterJoints;
QVector<float> clusterWeights;

Expand Down Expand Up @@ -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);
}
}
Expand Down

0 comments on commit 38caf61

Please sign in to comment.