Skip to content

Commit 8678073

Browse files
authored
impr: Add inverse matrices to orbit cam (#2150)
1 parent 2bc0067 commit 8678073

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

apps/typegpu-docs/src/examples/common/setup-orbit-camera.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ export const Camera = d.struct({
77
targetPos: d.vec4f,
88
view: d.mat4x4f,
99
projection: d.mat4x4f,
10+
viewInverse: d.mat4x4f,
11+
projectionInverse: d.mat4x4f,
1012
});
1113

1214
export interface CameraOptions {
@@ -57,11 +59,16 @@ export function setupOrbitCamera(
5759
cameraState.pitch = Math.asin(cameraVector.y / cameraState.radius);
5860
cameraState.target = tgt;
5961

62+
const view = calculateView(newPos, cameraState.target);
63+
const projection = calculateProj(canvas.clientWidth / canvas.clientHeight);
64+
6065
callback(Camera({
6166
position: newPos,
6267
targetPos: cameraState.target,
63-
view: calculateView(newPos, cameraState.target),
64-
projection: calculateProj(canvas.clientWidth / canvas.clientHeight),
68+
view,
69+
projection,
70+
viewInverse: invertMat(view),
71+
projectionInverse: invertMat(projection),
6572
}));
6673
}
6774

@@ -83,8 +90,11 @@ export function setupOrbitCamera(
8390
cameraState.yaw,
8491
);
8592

93+
const newView = calculateView(newCameraPos, cameraState.target);
94+
8695
callback({
87-
view: calculateView(newCameraPos, cameraState.target),
96+
view: newView,
97+
viewInverse: invertMat(newView),
8898
position: newCameraPos,
8999
});
90100
}
@@ -105,13 +115,17 @@ export function setupOrbitCamera(
105115
);
106116
const newView = calculateView(newPos, cameraState.target);
107117

108-
callback({ view: newView, position: newPos });
118+
callback({
119+
view: newView,
120+
viewInverse: invertMat(newView),
121+
position: newPos,
122+
});
109123
}
110124

111125
// resize observer
112126
const resizeObserver = new ResizeObserver(() => {
113127
const projection = calculateProj(canvas.clientWidth / canvas.clientHeight);
114-
callback({ projection });
128+
callback({ projection, projectionInverse: invertMat(projection) });
115129
});
116130
resizeObserver.observe(canvas);
117131

@@ -237,3 +251,7 @@ function calculateView(position: d.v4f, target: d.v4f) {
237251
function calculateProj(aspectRatio: number) {
238252
return m.mat4.perspective(Math.PI / 4, aspectRatio, 0.1, 1000, d.mat4x4f());
239253
}
254+
255+
function invertMat(matrix: d.m4x4f) {
256+
return m.mat4.invert(matrix, d.mat4x4f());
257+
}

packages/typegpu/tests/examples/individual/gravity.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ describe('gravity example', () => {
181181
targetPos: vec4f,
182182
view: mat4x4f,
183183
projection: mat4x4f,
184+
viewInverse: mat4x4f,
185+
projectionInverse: mat4x4f,
184186
}
185187
186188
@group(0) @binding(0) var<uniform> camera: Camera;
@@ -234,6 +236,8 @@ describe('gravity example', () => {
234236
targetPos: vec4f,
235237
view: mat4x4f,
236238
projection: mat4x4f,
239+
viewInverse: mat4x4f,
240+
projectionInverse: mat4x4f,
237241
}
238242
239243
@group(0) @binding(0) var<uniform> camera_1: Camera;

packages/typegpu/tests/examples/individual/phong-reflection.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ describe('phong reflection example', () => {
3232
targetPos: vec4f,
3333
view: mat4x4f,
3434
projection: mat4x4f,
35+
viewInverse: mat4x4f,
36+
projectionInverse: mat4x4f,
3537
}
3638
3739
@group(0) @binding(0) var<uniform> cameraUniform: Camera;

0 commit comments

Comments
 (0)