From 5de7976ded8f02ef9090d2e6a15fb56c50748b82 Mon Sep 17 00:00:00 2001 From: tibi <39774620+mentholyspirit@users.noreply.github.com> Date: Wed, 12 Jun 2024 09:07:47 +0300 Subject: [PATCH 1/2] Add ability to play animations on a separate instance --- .../gltfio-android/src/main/cpp/Animator.cpp | 15 +++++++++++++ .../android/filament/gltfio/Animator.java | 21 +++++++++++++++++++ libs/gltfio/include/gltfio/Animator.h | 6 +++++- libs/gltfio/src/Animator.cpp | 3 ++- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/android/gltfio-android/src/main/cpp/Animator.cpp b/android/gltfio-android/src/main/cpp/Animator.cpp index eade9634de8..94cfd73e5b8 100644 --- a/android/gltfio-android/src/main/cpp/Animator.cpp +++ b/android/gltfio-android/src/main/cpp/Animator.cpp @@ -17,12 +17,27 @@ #include #include +#include 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) { diff --git a/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java b/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java index cf2e86f071a..6add0c5d611 100644 --- a/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java +++ b/android/gltfio-android/src/main/java/com/google/android/filament/gltfio/Animator.java @@ -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 TransformManager. @@ -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); diff --git a/libs/gltfio/include/gltfio/Animator.h b/libs/gltfio/include/gltfio/Animator.h index 199555a40d3..1a2a702182e 100644 --- a/libs/gltfio/include/gltfio/Animator.h +++ b/libs/gltfio/include/gltfio/Animator.h @@ -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(asset), reinterpret_cast(instance)) + { + } + ~Animator(); }; } // namespace filament::gltfio diff --git a/libs/gltfio/src/Animator.cpp b/libs/gltfio/src/Animator.cpp index 6e53b705c60..878455a4c5d 100644 --- a/libs/gltfio/src/Animator.cpp +++ b/libs/gltfio/src/Animator.cpp @@ -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: { @@ -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; From a1be171e4c5765aff35e87a377b11fefad0e072c Mon Sep 17 00:00:00 2001 From: tibi <39774620+mentholyspirit@users.noreply.github.com> Date: Sat, 29 Jun 2024 15:23:58 +0300 Subject: [PATCH 2/2] Move constructor implementation to source file and add documentation --- libs/gltfio/include/gltfio/Animator.h | 13 ++++++++----- libs/gltfio/src/Animator.cpp | 3 +++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/libs/gltfio/include/gltfio/Animator.h b/libs/gltfio/include/gltfio/Animator.h index 1a2a702182e..fda05db29fc 100644 --- a/libs/gltfio/include/gltfio/Animator.h +++ b/libs/gltfio/include/gltfio/Animator.h @@ -97,6 +97,14 @@ class UTILS_PUBLIC Animator { // For internal use only. void addInstance(FFilamentInstance* instance); + /** Creates an Animator that will use the corresponding asset and instance, + * can be used to animate an instance with animation from another asset. + * When used, the caller is responsible for deleting it. + * The normal way of getting an Animator is to call FilamentInstance::getAnimator(). + */ + Animator(FilamentAsset *asset, FilamentInstance *instance); + ~Animator(); + private: /*! \cond PRIVATE */ @@ -112,11 +120,6 @@ class UTILS_PUBLIC Animator { Animator& operator=(const Animator&) = delete; AnimatorImpl* mImpl; -public: - Animator(FilamentAsset *asset, FilamentInstance *instance) : Animator(reinterpret_cast(asset), reinterpret_cast(instance)) - { - } - ~Animator(); }; } // namespace filament::gltfio diff --git a/libs/gltfio/src/Animator.cpp b/libs/gltfio/src/Animator.cpp index 878455a4c5d..af1cc107e51 100644 --- a/libs/gltfio/src/Animator.cpp +++ b/libs/gltfio/src/Animator.cpp @@ -254,6 +254,9 @@ void Animator::addInstance(FFilamentInstance* instance) { } } +Animator::Animator(FilamentAsset *asset, FilamentInstance *instance) : Animator(reinterpret_cast(asset), reinterpret_cast(instance)) { +} + Animator::~Animator() { delete mImpl; }