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;
|
typedef ThermionViewerImpl = ThermionViewerWasm;
|
||||||
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// A [ThermionViewer] implementation that forwards calls to
|
/// A [ThermionViewer] implementation that forwards calls to the
|
||||||
/// the (Emscripten-generated) ThermionDart JS module.
|
/// (Emscripten-generated) ThermionDart JS module.
|
||||||
///
|
///
|
||||||
class ThermionViewerWasm implements ThermionViewer {
|
class ThermionViewerWasm implements ThermionViewer {
|
||||||
late _EmscriptenModule _module;
|
late _EmscriptenModule _module;
|
||||||
@@ -46,8 +45,36 @@ class ThermionViewerWasm implements ThermionViewer {
|
|||||||
bool _initialized = false;
|
bool _initialized = false;
|
||||||
bool _rendering = 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;
|
JSNumber? _viewer;
|
||||||
@@ -1847,29 +1874,22 @@ class ThermionViewerWasm implements ThermionViewer {
|
|||||||
// TODO: implement zoomUpdate
|
// TODO: implement zoomUpdate
|
||||||
throw UnimplementedError();
|
throw UnimplementedError();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future setShadowType(ShadowType shadowType) async {
|
Future setShadowType(ShadowType shadowType) async {
|
||||||
_module.ccall(
|
_module.ccall("set_shadow_type", "void", ["void*".toJS, "int".toJS].toJS,
|
||||||
"set_shadow_type",
|
[_viewer!, shadowType.index.toJS].toJS, null);
|
||||||
"void",
|
|
||||||
["void*".toJS, "int".toJS].toJS,
|
|
||||||
[_viewer!, shadowType.index.toJS].toJS,
|
|
||||||
null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future setShadowsEnabled(bool enabled) async {
|
Future setShadowsEnabled(bool enabled) async {
|
||||||
_module.ccall(
|
_module.ccall("set_shadows_enabled", "void",
|
||||||
"set_shadows_enabled",
|
["void*".toJS, "bool".toJS].toJS, [_viewer!, enabled.toJS].toJS, null);
|
||||||
"void",
|
|
||||||
["void*".toJS, "bool".toJS].toJS,
|
|
||||||
[_viewer!, enabled.toJS].toJS,
|
|
||||||
null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future setSoftShadowOptions(double penumbraScale, double penumbraRatioScale) async {
|
Future setSoftShadowOptions(
|
||||||
|
double penumbraScale, double penumbraRatioScale) async {
|
||||||
_module.ccall(
|
_module.ccall(
|
||||||
"set_soft_shadow_options",
|
"set_soft_shadow_options",
|
||||||
"void",
|
"void",
|
||||||
|
|||||||
@@ -15,37 +15,6 @@
|
|||||||
#include <emscripten/val.h>
|
#include <emscripten/val.h>
|
||||||
#include <emscripten/fetch.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;
|
using emscripten::val;
|
||||||
|
|
||||||
extern "C"
|
extern "C"
|
||||||
@@ -60,8 +29,10 @@ extern "C"
|
|||||||
// return 0;
|
// return 0;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE void thermion_filament_web_load_resource_callback(void* data, int32_t length, void* context) {
|
std::string _assetPathPrefix;
|
||||||
((PendingCall*)context)->HandleResponse(data, length);
|
|
||||||
|
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() {
|
EMSCRIPTEN_KEEPALIVE EMSCRIPTEN_WEBGL_CONTEXT_HANDLE thermion_dart_web_create_gl_context() {
|
||||||
@@ -160,7 +131,7 @@ extern "C"
|
|||||||
// memcpy(data, request->data, request->numBytes);
|
// memcpy(data, request->data, request->numBytes);
|
||||||
// emscripten_fetch_close(request);
|
// emscripten_fetch_close(request);
|
||||||
// return ResourceBuffer { data, (int32_t) request->numBytes, _lastResourceId } ;
|
// return ResourceBuffer { data, (int32_t) request->numBytes, _lastResourceId } ;
|
||||||
auto pathString = std::string(path);
|
auto pathString = _assetPathPrefix + std::string(path);
|
||||||
void* data = nullptr;
|
void* data = nullptr;
|
||||||
int32_t numBytes = 0;
|
int32_t numBytes = 0;
|
||||||
|
|
||||||
@@ -199,12 +170,10 @@ extern "C"
|
|||||||
EM_ASM({
|
EM_ASM({
|
||||||
var filename = UTF8ToString($0);
|
var filename = UTF8ToString($0);
|
||||||
var data = new Uint8Array(HEAPU8.subarray($1, $1 + $2));
|
var data = new Uint8Array(HEAPU8.subarray($1, $1 + $2));
|
||||||
console.log('Analyinzg /indexed');
|
|
||||||
|
|
||||||
// Ensure the '/indexed' directory exists
|
// Ensure the '/indexed' directory exists
|
||||||
if (!FS.analyzePath('/indexed').exists) {
|
if (!FS.analyzePath('/indexed').exists) {
|
||||||
FS.mkdir('/indexed');
|
FS.mkdir('/indexed');
|
||||||
console.log('Made dir /indexed');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create all parent directories
|
// Create all parent directories
|
||||||
@@ -214,13 +183,10 @@ extern "C"
|
|||||||
currentPath += '/' + parts[i];
|
currentPath += '/' + parts[i];
|
||||||
if (!FS.analyzePath(currentPath).exists) {
|
if (!FS.analyzePath(currentPath).exists) {
|
||||||
FS.mkdir(currentPath);
|
FS.mkdir(currentPath);
|
||||||
console.log("Made dir " + currentPath);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('Writing file to /indexed/' + filename);
|
|
||||||
// Write the file
|
// Write the file
|
||||||
FS.writeFile('/indexed/' + filename, data);
|
FS.writeFile('/indexed/' + filename, data);
|
||||||
console.log('File written, syncing');
|
|
||||||
|
|
||||||
FS.syncfs(false, function (err) {
|
FS.syncfs(false, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class ThermionFlutterWebPlugin extends ThermionFlutterPlatform {
|
|||||||
var width = window.innerWidth;
|
var width = window.innerWidth;
|
||||||
var height = window.innerHeight;
|
var height = window.innerHeight;
|
||||||
|
|
||||||
var viewer = ThermionViewerWasm();
|
var viewer = ThermionViewerWasm(assetPathPrefix: "/assets/");
|
||||||
await viewer.initialize(width, height, uberArchivePath: uberArchivePath);
|
await viewer.initialize(width, height, uberArchivePath: uberArchivePath);
|
||||||
return viewer;
|
return viewer;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user