diff --git a/package/android/libs/filament/include/gltfio/Animator.h b/package/android/libs/filament/include/gltfio/Animator.h index 199555a4..3b5bcaad 100644 --- a/package/android/libs/filament/include/gltfio/Animator.h +++ b/package/android/libs/filament/include/gltfio/Animator.h @@ -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 */ @@ -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; diff --git a/package/cpp/core/AnimatorWrapper.cpp b/package/cpp/core/AnimatorWrapper.cpp index aeb0ff82..ff663c8e 100644 --- a/package/cpp/core/AnimatorWrapper.cpp +++ b/package/cpp/core/AnimatorWrapper.cpp @@ -16,6 +16,10 @@ void AnimatorWrapper::loadHybridMethods() { } Animator* AnimatorWrapper::getAnimator() { + if (_optionalAnimator != nullptr) { + return _optionalAnimator; + } + FilamentInstance* instance = _asset->getInstance(); if (instance == nullptr) { [[unlikely]]; @@ -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); @@ -58,4 +72,4 @@ std::string AnimatorWrapper::getAnimationName(int animationIndex) { Animator* animator = getAnimator(); return animator->getAnimationName(animationIndex); } -} // namespace margelo \ No newline at end of file +} // namespace margelo diff --git a/package/cpp/core/AnimatorWrapper.h b/package/cpp/core/AnimatorWrapper.h index 41a84293..2a95e55f 100644 --- a/package/cpp/core/AnimatorWrapper.h +++ b/package/cpp/core/AnimatorWrapper.h @@ -14,6 +14,12 @@ using namespace filament::gltfio; class AnimatorWrapper : public HybridObject { public: explicit AnimatorWrapper(const std::shared_ptr& 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& asset, Animator* optionalAnimator) : AnimatorWrapper(asset) { + _optionalAnimator = optionalAnimator; + } + ~AnimatorWrapper() override; void loadHybridMethods() override; @@ -30,6 +36,9 @@ class AnimatorWrapper : public HybridObject { private: std::shared_ptr _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 diff --git a/package/cpp/core/EngineWrapper.cpp b/package/cpp/core/EngineWrapper.cpp index e4a154fe..71ec296a 100644 --- a/package/cpp/core/EngineWrapper.cpp +++ b/package/cpp/core/EngineWrapper.cpp @@ -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; @@ -269,8 +262,11 @@ std::shared_ptr EngineWrapper::loadAsset(std::shared_ptr::adoptRef( - assetPtr, [assetLoader](gltfio::FilamentAsset* asset) { assetLoader->destroyAsset(asset); }); + auto scene = _scene->getScene(); + auto asset = References::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"); @@ -282,8 +278,6 @@ std::shared_ptr EngineWrapper::loadAsset(std::shared_ptraddAsset(asset); _resourceLoader->loadResources(asset.get()); - _animator = asset->getInstance()->getAnimator(); - asset->releaseSourceData(); return std::make_shared(asset); } diff --git a/package/cpp/core/EngineWrapper.h b/package/cpp/core/EngineWrapper.h index 72983759..9f486b74 100644 --- a/package/cpp/core/EngineWrapper.h +++ b/package/cpp/core/EngineWrapper.h @@ -86,7 +86,6 @@ class EngineWrapper : public HybridObject { std::function(std::string)> _getAssetBytes; std::shared_ptr _choreographer; std::shared_ptr _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 diff --git a/package/cpp/core/FilamentAssetWrapper.cpp b/package/cpp/core/FilamentAssetWrapper.cpp index 3c988df7..f4b8aee0 100644 --- a/package/cpp/core/FilamentAssetWrapper.cpp +++ b/package/cpp/core/FilamentAssetWrapper.cpp @@ -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); } /** @@ -40,4 +41,15 @@ std::shared_ptr FilamentAssetWrapper::getAnimator() { return std::make_shared(_asset); } -} // namespace margelo \ No newline at end of file +std::shared_ptr FilamentAssetWrapper::createAnimatorWithAnimationsFrom(std::shared_ptr 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(_asset, animator); +#else + return getAnimator(); +#endif +} + +} // namespace margelo diff --git a/package/cpp/core/FilamentAssetWrapper.h b/package/cpp/core/FilamentAssetWrapper.h index 741d5f81..d62946d9 100644 --- a/package/cpp/core/FilamentAssetWrapper.h +++ b/package/cpp/core/FilamentAssetWrapper.h @@ -22,6 +22,7 @@ class FilamentAssetWrapper : public HybridObject { std::shared_ptr getRoot(); void releaseSourceData(); std::shared_ptr getAnimator(); + std::shared_ptr createAnimatorWithAnimationsFrom(std::shared_ptr otherAsset); private: std::shared_ptr _asset; diff --git a/package/example/android/app/src/main/assets/custom/pijamas.glb b/package/example/android/app/src/main/assets/custom/pijamas.glb new file mode 100644 index 00000000..edf2de6b Binary files /dev/null and b/package/example/android/app/src/main/assets/custom/pijamas.glb differ diff --git a/package/example/android/app/src/main/assets/custom/pirate.glb b/package/example/android/app/src/main/assets/custom/pirate.glb new file mode 100644 index 00000000..09b5e4c2 Binary files /dev/null and b/package/example/android/app/src/main/assets/custom/pirate.glb differ diff --git a/package/example/android/link-assets-manifest.json b/package/example/android/link-assets-manifest.json index 88c14733..e68bdc99 100644 --- a/package/example/android/link-assets-manifest.json +++ b/package/example/android/link-assets-manifest.json @@ -8,6 +8,14 @@ { "path": "assets/pengu.glb", "sha1": "9c3aa64101b6af758735eeaafb48d482526d2c69" + }, + { + "path": "assets/pijamas.glb", + "sha1": "cdd1368ca552bdcb54457ed9628235a8de203dad" + }, + { + "path": "assets/pirate.glb", + "sha1": "d4290fad8455ae940baa90e59d70659c021590f7" } ] } diff --git a/package/example/assets/pijamas.glb b/package/example/assets/pijamas.glb new file mode 100644 index 00000000..edf2de6b Binary files /dev/null and b/package/example/assets/pijamas.glb differ diff --git a/package/example/assets/pirate.glb b/package/example/assets/pirate.glb new file mode 100644 index 00000000..09b5e4c2 Binary files /dev/null and b/package/example/assets/pirate.glb differ diff --git a/package/example/ios/FilamentExample.xcodeproj/project.pbxproj b/package/example/ios/FilamentExample.xcodeproj/project.pbxproj index 77b7b93c..696e91b6 100644 --- a/package/example/ios/FilamentExample.xcodeproj/project.pbxproj +++ b/package/example/ios/FilamentExample.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* 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 */; }; @@ -16,8 +17,7 @@ 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 */ @@ -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 = ""; }; 00E356F21AD99517003FC87E /* FilamentExampleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FilamentExampleTests.m; sourceTree = ""; }; + 034C2E5BF6EE449290528E8D /* pirate.glb */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = undefined; includeInIndex = 0; lastKnownFileType = unknown; name = pirate.glb; path = ../assets/pirate.glb; sourceTree = ""; }; 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 = ""; }; 13B07FB01A68108700A75B9A /* AppDelegate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = AppDelegate.mm; path = FilamentExample/AppDelegate.mm; sourceTree = ""; }; @@ -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 = ""; }; 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = FilamentExample/LaunchScreen.storyboard; sourceTree = ""; }; 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 = ""; }; + BE173E0B3B23427D84C92C91 /* pijamas.glb */ = {isa = PBXFileReference; explicitFileType = undefined; fileEncoding = undefined; includeInIndex = 0; lastKnownFileType = unknown; name = pijamas.glb; path = ../assets/pijamas.glb; sourceTree = ""; }; 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 = ""; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; }; - 3E2439B417DA497E83EB1F05 /* pengu.glb */ = {isa = PBXFileReference; name = "pengu.glb"; path = "../assets/pengu.glb"; sourceTree = ""; fileEncoding = undefined; lastKnownFileType = unknown; explicitFileType = undefined; includeInIndex = 0; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -151,6 +151,8 @@ children = ( 7A69823713B34897AF31CAF0 /* default_env_ibl.ktx */, 3E2439B417DA497E83EB1F05 /* pengu.glb */, + BE173E0B3B23427D84C92C91 /* pijamas.glb */, + 034C2E5BF6EE449290528E8D /* pirate.glb */, ); name = Resources; path = ""; @@ -167,16 +169,6 @@ path = Pods; sourceTree = ""; }; - 995DFB4F30584326AD631A9F /* Resources */ = { - isa = "PBXGroup"; - children = ( - 7A69823713B34897AF31CAF0 /* default_env_ibl.ktx */, - 3E2439B417DA497E83EB1F05 /* pengu.glb */, - ); - name = Resources; - sourceTree = ""; - path = ""; - }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -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; }; @@ -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; @@ -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; diff --git a/package/example/ios/Podfile.lock b/package/example/ios/Podfile.lock index 2c0ba5aa..cff623b0 100644 --- a/package/example/ios/Podfile.lock +++ b/package/example/ios/Podfile.lock @@ -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) @@ -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 diff --git a/package/example/ios/link-assets-manifest.json b/package/example/ios/link-assets-manifest.json index 88c14733..e68bdc99 100644 --- a/package/example/ios/link-assets-manifest.json +++ b/package/example/ios/link-assets-manifest.json @@ -8,6 +8,14 @@ { "path": "assets/pengu.glb", "sha1": "9c3aa64101b6af758735eeaafb48d482526d2c69" + }, + { + "path": "assets/pijamas.glb", + "sha1": "cdd1368ca552bdcb54457ed9628235a8de203dad" + }, + { + "path": "assets/pirate.glb", + "sha1": "d4290fad8455ae940baa90e59d70659c021590f7" } ] } diff --git a/package/filament_animator_feat.patch b/package/filament_animator_feat.patch new file mode 100644 index 00000000..ef9f2cd5 --- /dev/null +++ b/package/filament_animator_feat.patch @@ -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); diff --git a/package/package.json b/package/package.json index 22bdb879..6863631f 100644 --- a/package/package.json +++ b/package/package.json @@ -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", @@ -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": [ diff --git a/package/scripts/build-filament.sh b/package/scripts/build-filament.sh index f01e945e..0afe2df5 100755 --- a/package/scripts/build-filament.sh +++ b/package/scripts/build-filament.sh @@ -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!" diff --git a/package/src/FilamentView.tsx b/package/src/FilamentView.tsx index 51dc3b1f..266fc194 100644 --- a/package/src/FilamentView.tsx +++ b/package/src/FilamentView.tsx @@ -19,22 +19,28 @@ const indirectLightPath = Platform.select({ ios: 'default_env_ibl.ktx', })! +const pirateHatPath = Platform.select({ + android: 'custom/pirate.glb', + ios: 'pirate.glb', +})! + +const pijamaPath = Platform.select({ + android: 'custom/pijamas.glb', + ios: 'pijamas.glb', +})! + // Temporarily type _State = { animationNames: string[] currentAnimation: number } -const animationInterpolationTime = 5 // 1 second - export class FilamentView extends React.PureComponent { private readonly ref: React.RefObject private readonly engine = FilamentProxy.createEngine() private animator: Animator - - private prevAnimationIndex: number | null = null - private prevAnimationStarted: number | null = null - private animationInterpolation = 0 + private hatAnimator: Animator + private pijamaAnimator: Animator constructor(props: FilamentViewProps) { super(props) @@ -46,8 +52,18 @@ export class FilamentView extends React.PureComponent animationNames: Array.from({ length: this.animator.getAnimationCount() }, (_, i) => this.animator.getAnimationName(i)), currentAnimation: 0, } - pengu.releaseSourceData() // Cleanup memory after loading the asset - console.log('animationNames', this.state.animationNames) + + const pirateHatBuffer = FilamentProxy.getAssetByteBuffer(pirateHatPath) + const pirateHatAsset = this.engine.loadAsset(pirateHatBuffer) + this.hatAnimator = pirateHatAsset.createAnimatorWithAnimationsFrom(pengu) + const pijamaBuffer = FilamentProxy.getAssetByteBuffer(pijamaPath) + const pijamaAsset = this.engine.loadAsset(pijamaBuffer) + this.pijamaAnimator = pijamaAsset.createAnimatorWithAnimationsFrom(pengu) + + // Cleanup memory after loading the asset + pengu.releaseSourceData() + pirateHatAsset.releaseSourceData() + pijamaAsset.releaseSourceData() this.setup3dScene() } @@ -74,10 +90,6 @@ export class FilamentView extends React.PureComponent const modelBuffer = FilamentProxy.getAssetByteBuffer(penguModelPath) const penguAsset = this.engine.loadAsset(modelBuffer) - // By default all assets get added to the origin at 0,0,0, - // we transform it to fit into a unit cube at the origin using this utility: - this.engine.transformToUnitCube(penguAsset) - return penguAsset } @@ -92,31 +104,22 @@ export class FilamentView extends React.PureComponent } renderCallback = (_timestamp: number, _startTime: number, passedSeconds: number) => { - const cameraPosition: Float3 = [0, 0, 5] + const cameraPosition: Float3 = [0, 0, 8] const cameraTarget: Float3 = [0, 0, 0] const cameraUp: Float3 = [0, 1, 0] if (this.animator) { this.animator.applyAnimation(this.state.currentAnimation, passedSeconds) - if (this.prevAnimationIndex !== null) { - if (this.prevAnimationStarted === null) { - this.prevAnimationStarted = passedSeconds - } - this.animationInterpolation += passedSeconds - this.prevAnimationStarted - const alpha = this.animationInterpolation / animationInterpolationTime - - this.animator.applyCrossFade(this.prevAnimationIndex, this.prevAnimationStarted!, alpha) - console.log('this.animationInterpolation', this.animationInterpolation) - console.log('passedSeconds', passedSeconds) - if (this.animationInterpolation >= animationInterpolationTime) { - this.prevAnimationIndex = null - this.prevAnimationStarted = null - this.animationInterpolation = 0 - } - } - this.animator.updateBoneMatrices() } + if (this.hatAnimator) { + this.hatAnimator.applyAnimation(this.state.currentAnimation, passedSeconds) + this.hatAnimator.updateBoneMatrices() + } + if (this.pijamaAnimator) { + this.pijamaAnimator.applyAnimation(this.state.currentAnimation, passedSeconds) + this.pijamaAnimator.updateBoneMatrices() + } this.engine.getCamera().lookAt(cameraPosition, cameraTarget, cameraUp) } @@ -154,7 +157,7 @@ export class FilamentView extends React.PureComponent