Skip to content

Commit

Permalink
Feat/copy animations (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: Marc Rousavy <[email protected]>
  • Loading branch information
hannojg and mrousavy authored Mar 8, 2024
1 parent b470ec3 commit ca56b03
Show file tree
Hide file tree
Showing 20 changed files with 167 additions and 67 deletions.
9 changes: 8 additions & 1 deletion package/android/libs/filament/include/gltfio/Animator.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ class UTILS_PUBLIC Animator {
// For internal use only.
void addInstance(FFilamentInstance* instance);

// TODO(copy-animations): We currently copy animations from an asset onto another instance (different model than the original asset), we should replace this with once we found a good solution discussed here: https://github.com/google/filament/issues/7622
// Public constructor for creating an animator with the animations from an asset
// and applying the animations to a different instance.
Animator(FilamentAsset *asset, FilamentInstance *instance);

// TODO(copy-animations): We currently copy animations from an asset onto another instance (different model than the original asset), we should replace this with once we found a good solution discussed here: https://github.com/google/filament/issues/7622
~Animator();

private:

/*! \cond PRIVATE */
Expand All @@ -106,7 +114,6 @@ 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;
Expand Down
16 changes: 15 additions & 1 deletion package/cpp/core/AnimatorWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ void AnimatorWrapper::loadHybridMethods() {
}

Animator* AnimatorWrapper::getAnimator() {
if (_optionalAnimator != nullptr) {
return _optionalAnimator;
}

FilamentInstance* instance = _asset->getInstance();
if (instance == nullptr) {
[[unlikely]];
Expand All @@ -24,6 +28,16 @@ Animator* AnimatorWrapper::getAnimator() {
return instance->getAnimator();
}

// TODO(copy-animations): We currently copy animations from an asset onto another instance (different model than the original asset), we
// should replace this with once we found a good solution discussed here: https://github.com/google/filament/issues/7622
AnimatorWrapper::~AnimatorWrapper() {
if (_optionalAnimator != nullptr) {
#if ANDROID
delete _optionalAnimator;
#endif
}
}

void AnimatorWrapper::applyAnimation(int animationIndex, double time) {
Animator* animator = getAnimator();
animator->applyAnimation(animationIndex, time);
Expand Down Expand Up @@ -58,4 +72,4 @@ std::string AnimatorWrapper::getAnimationName(int animationIndex) {
Animator* animator = getAnimator();
return animator->getAnimationName(animationIndex);
}
} // namespace margelo
} // namespace margelo
9 changes: 9 additions & 0 deletions package/cpp/core/AnimatorWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ using namespace filament::gltfio;
class AnimatorWrapper : public HybridObject {
public:
explicit AnimatorWrapper(const std::shared_ptr<FilamentAsset>& asset) : HybridObject("AnimatorWrapper"), _asset(std::move(asset)) {}
// TODO(copy-animations): We currently copy animations from an asset onto another instance (different model than the original asset), we
// should replace this with once we found a good solution discussed here: https://github.com/google/filament/issues/7622
explicit AnimatorWrapper(const std::shared_ptr<FilamentAsset>& asset, Animator* optionalAnimator) : AnimatorWrapper(asset) {
_optionalAnimator = optionalAnimator;
}
~AnimatorWrapper() override;

void loadHybridMethods() override;

Expand All @@ -30,6 +36,9 @@ class AnimatorWrapper : public HybridObject {

private:
std::shared_ptr<FilamentAsset> _asset;
// TODO(copy-animations): We currently copy animations from an asset onto another instance (different model than the original asset), we
// should replace this with once we found a good solution discussed here: https://github.com/google/filament/issues/7622
Animator* _optionalAnimator;
};

} // namespace margelo
16 changes: 5 additions & 11 deletions package/cpp/core/EngineWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,6 @@ void EngineWrapper::renderFrame(double timestamp) {
_startTime = timestamp;
}

// if (_animator) {
// if (_animator->getAnimationCount() > 0) {
// _animator->applyAnimation(0, (timestamp - _startTime) / 1e9);
// }
// _animator->updateBoneMatrices();
// }

if (_renderCallback) {
// Call JS callback with scene information
double passedSeconds = (timestamp - _startTime) / 1e9;
Expand Down Expand Up @@ -269,8 +262,11 @@ std::shared_ptr<FilamentAssetWrapper> EngineWrapper::loadAsset(std::shared_ptr<F
throw std::runtime_error("Failed to load asset");
}
auto assetLoader = _assetLoader;
auto asset = References<gltfio::FilamentAsset>::adoptRef(
assetPtr, [assetLoader](gltfio::FilamentAsset* asset) { assetLoader->destroyAsset(asset); });
auto scene = _scene->getScene();
auto asset = References<gltfio::FilamentAsset>::adoptRef(assetPtr, [scene, assetLoader](gltfio::FilamentAsset* asset) {
scene->removeEntities(asset->getEntities(), asset->getEntityCount());
assetLoader->destroyAsset(asset);
});

if (!_scene) {
throw std::runtime_error("Scene not initialized");
Expand All @@ -282,8 +278,6 @@ std::shared_ptr<FilamentAssetWrapper> EngineWrapper::loadAsset(std::shared_ptr<F

_scene->addAsset(asset);
_resourceLoader->loadResources(asset.get());
_animator = asset->getInstance()->getAnimator();
asset->releaseSourceData();

return std::make_shared<FilamentAssetWrapper>(asset);
}
Expand Down
1 change: 0 additions & 1 deletion package/cpp/core/EngineWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class EngineWrapper : public HybridObject {
std::function<std::shared_ptr<FilamentBuffer>(std::string)> _getAssetBytes;
std::shared_ptr<Choreographer> _choreographer;
std::shared_ptr<Listener> _choreographerListener;
gltfio::Animator* _animator; // TODO: we currently only have one animator for one asset, need to have multiple in the future
double _startTime = 0;

// Internals that we might need to split out later
Expand Down
14 changes: 13 additions & 1 deletion package/cpp/core/FilamentAssetWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void FilamentAssetWrapper::loadHybridMethods() {
registerHybridMethod("getRoot", &FilamentAssetWrapper::getRoot, this);
registerHybridMethod("releaseSourceData", &FilamentAssetWrapper::releaseSourceData, this);
registerHybridMethod("getAnimator", &FilamentAssetWrapper::getAnimator, this);
registerHybridMethod("createAnimatorWithAnimationsFrom", &FilamentAssetWrapper::createAnimatorWithAnimationsFrom, this);
}

/**
Expand Down Expand Up @@ -40,4 +41,15 @@ std::shared_ptr<AnimatorWrapper> FilamentAssetWrapper::getAnimator() {
return std::make_shared<AnimatorWrapper>(_asset);
}

} // namespace margelo
std::shared_ptr<AnimatorWrapper> FilamentAssetWrapper::createAnimatorWithAnimationsFrom(std::shared_ptr<FilamentAssetWrapper> otherAsset) {
#if ANDROID
// TODO(copy-animations): We currently copy animations from an asset onto another instance (different model than the original asset), we
// should replace this with once we found a good solution discussed here: https://github.com/google/filament/issues/7622
Animator* animator = new gltfio::Animator(otherAsset->_asset.get(), _asset->getInstance());
return std::make_shared<AnimatorWrapper>(_asset, animator);
#else
return getAnimator();
#endif
}

} // namespace margelo
1 change: 1 addition & 0 deletions package/cpp/core/FilamentAssetWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class FilamentAssetWrapper : public HybridObject {
std::shared_ptr<EntityWrapper> getRoot();
void releaseSourceData();
std::shared_ptr<AnimatorWrapper> getAnimator();
std::shared_ptr<AnimatorWrapper> createAnimatorWithAnimationsFrom(std::shared_ptr<FilamentAssetWrapper> otherAsset);

private:
std::shared_ptr<gltfio::FilamentAsset> _asset;
Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions package/example/android/link-assets-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
{
"path": "assets/pengu.glb",
"sha1": "9c3aa64101b6af758735eeaafb48d482526d2c69"
},
{
"path": "assets/pijamas.glb",
"sha1": "cdd1368ca552bdcb54457ed9628235a8de203dad"
},
{
"path": "assets/pirate.glb",
"sha1": "d4290fad8455ae940baa90e59d70659c021590f7"
}
]
}
Binary file added package/example/assets/pijamas.glb
Binary file not shown.
Binary file added package/example/assets/pirate.glb
Binary file not shown.
32 changes: 16 additions & 16 deletions package/example/ios/FilamentExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
/* Begin PBXBuildFile section */
00E356F31AD99517003FC87E /* FilamentExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* FilamentExampleTests.m */; };
0B65F1CE0E1D4DBB9E25D0BD /* default_env_ibl.ktx in Resources */ = {isa = PBXBuildFile; fileRef = 7A69823713B34897AF31CAF0 /* default_env_ibl.ktx */; };
0BF3FBAC90C441EAA712EE28 /* pijamas.glb in Resources */ = {isa = PBXBuildFile; fileRef = BE173E0B3B23427D84C92C91 /* pijamas.glb */; };
0C80B921A6F3F58F76C31292 /* libPods-FilamentExample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5DCACB8F33CDC322A6C60F78 /* libPods-FilamentExample.a */; };
13B07FBC1A68108700A75B9A /* AppDelegate.mm in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.mm */; };
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; };
13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; };
59249653BB174593BEDB9F3D /* pengu.glb in Resources */ = {isa = PBXBuildFile; fileRef = 3E2439B417DA497E83EB1F05 /* pengu.glb */; };
7699B88040F8A987B510C191 /* libPods-FilamentExample-FilamentExampleTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 19F6CBCC0A4E27FBF8BF4A61 /* libPods-FilamentExample-FilamentExampleTests.a */; };
81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; };
0B65F1CE0E1D4DBB9E25D0BD /* default_env_ibl.ktx in Resources */ = {isa = PBXBuildFile; fileRef = 7A69823713B34897AF31CAF0 /* default_env_ibl.ktx */; };
59249653BB174593BEDB9F3D /* pengu.glb in Resources */ = {isa = PBXBuildFile; fileRef = 3E2439B417DA497E83EB1F05 /* pengu.glb */; };
94671BF6DAD54DF8A7B842C3 /* pirate.glb in Resources */ = {isa = PBXBuildFile; fileRef = 034C2E5BF6EE449290528E8D /* pirate.glb */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -34,6 +34,7 @@
00E356EE1AD99517003FC87E /* FilamentExampleTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FilamentExampleTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
00E356F11AD99517003FC87E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
00E356F21AD99517003FC87E /* FilamentExampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FilamentExampleTests.m; sourceTree = "<group>"; };
034C2E5BF6EE449290528E8D /* pirate.glb */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = undefined; includeInIndex = 0; lastKnownFileType = unknown; name = pirate.glb; path = ../assets/pirate.glb; sourceTree = "<group>"; };
13B07F961A680F5B00A75B9A /* FilamentExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FilamentExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
13B07FAF1A68108700A75B9A /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AppDelegate.h; path = FilamentExample/AppDelegate.h; sourceTree = "<group>"; };
13B07FB01A68108700A75B9A /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = FilamentExample/AppDelegate.mm; sourceTree = "<group>"; };
Expand All @@ -49,9 +50,8 @@
7A69823713B34897AF31CAF0 /* default_env_ibl.ktx */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = undefined; includeInIndex = 0; lastKnownFileType = unknown; name = default_env_ibl.ktx; path = ../assets/default_env_ibl.ktx; sourceTree = "<group>"; };
81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = FilamentExample/LaunchScreen.storyboard; sourceTree = "<group>"; };
89C6BE57DB24E9ADA2F236DE /* Pods-FilamentExample-FilamentExampleTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FilamentExample-FilamentExampleTests.release.xcconfig"; path = "Target Support Files/Pods-FilamentExample-FilamentExampleTests/Pods-FilamentExample-FilamentExampleTests.release.xcconfig"; sourceTree = "<group>"; };
BE173E0B3B23427D84C92C91 /* pijamas.glb */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = undefined; includeInIndex = 0; lastKnownFileType = unknown; name = pijamas.glb; path = ../assets/pijamas.glb; sourceTree = "<group>"; };
ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; };
7A69823713B34897AF31CAF0 /* default_env_ibl.ktx */ = {isa = PBXFileReference; name = "default_env_ibl.ktx"; path = "../assets/default_env_ibl.ktx"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
3E2439B417DA497E83EB1F05 /* pengu.glb */ = {isa = PBXFileReference; name = "pengu.glb"; path = "../assets/pengu.glb"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -151,6 +151,8 @@
children = (
7A69823713B34897AF31CAF0 /* default_env_ibl.ktx */,
3E2439B417DA497E83EB1F05 /* pengu.glb */,
BE173E0B3B23427D84C92C91 /* pijamas.glb */,
034C2E5BF6EE449290528E8D /* pirate.glb */,
);
name = Resources;
path = "";
Expand All @@ -167,16 +169,6 @@
path = Pods;
sourceTree = "<group>";
};
995DFB4F30584326AD631A9F /* Resources */ = {
isa = "PBXGroup";
children = (
7A69823713B34897AF31CAF0 /* default_env_ibl.ktx */,
3E2439B417DA497E83EB1F05 /* pengu.glb */,
);
name = Resources;
sourceTree = "<group>";
path = "";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -274,6 +266,8 @@
13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */,
0B65F1CE0E1D4DBB9E25D0BD /* default_env_ibl.ktx in Resources */,
59249653BB174593BEDB9F3D /* pengu.glb in Resources */,
0BF3FBAC90C441EAA712EE28 /* pijamas.glb in Resources */,
94671BF6DAD54DF8A7B842C3 /* pirate.glb in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -613,7 +607,10 @@
"-DFOLLY_USE_LIBCPP=1",
"-DFOLLY_CFG_NO_COROUTINES=1",
);
OTHER_LDFLAGS = "$(inherited) ";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
Expand Down Expand Up @@ -681,7 +678,10 @@
"-DFOLLY_USE_LIBCPP=1",
"-DFOLLY_CFG_NO_COROUTINES=1",
);
OTHER_LDFLAGS = "$(inherited) ";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
Expand Down
4 changes: 2 additions & 2 deletions package/example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ PODS:
- React-Mapbuffer (0.73.4):
- glog
- React-debug
- react-native-filament (0.0.1):
- react-native-filament (0.1.0):
- Filament
- glog
- RCT-Folly (= 2022.05.16.00)
Expand Down Expand Up @@ -1297,7 +1297,7 @@ SPEC CHECKSUMS:
React-jsinspector: 9ac353eccf6ab54d1e0a33862ba91221d1e88460
React-logger: 0a57b68dd2aec7ff738195f081f0520724b35dab
React-Mapbuffer: 63913773ed7f96b814a2521e13e6d010282096ad
react-native-filament: 457f8a2d45ec68ef004c0de3357e4457abdcd9de
react-native-filament: 0bd3b196f2886a27b01457fe0a5b03a4e1f3d4da
React-nativeconfig: d7af5bae6da70fa15ce44f045621cf99ed24087c
React-NativeModulesApple: 0123905d5699853ac68519607555a9a4f5c7b3ac
React-perflogger: 8a1e1af5733004bdd91258dcefbde21e0d1faccd
Expand Down
8 changes: 8 additions & 0 deletions package/example/ios/link-assets-manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
{
"path": "assets/pengu.glb",
"sha1": "9c3aa64101b6af758735eeaafb48d482526d2c69"
},
{
"path": "assets/pijamas.glb",
"sha1": "cdd1368ca552bdcb54457ed9628235a8de203dad"
},
{
"path": "assets/pirate.glb",
"sha1": "d4290fad8455ae940baa90e59d70659c021590f7"
}
]
}
39 changes: 39 additions & 0 deletions package/filament_animator_feat.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
diff --git a/libs/gltfio/include/gltfio/Animator.h b/libs/gltfio/include/gltfio/Animator.h
index 199555a40..f426edf0e 100644
--- a/libs/gltfio/include/gltfio/Animator.h
+++ b/libs/gltfio/include/gltfio/Animator.h
@@ -97,6 +97,12 @@ public:
// For internal use only.
void addInstance(FFilamentInstance* instance);

+ // Public constructor for creating an animator with the animations from an asset
+ // and applying the animations to a different instance.
+ Animator(FilamentAsset *asset, FilamentInstance *instance);
+
+ ~Animator();
+
private:

/*! \cond PRIVATE */
@@ -106,7 +112,6 @@ private:

// 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;
diff --git a/libs/gltfio/src/Animator.cpp b/libs/gltfio/src/Animator.cpp
index 99a4269d0..036adcb8d 100644
--- a/libs/gltfio/src/Animator.cpp
+++ b/libs/gltfio/src/Animator.cpp
@@ -238,6 +238,9 @@ Animator::Animator(FFilamentAsset const* asset, FFilamentInstance* instance) {
}
}

+Animator::Animator(FilamentAsset *asset, FilamentInstance *instance) : Animator(downcast(asset), downcast(instance)) {
+}
+
void Animator::applyCrossFade(size_t previousAnimIndex, float previousAnimTime, float alpha) {
mImpl->stashCrossFade();
applyAnimation(previousAnimIndex, previousAnimTime);
5 changes: 3 additions & 2 deletions package/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-filament",
"version": "0.0.1",
"version": "0.1.0",
"description": "A real-time physically based 3D rendering engine for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down Expand Up @@ -32,7 +32,8 @@
"clean": "rm -rf android/build example/android/build example/android/app/build example/ios/build lib",
"prepare": "git submodule update --init --recursive && bob build",
"update-submodule": "git submodule update --remote --merge",
"build-filament": "scripts/build-filament.sh",
"patch-filament": "cd ../filament ; git apply ../package/filament_animator_feat.patch ; cd -",
"build-filament": "yarn patch-filament && scripts/build-filament.sh",
"release": "yarn build-filament && release-it"
},
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion package/scripts/build-filament.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ echo "Building Filament for Android ($target)"
echo "Copying Filament Android libraries to react-native-filament..."
rm -rf ../package/android/libs/filament
mkdir -p ../package/android/libs/filament
cp -rf out/android-release/filament ../package/android/libs
cp -rf out/android-$target/filament ../package/android/libs

echo "Done!"
Loading

0 comments on commit ca56b03

Please sign in to comment.