@flighthq/camera 0.5.1-next.973.788d3d7 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/README.md +2 -2
  2. package/dist/.tsbuildinfo.contract +1 -0
  3. package/dist/.tsbuildinfo.runtime +1 -0
  4. package/dist/basis.d.ts +2 -1
  5. package/dist/basis.d.ts.map +1 -1
  6. package/dist/basis.js.map +1 -1
  7. package/dist/camera.d.ts +3 -1
  8. package/dist/camera.d.ts.map +1 -1
  9. package/dist/camera.js +9 -3
  10. package/dist/camera.js.map +1 -1
  11. package/dist/camera2d.d.ts +5 -3
  12. package/dist/camera2d.d.ts.map +1 -1
  13. package/dist/camera2d.js +14 -11
  14. package/dist/camera2d.js.map +1 -1
  15. package/dist/contract.camera-types.d.ts +2 -0
  16. package/dist/contract.camera-types.d.ts.map +1 -0
  17. package/dist/contract.camera-types.js +4 -0
  18. package/dist/contract.camera-types.js.map +1 -0
  19. package/dist/contract.d.ts +20 -19
  20. package/dist/contract.d.ts.map +1 -1
  21. package/dist/contract.js +20 -19
  22. package/dist/contract.js.map +1 -1
  23. package/dist/cubeCapture.d.ts +4 -0
  24. package/dist/cubeCapture.d.ts.map +1 -0
  25. package/dist/cubeCapture.js +38 -0
  26. package/dist/cubeCapture.js.map +1 -0
  27. package/dist/culling.d.ts +2 -1
  28. package/dist/culling.d.ts.map +1 -1
  29. package/dist/culling.js +1 -1
  30. package/dist/culling.js.map +1 -1
  31. package/dist/depth.d.ts +10 -1
  32. package/dist/depth.d.ts.map +1 -1
  33. package/dist/depth.js +13 -22
  34. package/dist/depth.js.map +1 -1
  35. package/dist/enableCameraGuards.d.ts +2 -1
  36. package/dist/enableCameraGuards.d.ts.map +1 -1
  37. package/dist/enableCameraGuards.js +13 -13
  38. package/dist/enableCameraGuards.js.map +1 -1
  39. package/dist/explainCamera3DView.d.ts +1 -1
  40. package/dist/explainCamera3DView.d.ts.map +1 -1
  41. package/dist/frustumCorners.d.ts +2 -1
  42. package/dist/frustumCorners.d.ts.map +1 -1
  43. package/dist/frustumCorners.js +1 -1
  44. package/dist/frustumCorners.js.map +1 -1
  45. package/dist/index.d.ts +20 -1
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +20 -1
  48. package/dist/index.js.map +1 -1
  49. package/dist/intersection.d.ts +2 -1
  50. package/dist/intersection.d.ts.map +1 -1
  51. package/dist/intersection.js +1 -1
  52. package/dist/intersection.js.map +1 -1
  53. package/dist/parallax.d.ts +3 -2
  54. package/dist/parallax.d.ts.map +1 -1
  55. package/dist/parallax.js +5 -5
  56. package/dist/parallax.js.map +1 -1
  57. package/dist/picking.d.ts +2 -1
  58. package/dist/picking.d.ts.map +1 -1
  59. package/dist/picking.js +1 -1
  60. package/dist/picking.js.map +1 -1
  61. package/dist/projection.d.ts +6 -2
  62. package/dist/projection.d.ts.map +1 -1
  63. package/dist/projection.js +22 -4
  64. package/dist/projection.js.map +1 -1
  65. package/dist/projection2d.d.ts +4 -3
  66. package/dist/projection2d.d.ts.map +1 -1
  67. package/dist/projection2d.js +5 -5
  68. package/dist/projection2d.js.map +1 -1
  69. package/dist/reflection.d.ts +5 -0
  70. package/dist/reflection.d.ts.map +1 -0
  71. package/dist/reflection.js +58 -0
  72. package/dist/reflection.js.map +1 -0
  73. package/dist/shadowCamera.d.ts +2 -1
  74. package/dist/shadowCamera.d.ts.map +1 -1
  75. package/dist/shadowCamera.js +2 -2
  76. package/dist/shadowCamera.js.map +1 -1
  77. package/dist/types.d.ts +80 -0
  78. package/dist/types.d.ts.map +1 -0
  79. package/dist/types.js +2 -0
  80. package/dist/types.js.map +1 -0
  81. package/dist/viewMatrix.d.ts +3 -2
  82. package/dist/viewMatrix.d.ts.map +1 -1
  83. package/dist/viewMatrix.js +2 -2
  84. package/dist/viewMatrix.js.map +1 -1
  85. package/dist/visibleBounds.d.ts +3 -2
  86. package/dist/visibleBounds.d.ts.map +1 -1
  87. package/dist/visibleBounds.js +4 -4
  88. package/dist/visibleBounds.js.map +1 -1
  89. package/dist/zoom.d.ts +2 -2
  90. package/dist/zoom.d.ts.map +1 -1
  91. package/dist/zoom.js +4 -4
  92. package/dist/zoom.js.map +1 -1
  93. package/package.json +8 -6
  94. package/src/basis.test.ts +3 -3
  95. package/src/camera.test.ts +68 -3
  96. package/src/camera2d.test.ts +19 -5
  97. package/src/cubeCapture.test.ts +120 -0
  98. package/src/culling.test.ts +3 -3
  99. package/src/depth.test.ts +3 -3
  100. package/src/enableCameraGuards.test.ts +48 -24
  101. package/src/explainCamera3DView.test.ts +10 -7
  102. package/src/frustumCorners.test.ts +4 -4
  103. package/src/intersection.test.ts +3 -3
  104. package/src/parallax.test.ts +8 -8
  105. package/src/picking.test.ts +3 -3
  106. package/src/projection.test.ts +56 -4
  107. package/src/projection2d.test.ts +23 -15
  108. package/src/reflection.test.ts +138 -0
  109. package/src/shadowCamera.test.ts +4 -4
  110. package/src/viewMatrix.test.ts +10 -10
  111. package/src/visibleBounds.test.ts +14 -14
  112. package/src/zoom.test.ts +11 -11
