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

View File

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

View File

@@ -128,16 +128,37 @@ abstract class ThermionViewer {
Future destroyLights();
///
/// Load the .glb asset at the given path, adding all entities to the scene.
/// 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.
/// Load the glTF asset at the given path (.glb or .gltf)
///
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,
int numInstances = 1,
bool keepData = false,
String? relativeResourcePath});
String? resourceUri,
bool loadAsync = false});
///
/// 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,
{
String? relativeResourcePath,
String? resourceUri,
int numInstances = 1,
bool keepData = false,
int priority = 4,