Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export first camera pose in html viewer #314

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/splat-serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ const encodeBase64 = (bytes: Uint8Array) => {
};

const serializeViewer = async (splats: Splat[], write: WriteFunc) => {
const { events } = splats[0].scene;

// create compressed PLY data
let compressedData: Uint8Array;
serializePlyCompressed(splats, (data, finalWrite) => {
Expand All @@ -909,11 +911,17 @@ const serializeViewer = async (splats: Splat[], write: WriteFunc) => {
const plyModel = encodeBase64(compressedData);

// use camera clear color
const bgClr = splats[0].scene.events.invoke('bgClr');
const bgClr = events.invoke('bgClr');
const pose = events.invoke('camera.poses')?.[0];
const p = pose && pose.position;
const t = pose && pose.target;

const html = ViewerHtmlTemplate
.replace('{{backgroundColor}}', `rgb(${bgClr.r * 255} ${bgClr.g * 255} ${bgClr.b * 255})`)
.replace('{{clearColor}}', `${bgClr.r} ${bgClr.g} ${bgClr.b}`)
.replace('{{plyModel}}', plyModel);
.replace('{{plyModel}}', plyModel)
.replace('{{resetPosition}}', pose ? `new Vec3(${p.x}, ${p.y}, ${p.z})` : 'null')
.replace('{{resetTarget}}', pose ? `new Vec3(${t.x}, ${t.y}, ${t.z})` : 'null');

await write(new TextEncoder().encode(html), true);
};
Expand Down
7 changes: 5 additions & 2 deletions src/templates/viewer-html-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ const template = /* html */ `
const entityElement = await document.querySelector('pc-entity[name="camera"]').ready();
const entity = entityElement.entity;

const resetPosition = {{resetPosition}};
const resetTarget = {{resetTarget}};

class FrameScene extends Script {
frameScene(bbox) {
const sceneSize = bbox.halfExtents.length();
Expand All @@ -182,7 +185,7 @@ const template = /* html */ `
resetCamera(bbox) {
const sceneSize = bbox.halfExtents.length();
this.entity.script.cameraControls.sceneSize = sceneSize * 0.2;
this.entity.script.cameraControls.focus(Vec3.ZERO, new Vec3(2, 1, 2));
this.entity.script.cameraControls.focus(resetTarget ?? Vec3.ZERO, resetPosition ?? new Vec3(2, 1, 2));
}

calcBound() {
Expand All @@ -195,7 +198,7 @@ const template = /* html */ `

const bbox = this.calcBound();

if (bbox.halfExtents.length() > 100) {
if (bbox.halfExtents.length() > 100 || resetPosition || resetTarget) {
this.resetCamera(bbox);
} else {
this.frameScene(bbox);
Expand Down
Loading