@@ -0,0 +1,120 @@
1
+ import type { Vector3Like } from '@flighthq/geometry/contract';
2
+ import {
3
+ CubeFaceNegativeX,
4
+ CubeFaceNegativeY,
5
+ CubeFaceNegativeZ,
6
+ CubeFacePositiveX,
7
+ CubeFacePositiveY,
8
+ CubeFacePositiveZ,
9
+ } from '@flighthq/texture/contract';
10
+ import { describe, expect, it } from 'vitest';
11
+
12
+ import { getCamera3DPosition } from './basis.ts';
13
+ import { createCamera3D } from './camera.ts';
14
+ import { getCubeCaptureFaceCamera3D } from './cubeCapture.ts';
15
+ import { createPerspectiveProjection } from './projection.ts';
16
+
17
+ function near(a: number, b: number, eps = 1e-5): void {
18
+ expect(a).toBeCloseTo(b, -Math.log10(eps));
19
+ }
20
+
21
+ function makeCamera() {
22
+ return createCamera3D({
23
+ far: 1000,
24
+ near: 0.1,
25
+ projection: createPerspectiveProjection({ aspect: 1, fovY: 1 }),
26
+ });
27
+ }
28
+
29
+ describe('getCubeCaptureFaceCamera3D', () => {
30
+ it('sets a 90-degree FOV square perspective projection', () => {
31
+ const cam = makeCamera();
32
+ getCubeCaptureFaceCamera3D(cam, { x: 0, y: 0, z: 0 }, CubeFacePositiveX);
33
+ expect(cam.projection.kind).toBe('perspective');
34
+ if (cam.projection.kind === 'perspective') {
35
+ near(cam.projection.fovY, Math.PI * 0.5);
36
+ near(cam.projection.aspect, 1);
37
+ }
38
+ });
39
+
40
+ it('preserves near and far', () => {
41
+ const cam = makeCamera();
42
+ getCubeCaptureFaceCamera3D(cam, { x: 0, y: 0, z: 0 }, CubeFacePositiveX);
43
+ expect(cam.near).toBe(0.1);
44
+ expect(cam.far).toBe(1000);
45
+ });
46
+
47
+ it('positions the camera at the given point for every face', () => {
48
+ const cam = makeCamera();
49
+ const pos: Vector3Like = { x: 0, y: 0, z: 0 };
50
+ const origin = { x: 3, y: -2, z: 7 };
51
+ for (let face = 0; face < 6; face++) {
52
+ getCubeCaptureFaceCamera3D(cam, origin, face);
53
+ getCamera3DPosition(pos, cam);
54
+ near(pos.x, origin.x);
55
+ near(pos.y, origin.y);
56
+ near(pos.z, origin.z);
57
+ }
58
+ });
59
+
60
+ it('produces 6 distinct view matrices', () => {
61
+ const cam = makeCamera();
62
+ const views: number[][] = [];
63
+ for (let face = 0; face < 6; face++) {
64
+ getCubeCaptureFaceCamera3D(cam, { x: 0, y: 0, z: 0 }, face);
65
+ views.push(Array.from(cam.view.m));
66
+ }
67
+ for (let i = 0; i < 6; i++) {
68
+ for (let j = i + 1; j < 6; j++) {
69
+ const same = views[i].every((v, k) => Math.abs(v - views[j][k]) < 1e-6);
70
+ expect(same).toBe(false);
71
+ }
72
+ }
73
+ });
74
+
75
+ it('looks along the correct axis for each face', () => {
76
+ const cam = makeCamera();
77
+ const pos = { x: 0, y: 0, z: 0 };
78
+
79
+ getCubeCaptureFaceCamera3D(cam, pos, CubeFacePositiveX);
80
+ const fwdPX = extractForward(cam);
81
+ near(fwdPX.x, 1);
82
+ near(fwdPX.y, 0);
83
+ near(fwdPX.z, 0);
84
+
85
+ getCubeCaptureFaceCamera3D(cam, pos, CubeFaceNegativeX);
86
+ const fwdNX = extractForward(cam);
87
+ near(fwdNX.x, -1);
88
+ near(fwdNX.y, 0);
89
+ near(fwdNX.z, 0);
90
+
91
+ getCubeCaptureFaceCamera3D(cam, pos, CubeFacePositiveY);
92
+ const fwdPY = extractForward(cam);
93
+ near(fwdPY.x, 0);
94
+ near(fwdPY.y, 1);
95
+ near(fwdPY.z, 0);
96
+
97
+ getCubeCaptureFaceCamera3D(cam, pos, CubeFaceNegativeY);
98
+ const fwdNY = extractForward(cam);
99
+ near(fwdNY.x, 0);
100
+ near(fwdNY.y, -1);
101
+ near(fwdNY.z, 0);
102
+
103
+ getCubeCaptureFaceCamera3D(cam, pos, CubeFacePositiveZ);
104
+ const fwdPZ = extractForward(cam);
105
+ near(fwdPZ.x, 0);
106
+ near(fwdPZ.y, 0);
107
+ near(fwdPZ.z, 1);
108
+
109
+ getCubeCaptureFaceCamera3D(cam, pos, CubeFaceNegativeZ);
110
+ const fwdNZ = extractForward(cam);
111
+ near(fwdNZ.x, 0);
112
+ near(fwdNZ.y, 0);
113
+ near(fwdNZ.z, -1);
114
+ });
115
+ });
116
+
117
+ function extractForward(cam: ReturnType<typeof createCamera3D>): Vector3Like {
118
+ const m = cam.view.m;
119
+ return { x: -m[2], y: -m[6], z: -m[10] };
120
+ }
@@ -1,13 +1,13 @@
1
1
  import { createAabb, createBoundingSphere, createFrustum, createVector3 } from '@flighthq/geometry/contract';
