diff --git a/libs/viewer/include/viewer/SimpleViewer.h b/libs/viewer/include/viewer/SimpleViewer.h index 6573034321..a8c534f675 100644 --- a/libs/viewer/include/viewer/SimpleViewer.h +++ b/libs/viewer/include/viewer/SimpleViewer.h @@ -31,6 +31,7 @@ #include +#include #include namespace filagui { @@ -243,7 +244,7 @@ private: float mCurvePlot[1024 * 3]; }; -filament::math::mat4f fitIntoUnitCube(const filament::Aabb& bounds); +filament::math::mat4f fitIntoUnitCube(const filament::Aabb& bounds, float zoffset); } // namespace viewer } // namespace filament diff --git a/libs/viewer/src/SimpleViewer.cpp b/libs/viewer/src/SimpleViewer.cpp index aadd51b8c9..b82091f19c 100644 --- a/libs/viewer/src/SimpleViewer.cpp +++ b/libs/viewer/src/SimpleViewer.cpp @@ -42,7 +42,7 @@ using namespace filament::math; namespace filament { namespace viewer { -filament::math::mat4f fitIntoUnitCube(const filament::Aabb& bounds) { +filament::math::mat4f fitIntoUnitCube(const filament::Aabb& bounds, float zoffset) { using namespace filament::math; auto minpt = bounds.min; auto maxpt = bounds.max; @@ -51,7 +51,7 @@ filament::math::mat4f fitIntoUnitCube(const filament::Aabb& bounds) { maxExtent = std::max(maxExtent, maxpt.z - minpt.z); float scaleFactor = 2.0f / maxExtent; float3 center = (minpt + maxpt) / 2.0f; - center.z += 4.0f / scaleFactor; + center.z += zoffset / scaleFactor; return mat4f::scaling(float3(scaleFactor)) * mat4f::translation(-center); } @@ -348,7 +348,7 @@ void SimpleViewer::populateScene(FilamentAsset* asset, bool scale, if (scale) { auto& tcm = mEngine->getTransformManager(); auto root = tcm.getInstance(mAsset->getRoot()); - filament::math::mat4f transform = fitIntoUnitCube(mAsset->getBoundingBox()); + filament::math::mat4f transform = fitIntoUnitCube(mAsset->getBoundingBox(), 4); tcm.setTransform(root, transform); } diff --git a/samples/gltf_viewer.cpp b/samples/gltf_viewer.cpp index d70c99848f..207751ffb0 100644 --- a/samples/gltf_viewer.cpp +++ b/samples/gltf_viewer.cpp @@ -261,7 +261,7 @@ static void createGroundPlane(Engine* engine, Scene* scene, App& app) { Aabb aabb = app.asset->getBoundingBox(); if (!app.actualSize) { - mat4f transform = fitIntoUnitCube(aabb); + mat4f transform = fitIntoUnitCube(aabb, 4); aabb = aabb.transform(transform); } diff --git a/web/filament-js/filament.d.ts b/web/filament-js/filament.d.ts index a3231463ff..ee4304b1e0 100644 --- a/web/filament-js/filament.d.ts +++ b/web/filament-js/filament.d.ts @@ -143,6 +143,9 @@ export interface View$VignetteOptions { enabled?: boolean; } +export function fitIntoUnitCube(box: Aabb): mat4; +export function multiplyMatrices(a: mat4, b: mat4): mat4; + // Clients should use the [PixelBuffer/CompressedPixelBuffer] helper function to contruct PixelBufferDescriptor objects. export class driver$PixelBufferDescriptor { constructor(byteLength: number, format: PixelDataFormat, datatype: PixelDataType); diff --git a/web/filament-js/jsbindings.cpp b/web/filament-js/jsbindings.cpp index 7ddcf6a781..3e02d3e47b 100644 --- a/web/filament-js/jsbindings.cpp +++ b/web/filament-js/jsbindings.cpp @@ -1903,6 +1903,14 @@ class_("SimpleViewer") .function("keyUpEvent", &SimpleViewer::keyUpEvent) .function("keyPressEvent", &SimpleViewer::keyPressEvent); +function("fitIntoUnitCube", EMBIND_LAMBDA(flatmat4, (Aabb box, float zoffset), { + return flatmat4 { fitIntoUnitCube(box, zoffset) }; +})); + +function("multiplyMatrices", EMBIND_LAMBDA(flatmat4, (flatmat4 a, flatmat4 b), { + return flatmat4 { a.m * b.m }; +})); + } // EMSCRIPTEN_BINDINGS #pragma clang diagnostic pop