From 5ba5d7d6eacc97b2e7d45e0a01d8e673d97b3746 Mon Sep 17 00:00:00 2001 From: Nick Fisher Date: Fri, 3 Nov 2023 13:10:02 +0800 Subject: [PATCH] add mutex for animation updates --- ios/include/AssetManager.hpp | 3 +++ ios/src/AssetManager.cpp | 12 +++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/ios/include/AssetManager.hpp b/ios/include/AssetManager.hpp index 8d9aff1a..fe2c76f5 100644 --- a/ios/include/AssetManager.hpp +++ b/ios/include/AssetManager.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include #include @@ -82,6 +84,7 @@ namespace polyvox { gltfio::ResourceLoader* _gltfResourceLoader = nullptr; gltfio::TextureProvider* _stbDecoder = nullptr; gltfio::TextureProvider* _ktxDecoder = nullptr; + std::mutex _animationMutex; vector _assets; tsl::robin_map _entityIdLookup; diff --git a/ios/src/AssetManager.cpp b/ios/src/AssetManager.cpp index 5e3b8825..1a70f45d 100644 --- a/ios/src/AssetManager.cpp +++ b/ios/src/AssetManager.cpp @@ -257,7 +257,7 @@ FilamentAsset* AssetManager::getAssetByEntityId(EntityId entityId) { void AssetManager::updateAnimations() { - + std::lock_guard lock(_animationMutex); RenderableManager &rm = _engine->getRenderableManager(); for (auto& asset : _assets) { @@ -467,7 +467,8 @@ bool AssetManager::setMorphAnimationBuffer( int numMorphTargets, int numFrames, float frameLengthInMs) { - + std::lock_guard lock(_animationMutex); + const auto& pos = _entityIdLookup.find(entityId); if(pos == _entityIdLookup.end()) { Log("ERROR: asset not found for entity."); @@ -543,7 +544,8 @@ bool AssetManager::setBoneAnimationBuffer( const char** const meshNames, int numMeshTargets, float frameLengthInMs) { - + std::lock_guard lock(_animationMutex); + const auto& pos = _entityIdLookup.find(entityId); if(pos == _entityIdLookup.end()) { Log("ERROR: asset not found for entity."); @@ -632,6 +634,8 @@ bool AssetManager::setBoneAnimationBuffer( void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse, bool replaceActive, float crossfade) { + std::lock_guard lock(_animationMutex); + if(index < 0) { Log("ERROR: glTF animation index must be greater than zero."); return; @@ -686,6 +690,8 @@ void AssetManager::playAnimation(EntityId e, int index, bool loop, bool reverse, } void AssetManager::stopAnimation(EntityId entityId, int index) { + std::lock_guard lock(_animationMutex); + const auto& pos = _entityIdLookup.find(entityId); if(pos == _entityIdLookup.end()) { Log("ERROR: asset not found for entity.");