diff --git a/libraries/render-utils/src/MeshPartPayload.cpp b/libraries/render-utils/src/MeshPartPayload.cpp index 5e47ed8b0f..333371ed76 100644 --- a/libraries/render-utils/src/MeshPartPayload.cpp +++ b/libraries/render-utils/src/MeshPartPayload.cpp @@ -374,12 +374,7 @@ void ModelMeshPartPayload::updateTransformForSkinnedMesh(const Transform& transf _transform = transform; if (clusterMatrices.size() > 0) { - _worldBound = AABox(); - for (auto& clusterMatrix : clusterMatrices) { - AABox clusterBound = _localBound; - clusterBound.transform(clusterMatrix); - _worldBound += clusterBound; - } + _worldBound = _adjustedLocalBound; _worldBound.transform(_transform); if (clusterMatrices.size() == 1) { _transform = _transform.worldTransform(Transform(clusterMatrices[0])); @@ -612,3 +607,15 @@ void ModelMeshPartPayload::render(RenderArgs* args) const { const int INDICES_PER_TRIANGLE = 3; args->_details._trianglesRendered += _drawPart._numIndices / INDICES_PER_TRIANGLE; } + +void ModelMeshPartPayload::computeAdjustedLocalBound(const QVector& clusterMatrices) { + _adjustedLocalBound = _localBound; + if (clusterMatrices.size() > 0) { + _adjustedLocalBound.transform(clusterMatrices[0]); + for (int i = 1; i < clusterMatrices.size(); ++i) { + AABox clusterBound = _localBound; + clusterBound.transform(clusterMatrices[i]); + _adjustedLocalBound += clusterBound; + } + } +} diff --git a/libraries/render-utils/src/MeshPartPayload.h b/libraries/render-utils/src/MeshPartPayload.h index 1f3778c34a..c585c95025 100644 --- a/libraries/render-utils/src/MeshPartPayload.h +++ b/libraries/render-utils/src/MeshPartPayload.h @@ -1,5 +1,5 @@ // -// ModelMeshPartPayload.h +// MeshPartPayload.h // interface/src/renderer // // Created by Sam Gateau on 10/3/15. @@ -61,6 +61,7 @@ public: bool _hasColorAttrib { false }; model::Box _localBound; + model::Box _adjustedLocalBound; mutable model::Box _worldBound; std::shared_ptr _drawMesh; @@ -105,6 +106,8 @@ public: void initCache(); + void computeAdjustedLocalBound(const QVector& clusterMatrices); + Model* _model; int _meshIndex; diff --git a/libraries/render-utils/src/Model.cpp b/libraries/render-utils/src/Model.cpp index 59b997b2cc..018a7e6954 100644 --- a/libraries/render-utils/src/Model.cpp +++ b/libraries/render-utils/src/Model.cpp @@ -1149,6 +1149,8 @@ void Model::simulate(float deltaTime, bool fullUpdate) { // update the world space transforms for all joints glm::mat4 parentTransform = glm::scale(_scale) * glm::translate(_offset); updateRig(deltaTime, parentTransform); + + computeMeshPartLocalBounds(); } } @@ -1158,6 +1160,14 @@ void Model::updateRig(float deltaTime, glm::mat4 parentTransform) { _rig->updateAnimations(deltaTime, parentTransform); } +void Model::computeMeshPartLocalBounds() { + for (auto& part : _modelMeshRenderItemsSet) { + assert(part->_meshIndex < _modelMeshRenderItemsSet.size()); + const Model::MeshState& state = _meshStates.at(part->_meshIndex); + part->computeAdjustedLocalBound(state.clusterMatrices); + } +} + // virtual void Model::updateClusterMatrices() { PerformanceTimer perfTimer("Model::updateClusterMatrices"); @@ -1334,6 +1344,7 @@ void Model::createVisibleRenderItemSet() { shapeID++; } } + computeMeshPartLocalBounds(); } void Model::createCollisionRenderItemSet() { diff --git a/libraries/render-utils/src/Model.h b/libraries/render-utils/src/Model.h index 8b6992394f..49890bfb04 100644 --- a/libraries/render-utils/src/Model.h +++ b/libraries/render-utils/src/Model.h @@ -244,7 +244,6 @@ public: public: QVector clusterMatrices; gpu::BufferPointer clusterBuffer; - }; const MeshState& getMeshState(int index) { return _meshStates.at(index); } @@ -319,6 +318,7 @@ protected: void scaleToFit(); void snapToRegistrationPoint(); + void computeMeshPartLocalBounds(); virtual void updateRig(float deltaTime, glm::mat4 parentTransform); /// Restores the indexed joint to its default position.