Skip to content

Commit

Permalink
editor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanebert committed Dec 10, 2023
1 parent eaeed45 commit c941ced
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
21 changes: 3 additions & 18 deletions examples/editor/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as SPLAT from "gsplat";
import { findIntersectedPoint } from "./utils";
import { findIntersectedPoint, getPointerDirection } from "./utils";

const canvas = document.getElementById("canvas") as HTMLCanvasElement;
const progressDialog = document.getElementById("progress-dialog") as HTMLDialogElement;
Expand All @@ -22,23 +22,8 @@ async function main() {
const handleClicked = (event: MouseEvent) => {
const x = (event.clientX / canvas.clientWidth) * 2 - 1;
const y = -(event.clientY / canvas.clientHeight) * 2 + 1;
const clipSpaceCoords = new SPLAT.Vector4(x, y, -1, 1);

const inverseProjectionMatrix = camera.data.projectionMatrix.invert();
const cameraSpaceCoords = clipSpaceCoords.multiply(inverseProjectionMatrix);

const inverseViewMatrix = camera.data.viewMatrix.invert();
const worldSpaceCoords = cameraSpaceCoords.multiply(inverseViewMatrix);
const worldSpacePosition = new SPLAT.Vector3(
worldSpaceCoords.x / worldSpaceCoords.w,
worldSpaceCoords.y / worldSpaceCoords.w,
worldSpaceCoords.z / worldSpaceCoords.w,
);

const origin = camera.position;
const direction = worldSpacePosition.subtract(origin).normalize();

const closestSplat = findIntersectedPoint(bonsai, origin, direction);
const direction = getPointerDirection(x, y, camera);
const closestSplat = findIntersectedPoint(bonsai, camera.position, direction);
if (closestSplat !== null) {
console.log(`Clicked on splat ${closestSplat}`);
}
Expand Down
20 changes: 19 additions & 1 deletion examples/editor/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,22 @@ function findIntersectedPoint(splat: SPLAT.Splat, origin: SPLAT.Vector3, directi
return closestPoint;
}

export { findIntersectedPoint };
function getPointerDirection(x: number, y: number, camera: SPLAT.Camera) {
const clipSpaceCoords = new SPLAT.Vector4(x, y, -1, 1);

const inverseProjectionMatrix = camera.data.projectionMatrix.invert();
const cameraSpaceCoords = clipSpaceCoords.multiply(inverseProjectionMatrix);

const inverseViewMatrix = camera.data.viewMatrix.invert();
const worldSpaceCoords = cameraSpaceCoords.multiply(inverseViewMatrix);
const worldSpacePosition = new SPLAT.Vector3(
worldSpaceCoords.x / worldSpaceCoords.w,
worldSpaceCoords.y / worldSpaceCoords.w,
worldSpaceCoords.z / worldSpaceCoords.w,
);

const direction = worldSpacePosition.subtract(camera.position).normalize();
return direction;
}

export { findIntersectedPoint, getPointerDirection };

0 comments on commit c941ced

Please sign in to comment.