fix up morph target animations and add stopAnimation function
This commit is contained in:
@@ -539,9 +539,11 @@ namespace polyvox
|
||||
Log("Set viewport to width: %d height: %d scaleFactor : %f", width, height, contentScaleFactor);
|
||||
}
|
||||
|
||||
void FilamentViewer::animateWeights(float *data, int numWeights, int numFrames, float frameRate)
|
||||
void FilamentViewer::animateWeights(float *data, int numWeights, int numFrames, float frameLengthInMs)
|
||||
{
|
||||
morphAnimationBuffer = std::make_unique<MorphAnimationBuffer>(data, numWeights, numFrames, 1000 / frameRate);
|
||||
// assert numWeights == asset.numWeights ?
|
||||
Log("Making morph animation buffer with %d weights across %d frames and frame length %f ms ", numWeights, numFrames, frameLengthInMs);
|
||||
morphAnimationBuffer = std::make_unique<MorphAnimationBuffer>(data, numWeights, numFrames, frameLengthInMs);
|
||||
}
|
||||
|
||||
void FilamentViewer::updateMorphAnimation()
|
||||
@@ -549,20 +551,19 @@ namespace polyvox
|
||||
|
||||
if (morphAnimationBuffer->frameIndex >= morphAnimationBuffer->numFrames)
|
||||
{
|
||||
duration<double, std::milli> dur = high_resolution_clock::now() - morphAnimationBuffer->startTime;
|
||||
Log("Morph animation completed in %f ms (%d frames at framerate %f), final frame was %d", dur.count(), morphAnimationBuffer->numFrames, 1000 / morphAnimationBuffer->frameLengthInMs, morphAnimationBuffer->frameIndex);
|
||||
morphAnimationBuffer = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
if (morphAnimationBuffer->frameIndex == -1)
|
||||
} else if (morphAnimationBuffer->frameIndex == -1)
|
||||
{
|
||||
morphAnimationBuffer->frameIndex++;
|
||||
morphAnimationBuffer->startTime = std::chrono::high_resolution_clock::now();
|
||||
morphAnimationBuffer->startTime = high_resolution_clock::now();
|
||||
applyWeights(morphAnimationBuffer->frameData, morphAnimationBuffer->numWeights);
|
||||
}
|
||||
else
|
||||
{
|
||||
std::chrono::duration<double, std::milli> dur = std::chrono::high_resolution_clock::now() - morphAnimationBuffer->startTime;
|
||||
int frameIndex = dur.count() / morphAnimationBuffer->frameLength;
|
||||
duration<double, std::milli> dur = high_resolution_clock::now() - morphAnimationBuffer->startTime;
|
||||
int frameIndex = static_cast<int>(dur.count() / morphAnimationBuffer->frameLengthInMs);
|
||||
if (frameIndex != morphAnimationBuffer->frameIndex)
|
||||
{
|
||||
morphAnimationBuffer->frameIndex = frameIndex;
|
||||
@@ -579,15 +580,20 @@ namespace polyvox
|
||||
}
|
||||
}
|
||||
|
||||
void FilamentViewer::stopAnimation() {
|
||||
// TODO - does this need to be threadsafe?
|
||||
embeddedAnimationBuffer = nullptr;
|
||||
}
|
||||
|
||||
void FilamentViewer::updateEmbeddedAnimation() {
|
||||
duration<double> dur = duration_cast<duration<double>>(std::chrono::high_resolution_clock::now() - embeddedAnimationBuffer->lastTime);
|
||||
duration<double> dur = duration_cast<duration<double>>(high_resolution_clock::now() - embeddedAnimationBuffer->lastTime);
|
||||
float startTime = 0;
|
||||
if(!embeddedAnimationBuffer->hasStarted) {
|
||||
embeddedAnimationBuffer->hasStarted = true;
|
||||
embeddedAnimationBuffer->lastTime = std::chrono::high_resolution_clock::now();
|
||||
embeddedAnimationBuffer->lastTime = high_resolution_clock::now();
|
||||
} else if(dur.count() >= embeddedAnimationBuffer->duration) {
|
||||
if(embeddedAnimationBuffer->loop) {
|
||||
embeddedAnimationBuffer->lastTime = std::chrono::high_resolution_clock::now();
|
||||
embeddedAnimationBuffer->lastTime = high_resolution_clock::now();
|
||||
} else {
|
||||
embeddedAnimationBuffer = nullptr;
|
||||
return;
|
||||
|
||||
@@ -76,12 +76,12 @@ namespace polyvox {
|
||||
MorphAnimationBuffer(float* frameData,
|
||||
int numWeights,
|
||||
int numFrames,
|
||||
float frameLength) : frameData(frameData), numWeights(numWeights), numFrames(numFrames), frameLength(frameLength) {
|
||||
float frameLength) : frameData(frameData), numWeights(numWeights), numFrames(numFrames), frameLengthInMs(frameLength) {
|
||||
}
|
||||
|
||||
int frameIndex = -1;
|
||||
int numFrames;
|
||||
float frameLength;
|
||||
float frameLengthInMs;
|
||||
time_point_t startTime;
|
||||
|
||||
float* frameData;
|
||||
@@ -102,10 +102,32 @@ namespace polyvox {
|
||||
unique_ptr<vector<string>> getTargetNames(const char* meshName);
|
||||
unique_ptr<vector<string>> getAnimationNames();
|
||||
Manipulator<float>* manipulator;
|
||||
|
||||
|
||||
///
|
||||
/// Manually set the weights for all morph targets in the assets to the provided values.
|
||||
/// See [animateWeights] if you want to automatically
|
||||
///
|
||||
void applyWeights(float* weights, int count);
|
||||
void animateWeights(float* data, int numWeights, int length, float frameRate);
|
||||
|
||||
///
|
||||
/// Update the asset's morph target weights every "frame" (which is an arbitrary length of time, i.e. this is not the same as a frame at the framerate of the underlying rendering framework).
|
||||
/// Accordingly:
|
||||
/// length(data) = numWeights * numFrames
|
||||
/// total_animation_duration_in_ms = number_of_frames * frameLengthInMs
|
||||
///
|
||||
void animateWeights(float* data, int numWeights, int numFrames, float frameLengthInMs);
|
||||
|
||||
///
|
||||
/// Play an embedded animation (i.e. an animation node embedded in the GLTF asset). If [loop] is true, the animation will repeat indefinitely.
|
||||
///
|
||||
void playAnimation(int index, bool loop);
|
||||
|
||||
///
|
||||
/// Immediately stop the currently playing animation. NOOP if no animation is playing.
|
||||
///
|
||||
void stopAnimation();
|
||||
|
||||
bool setCamera(const char* nodeName);
|
||||
void destroySwapChain();
|
||||
void createSwapChain(void* surface);
|
||||
|
||||
Reference in New Issue
Block a user