ADD: glTF instance rotation, animation
This commit is contained in:
@@ -1044,9 +1044,56 @@ void VulkanEngine::init_pipelines()
|
||||
metalRoughMaterial.build_pipelines(this);
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
// Rebuild a node's world transform in glTF local space, layering per-instance
|
||||
// local offsets on top of the base localTransform at each node in the chain.
|
||||
glm::mat4 build_node_world_with_overrides(const Node *node,
|
||||
const std::unordered_map<const Node*, glm::mat4> &overrides)
|
||||
{
|
||||
if (!node)
|
||||
{
|
||||
return glm::mat4(1.0f);
|
||||
}
|
||||
|
||||
std::vector<const Node*> chain;
|
||||
const Node *cur = node;
|
||||
while (cur)
|
||||
{
|
||||
chain.push_back(cur);
|
||||
std::shared_ptr<Node> parent = cur->parent.lock();
|
||||
cur = parent ? parent.get() : nullptr;
|
||||
}
|
||||
|
||||
glm::mat4 world(1.0f);
|
||||
for (auto it = chain.rbegin(); it != chain.rend(); ++it)
|
||||
{
|
||||
const Node *n = *it;
|
||||
glm::mat4 local = n->localTransform;
|
||||
auto ovIt = overrides.find(n);
|
||||
if (ovIt != overrides.end())
|
||||
{
|
||||
// Layer the override in local space for this instance.
|
||||
local = local * ovIt->second;
|
||||
}
|
||||
world = world * local;
|
||||
}
|
||||
return world;
|
||||
}
|
||||
}
|
||||
|
||||
void MeshNode::Draw(const glm::mat4 &topMatrix, DrawContext &ctx)
|
||||
{
|
||||
glm::mat4 nodeMatrix = topMatrix * worldTransform;
|
||||
glm::mat4 nodeMatrix;
|
||||
if (ctx.gltfNodeLocalOverrides && !ctx.gltfNodeLocalOverrides->empty())
|
||||
{
|
||||
glm::mat4 world = build_node_world_with_overrides(this, *ctx.gltfNodeLocalOverrides);
|
||||
nodeMatrix = topMatrix * world;
|
||||
}
|
||||
else
|
||||
{
|
||||
nodeMatrix = topMatrix * worldTransform;
|
||||
}
|
||||
|
||||
if (!mesh)
|
||||
{
|
||||
|
||||
@@ -832,12 +832,8 @@ std::optional<std::shared_ptr<LoadedGLTF> > loadGltf(VulkanEngine *engine, std::
|
||||
}
|
||||
}
|
||||
|
||||
if (!file.animations.empty())
|
||||
{
|
||||
file.activeAnimation = 0;
|
||||
file.animationTime = 0.0f;
|
||||
file.animationLoop = true;
|
||||
}
|
||||
// Default animation state is now owned by SceneManager per static scene / instance.
|
||||
// LoadedGLTF only stores shared animation clips.
|
||||
}
|
||||
|
||||
// We no longer need glTF-owned buffer payloads; free any large vectors
|
||||
@@ -896,62 +892,72 @@ void LoadedGLTF::refreshAllTransforms()
|
||||
}
|
||||
}
|
||||
|
||||
void LoadedGLTF::setActiveAnimation(int index, bool resetTime)
|
||||
void LoadedGLTF::setActiveAnimation(AnimationState &state, int index, bool resetTime)
|
||||
{
|
||||
if (animations.empty())
|
||||
{
|
||||
activeAnimation = -1;
|
||||
state.activeAnimation = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (index < 0 || index >= static_cast<int>(animations.size()))
|
||||
if (index < 0)
|
||||
{
|
||||
state.activeAnimation = -1;
|
||||
if (resetTime)
|
||||
{
|
||||
state.animationTime = 0.0f;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (index >= static_cast<int>(animations.size()))
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
activeAnimation = index;
|
||||
state.activeAnimation = index;
|
||||
if (resetTime)
|
||||
{
|
||||
animationTime = 0.0f;
|
||||
state.animationTime = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void LoadedGLTF::setActiveAnimation(const std::string &name, bool resetTime)
|
||||
void LoadedGLTF::setActiveAnimation(AnimationState &state, const std::string &name, bool resetTime)
|
||||
{
|
||||
for (size_t i = 0; i < animations.size(); ++i)
|
||||
{
|
||||
if (animations[i].name == name)
|
||||
{
|
||||
setActiveAnimation(static_cast<int>(i), resetTime);
|
||||
setActiveAnimation(state, static_cast<int>(i), resetTime);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LoadedGLTF::updateAnimation(float dt)
|
||||
void LoadedGLTF::updateAnimation(float dt, AnimationState &state)
|
||||
{
|
||||
if (animations.empty()) return;
|
||||
if (activeAnimation < 0 || activeAnimation >= static_cast<int>(animations.size())) return;
|
||||
if (state.activeAnimation < 0 || state.activeAnimation >= static_cast<int>(animations.size())) return;
|
||||
if (dt <= 0.0f) return;
|
||||
|
||||
Animation &clip = animations[activeAnimation];
|
||||
Animation &clip = animations[state.activeAnimation];
|
||||
if (clip.duration <= 0.0f) return;
|
||||
|
||||
animationTime += dt;
|
||||
if (animationLoop)
|
||||
state.animationTime += dt;
|
||||
if (state.animationLoop)
|
||||
{
|
||||
animationTime = std::fmod(animationTime, clip.duration);
|
||||
if (animationTime < 0.0f)
|
||||
state.animationTime = std::fmod(state.animationTime, clip.duration);
|
||||
if (state.animationTime < 0.0f)
|
||||
{
|
||||
animationTime += clip.duration;
|
||||
state.animationTime += clip.duration;
|
||||
}
|
||||
}
|
||||
else if (animationTime > clip.duration)
|
||||
else if (state.animationTime > clip.duration)
|
||||
{
|
||||
animationTime = clip.duration;
|
||||
state.animationTime = clip.duration;
|
||||
}
|
||||
|
||||
float t = animationTime;
|
||||
float t = state.animationTime;
|
||||
|
||||
for (auto &ch: clip.channels)
|
||||
{
|
||||
|
||||
@@ -98,20 +98,24 @@ struct LoadedGLTF : public IRenderable
|
||||
std::vector<AnimationChannel> channels;
|
||||
};
|
||||
|
||||
struct AnimationState
|
||||
{
|
||||
int activeAnimation = -1;
|
||||
float animationTime = 0.f;
|
||||
bool animationLoop = true;
|
||||
};
|
||||
|
||||
std::vector<Animation> animations;
|
||||
int activeAnimation = -1;
|
||||
float animationTime = 0.f;
|
||||
bool animationLoop = true;
|
||||
|
||||
// Optional debug name (e.g., key used when loaded into SceneManager)
|
||||
std::string debugName;
|
||||
|
||||
// Animation helpers
|
||||
void updateAnimation(float dt);
|
||||
void updateAnimation(float dt, AnimationState &state);
|
||||
void refreshAllTransforms();
|
||||
std::shared_ptr<Node> getNode(const std::string &name);
|
||||
void setActiveAnimation(int index, bool resetTime = true);
|
||||
void setActiveAnimation(const std::string &name, bool resetTime = true);
|
||||
void setActiveAnimation(AnimationState &state, int index, bool resetTime = true);
|
||||
void setActiveAnimation(AnimationState &state, const std::string &name, bool resetTime = true);
|
||||
|
||||
~LoadedGLTF()
|
||||
{
|
||||
|
||||
@@ -123,6 +123,7 @@ void SceneManager::update_scene()
|
||||
mainDrawContext.OpaqueSurfaces.clear();
|
||||
mainDrawContext.TransparentSurfaces.clear();
|
||||
mainDrawContext.nextID = 1;
|
||||
mainDrawContext.gltfNodeLocalOverrides = nullptr;
|
||||
|
||||
mainCamera.update();
|
||||
|
||||
@@ -140,30 +141,6 @@ void SceneManager::update_scene()
|
||||
dt = 0.1f;
|
||||
}
|
||||
|
||||
// Advance glTF animations once per unique LoadedGLTF
|
||||
if (dt > 0.f)
|
||||
{
|
||||
std::unordered_set<LoadedGLTF *> animatedScenes;
|
||||
|
||||
auto updateSceneAnim = [&](std::shared_ptr<LoadedGLTF> &scene) {
|
||||
if (!scene) return;
|
||||
LoadedGLTF *ptr = scene.get();
|
||||
if (animatedScenes.insert(ptr).second)
|
||||
{
|
||||
ptr->updateAnimation(dt);
|
||||
}
|
||||
};
|
||||
|
||||
for (auto &[name, scene] : loadedScenes)
|
||||
{
|
||||
updateSceneAnim(scene);
|
||||
}
|
||||
for (auto &[name, inst] : dynamicGLTFInstances)
|
||||
{
|
||||
updateSceneAnim(inst.scene);
|
||||
}
|
||||
}
|
||||
|
||||
auto tagOwner = [&](RenderObject::OwnerType type, const std::string &name,
|
||||
size_t opaqueBegin, size_t transpBegin)
|
||||
{
|
||||
@@ -179,29 +156,57 @@ void SceneManager::update_scene()
|
||||
}
|
||||
};
|
||||
|
||||
// Draw all loaded GLTF scenes (static world)
|
||||
// Draw all loaded GLTF scenes (static world), advancing their independent animation states.
|
||||
for (auto &[name, scene] : loadedScenes)
|
||||
{
|
||||
if (scene)
|
||||
if (!scene)
|
||||
{
|
||||
const size_t opaqueStart = mainDrawContext.OpaqueSurfaces.size();
|
||||
const size_t transpStart = mainDrawContext.TransparentSurfaces.size();
|
||||
scene->Draw(glm::mat4{1.f}, mainDrawContext);
|
||||
tagOwner(RenderObject::OwnerType::StaticGLTF, name, opaqueStart, transpStart);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Advance this scene's animation state (independent of instances).
|
||||
if (dt > 0.f)
|
||||
{
|
||||
auto &animState = sceneAnimations[name];
|
||||
scene->updateAnimation(dt, animState);
|
||||
}
|
||||
|
||||
const size_t opaqueStart = mainDrawContext.OpaqueSurfaces.size();
|
||||
const size_t transpStart = mainDrawContext.TransparentSurfaces.size();
|
||||
mainDrawContext.gltfNodeLocalOverrides = nullptr;
|
||||
scene->Draw(glm::mat4{1.f}, mainDrawContext);
|
||||
mainDrawContext.gltfNodeLocalOverrides = nullptr;
|
||||
tagOwner(RenderObject::OwnerType::StaticGLTF, name, opaqueStart, transpStart);
|
||||
}
|
||||
|
||||
// dynamic GLTF instances
|
||||
for (const auto &kv: dynamicGLTFInstances)
|
||||
// dynamic GLTF instances (each with its own animation state)
|
||||
for (auto &kv : dynamicGLTFInstances)
|
||||
{
|
||||
const GLTFInstance &inst = kv.second;
|
||||
if (inst.scene)
|
||||
GLTFInstance &inst = kv.second;
|
||||
if (!inst.scene)
|
||||
{
|
||||
const size_t opaqueStart = mainDrawContext.OpaqueSurfaces.size();
|
||||
const size_t transpStart = mainDrawContext.TransparentSurfaces.size();
|
||||
inst.scene->Draw(inst.transform, mainDrawContext);
|
||||
tagOwner(RenderObject::OwnerType::GLTFInstance, kv.first, opaqueStart, transpStart);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (dt > 0.f)
|
||||
{
|
||||
inst.scene->updateAnimation(dt, inst.animation);
|
||||
}
|
||||
|
||||
const size_t opaqueStart = mainDrawContext.OpaqueSurfaces.size();
|
||||
const size_t transpStart = mainDrawContext.TransparentSurfaces.size();
|
||||
// Enable per-instance node pose overrides while drawing this instance.
|
||||
if (!inst.nodeLocalOverrides.empty())
|
||||
{
|
||||
mainDrawContext.gltfNodeLocalOverrides = &inst.nodeLocalOverrides;
|
||||
}
|
||||
else
|
||||
{
|
||||
mainDrawContext.gltfNodeLocalOverrides = nullptr;
|
||||
}
|
||||
inst.scene->Draw(inst.transform, mainDrawContext);
|
||||
mainDrawContext.gltfNodeLocalOverrides = nullptr;
|
||||
tagOwner(RenderObject::OwnerType::GLTFInstance, kv.first, opaqueStart, transpStart);
|
||||
}
|
||||
|
||||
// Default primitives are added as dynamic instances by the engine.
|
||||
@@ -371,7 +376,21 @@ void SceneManager::loadScene(const std::string &name, std::shared_ptr<LoadedGLTF
|
||||
{
|
||||
scene->debugName = name;
|
||||
}
|
||||
loadedScenes[name] = std::move(scene);
|
||||
loadedScenes[name] = scene;
|
||||
|
||||
// Initialize default animation state for this named scene (play first clip if present).
|
||||
if (scene && !scene->animations.empty())
|
||||
{
|
||||
LoadedGLTF::AnimationState st{};
|
||||
st.activeAnimation = 0;
|
||||
st.animationTime = 0.0f;
|
||||
st.animationLoop = true;
|
||||
sceneAnimations[name] = st;
|
||||
}
|
||||
else
|
||||
{
|
||||
sceneAnimations.erase(name);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<LoadedGLTF> SceneManager::getScene(const std::string &name)
|
||||
@@ -400,6 +419,7 @@ void SceneManager::cleanup()
|
||||
// Drop our references to GLTF scenes. Their destructors call clearAll()
|
||||
// exactly once to release GPU resources.
|
||||
loadedScenes.clear();
|
||||
sceneAnimations.clear();
|
||||
loadedNodes.clear();
|
||||
}
|
||||
|
||||
@@ -453,7 +473,16 @@ void SceneManager::addGLTFInstance(const std::string &name, std::shared_ptr<Load
|
||||
fmt::println("[SceneManager] addGLTFInstance '{}' (scene='{}')",
|
||||
name,
|
||||
scene->debugName.empty() ? "<unnamed>" : scene->debugName.c_str());
|
||||
dynamicGLTFInstances[name] = GLTFInstance{std::move(scene), transform};
|
||||
GLTFInstance inst{};
|
||||
inst.scene = std::move(scene);
|
||||
inst.transform = transform;
|
||||
if (inst.scene && !inst.scene->animations.empty())
|
||||
{
|
||||
inst.animation.activeAnimation = 0;
|
||||
inst.animation.animationTime = 0.0f;
|
||||
inst.animation.animationLoop = true;
|
||||
}
|
||||
dynamicGLTFInstances[name] = std::move(inst);
|
||||
}
|
||||
|
||||
bool SceneManager::removeGLTFInstance(const std::string &name)
|
||||
@@ -519,6 +548,71 @@ void SceneManager::clearGLTFInstances()
|
||||
pendingGLTFRelease.size());
|
||||
}
|
||||
|
||||
bool SceneManager::setGLTFInstanceNodeOffset(const std::string &instanceName,
|
||||
const std::string &nodeName,
|
||||
const glm::mat4 &offset)
|
||||
{
|
||||
auto it = dynamicGLTFInstances.find(instanceName);
|
||||
if (it == dynamicGLTFInstances.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
GLTFInstance &inst = it->second;
|
||||
if (!inst.scene)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto nodePtr = inst.scene->getNode(nodeName);
|
||||
if (!nodePtr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
inst.nodeLocalOverrides[nodePtr.get()] = offset;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SceneManager::clearGLTFInstanceNodeOffset(const std::string &instanceName,
|
||||
const std::string &nodeName)
|
||||
{
|
||||
auto it = dynamicGLTFInstances.find(instanceName);
|
||||
if (it == dynamicGLTFInstances.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
GLTFInstance &inst = it->second;
|
||||
if (!inst.scene)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto nodePtr = inst.scene->getNode(nodeName);
|
||||
if (!nodePtr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
auto ovIt = inst.nodeLocalOverrides.find(nodePtr.get());
|
||||
if (ovIt == inst.nodeLocalOverrides.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
inst.nodeLocalOverrides.erase(ovIt);
|
||||
return true;
|
||||
}
|
||||
|
||||
void SceneManager::clearGLTFInstanceNodeOffsets(const std::string &instanceName)
|
||||
{
|
||||
auto it = dynamicGLTFInstances.find(instanceName);
|
||||
if (it == dynamicGLTFInstances.end())
|
||||
{
|
||||
return;
|
||||
}
|
||||
it->second.nodeLocalOverrides.clear();
|
||||
}
|
||||
|
||||
bool SceneManager::setSceneAnimation(const std::string &sceneName, int animationIndex, bool resetTime)
|
||||
{
|
||||
auto it = loadedScenes.find(sceneName);
|
||||
@@ -527,7 +621,8 @@ bool SceneManager::setSceneAnimation(const std::string &sceneName, int animation
|
||||
return false;
|
||||
}
|
||||
|
||||
it->second->setActiveAnimation(animationIndex, resetTime);
|
||||
auto &animState = sceneAnimations[sceneName];
|
||||
it->second->setActiveAnimation(animState, animationIndex, resetTime);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -539,7 +634,8 @@ bool SceneManager::setSceneAnimation(const std::string &sceneName, const std::st
|
||||
return false;
|
||||
}
|
||||
|
||||
it->second->setActiveAnimation(animationName, resetTime);
|
||||
auto &animState = sceneAnimations[sceneName];
|
||||
it->second->setActiveAnimation(animState, animationName, resetTime);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -551,7 +647,8 @@ bool SceneManager::setSceneAnimationLoop(const std::string &sceneName, bool loop
|
||||
return false;
|
||||
}
|
||||
|
||||
it->second->animationLoop = loop;
|
||||
auto &animState = sceneAnimations[sceneName];
|
||||
animState.animationLoop = loop;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -563,7 +660,8 @@ bool SceneManager::setGLTFInstanceAnimation(const std::string &instanceName, int
|
||||
return false;
|
||||
}
|
||||
|
||||
it->second.scene->setActiveAnimation(animationIndex, resetTime);
|
||||
LoadedGLTF::AnimationState &animState = it->second.animation;
|
||||
it->second.scene->setActiveAnimation(animState, animationIndex, resetTime);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -575,7 +673,8 @@ bool SceneManager::setGLTFInstanceAnimation(const std::string &instanceName, con
|
||||
return false;
|
||||
}
|
||||
|
||||
it->second.scene->setActiveAnimation(animationName, resetTime);
|
||||
LoadedGLTF::AnimationState &animState = it->second.animation;
|
||||
it->second.scene->setActiveAnimation(animState, animationName, resetTime);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -587,6 +686,6 @@ bool SceneManager::setGLTFInstanceAnimationLoop(const std::string &instanceName,
|
||||
return false;
|
||||
}
|
||||
|
||||
it->second.scene->animationLoop = loop;
|
||||
it->second.animation.animationLoop = loop;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -50,6 +50,9 @@ struct DrawContext
|
||||
std::vector<RenderObject> TransparentSurfaces;
|
||||
// Monotonic counter used to assign stable per-frame object IDs.
|
||||
uint32_t nextID = 1;
|
||||
// Optional per-instance glTF node local overrides (additive layer in local space).
|
||||
// When non-null, MeshNode::Draw will rebuild world transforms using these offsets.
|
||||
const std::unordered_map<const Node*, glm::mat4> *gltfNodeLocalOverrides = nullptr;
|
||||
};
|
||||
|
||||
class SceneManager
|
||||
@@ -105,6 +108,10 @@ public:
|
||||
{
|
||||
std::shared_ptr<LoadedGLTF> scene;
|
||||
glm::mat4 transform{1.f};
|
||||
LoadedGLTF::AnimationState animation;
|
||||
// Per-instance local-space pose offsets for nodes in this glTF scene.
|
||||
// The offset matrix is post-multiplied onto the node's localTransform.
|
||||
std::unordered_map<const Node*, glm::mat4> nodeLocalOverrides;
|
||||
};
|
||||
|
||||
void addGLTFInstance(const std::string &name, std::shared_ptr<LoadedGLTF> scene,
|
||||
@@ -113,6 +120,14 @@ public:
|
||||
bool getGLTFInstanceTransform(const std::string &name, glm::mat4 &outTransform);
|
||||
bool setGLTFInstanceTransform(const std::string &name, const glm::mat4 &transform);
|
||||
void clearGLTFInstances();
|
||||
// Per-instance glTF node pose overrides (local-space, layered on top of animation/base TRS).
|
||||
// 'offset' is post-multiplied onto the node's localTransform for this instance only.
|
||||
bool setGLTFInstanceNodeOffset(const std::string &instanceName,
|
||||
const std::string &nodeName,
|
||||
const glm::mat4 &offset);
|
||||
bool clearGLTFInstanceNodeOffset(const std::string &instanceName,
|
||||
const std::string &nodeName);
|
||||
void clearGLTFInstanceNodeOffsets(const std::string &instanceName);
|
||||
|
||||
// Animation control helpers (glTF)
|
||||
// Note: a LoadedGLTF may be shared by multiple instances; changing
|
||||
@@ -167,6 +182,8 @@ private:
|
||||
std::vector<PointLight> pointLights;
|
||||
|
||||
std::unordered_map<std::string, std::shared_ptr<LoadedGLTF> > loadedScenes;
|
||||
// Per-named static glTF scene animation state (independent of instances).
|
||||
std::unordered_map<std::string, LoadedGLTF::AnimationState> sceneAnimations;
|
||||
std::unordered_map<std::string, std::shared_ptr<Node> > loadedNodes;
|
||||
std::unordered_map<std::string, MeshInstance> dynamicMeshInstances;
|
||||
std::unordered_map<std::string, GLTFInstance> dynamicGLTFInstances;
|
||||
|
||||
Reference in New Issue
Block a user