rename relativeResourcePath to resourceUri

move resource loader from viewer to FilamentApp
This commit is contained in:
Nick Fisher
2025-05-17 21:50:58 +08:00
parent fe11479b08
commit d3ec825688
3 changed files with 56 additions and 21 deletions

View File

@@ -5,7 +5,7 @@ class FilamentConfig<T, U> {
final Backend backend; final Backend backend;
final T? renderCallback; final T? renderCallback;
final U? renderCallbackOwner; final U? renderCallbackOwner;
Future<Uint8List> Function(String)? resourceLoader; Future<Uint8List> Function(String)? loadResource;
final U? platform; final U? platform;
final U? sharedContext; final U? sharedContext;
final String? uberArchivePath; final String? uberArchivePath;
@@ -14,7 +14,7 @@ class FilamentConfig<T, U> {
FilamentConfig( FilamentConfig(
{required this.backend, {required this.backend,
required this.resourceLoader, required this.loadResource,
this.uberArchivePath, this.uberArchivePath,
this.renderCallback, this.renderCallback,
this.renderCallbackOwner, this.renderCallbackOwner,
@@ -44,6 +44,11 @@ abstract class FilamentApp<T> {
required this.renderableManager, required this.renderableManager,
required this.ubershaderMaterialProvider}); required this.ubershaderMaterialProvider});
///
///
///
Future<Uint8List> loadResource(String uri);
/// ///
/// ///
/// ///
@@ -277,7 +282,7 @@ abstract class FilamentApp<T> {
int priority = 4, int priority = 4,
int layer = 0, int layer = 0,
bool loadResourcesAsync = false, bool loadResourcesAsync = false,
String? relativeResourcePath}); String? resourceUri});
/// ///
/// ///

View File

