Skip to content

Commit

Permalink
Add ability to play animations on a separate instance
Browse files Browse the repository at this point in the history
  • Loading branch information
mentholyspirit committed Jun 15, 2024
1 parent b8ec165 commit 8f617eb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
15 changes: 15 additions & 0 deletions android/gltfio-android/src/main/cpp/Animator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,27 @@
#include <jni.h>

#include <gltfio/Animator.h>
#include <gltfio/FilamentInstance.h>

using namespace filament;
using namespace filament::math;
using namespace filament::gltfio;
using namespace utils;

extern "C" JNIEXPORT jlong JNICALL
Java_com_google_android_filament_gltfio_Animator_nCreateAnimatorFromAssetAndInstance(JNIEnv*, jclass, jlong nativeAsset, jlong nativeInstance) {
FilamentAsset* asset = (FilamentAsset*) nativeAsset;
FilamentInstance* instance = (FilamentInstance*) nativeInstance;
Animator* animator = new Animator(asset, instance);
return (jlong)animator;
}

extern "C" JNIEXPORT void JNICALL
Java_com_google_android_filament_gltfio_Animator_nDestroyAnimator(JNIEnv*, jclass, jlong nativeAnimator) {
Animator* animator = (Animator*) nativeAnimator;
delete animator;
}

extern "C" JNIEXPORT void JNICALL
Java_com_google_android_filament_gltfio_Animator_nApplyAnimation(JNIEnv*, jclass, jlong nativeAnimator,
jint index, jfloat time) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,30 @@
*/
public class Animator {
private long mNativeObject;
private Boolean mIsOwner = false;

Animator(long nativeObject) {
mNativeObject = nativeObject;
}

public Animator(FilamentAsset asset, FilamentInstance instance) {
mNativeObject = nCreateAnimatorFromAssetAndInstance(asset.getNativeObject(), instance.getNativeObject());
mIsOwner = true;
}

@Override
public void finalize() {
try {
super.finalize();
} catch (Throwable t) { // Ignore
} finally {
if (mIsOwner) {
nDestroyAnimator(mNativeObject);
mNativeObject = 0;
}
}
}

/**
* Applies rotation, translation, and scale to entities that have been targeted by the given
* animation definition. Uses <code>TransformManager</code>.
Expand Down Expand Up @@ -137,6 +156,8 @@ void clearNativeObject() {
mNativeObject = 0;
}

private static native long nCreateAnimatorFromAssetAndInstance(long nativeAsset, long nativeInstance);
private static native void nDestroyAnimator(long nativeAnimator);
private static native void nApplyAnimation(long nativeAnimator, int index, float time);
private static native void nUpdateBoneMatrices(long nativeAnimator);
private static native void nApplyCrossFade(long nativeAnimator, int animIndex, float animTime, float alpha);
Expand Down
6 changes: 5 additions & 1 deletion libs/gltfio/include/gltfio/Animator.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ class UTILS_PUBLIC Animator {

// If "instance" is null, then this is the primary animator.
Animator(FFilamentAsset const* asset, FFilamentInstance* instance);
~Animator();

Animator(const Animator& animator) = delete;
Animator(Animator&& animator) = delete;
Animator& operator=(const Animator&) = delete;

AnimatorImpl* mImpl;
public:
Animator(FilamentAsset *asset, FilamentInstance *instance) : Animator(reinterpret_cast<FFilamentAsset*>(asset), reinterpret_cast<FFilamentInstance*>(instance))
{
}
~Animator();
};

} // namespace filament::gltfio
Expand Down
3 changes: 2 additions & 1 deletion libs/gltfio/src/Animator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,6 @@ void AnimatorImpl::applyAnimation(const Channel& channel, float t, size_t prevIn
const TimeValues& times = sampler->times;
TrsTransformManager::Instance trsNode = trsTransformManager->getInstance(channel.targetEntity);
TransformManager::Instance node = transformManager->getInstance(channel.targetEntity);

switch (channel.transformType) {

case Channel::SCALE: {
Expand Down Expand Up @@ -561,9 +560,11 @@ void AnimatorImpl::updateBoneMatrices(FFilamentInstance* instance) {
}
for (size_t boneIndex = 0; boneIndex < njoints; ++boneIndex) {
const auto& joint = skin.joints[boneIndex];
assert_invariant(assetSkin.inverseBindMatrices.size() > boneIndex);
const mat4f& inverseBindMatrix = assetSkin.inverseBindMatrices[boneIndex];
TransformManager::Instance jointInstance = transformManager->getInstance(joint);
mat4 globalJointTransform = transformManager->getWorldTransformAccurate(jointInstance);
assert_invariant(boneMatrices.size() > boneIndex);
boneMatrices[boneIndex] =
mat4f{ inverseGlobalTransform * globalJointTransform } *
inverseBindMatrix;
Expand Down

0 comments on commit 8f617eb

Please sign in to comment.