Skip to content

Commit

Permalink
better splat sorting using bounding volume center
Browse files Browse the repository at this point in the history
  • Loading branch information
keyboardspecialist committed Jan 31, 2025
1 parent a2cefeb commit e4cb8e3
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions packages/engine/Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -2321,37 +2321,21 @@ function backToFront(a, b, position) {
);
}

const scratchCartA = new Cartesian3();
const scratchCartB = new Cartesian3();

function backToFrontSplats(a, b, scene) {
const boxA = a.orientedBoundingBox;
const boxB = b.orientedBoundingBox;
const camera = scene.camera;

const cameraSpaceA = Matrix4.multiplyByPoint(
camera.viewMatrix,
boxA.center,
scratchCartA,
);
const cameraSpaceB = Matrix4.multiplyByPoint(
camera.viewMatrix,
boxB.center,
scratchCartB,
);
const scratchCart3 = new Cartesian3();
function distanceSquaredToCenter(center, position) {
const diff = Cartesian3.subtract(center, position, scratchCart3);
const distance = Math.max(0.0, Cartesian3.magnitude(diff));
return distance * distance;
}

const sqrDistA = cameraSpaceA.z * cameraSpaceA.z;
const sqrDistB = cameraSpaceB.z * cameraSpaceB.z;
function backToFrontSplats(a, b, position) {
const boxA = a.boundingVolume;
const boxB = b.boundingVolume;

const viewOffsetA = Math.sqrt(
cameraSpaceA.x * cameraSpaceA.x + cameraSpaceA.y * cameraSpaceA.y,
);
const viewOffsetB = Math.sqrt(
cameraSpaceB.x * cameraSpaceB.x + cameraSpaceB.y * cameraSpaceB.y,
return (
distanceSquaredToCenter(boxB.center, position) -
distanceSquaredToCenter(boxA.center, position)
);

const weight = 10;
return sqrDistB + viewOffsetB * weight - (sqrDistA + viewOffsetA * weight);
}

function frontToBack(a, b, position) {
Expand Down Expand Up @@ -2427,7 +2411,7 @@ function performGaussianSplatPass(scene, passState, frustumCommands) {
commands.length = frustumCommands.indices[Pass.GAUSSIAN_SPLATS];

//still necessary?
mergeSort(commands, backToFrontSplats, scene);
mergeSort(commands, backToFrontSplats, scene.camera.positionWC);

for (let i = 0; i < commands.length; ++i) {
executeCommand(commands[i], scene, passState);
Expand Down

0 comments on commit e4cb8e3

Please sign in to comment.