Skip to content

Commit

Permalink
Splat updates (#269)
Browse files Browse the repository at this point in the history
* add splat aabb and focus point updates
  • Loading branch information
slimbuck committed Oct 31, 2023
1 parent 3bfba26 commit 07bbd79
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/splat/splat-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class SplatData {
}

// calculate scene aabb taking into account splat size
calcAabb(result: BoundingBox) {
calcAabb(result: BoundingBox, pred?: (index: number) => boolean) {
const x = this.getProp('x');
const y = this.getProp('y');
const z = this.getProp('z');
Expand All @@ -144,11 +144,13 @@ class SplatData {
x: 0, y: 0, z: 0, rx: 0, ry: 0, rz: 0, rw: 0, sx: 0, sy: 0, sz: 0
};

// initialize aabb
result.center.set(x[0], y[0], z[0]);
result.halfExtents.set(0, 0, 0);
let first = true;

for (let i = 0; i < this.numSplats; ++i) {
if (pred && !pred(i)) {
continue;
}

splat.x = x[i];
splat.y = y[i];
splat.z = z[i];
Expand All @@ -160,9 +162,39 @@ class SplatData {
splat.sy = Math.exp(sy[i]);
splat.sz = Math.exp(sz[i]);

calcSplatAabb(aabb2, splat);
result.add(aabb2);
if (first) {
first = false;
calcSplatAabb(result, splat);
} else {
calcSplatAabb(aabb2, splat);
result.add(aabb2);
}
}

return !first;
}

calcFocalPoint(result: Vec3, pred?: (index: number) => boolean) {
const x = this.getProp('x');
const y = this.getProp('y');
const z = this.getProp('z');

const sx = this.getProp('scale_0');
const sy = this.getProp('scale_1');
const sz = this.getProp('scale_2');

let sum = 0;
for (let i = 0; i < this.numSplats; ++i) {
if (pred && !pred(i)) {
continue;
}
const weight = 1.0 / (1.0 + Math.exp(Math.max(sx[i], sy[i], sz[i])));
result.x += x[i] * weight;
result.y += y[i] * weight;
result.z += z[i] * weight;
sum += weight;
}
result.mulScalar(1 / sum);
}

renderWireframeBounds(app: AppBase, worldMat: Mat4) {
Expand Down Expand Up @@ -210,26 +242,6 @@ class SplatData {
app.drawLines(debugLines, debugColor);
}
}

calcFocalPoint(result: Vec3) {
const x = this.getProp('x');
const y = this.getProp('y');
const z = this.getProp('z');

const sx = this.getProp('scale_0');
const sy = this.getProp('scale_1');
const sz = this.getProp('scale_2');

let sum = 0;
for (let i = 0; i < this.numSplats; ++i) {
const weight = 1.0 / (1.0 + Math.exp(Math.max(sx[i], sy[i], sz[i])));
result.x += x[i] * weight;
result.y += y[i] * weight;
result.z += z[i] * weight;
sum += weight;
}
result.mulScalar(1 / sum);
}
}

export { SplatData };
12 changes: 12 additions & 0 deletions src/splat/splat-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ class SplatResource extends ContainerResource {
return result;
}

updateFocalPoint() {
this.splatData.calcFocalPoint(this.focalPoint);
}

updateAabb() {
this.splatData.calcAabb(this.customAabb);

this.instances.forEach((instance) => {
instance.entity.render.customAabb = this.customAabb;
});
}

getFocalPoint(): Vec3 {
const instance = this.instances[0];
return instance?.entity.getWorldTransform().transformPoint(this.focalPoint);
Expand Down

0 comments on commit 07bbd79

Please sign in to comment.