-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Make asset loading asynchronous and introduce hooks (#44)
Co-authored-by: Marc Rousavy <me@mrousavy.com>
- Loading branch information
Showing
16 changed files
with
175 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// | ||
// Created by Marc Rousavy on 12.03.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <fbjni/fbjni.h> | ||
#include <functional> | ||
|
||
namespace facebook { | ||
namespace jni { | ||
|
||
template <typename T> T WithJNIScope(std::function<T()>&& lambda) { | ||
T result; | ||
jni::ThreadScope::WithClassLoader([&result, lambda = std::move(lambda)]() { result = lambda(); }); | ||
return std::move(result); | ||
} | ||
|
||
} // namespace jni | ||
} // namespace facebook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useEffect, useState } from 'react' | ||
import { Asset } from '../native/FilamentBuffer' | ||
import { FilamentProxy } from '../native/FilamentProxy' | ||
|
||
export interface AssetProps { | ||
/** | ||
* A web URL (http:// or https://), local file (file://) or resource ID of the bundled asset. | ||
*/ | ||
path: string | ||
} | ||
|
||
/** | ||
* Asynchronously load an asset from the given web URL, local file path, or resource ID. | ||
*/ | ||
export function useAsset({ path }: AssetProps): Asset | undefined { | ||
const [asset, setAsset] = useState<Asset>() | ||
|
||
useEffect(() => { | ||
FilamentProxy.loadAsset(path) | ||
.then((a) => setAsset(a)) | ||
.catch((e) => console.error(`Failed to load asset ${path}!`, e)) | ||
}, [path]) | ||
|
||
return asset | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useEffect, useMemo } from 'react' | ||
import { AssetProps, useAsset } from './useAsset' | ||
import { Animator, Engine } from '../types' | ||
import { FilamentAsset } from '../types/FilamentAsset' | ||
|
||
interface ModelProps extends AssetProps { | ||
/** | ||
* The Filament engine this model should be loaded into. | ||
*/ | ||
engine: Engine | ||
/** | ||
* Whether source data of the model should be released after loading, or not. | ||
* @default true | ||
*/ | ||
shouldReleaseSourceData?: boolean | ||
} | ||
|
||
/** | ||
* The resulting filament model, or `'loading'` if not yet available. | ||
*/ | ||
export type FilamentModel = | ||
| { | ||
state: 'loaded' | ||
animator: Animator | ||
asset: FilamentAsset | ||
} | ||
| { | ||
state: 'loading' | ||
} | ||
|
||
/** | ||
* Use a Filament Model that gets asynchronously loaded into the given Engine. | ||
* @example | ||
* ```ts | ||
* const engine = useEngine() | ||
* const pengu = useModel({ engine: engine, path: PENGU_PATH }) | ||
* ``` | ||
*/ | ||
export function useModel({ path, engine, shouldReleaseSourceData }: ModelProps): FilamentModel { | ||
const asset = useAsset({ path: path }) | ||
|
||
const engineAsset = useMemo(() => { | ||
if (asset == null) return undefined | ||
return engine.loadAsset(asset) | ||
}, [asset, engine]) | ||
|
||
const animator = useMemo(() => engineAsset?.getAnimator(), [engineAsset]) | ||
|
||
useEffect(() => { | ||
if (shouldReleaseSourceData) { | ||
// releases CPU memory for bindings | ||
engineAsset?.releaseSourceData() | ||
} | ||
}, [engineAsset, shouldReleaseSourceData]) | ||
|
||
if (asset == null || engineAsset == null || animator == null) { | ||
return { | ||
state: 'loading', | ||
} | ||
} | ||
return { | ||
state: 'loaded', | ||
asset: engineAsset, | ||
animator: animator, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export interface FilamentBuffer {} | ||
|
||
export type Asset = FilamentBuffer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters