fix: (wasm) use correct coords for pick, free memory correctly, keep pixelratio copy

This commit is contained in:
Nick Fisher
2024-08-27 21:50:07 +08:00
parent d52b23d6b5
commit 7ac7ae43ab

View File

@@ -93,7 +93,8 @@ class ThermionViewerWasm implements ThermionViewer {
int _width = 0; int _width = 0;
int _height = 0; int _height = 0;
Future initialize(int width, int height, {String? uberArchivePath}) async { Future initialize(double width, double height, double pixelRatio,
{String? uberArchivePath}) async {
if (!_initialized) { if (!_initialized) {
await _initializeModule(); await _initializeModule();
_initialized = true; _initialized = true;
@@ -101,6 +102,7 @@ class ThermionViewerWasm implements ThermionViewer {
_setAssetPathPrefix(assetPathPrefix!); _setAssetPathPrefix(assetPathPrefix!);
} }
} }
this.pixelRatio = pixelRatio;
final context = _module!.ccall("thermion_dart_web_create_gl_context", "int", final context = _module!.ccall("thermion_dart_web_create_gl_context", "int",
<JSString>[].toJS, <JSAny>[].toJS, null); <JSString>[].toJS, <JSAny>[].toJS, null);
@@ -116,8 +118,8 @@ class ThermionViewerWasm implements ThermionViewer {
["void*".toJS, "void*".toJS, "void*".toJS, "string".toJS].toJS, ["void*".toJS, "void*".toJS, "void*".toJS, "string".toJS].toJS,
[context!, loader, null, uberArchivePath?.toJS].toJS, [context!, loader, null, uberArchivePath?.toJS].toJS,
null) as JSNumber; null) as JSNumber;
await createSwapChain(width, height); await createSwapChain(width.ceil(), height.ceil());
updateViewportAndCameraProjection(width, height, 1.0); updateViewportAndCameraProjection(width.ceil(), height.ceil(), 1.0);
_sceneManager = _module!.ccall("get_scene_manager", "void*", _sceneManager = _module!.ccall("get_scene_manager", "void*",
["void*".toJS].toJS, [_viewer!].toJS, null) as JSNumber; ["void*".toJS].toJS, [_viewer!].toJS, null) as JSNumber;
@@ -174,14 +176,15 @@ class ThermionViewerWasm implements ThermionViewer {
if (width == 0 || height == 0) { if (width == 0 || height == 0) {
throw Exception("Width/height must be greater than zero"); throw Exception("Width/height must be greater than zero");
} }
_width = width; _width = (width * pixelRatio).ceil();
_height = height; _height = (height * pixelRatio).ceil();
viewportDimensions = (width.toDouble(), height.toDouble()); viewportDimensions = (_width.toDouble(), _height.toDouble());
print("Update viewport camera projection : $_width $height");
_module!.ccall( _module!.ccall(
"update_viewport_and_camera_projection", "update_viewport_and_camera_projection",
"void", "void",
["void*".toJS, "uint32_t".toJS, "uint32_t".toJS, "float".toJS].toJS, ["void*".toJS, "uint32_t".toJS, "uint32_t".toJS, "float".toJS].toJS,
[_viewer!, width.toJS, height.toJS, scaleFactor.toJS].toJS, [_viewer!, _width.toJS, _height.toJS, scaleFactor.toJS].toJS,
null); null);
} }
@@ -1290,11 +1293,12 @@ class ThermionViewerWasm implements ThermionViewer {
@override @override
Future<Matrix4> getCameraModelMatrix() async { Future<Matrix4> getCameraModelMatrix() async {
final ptr = _module!._malloc(16 * 8) as JSNumber; final ptr = _module!.ccall("get_camera_model_matrix", "void*",
_module!.ccall("get_camera_model_matrix", "void", ["void*".toJS].toJS, [_viewer!].toJS, null) as JSNumber;
["void*".toJS, "double*".toJS].toJS, [_viewer!, ptr].toJS, null);
final matrix = _matrixFromPtr(ptr); final matrix = _matrixFromPtr(ptr);
_module!._free(ptr); _module!.ccall(
"thermion_flutter_free", "void", ["void*".toJS].toJS, [ptr].toJS, null);
return matrix; return matrix;
} }
@@ -1448,7 +1452,7 @@ class ThermionViewerWasm implements ThermionViewer {
@override @override
void pick(int x, int y) async { void pick(int x, int y) async {
x = (x * pixelRatio).ceil(); x = (x * pixelRatio).ceil();
y = (y * pixelRatio).ceil(); y = (viewportDimensions.$2 - (y * pixelRatio)).ceil();
_module!.ccall( _module!.ccall(
"filament_pick", "filament_pick",
"void", "void",