diff --git a/thermion_dart/lib/src/filament/src/interface/filament_app.dart b/thermion_dart/lib/src/filament/src/interface/filament_app.dart index 5dc90173..3e1a73e3 100644 --- a/thermion_dart/lib/src/filament/src/interface/filament_app.dart +++ b/thermion_dart/lib/src/filament/src/interface/filament_app.dart @@ -5,7 +5,7 @@ class FilamentConfig { final Backend backend; final T? renderCallback; final U? renderCallbackOwner; - Future Function(String)? resourceLoader; + Future Function(String)? loadResource; final U? platform; final U? sharedContext; final String? uberArchivePath; @@ -14,7 +14,7 @@ class FilamentConfig { FilamentConfig( {required this.backend, - required this.resourceLoader, + required this.loadResource, this.uberArchivePath, this.renderCallback, this.renderCallbackOwner, @@ -44,6 +44,11 @@ abstract class FilamentApp { required this.renderableManager, required this.ubershaderMaterialProvider}); + /// + /// + /// + Future loadResource(String uri); + /// /// /// @@ -277,7 +282,7 @@ abstract class FilamentApp { int priority = 4, int layer = 0, bool loadResourcesAsync = false, - String? relativeResourcePath}); + String? resourceUri}); /// /// diff --git a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_viewer_ffi.dart b/thermion_dart/lib/src/viewer/src/ffi/src/thermion_viewer_ffi.dart index 71d7f697..8078e006 100644 --- a/thermion_dart/lib/src/viewer/src/ffi/src/thermion_viewer_ffi.dart +++ b/thermion_dart/lib/src/viewer/src/ffi/src/thermion_viewer_ffi.dart @@ -27,12 +27,11 @@ class ThermionViewerFFI extends ThermionViewer { late final FFIView view; late final FFIScene scene; late final Pointer animationManager; - late final Future 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((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((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) { diff --git a/thermion_dart/lib/src/viewer/src/thermion_viewer_base.dart b/thermion_dart/lib/src/viewer/src/thermion_viewer_base.dart index 5befca79..186da226 100644 --- a/thermion_dart/lib/src/viewer/src/thermion_viewer_base.dart +++ b/thermion_dart/lib/src/viewer/src/thermion_viewer_base.dart @@ -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) + /// + /// 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 loadGltf(String path, - { bool addToScene=true, + /// + Future 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 loadGltfFromBuffer(Uint8List data, { - String? relativeResourcePath, + String? resourceUri, int numInstances = 1, bool keepData = false, int priority = 4,