@@ -27,12 +27,11 @@ class ThermionViewerFFI extends ThermionViewer {
late final FFIView view; late final FFIView view;
late final FFIScene scene; late final FFIScene scene;
late final Pointer<TAnimationManager> animationManager; late final Pointer<TAnimationManager> animationManager;
late final Future<Uint8List> Function(String path) loadAssetFromUri;
/// ///
/// ///
/// ///
ThermionViewerFFI({required this.loadAssetFromUri}) { ThermionViewerFFI() {
if (FilamentApp.instance == null) { if (FilamentApp.instance == null) {
throw Exception("FilamentApp has not been created"); throw Exception("FilamentApp has not been created");
} }
@@ -200,7 +199,7 @@ class ThermionViewerFFI extends ThermionViewer {
/// ///
@override @override
Future setBackgroundImage(String path, {bool fillHeight = false}) async { Future setBackgroundImage(String path, {bool fillHeight = false}) async {
final imageData = await loadAssetFromUri(path); final imageData = await FilamentApp.instance!.loadResource(path);
_backgroundImage ??= await BackgroundImage.create(this, scene); _backgroundImage ??= await BackgroundImage.create(this, scene);
await _backgroundImage!.setImage(imageData); await _backgroundImage!.setImage(imageData);
} }
@@ -233,7 +232,7 @@ class ThermionViewerFFI extends ThermionViewer {
/// ///
@override @override
Future loadSkybox(String skyboxPath) async { Future loadSkybox(String skyboxPath) async {
var data = await loadAssetFromUri(skyboxPath); var data = await FilamentApp.instance!.loadResource(skyboxPath);
skybox = await withPointerCallback<TSkybox>((cb) { skybox = await withPointerCallback<TSkybox>((cb) {
Engine_buildSkyboxRenderThread( Engine_buildSkyboxRenderThread(
@@ -255,7 +254,7 @@ class ThermionViewerFFI extends ThermionViewer {
if (FILAMENT_WASM) { if (FILAMENT_WASM) {
//stackPtr = stackSave(); //stackPtr = stackSave();
} }
var data = await loadAssetFromUri(lightingPath); var data = await FilamentApp.instance!.loadResource(lightingPath);
indirectLight = await withPointerCallback<TIndirectLight>((cb) { indirectLight = await withPointerCallback<TIndirectLight>((cb) {
Engine_buildIndirectLightRenderThread( Engine_buildIndirectLightRenderThread(
@@ -386,14 +385,24 @@ class ThermionViewerFFI extends ThermionViewer {
{bool addToScene = true, {bool addToScene = true,
int numInstances = 1, int numInstances = 1,
bool keepData = false, bool keepData = false,
String? relativeResourcePath}) async { String? resourceUri,
final data = await loadAssetFromUri(path); bool loadAsync = false}) async {
final data = await FilamentApp.instance!.loadResource(path);
if (resourceUri == null) {
var split = path.split("/");
resourceUri ??= split.take(split.length - 1).join("/");
}
if (!resourceUri.endsWith("/")) {
resourceUri = "${resourceUri}/";
}
return loadGltfFromBuffer(data, return loadGltfFromBuffer(data,
addToScene: addToScene, addToScene: addToScene,
numInstances: numInstances, numInstances: numInstances,
keepData: keepData, keepData: keepData,
relativeResourcePath: relativeResourcePath); resourceUri: resourceUri,
loadResourcesAsync: loadAsync);
} }
/// ///
@@ -407,7 +416,7 @@ class ThermionViewerFFI extends ThermionViewer {
int priority = 4, int priority = 4,
int layer = 0, int layer = 0,
bool loadResourcesAsync = false, bool loadResourcesAsync = false,
String? relativeResourcePath}) async { String? resourceUri}) async {
var asset = await FilamentApp.instance!.loadGltfFromBuffer( var asset = await FilamentApp.instance!.loadGltfFromBuffer(
data, animationManager, data, animationManager,
numInstances: numInstances, numInstances: numInstances,
@@ -415,7 +424,7 @@ class ThermionViewerFFI extends ThermionViewer {
priority: priority, priority: priority,
layer: layer, layer: layer,
loadResourcesAsync: loadResourcesAsync, loadResourcesAsync: loadResourcesAsync,
relativeResourcePath: relativeResourcePath) as FFIAsset; resourceUri: resourceUri) as FFIAsset;
_assets.add(asset); _assets.add(asset);
if (addToScene) { if (addToScene) {

View File

@@ -128,16 +128,37 @@ abstract class ThermionViewer {
Future destroyLights(); Future destroyLights();
/// ///
/// Load the .glb asset at the given path, adding all entities to the scene. /// Load the glTF asset at the given path (.glb or .gltf)
/// Specify [numInstances] to create multiple instances (this is more efficient than dynamically instantating at a later time). You can then retrieve the created instances with [getInstances].
/// If you want to be able to call [createInstance] at a later time, you must pass true for [keepData].
/// If [keepData] is false, the source glTF data will be released and [createInstance] will throw an exception.
/// ///
Future<ThermionAsset> loadGltf(String path, /// If the file is a .gltf and [resourceUri] is not specified,
/// all resources will be loaded relative to the URI of the file (so if
/// [uri] is asset://assets/scene.gltf, the loader will attempt to load
/// asset://assets/scene.bin, asset://assets/texture.png, and so on).
///
/// If [resourceUri] is specified, resources will be loaded relative to
/// that path.
///
/// If [addToScene] is [true], all renderable entities (including lights)
/// in the asset will be added to the scene.
///
/// If you want to dynamically create instances of this asset after it is
/// instantiated, pass [kee]
/// Alternatively, specifying [numInstances] will pre-allocate the specified
/// number of instances. This is more efficient than dynamically instantating at a later time.
/// You can then retrieve the created instances with [getInstances].
///
/// If [keepData] is false and [numInstances] is 1,
/// the source glTF data will be released and [createInstance]
/// will throw an exception.
///
///
Future<ThermionAsset> loadGltf(String uri,
{ bool addToScene = true, { bool addToScene = true,
int numInstances = 1, int numInstances = 1,
bool keepData = false, bool keepData = false,
String? relativeResourcePath}); String? resourceUri,
bool loadAsync = false});
/// ///
/// Load the .glb asset from the specified buffer, adding all entities to the scene. /// Load the .glb asset from the specified buffer, adding all entities to the scene.
@@ -150,7 +171,7 @@ abstract class ThermionViewer {
/// ///
Future<ThermionAsset> loadGltfFromBuffer(Uint8List data, Future<ThermionAsset> loadGltfFromBuffer(Uint8List data,
{ {
String? relativeResourcePath, String? resourceUri,
int numInstances = 1, int numInstances = 1,
bool keepData = false, bool keepData = false,
int priority = 4, int priority = 4,