Skip to content

Commit 0975047

Browse files
Add ability to play animations on a separate instance
1 parent c7202c5 commit 0975047

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

android/gltfio-android/src/main/cpp/Animator.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,27 @@
1717
#include <jni.h>
1818

1919
#include <gltfio/Animator.h>
20+
#include <gltfio/FilamentInstance.h>
2021

2122
using namespace filament;
2223
using namespace filament::math;
2324
using namespace filament::gltfio;
2425
using namespace utils;
2526

27+
extern "C" JNIEXPORT jlong JNICALL
28+
Java_com_google_android_filament_gltfio_Animator_nCreateAnimatorFromAssetAndInstance(JNIEnv*, jclass, jlong nativeAsset, jlong nativeInstance) {
29+
FilamentAsset* asset = (FilamentAsset*) nativeAsset;
30+
FilamentInstance* instance = (FilamentInstance*) nativeInstance;
31+
Animator* animator = new Animator(asset, instance);
32+
return (jlong)animator;
33+
}
34+
35+
extern "C" JNIEXPORT void JNICALL
36+
Java_com_google_android_filament_gltfio_Animator_nDestroyAnimator(JNIEnv*, jclass, jlong nativeAnimator) {
37+
Animator* animator = (Animator*) nativeAnimator;
38+
delete animator;
39+
}
40+
2641
extern "C" JNIEXPORT void JNICALL
2742
Java_com_google_android_filament_gltfio_Animator_nApplyAnimation(JNIEnv*, jclass, jlong nativeAnimator,
2843
jint index, jfloat time) {

android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java

+21
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,30 @@
3737
*/
3838
public class Animator {
3939
private long mNativeObject;
40+
private Boolean mIsOwner = false;
4041

4142
Animator(long nativeObject) {
4243
mNativeObject = nativeObject;
4344
}
4445

46+
public Animator(FilamentAsset asset, FilamentInstance instance) {
47+
mNativeObject = nCreateAnimatorFromAssetAndInstance(asset.getNativeObject(), instance.getNativeObject());
48+
mIsOwner = true;
49+
}
50+
51+
@Override
52+
public void finalize() {
53+
try {
54+
super.finalize();
55+
} catch (Throwable t) { // Ignore
56+
} finally {
57+
if (mIsOwner) {
58+
nDestroyAnimator(mNativeObject);
59+
mNativeObject = 0;
60+
}
61+
}
62+
}
63+
4564
/**
4665
* Applies rotation, translation, and scale to entities that have been targeted by the given
4766
* animation definition. Uses <code>TransformManager</code>.
@@ -137,6 +156,8 @@ void clearNativeObject() {
137156
mNativeObject = 0;
138157
}
139158

159+
private static native long nCreateAnimatorFromAssetAndInstance(long nativeAsset, long nativeInstance);
160+
private static native void nDestroyAnimator(long nativeAnimator);
140161
private static native void nApplyAnimation(long nativeAnimator, int index, float time);
141162
private static native void nUpdateBoneMatrices(long nativeAnimator);
142163
private static native void nApplyCrossFade(long nativeAnimator, int animIndex, float animTime, float alpha);

libs/gltfio/include/gltfio/Animator.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,17 @@ class UTILS_PUBLIC Animator {
106106

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

111110
Animator(const Animator& animator) = delete;
112111
Animator(Animator&& animator) = delete;
113112
Animator& operator=(const Animator&) = delete;
114113

115114
AnimatorImpl* mImpl;
115+
public:
116+
Animator(FilamentAsset *asset, FilamentInstance *instance) : Animator(reinterpret_cast<FFilamentAsset*>(asset), reinterpret_cast<FFilamentInstance*>(instance))
117+
{
118+
}
119+
~Animator();
116120
};
117121

118122
} // namespace filament::gltfio

libs/gltfio/src/Animator.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,6 @@ void AnimatorImpl::applyAnimation(const Channel& channel, float t, size_t prevIn
437437
const TimeValues& times = sampler->times;
438438
TrsTransformManager::Instance trsNode = trsTransformManager->getInstance(channel.targetEntity);
439439
TransformManager::Instance node = transformManager->getInstance(channel.targetEntity);
440-
441440
switch (channel.transformType) {
442441

443442
case Channel::SCALE: {
@@ -561,9 +560,11 @@ void AnimatorImpl::updateBoneMatrices(FFilamentInstance* instance) {
561560
}
562561
for (size_t boneIndex = 0; boneIndex < njoints; ++boneIndex) {
563562
const auto& joint = skin.joints[boneIndex];
563+
assert_invariant(assetSkin.inverseBindMatrices.size() > boneIndex);
564564
const mat4f& inverseBindMatrix = assetSkin.inverseBindMatrices[boneIndex];
565565
TransformManager::Instance jointInstance = transformManager->getInstance(joint);
566566
mat4 globalJointTransform = transformManager->getWorldTransformAccurate(jointInstance);
567+
assert_invariant(boneMatrices.size() > boneIndex);
567568
boneMatrices[boneIndex] =
568569
mat4f{ inverseGlobalTransform * globalJointTransform } *
569570
inverseBindMatrix;

0 commit comments

Comments
 (0)