2
2
 
3
- import { createCamera3D, setCamera3DViewMatrix4FromLookAt } from './camera';
3
+ import { createCamera3D, setCamera3DViewMatrix4FromLookAt } from './camera.ts';
4
4
  import {
5
5
  getCamera3DFrustum,
6
6
  isBoxInCamera3DFrustum,
7
7
  isPointInCamera3DFrustum,
8
8
  isSphereInCamera3DFrustum,
9
- } from './culling';
10
- import { createPerspectiveProjection } from './projection';
9
+ } from './culling.ts';
10
+ import { createPerspectiveProjection } from './projection.ts';
11
11
 
12
12
  function makeCamera() {
13
13
  const camera = createCamera3D({
package/src/depth.test.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { createCamera3D } from './camera';
2
- import { getCamera3DLinearDepth, getCamera3DViewSpaceZ } from './depth';
3
- import { createOrthographicProjection, createPerspectiveProjection } from './projection';
1
+ import { createCamera3D } from './camera.ts';
2
+ import { getCamera3DLinearDepth, getCamera3DViewSpaceZ } from './depth.ts';
3
+ import { createOrthographicProjection, createPerspectiveProjection } from './projection.ts';
4
4
 
5
5
  function makeCamera(near = 0.1, far = 100) {
6
6
  return createCamera3D({
@@ -1,20 +1,36 @@
1
- import { createMatrix4, createRectangle } from '@flighthq/geometry/contract';
2
- import { clearLogOnceKeys, setLogSink } from '@flighthq/log/contract';
3
- import type { Camera2D, LogEntry } from '@flighthq/types/contract';
4
- import { afterEach, describe, expect, test } from 'vitest';
1
+ import type { Camera2D } from '@flighthq/camera/contract';
2
+ import { createMatrix4, createMatrix4FromValues, createRectangle } from '@flighthq/geometry/contract';
3
+ import type { LogState } from '@flighthq/log/contract';
4
+ import { clearLogOnceKeys, createLogState, getMemoryLogSinkEntries, setLogSink } from '@flighthq/log/contract';
5
+ import type { LogEntry, MemoryLogSink } from '@flighthq/log/contract';
6
+ import { afterEach, beforeEach, describe, expect, test } from 'vitest';
5
7
 
6
- import { createCamera3D, setCamera3DViewMatrix4FromMatrix4 } from './camera';
7
- import { createCamera2D } from './camera2d';
8
- import { areCameraGuardsEnabled, disableCameraGuards, enableCameraGuards } from './enableCameraGuards';
9
- import { createPerspectiveProjection } from './projection';
10
- import { getCamera2DVisibleBounds } from './visibleBounds';
8
+ import { createCamera3D, setCamera3DViewMatrix4FromMatrix4 } from './camera.ts';
9
+ import { createCamera2D } from './camera2d.ts';
10
+ import { areCameraGuardsEnabled, disableCameraGuards, enableCameraGuards } from './enableCameraGuards.ts';
11
+ import { createPerspectiveProjection } from './projection.ts';
12
+ import { getCamera2DVisibleBounds } from './visibleBounds.ts';
11
13
 
12
- beforeEach(() => clearLogOnceKeys());
14
+ function clock(): number {
15
+ return 0;
16
+ }
17
+
18
+ function readLogEntries(sink: Readonly<MemoryLogSink>): LogEntry[] {
19
+ const entries: LogEntry[] = [];
20
+ getMemoryLogSinkEntries(entries, sink);
21
+ return entries;
22
+ }
23
+
24
+ beforeEach(() => {
25
+ logState = createLogState();
26
+ });
27
+
28
+ beforeEach(() => clearLogOnceKeys(logState));
13
29
 
14
30
  describe('areCameraGuardsEnabled', () => {
15
31
  test('reports whether the guards are installed', () => {
16
32
  expect(areCameraGuardsEnabled()).toBe(false);
17
- enableCameraGuards();
33
+ enableCameraGuards(logState, clock);
18
34
  expect(areCameraGuardsEnabled()).toBe(true);
19
35
  disableCameraGuards();
20
36
  expect(areCameraGuardsEnabled()).toBe(false);
@@ -23,9 +39,9 @@ describe('areCameraGuardsEnabled', () => {
23
39
 
24
40
  describe('disableCameraGuards', () => {
25
41
  test('leaves the core silent again once removed', () => {
26
- enableCameraGuards();
42
+ enableCameraGuards(logState, clock);
27
43
  disableCameraGuards();
28
- expect(captureLog(() => getCamera2DVisibleBounds(degenerateCamera(), createRectangle()))).toHaveLength(0);
44
+ expect(captureLog(() => getCamera2DVisibleBounds(degenerateCamera(), 64, 64, createRectangle()))).toHaveLength(0);
29
45
  });
30
46
  });
31
47
 
@@ -34,22 +50,25 @@ describe('enableCameraGuards', () => {
34
50
  // degenerate key — a second would be deduplicated away and assert nothing. The seam itself is tested
35
51
  // directly, and deterministically, in visibleBounds.test.ts.
36
52
  test('warns, naming the zoom and the consequence, when the visible bounds cannot be computed', () => {
37
- enableCameraGuards();
38
- const entries = captureLog(() => getCamera2DVisibleBounds(degenerateCamera(), createRectangle()));
53
+ enableCameraGuards(logState, clock);
54
+ const entries = captureLog(() => getCamera2DVisibleBounds(degenerateCamera(), 64, 64, createRectangle()));
39
55
  expect(entries).toHaveLength(1);
40
56
  expect(entries[0].data).toMatchObject({ zoom: 0 });
41
57
  expect(String((entries[0].data as { message: string }).message)).toContain('nothing is culled');
42
58
  });
43
59
 
44
60
  test('warns on a non-orthonormal 3D view, naming the cause rather than a consumer', () => {
45
- enableCameraGuards();
61
+ enableCameraGuards(logState, clock);
46
62
  const entries = captureLog(() => {
47
63
  const camera = createCamera3D({
48
64
  far: 100,
49
65
  near: 0.1,
50
66
  projection: createPerspectiveProjection({ aspect: 1, fovY: 1 }),
51
67
  });
52
- setCamera3DViewMatrix4FromMatrix4(camera, createMatrix4(3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1));
68
+ setCamera3DViewMatrix4FromMatrix4(
69
+ camera,
70
+ createMatrix4FromValues(3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1),
71
+ );
53
72
  });
54
73
  expect(entries).toHaveLength(1);
55
74
  expect(String((entries[0].data as { message: string }).message)).toContain('not orthonormal');
@@ -58,21 +77,24 @@ describe('enableCameraGuards', () => {
58
77
  // Reflections satisfy the precondition, so warning on them would be a false positive on a legitimate
59
78
  // water/mirror camera.
60
79
  test('stays silent for a reflection view', () => {
61
- enableCameraGuards();
80
+ enableCameraGuards(logState, clock);
62
81
  const entries = captureLog(() => {
63
82
  const camera = createCamera3D({
64
83
  far: 100,
65
84
  near: 0.1,
66
85
  projection: createPerspectiveProjection({ aspect: 1, fovY: 1 }),
67
86
  });
68
- setCamera3DViewMatrix4FromMatrix4(camera, createMatrix4(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 3, 4, 1));
87
+ setCamera3DViewMatrix4FromMatrix4(
88
+ camera,
89
+ createMatrix4FromValues(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 2, 3, 4, 1),
90
+ );
69
91
  });
70
92
  expect(entries).toHaveLength(0);
71
93
  });
72
94
 
73
95
  test('stays silent for a camera whose view matrix inverts', () => {
74
- enableCameraGuards();
75
- expect(captureLog(() => getCamera2DVisibleBounds(createCamera2D(64, 64), createRectangle()))).toHaveLength(0);
96
+ enableCameraGuards(logState, clock);
97
+ expect(captureLog(() => getCamera2DVisibleBounds(createCamera2D(), 64, 64, createRectangle()))).toHaveLength(0);
76
98
  });
77
99
  });
78
100
 
@@ -82,15 +104,17 @@ afterEach(() => {
82
104
 
83
105
  function captureLog(run: () => void): LogEntry[] {
84
106
  const entries: LogEntry[] = [];
85
- setLogSink((entry) => entries.push(entry));
107
+ setLogSink(logState, (entry) => entries.push(entry));
86
108
  try {
87
109
  run();
88
110
  } finally {
89
- setLogSink(null);
111
+ setLogSink(logState, null);
90
112
  }
91
113
  return entries;
92
114
  }
93
115
 
94
116
  function degenerateCamera(): Camera2D {
95
- return createCamera2D(64, 64, { zoom: 0 });
117
+ return createCamera2D({ zoom: 0 });
96
118
  }
119
+
120
+ let logState: LogState = createLogState();
@@ -1,9 +1,9 @@
1
- import { createMatrix4 } from '@flighthq/geometry/contract';
1
+ import { createMatrix4, createMatrix4FromValues } from '@flighthq/geometry/contract';
2
2
  import { describe, expect, test } from 'vitest';
3
3
 
4
- import { createCamera3D, setCamera3DViewMatrix4FromLookAt, setCamera3DViewMatrix4FromMatrix4 } from './camera';
5
- import { explainCamera3DView } from './explainCamera3DView';
6
- import { createPerspectiveProjection } from './projection';
4
+ import { createCamera3D, setCamera3DViewMatrix4FromLookAt, setCamera3DViewMatrix4FromMatrix4 } from './camera.ts';
5
+ import { explainCamera3DView } from './explainCamera3DView.ts';
6
+ import { createPerspectiveProjection } from './projection.ts';
7
7
 
8
8
  describe('explainCamera3DView', () => {
9
9
  test('accepts a rigid lookAt view', () => {
@@ -20,7 +20,7 @@ describe('explainCamera3DView', () => {
20
20
  // transpose is lost. It must not be reported as a violation.
21
21
  test('accepts a reflection view and reports it as one', () => {
22
22
  const camera = perspectiveCamera();
23
- setCamera3DViewMatrix4FromMatrix4(camera, createMatrix4(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 5, 7, 9, 1));
23
+ setCamera3DViewMatrix4FromMatrix4(camera, createMatrix4FromValues(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 5, 7, 9, 1));
24
24
  const explanation = explainCamera3DView(camera);
25
25
  expect(explanation.isOrthonormal).toBe(true);
26
26
  expect(explanation.isReflection).toBe(true);
@@ -29,7 +29,7 @@ describe('explainCamera3DView', () => {
29
29
 
30
30
  test('reports a scaled view as non-orthonormal, with the deviation', () => {
31
31
  const camera = perspectiveCamera();
32
- setCamera3DViewMatrix4FromMatrix4(camera, createMatrix4(2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1));
32
+ setCamera3DViewMatrix4FromMatrix4(camera, createMatrix4FromValues(2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 1));
33
33
  const explanation = explainCamera3DView(camera);
34
34
  expect(explanation.isOrthonormal).toBe(false);
35
35
  expect(explanation.scaleDeviation).toBeCloseTo(1, 6);
@@ -37,7 +37,10 @@ describe('explainCamera3DView', () => {
37
37
 
38
38
  test('reports a sheared view as non-orthonormal', () => {
39
39
  const camera = perspectiveCamera();
40
- setCamera3DViewMatrix4FromMatrix4(camera, createMatrix4(1, 0, 0, 0, 0.5, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1));
40
+ setCamera3DViewMatrix4FromMatrix4(
41
+ camera,
42
+ createMatrix4FromValues(1, 0, 0, 0, 0.5, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1),
43
+ );
41
44
  const explanation = explainCamera3DView(camera);
42
45
  expect(explanation.isOrthonormal).toBe(false);
43
46
  expect(explanation.shearDeviation).toBeGreaterThan(0.1);
@@ -1,9 +1,9 @@
1
1
  import { createVector3 } from '@flighthq/geometry/contract';
2
- import type { Vector3Like } from '@flighthq/types/contract';
2
+ import type { Vector3Like } from '@flighthq/geometry/contract';
3
3
 
4
- import { createCamera3D, setCamera3DViewMatrix4FromLookAt } from './camera';
5
- import { getCamera3DFrustumCorners } from './frustumCorners';
6
- import { createPerspectiveProjection } from './projection';
4
+ import { createCamera3D, setCamera3DViewMatrix4FromLookAt } from './camera.ts';
5
+ import { getCamera3DFrustumCorners } from './frustumCorners.ts';
6
+ import { createPerspectiveProjection } from './projection.ts';
7
7
 
8
8
  type FrustumCorners = [
9
9
  Vector3Like,
@@ -1,8 +1,8 @@
1
1
  import { createBoundingSphere, createPlane, createRay3D, createVector3 } from '@flighthq/geometry/contract';
2
2
 
3
- import { createCamera3D, setCamera3DViewMatrix4FromLookAt } from './camera';
4
- import { getCamera3DRayThroughBoundingSphere, intersectCamera3DRayWithPlane } from './intersection';
5
- import { createPerspectiveProjection } from './projection';
3
+ import { createCamera3D, setCamera3DViewMatrix4FromLookAt } from './camera.ts';
4
+ import { getCamera3DRayThroughBoundingSphere, intersectCamera3DRayWithPlane } from './intersection.ts';
5
+ import { createPerspectiveProjection } from './projection.ts';
6
6
 
7
7
  function makeCamera() {
8
8
  const camera = createCamera3D({
@@ -1,30 +1,30 @@
1
1
  import { createVector2 } from '@flighthq/geometry/contract';
2
2
  import { describe, expect, it } from 'vitest';
3
3
 
4
- import { createCamera2D } from './camera2d';
5
- import { getCamera2DParallaxPoint } from './parallax';
4
+ import { createCamera2D } from './camera2d.ts';
5
+ import { getCamera2DParallaxPoint } from './parallax.ts';
6
6
 
7
7
  describe('getCamera2DParallaxPoint', () => {
8
8
  it('returns no offset at factor 0 (screen-locked layer)', () => {
9
- const camera = createCamera2D(800, 600, { x: 100, y: 50 });
9
+ const camera = createCamera2D({ x: 100, y: 50 });
10
10
  const out = createVector2();
11
- getCamera2DParallaxPoint(camera, 0, out);
11
+ getCamera2DParallaxPoint(camera, 800, 600, 0, out);
12
12
  expect(out.x).toBeCloseTo(0, 9);
13
13
  expect(out.y).toBeCloseTo(0, 9);
14
14
  });
15
15
 
16
16
  it('returns the full camera screen offset at factor 1 (world-locked layer)', () => {
17
- const camera = createCamera2D(800, 600, { x: 100, y: 50 });
17
+ const camera = createCamera2D({ x: 100, y: 50 });
18
18
  const out = createVector2();
19
- getCamera2DParallaxPoint(camera, 1, out);
19
+ getCamera2DParallaxPoint(camera, 800, 600, 1, out);
20
20
  expect(out.x).toBeCloseTo(-100, 9);
21
21
  expect(out.y).toBeCloseTo(-50, 9);
22
22
  });
23
23
 
24
24
  it('returns half the camera offset at factor 0.5', () => {
25
- const camera = createCamera2D(800, 600, { x: 100, y: 50 });
25
+ const camera = createCamera2D({ x: 100, y: 50 });
26
26
  const out = createVector2();
27
- getCamera2DParallaxPoint(camera, 0.5, out);
27
+ getCamera2DParallaxPoint(camera, 800, 600, 0.5, out);
28
28
  expect(out.x).toBeCloseTo(-50, 9);
29
29
  expect(out.y).toBeCloseTo(-25, 9);
30
30
  });
@@ -1,8 +1,8 @@
1
1
  import { createRay3D, createVector3 } from '@flighthq/geometry/contract';
2
2
 
3
- import { createCamera3D, setCamera3DViewMatrix4FromLookAt } from './camera';
4
- import { getCamera3DScreenToWorldRay, getCamera3DWorldToScreen } from './picking';
5
- import { createPerspectiveProjection } from './projection';
3
+ import { createCamera3D, setCamera3DViewMatrix4FromLookAt } from './camera.ts';
4
+ import { getCamera3DScreenToWorldRay, getCamera3DWorldToScreen } from './picking.ts';
5
+ import { createPerspectiveProjection } from './projection.ts';
6
6
 
7
7
  function makeCamera() {
8
8
  return createCamera3D({
@@ -1,17 +1,25 @@
1
- import { createMatrix4, createPerspectiveMatrix4, setOrthographicMatrix4 } from '@flighthq/geometry/contract';
2
- import { EntityRuntimeKey } from '@flighthq/types/contract';
3
- import type { OrthographicProjection, PerspectiveProjection } from '@flighthq/types/contract';
1
+ import type { OrthographicProjection, PerspectiveProjection } from '@flighthq/camera/contract';
2
+ import { EntityRuntimeKey } from '@flighthq/entity/contract';
3
+ import {
4
+ createMatrix4,
5
+ createPerspectiveMatrix4,
6
+ setPerspectiveMatrix4,
7
+ setOrthographicMatrix4,
8
+ } from '@flighthq/geometry/contract';
4
9
 
5
10
  import {
6
11
  createOrthographicProjection,
7
12
  createPerspectiveProjection,
13
+ createRawProjection,
8
14
  getOrthographicProjectionTexelSize,
9
15
  initializeOrthographicProjection,
10
16
  initializePerspectiveProjection,
17
+ initializeRawProjection,
11
18
  isOrthographicProjection,
12
19
  isPerspectiveProjection,
20
+ isRawProjection,
13
21
  setProjectionMatrix4,
14
- } from './projection';
22
+ } from './projection.ts';
15
23
 
16
24
  describe('createOrthographicProjection', () => {
17
25
  it('builds an orthographic descriptor from half-extents', () => {
@@ -38,6 +46,20 @@ describe('createPerspectiveProjection', () => {
38
46
  });
39
47
  });
40
48
 
49
+ describe('createRawProjection', () => {
50
+ it('builds a raw projection from a matrix, copying the data', () => {
51
+ const source = createPerspectiveMatrix4(Math.tan(Math.PI / 6), 1.5, 0.1, 100);
52
+ const projection = createRawProjection({ matrix: source });
53
+ expect(EntityRuntimeKey in projection).toBe(true);
54
+ expect(projection.kind).toBe('raw');
55
+ for (let i = 0; i < 16; i++) {
56
+ expect(projection.matrix.m[i]).toBeCloseTo(source.m[i]);
57
+ }
58
+ source.m[0] = 999;
59
+ expect(projection.matrix.m[0]).not.toBe(999);
60
+ });
61
+ });
62
+
41
63
  describe('getOrthographicProjectionTexelSize', () => {
42
64
  it('uses the larger world-space texel footprint for a non-square projection and map', () => {
43
65
  const projection = createOrthographicProjection({ halfHeight: 20, halfWidth: 10 });
@@ -59,6 +81,12 @@ describe('initializePerspectiveProjection', () => {
59
81
  });
60
82
  });
61
83
 
84
+ describe('initializeRawProjection', () => {
85
+ it('is the construction initializer of createRawProjection', () => {
86
+ expect(typeof initializeRawProjection).toBe('function');
87
+ });
88
+ });
89
+
62
90
  describe('isOrthographicProjection', () => {
63
91
  it('is true for orthographic and false for perspective', () => {
64
92
  const ortho = createOrthographicProjection({ halfHeight: 1, halfWidth: 1 });
@@ -76,6 +104,17 @@ describe('isPerspectiveProjection', () => {
76
104
  });
77
105
  });
78
106
 
107
+ describe('isRawProjection', () => {
108
+ it('is true for raw and false for perspective and orthographic', () => {
109
+ const raw = createRawProjection({ matrix: createMatrix4() });
110
+ const persp = createPerspectiveProjection({ aspect: 1, fovY: 1 });
111
+ const ortho = createOrthographicProjection({ halfHeight: 1, halfWidth: 1 });
112
+ expect(isRawProjection(raw)).toBe(true);
113
+ expect(isRawProjection(persp)).toBe(false);
114
+ expect(isRawProjection(ortho)).toBe(false);
115
+ });
116
+ });
117
+
79
118
  describe('setProjectionMatrix4', () => {
80
119
  it('matches geometry perspective builder using tan(fovY/2)', () => {
81
120
  const fovY = Math.PI / 2;
@@ -106,6 +145,19 @@ describe('setProjectionMatrix4', () => {
106
145
  }
107
146
  });
108
147
 
148
+ it('copies the raw projection matrix verbatim, ignoring aspect/near/far', () => {
149
+ const source = createMatrix4();
150
+ setPerspectiveMatrix4(source, Math.tan(Math.PI / 4), 1.5, 0.1, 100);
151
+ const projection = createRawProjection({ matrix: source });
152
+
153
+ const out = createMatrix4();
154
+ setProjectionMatrix4(out, projection, 999, 0, 1);
155
+
156
+ for (let i = 0; i < 16; i++) {
157
+ expect(out.m[i]).toBeCloseTo(source.m[i]);
158
+ }
159
+ });
160
+
109
161
  it('matches geometry orthographic builder using symmetric half-extents', () => {
110
162
  const projection: OrthographicProjection = createOrthographicProjection({ halfHeight: 3, halfWidth: 5 });
111
163
  const near = 1;
@@ -1,22 +1,22 @@
1
1
  import { createVector2 } from '@flighthq/geometry/contract';
2
2
  import { describe, expect, it } from 'vitest';
3
3
 
4
- import { createCamera2D } from './camera2d';
5
- import { projectCamera2DPoint, unprojectCamera2DPoint } from './projection2d';
4
+ import { createCamera2D } from './camera2d.ts';
5
+ import { projectCamera2DPoint, unprojectCamera2DPoint } from './projection2d.ts';
6
6
 
7
7
  describe('projectCamera2DPoint', () => {
8
8
  it('projects the camera center to the viewport center', () => {
9
- const camera = createCamera2D(800, 600);
9
+ const camera = createCamera2D();
10
10
  const out = createVector2();
11
- projectCamera2DPoint(camera, 0, 0, out);
11
+ projectCamera2DPoint(camera, 800, 600, 0, 0, out);
12
12
  expect(out.x).toBeCloseTo(400, 9);
13
13
  expect(out.y).toBeCloseTo(300, 9);
14
14
  });
15
15
 
16
16
  it('accounts for zoom', () => {
17
- const camera = createCamera2D(800, 600, { zoom: 2 });
17
+ const camera = createCamera2D({ zoom: 2 });
18
18
  const out = createVector2();
19
- projectCamera2DPoint(camera, 100, 0, out);
19
+ projectCamera2DPoint(camera, 800, 600, 100, 0, out);
20
20
  expect(out.x).toBeCloseTo(600, 9);
21
21
  expect(out.y).toBeCloseTo(300, 9);
22
22
  });
@@ -24,19 +24,25 @@ describe('projectCamera2DPoint', () => {
24
24
 
25
25
  describe('unprojectCamera2DPoint', () => {
26
26
  it('unprojects the viewport center to the camera position', () => {
27
- const camera = createCamera2D(800, 600, { x: 25, y: 40 });
27
+ const camera = createCamera2D({ x: 25, y: 40 });
28
28
  const out = createVector2();
29
- unprojectCamera2DPoint(camera, 400, 300, out);
29
+ unprojectCamera2DPoint(camera, 800, 600, 400, 300, out);
30
30
  expect(out.x).toBeCloseTo(25, 9);
31
31
  expect(out.y).toBeCloseTo(40, 9);
32
32
  });
33
33
 
34
34
  it('round-trips project then unproject across zooms and rotations', () => {
35
35
  const cameras = [
36
- createCamera2D(800, 600),
37
- createCamera2D(800, 600, { x: 120, y: -80, zoom: 2 }),
38
- createCamera2D(1024, 768, { x: -50, y: 200, zoom: 0.5, rotation: Math.PI / 3 }),
39
- createCamera2D(640, 480, { x: 10, y: 10, zoom: 1.5, rotation: -Math.PI / 6 }),
36
+ createCamera2D(),
37
+ createCamera2D({ x: 120, y: -80, zoom: 2 }),
38
+ createCamera2D({ x: -50, y: 200, zoom: 0.5, rotation: Math.PI / 3 }),
39
+ createCamera2D({ x: 10, y: 10, zoom: 1.5, rotation: -Math.PI / 6 }),
40
+ ];
41
+ const viewports: ReadonlyArray<readonly [number, number]> = [
42
+ [800, 600],
43
+ [800, 600],
44
+ [1024, 768],
45
+ [640, 480],
40
46
  ];
41
47
  const points: ReadonlyArray<readonly [number, number]> = [
42
48
  [0, 0],
@@ -46,10 +52,12 @@ describe('unprojectCamera2DPoint', () => {
46
52
  ];
47
53
  const screen = createVector2();
48
54
  const world = createVector2();
49
- for (const camera of cameras) {
55
+ for (let i = 0; i < cameras.length; i++) {
56
+ const camera = cameras[i];
57
+ const [vw, vh] = viewports[i];
50
58
  for (const [px, py] of points) {
51
- projectCamera2DPoint(camera, px, py, screen);
52
- unprojectCamera2DPoint(camera, screen.x, screen.y, world);
59
+ projectCamera2DPoint(camera, vw, vh, px, py, screen);
60
+ unprojectCamera2DPoint(camera, vw, vh, screen.x, screen.y, world);
53
61
  expect(world.x).toBeCloseTo(px, 6);
54
62
  expect(world.y).toBeCloseTo(py, 6);
55
63
  }