feat: allow passing assetPathPrefix to ThermionViewerWasm to account for Flutter build asset paths
This commit is contained in:
@@ -35,10 +35,9 @@ extension type _EmscriptenModule(JSObject _) implements JSObject {
|
||||
|
||||
typedef ThermionViewerImpl = ThermionViewerWasm;
|
||||
|
||||
|
||||
///
|
||||
/// A [ThermionViewer] implementation that forwards calls to
|
||||
/// the (Emscripten-generated) ThermionDart JS module.
|
||||
/// A [ThermionViewer] implementation that forwards calls to the
|
||||
/// (Emscripten-generated) ThermionDart JS module.
|
||||
///
|
||||
class ThermionViewerWasm implements ThermionViewer {
|
||||
late _EmscriptenModule _module;
|
||||
@@ -46,8 +45,36 @@ class ThermionViewerWasm implements ThermionViewer {
|
||||
bool _initialized = false;
|
||||
bool _rendering = false;
|
||||
|
||||
ThermionViewerWasm({JSObject? module, String moduleName = "thermion_dart"}) {
|
||||
_module = module as _EmscriptenModule? ?? window.getProperty<_EmscriptenModule>(moduleName.toJS);
|
||||
///
|
||||
/// Construct an instance of this class by explicitly passing the
|
||||
/// module instance via the [module] property, or by specifying [moduleName],
|
||||
/// being the name of the window property where the module has already been
|
||||
/// loaded.
|
||||
///
|
||||
/// Pass [assetPathPrefix] if you need to prepend a path to all asset paths
|
||||
/// (e.g. on Flutter where the asset directory /foo is actually shipped under
|
||||
/// the directory /assets/foo, you would construct this as:
|
||||
///
|
||||
/// final viewer = ThermionViewerWasm(assetPathPrefix:"/assets/")
|
||||
///
|
||||
ThermionViewerWasm(
|
||||
{JSObject? module,
|
||||
String moduleName = "thermion_dart",
|
||||
String? assetPathPrefix}) {
|
||||
_module = module as _EmscriptenModule? ??
|
||||
window.getProperty<_EmscriptenModule>(moduleName.toJS);
|
||||
if (assetPathPrefix != null) {
|
||||
_setAssetPathPrefix(assetPathPrefix);
|
||||
}
|
||||
}
|
||||
|
||||
void _setAssetPathPrefix(String assetPathPrefix) {
|
||||
_module.ccall(
|
||||
"thermion_dart_web_set_asset_path_prefix",
|
||||
"void",
|
||||
<JSString>["string".toJS].toJS,
|
||||
<JSAny>[assetPathPrefix.toJS].toJS,
|
||||
null);
|
||||
}
|
||||
|
||||
JSNumber? _viewer;
|
||||
@@ -1847,29 +1874,22 @@ class ThermionViewerWasm implements ThermionViewer {
|
||||
// TODO: implement zoomUpdate
|
||||
throw UnimplementedError();
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
Future setShadowType(ShadowType shadowType) async {
|
||||
_module.ccall(
|
||||
"set_shadow_type",
|
||||
"void",
|
||||
["void*".toJS, "int".toJS].toJS,
|
||||
[_viewer!, shadowType.index.toJS].toJS,
|
||||
null);
|
||||
_module.ccall("set_shadow_type", "void", ["void*".toJS, "int".toJS].toJS,
|
||||
[_viewer!, shadowType.index.toJS].toJS, null);
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
Future setShadowsEnabled(bool enabled) async {
|
||||
_module.ccall(
|
||||
"set_shadows_enabled",
|
||||
"void",
|
||||
["void*".toJS, "bool".toJS].toJS,
|
||||
[_viewer!, enabled.toJS].toJS,
|
||||
null);
|
||||
_module.ccall("set_shadows_enabled", "void",
|
||||
["void*".toJS, "bool".toJS].toJS, [_viewer!, enabled.toJS].toJS, null);
|
||||
}
|
||||
|
||||
|
||||
@override
|
||||
Future setSoftShadowOptions(double penumbraScale, double penumbraRatioScale) async {
|
||||
Future setSoftShadowOptions(
|
||||
double penumbraScale, double penumbraRatioScale) async {
|
||||
_module.ccall(
|
||||
"set_soft_shadow_options",
|
||||
"void",
|
||||
|
||||
@@ -15,37 +15,6 @@
|
||||
#include <emscripten/val.h>
|
||||
#include <emscripten/fetch.h>
|
||||
|
||||
class PendingCall
|
||||
{
|
||||
public:
|
||||
PendingCall()
|
||||
{
|
||||
}
|
||||
~PendingCall() {}
|
||||
|
||||
void Wait()
|
||||
{
|
||||
std::future<int32_t> accumulate_future = prom.get_future();
|
||||
std::cout << "Loaded asset from Flutter of length " << accumulate_future.get() << std::endl;
|
||||
}
|
||||
|
||||
void HandleResponse(void* data, int32_t length)
|
||||
{
|
||||
this->data = data;
|
||||
this->length = length;
|
||||
prom.set_value(length);
|
||||
}
|
||||
void* data = nullptr;
|
||||
int32_t length = 0;
|
||||
|
||||
private:
|
||||
std::mutex mutex_;
|
||||
std::condition_variable cv_;
|
||||
bool notified_ = false;
|
||||
std::promise<int32_t> prom;
|
||||
|
||||
};
|
||||
|
||||
using emscripten::val;
|
||||
|
||||
extern "C"
|
||||
@@ -60,8 +29,10 @@ extern "C"
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void thermion_filament_web_load_resource_callback(void* data, int32_t length, void* context) {
|
||||
((PendingCall*)context)->HandleResponse(data, length);
|
||||
std::string _assetPathPrefix;
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE void thermion_dart_web_set_asset_path_prefix(const char* prefix) {
|
||||
_assetPathPrefix = std::string(prefix);
|
||||
}
|
||||
|
||||
EMSCRIPTEN_KEEPALIVE EMSCRIPTEN_WEBGL_CONTEXT_HANDLE thermion_dart_web_create_gl_context() {
|
||||
@@ -160,7 +131,7 @@ extern "C"
|
||||
// memcpy(data, request->data, request->numBytes);
|
||||
// emscripten_fetch_close(request);
|
||||
// return ResourceBuffer { data, (int32_t) request->numBytes, _lastResourceId } ;
|
||||
auto pathString = std::string(path);
|
||||
auto pathString = _assetPathPrefix + std::string(path);
|
||||
void* data = nullptr;
|
||||
int32_t numBytes = 0;
|
||||
|
||||
@@ -199,12 +170,10 @@ extern "C"
|
||||
EM_ASM({
|
||||
var filename = UTF8ToString($0);
|
||||
var data = new Uint8Array(HEAPU8.subarray($1, $1 + $2));
|
||||
console.log('Analyinzg /indexed');
|
||||
|
||||
// Ensure the '/indexed' directory exists
|
||||
if (!FS.analyzePath('/indexed').exists) {
|
||||
FS.mkdir('/indexed');
|
||||
console.log('Made dir /indexed');
|
||||
}
|
||||
|
||||
// Create all parent directories
|
||||
@@ -214,13 +183,10 @@ extern "C"
|
||||
currentPath += '/' + parts[i];
|
||||
if (!FS.analyzePath(currentPath).exists) {
|
||||
FS.mkdir(currentPath);
|
||||
console.log("Made dir " + currentPath);
|
||||
}
|
||||
}
|
||||
console.log('Writing file to /indexed/' + filename);
|
||||
// Write the file
|
||||
FS.writeFile('/indexed/' + filename, data);
|
||||
console.log('File written, syncing');
|
||||
|
||||
FS.syncfs(false, function (err) {
|
||||
if (err) {
|
||||
|
||||
@@ -35,7 +35,7 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform {
|
||||
var width = window.innerWidth;
|
||||
var height = window.innerHeight;
|
||||
|
||||
var viewer = ThermionViewerWasm();
|
||||
var viewer = ThermionViewerWasm(assetPathPrefix: "/assets/");
|
||||
await viewer.initialize(width, height, uberArchivePath: uberArchivePath);
|
||||
return viewer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user