@flighthq/lighting 0.5.1-next.920.da8f530 → 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 (80) 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/ambientLight.d.ts +2 -1
  5. package/dist/ambientLight.d.ts.map +1 -1
  6. package/dist/ambientLight.js +3 -3
  7. package/dist/ambientLight.js.map +1 -1
  8. package/dist/areaLight.d.ts +3 -1
  9. package/dist/areaLight.d.ts.map +1 -1
  10. package/dist/areaLight.js +4 -4
  11. package/dist/areaLight.js.map +1 -1
  12. package/dist/contract.d.ts +20 -17
  13. package/dist/contract.d.ts.map +1 -1
  14. package/dist/contract.js +20 -17
  15. package/dist/contract.js.map +1 -1
  16. package/dist/contract.lighting-types.d.ts +2 -0
  17. package/dist/contract.lighting-types.d.ts.map +1 -0
  18. package/dist/contract.lighting-types.js +4 -0
  19. package/dist/contract.lighting-types.js.map +1 -0
  20. package/dist/directionalLight.d.ts +2 -1
  21. package/dist/directionalLight.d.ts.map +1 -1
  22. package/dist/directionalLight.js +3 -3
  23. package/dist/directionalLight.js.map +1 -1
  24. package/dist/environment.d.ts +2 -1
  25. package/dist/environment.d.ts.map +1 -1
  26. package/dist/environment.js +3 -3
  27. package/dist/environment.js.map +1 -1
  28. package/dist/hemisphereLight.d.ts +2 -1
  29. package/dist/hemisphereLight.d.ts.map +1 -1
  30. package/dist/hemisphereLight.js +3 -3
  31. package/dist/hemisphereLight.js.map +1 -1
  32. package/dist/index.d.ts +1 -1
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +1 -1
  35. package/dist/index.js.map +1 -1
  36. package/dist/lightAnalysis.d.ts +2 -1
  37. package/dist/lightAnalysis.d.ts.map +1 -1
  38. package/dist/lightAnalysis.js +1 -1
  39. package/dist/lightAnalysis.js.map +1 -1
  40. package/dist/lightIntensity.d.ts +3 -1
  41. package/dist/lightIntensity.d.ts.map +1 -1
  42. package/dist/lightIntensity.js +19 -1
  43. package/dist/lightIntensity.js.map +1 -1
  44. package/dist/lightProbe.d.ts +69 -0
  45. package/dist/lightProbe.d.ts.map +1 -0
  46. package/dist/lightProbe.js +229 -0
  47. package/dist/lightProbe.js.map +1 -0
  48. package/dist/pointLight.d.ts +2 -1
  49. package/dist/pointLight.d.ts.map +1 -1
  50. package/dist/pointLight.js +8 -4
  51. package/dist/pointLight.js.map +1 -1
  52. package/dist/sceneForwardLights.d.ts +3 -2
  53. package/dist/sceneForwardLights.d.ts.map +1 -1
  54. package/dist/sceneForwardLights.js +25 -9
  55. package/dist/sceneForwardLights.js.map +1 -1
  56. package/dist/sceneLights.d.ts +2 -1
  57. package/dist/sceneLights.d.ts.map +1 -1
  58. package/dist/sceneLights.js +2 -2
  59. package/dist/sceneLights.js.map +1 -1
  60. package/dist/spotLight.d.ts +2 -1
  61. package/dist/spotLight.d.ts.map +1 -1
  62. package/dist/spotLight.js +7 -3
  63. package/dist/spotLight.js.map +1 -1
  64. package/dist/types.d.ts +312 -0
  65. package/dist/types.d.ts.map +1 -0
  66. package/dist/types.js +59 -0
  67. package/dist/types.js.map +1 -0
  68. package/package.json +8 -6
  69. package/src/ambientLight.test.ts +2 -3
  70. package/src/areaLight.test.ts +2 -2
  71. package/src/directionalLight.test.ts +2 -2
  72. package/src/environment.test.ts +7 -7
  73. package/src/hemisphereLight.test.ts +2 -3
  74. package/src/lightAnalysis.test.ts +21 -21
  75. package/src/lightIntensity.test.ts +50 -3
  76. package/src/lightProbe.test.ts +362 -0
  77. package/src/pointLight.test.ts +2 -2
  78. package/src/sceneForwardLights.test.ts +59 -4
  79. package/src/sceneLights.test.ts +5 -5
  80. package/src/spotLight.test.ts +2 -2
@@ -1,6 +1,5 @@
1
- import { AmbientLightKind, LuxLightUnit, UnitlessLightUnit } from '@flighthq/types/contract';
2
-
3
- import { cloneAmbientLight, createAmbientLight, initializeAmbientLight } from './ambientLight';
1
+ import { cloneAmbientLight, createAmbientLight, initializeAmbientLight } from './ambientLight.ts';
2
+ import { AmbientLightKind, LuxLightUnit, UnitlessLightUnit } from './types.ts';
4
3
 
5
4
  describe('cloneAmbientLight', () => {
6
5
  it('creates an independent copy with the same fields', () => {
@@ -1,7 +1,7 @@
1
1
  import { createVector3 } from '@flighthq/geometry/contract';
2
- import { AreaLightKind, LumenLightUnit, UnitlessLightUnit } from '@flighthq/types/contract';
3
2
 
4
- import { cloneAreaLight, createAreaLight, initializeAreaLight, setAreaLightOrientation } from './areaLight';
3
+ import { cloneAreaLight, createAreaLight, initializeAreaLight, setAreaLightOrientation } from './areaLight.ts';
4
+ import { AreaLightKind, LumenLightUnit, UnitlessLightUnit } from './types.ts';
5
5
 
6
6
  describe('cloneAreaLight', () => {
7
7
  it('creates an independent copy with fresh position/direction/right/up vectors', () => {
@@ -1,5 +1,4 @@
1
1
  import { createVector3 } from '@flighthq/geometry/contract';
2
- import { DirectionalLightKind, LuxLightUnit, UnitlessLightUnit } from '@flighthq/types/contract';
3
2
 
4
3
  import {
5
4
  cloneDirectionalLight,
@@ -7,7 +6,8 @@ import {
7
6
  initializeDirectionalLight,
8
7
  setDirectionalLightDirection,
9
8
  setDirectionalLightTarget,
10
- } from './directionalLight';
9
+ } from './directionalLight.ts';
10
+ import { DirectionalLightKind, LuxLightUnit, UnitlessLightUnit } from './types.ts';
11
11
 
12
12
  describe('cloneDirectionalLight', () => {
13
13
  it('creates an independent copy with a fresh direction vector', () => {
@@ -1,19 +1,19 @@
1
- import { allocateEntity, finishEntity } from '@flighthq/entity/contract';
1
+ import { allocateEntity } from '@flighthq/entity/contract';
2
2
  import { createVector2 } from '@flighthq/geometry/contract';
3
- import type { Texture, Sampler } from '@flighthq/types/contract';
4
- import { EnvironmentKind } from '@flighthq/types/contract';
3
+ import type { Texture, Sampler } from '@flighthq/texture/contract';
5
4
 
6
- import { cloneEnvironment, createEnvironment, initializeEnvironment } from './environment';
5
+ import { cloneEnvironment, createEnvironment, initializeEnvironment } from './environment.ts';
6
+ import { EnvironmentKind } from './types.ts';
7
7
 
8
8
  function createTestCubeTexture(): Texture {
9
- const sampler = allocateEntity<any>();
9
+ const sampler = allocateEntity<any>() as any;
10
10
  sampler.anisotropy = 1;
11
11
  sampler.magFilter = 'linear';
12
12
  sampler.minFilter = 'linear';
13
13
  sampler.mipmaps = false;
14
14
  sampler.wrapU = 'clamp-to-edge';
15
15
  sampler.wrapV = 'clamp-to-edge';
16
- const out = allocateEntity<any>();
16
+ const out = allocateEntity<any>() as any;
17
17
  out.colorSpace = 'linear';
18
18
  out.flipX = false;
19
19
  out.flipY = false;
@@ -24,7 +24,7 @@ function createTestCubeTexture(): Texture {
24
24
  out.uvRotation = 0;
25
25
  out.uvScale = createVector2(1, 1);
26
26
  out.version = 0;
27
- return finishEntity(out);
27
+ return out;
28
28
  }
29
29
 
30
30
  describe('cloneEnvironment', () => {
@@ -1,6 +1,5 @@
1
- import { HemisphereLightKind, LuxLightUnit, UnitlessLightUnit } from '@flighthq/types/contract';
2
-
3
- import { cloneHemisphereLight, createHemisphereLight, initializeHemisphereLight } from './hemisphereLight';
1
+ import { cloneHemisphereLight, createHemisphereLight, initializeHemisphereLight } from './hemisphereLight.ts';
2
+ import { HemisphereLightKind, LuxLightUnit, UnitlessLightUnit } from './types.ts';
4
3
 
5
4
  describe('cloneHemisphereLight', () => {
6
5
  it('creates an independent copy with the same fields', () => {
@@ -1,20 +1,20 @@
1
- import { allocateEntity, finishEntity } from '@flighthq/entity/contract';
1
+ import { allocateEntity } from '@flighthq/entity/contract';
2
2
  import { createBoundingSphere } from '@flighthq/geometry/contract';
3
3
 
4
- import { createAmbientLight } from './ambientLight';
5
- import { createAreaLight } from './areaLight';
6
- import { createDirectionalLight } from './directionalLight';
7
- import { createEnvironment } from './environment';
8
- import { createHemisphereLight } from './hemisphereLight';
4
+ import { createAmbientLight } from './ambientLight.ts';
5
+ import { createAreaLight } from './areaLight.ts';
6
+ import { createDirectionalLight } from './directionalLight.ts';
7
+ import { createEnvironment } from './environment.ts';
8
+ import { createHemisphereLight } from './hemisphereLight.ts';
9
9
  import {
10
10
  getLightContributionAtBoundingSphere,
11
11
  getLightInfluenceBounds,
12
12
  getLightLuminance,
13
13
  hasLightInfluenceOnBounds,
14
14
  isLightCastingShadow,
15
- } from './lightAnalysis';
16
- import { createPointLight } from './pointLight';
17
- import { createSpotLight } from './spotLight';
15
+ } from './lightAnalysis.ts';
16
+ import { createPointLight } from './pointLight.ts';
17
+ import { createSpotLight } from './spotLight.ts';
18
18
 
19
19
  describe('getLightContributionAtBoundingSphere', () => {
20
20
  it('matches inverse-square attenuation for an infinite-range point light', () => {
@@ -161,12 +161,12 @@ describe('getLightLuminance', () => {
161
161
  });
162
162
 
163
163
  it('returns zero for an open custom kind even when it resembles a colored light', () => {
164
- const _light = allocateEntity<any>();
164
+ const _light = allocateEntity<any>() as any;
165
165
  _light.color = 0xffffffff;
166
166
  _light.enabled = true;
167
167
  _light.intensity = 5;
168
168
  _light.kind = 'acme:GlowLight';
169
- const light = finishEntity(_light);
169
+ const light = _light;
170
170
  expect(getLightLuminance(light)).toBe(0);
171
171
  });
172
172
 
@@ -293,22 +293,22 @@ describe('isLightCastingShadow', () => {
293
293
  // is the assertion that separates the two: a truthiness check would accept `undefined` and report
294
294
  // this as working.
295
295
  it('returns strict false for a custom light kind that declares no castsShadow', () => {
296
- const _light = allocateEntity<any>();
296
+ const _light = allocateEntity<any>() as any;
297
297
  _light.color = 0xffffffff;
298
298
  _light.intensity = 1;
299
299
  _light.kind = 'acme:GlowLight';
300
- const light = finishEntity(_light);
300
+ const light = _light;
301
301
  expect(isLightCastingShadow(light)).toBe(false);
302
302
  });
303
303
 
304
304
  // Guards the return TYPE rather than its truthiness, so the contract cannot regress to `undefined`
305
305
  // while every `toBe(false)` assertion still passes by coincidence.
306
306
  it('returns a boolean for every light, including custom kinds', () => {
307
- const _custom = allocateEntity<any>();
307
+ const _custom = allocateEntity<any>() as any;
308
308
  _custom.color = 0xffffffff;
309
309
  _custom.intensity = 1;
310
310
  _custom.kind = 'acme:GlowLight';
311
- const custom = finishEntity(_custom);
311
+ const custom = _custom;
312
312
  expect(typeof isLightCastingShadow(custom)).toBe('boolean');
313
313
  expect(typeof isLightCastingShadow(createAmbientLight())).toBe('boolean');
314
314
  expect(typeof isLightCastingShadow(createDirectionalLight({ castsShadow: true }))).toBe('boolean');
@@ -317,14 +317,14 @@ describe('isLightCastingShadow', () => {
317
317
  // A custom kind that DOES opt in must still be honoured — the fix must not flatten every custom
318
318
  // light to false, which would be the easy over-correction.
319
319
  it('honours castsShadow on a custom light kind that declares it', () => {
320
- const _casting = allocateEntity<any>();
320
+ const _casting = allocateEntity<any>() as any;
321
321
  _casting.castsShadow = true;
322
322
  _casting.kind = 'acme:GlowLight';
323
- const casting = finishEntity(_casting);
324
- const _notCasting = allocateEntity<any>();
323
+ const casting = _casting;
324
+ const _notCasting = allocateEntity<any>() as any;
325
325
  _notCasting.castsShadow = false;
326
326
  _notCasting.kind = 'acme:GlowLight';
327
- const notCasting = finishEntity(_notCasting);
327
+ const notCasting = _notCasting;
328
328
  expect(isLightCastingShadow(casting)).toBe(true);
329
329
  expect(isLightCastingShadow(notCasting)).toBe(false);
330
330
  });
@@ -332,10 +332,10 @@ describe('isLightCastingShadow', () => {
332
332
  // A non-boolean `castsShadow` is a programmer error, not a configuration; the query still has to
333
333
  // return a boolean rather than leaking the foreign value to a caller that declared it cannot get one.
334
334
  it('returns a boolean when a custom kind carries a non-boolean castsShadow', () => {
335
- const _light = allocateEntity<any>();
335
+ const _light = allocateEntity<any>() as any;
336
336
  _light.castsShadow = 1;
337
337
  _light.kind = 'acme:GlowLight';
338
- const light = finishEntity(_light);
338
+ const light = _light;
339
339
  expect(typeof isLightCastingShadow(light)).toBe('boolean');
340
340
  });
341
341
  });
@@ -1,6 +1,12 @@
1
- import { CandelaLightUnit, LumenLightUnit, LuxLightUnit, UnitlessLightUnit } from '@flighthq/types/contract';
2
-
3
- import { applyLightExposure, convertLightIntensity, getLightLinearIntensity } from './lightIntensity';
1
+ import {
2
+ applyLightExposure,
3
+ convertLightIntensity,
4
+ getLightLinearIntensity,
5
+ getLightPhotometricIntensity,
6
+ setLightPhotometricIntensity,
7
+ } from './lightIntensity.ts';
8
+ import type { LightUnit, PhotometricLightLike } from './types.ts';
9
+ import { CandelaLightUnit, LumenLightUnit, LuxLightUnit, UnitlessLightUnit } from './types.ts';
4
10
 
5
11
  describe('applyLightExposure', () => {
6
12
  it('returns the intensity unchanged at 0 EV', () => {
@@ -81,3 +87,44 @@ describe('getLightLinearIntensity', () => {
81
87
  expect(perLumen).toBeCloseTo(perCandela / (4 * Math.PI), 12);
82
88
  });
83
89
  });
90
+
91
+ describe('getLightPhotometricIntensity', () => {
92
+ it('reads a light back in the unit it declares', () => {
93
+ // 100000 lux is the anchor for linear 1.0, so a sun at linear 1 reads back as 100000 lux. This is
94
+ // the connection the unit field previously did not have to the light carrying it.
95
+ const sun: PhotometricLightLike = { intensity: 1, intensityUnit: LuxLightUnit };
96
+ expect(getLightPhotometricIntensity(sun)).toBeCloseTo(100000, 6);
97
+ });
98
+
99
+ it('passes a Unitless light through, because linear IS its authored reading', () => {
100
+ const plain: PhotometricLightLike = { intensity: 2.5, intensityUnit: UnitlessLightUnit };
101
+ expect(getLightPhotometricIntensity(plain)).toBe(2.5);
102
+ });
103
+
104
+ it('reads candela and lumen through their own anchors, not through lux', () => {
105
+ const point: PhotometricLightLike = { intensity: 1, intensityUnit: CandelaLightUnit };
106
+ const flux: PhotometricLightLike = { intensity: 1, intensityUnit: LumenLightUnit };
107
+ expect(getLightPhotometricIntensity(point)).toBeCloseTo(100000, 6);
108
+ // A lumen is 1/(4*PI) of a candela's contribution, so linear 1 is 4*PI times as many lumens.
109
+ expect(getLightPhotometricIntensity(flux)).toBeCloseTo(100000 * 4 * Math.PI, 3);
110
+ });
111
+ });
112
+
113
+ describe('setLightPhotometricIntensity', () => {
114
+ it('stores the dimensionless multiplier, keeping units out of the render path', () => {
115
+ const sun: PhotometricLightLike = { intensity: 0, intensityUnit: LuxLightUnit };
116
+ setLightPhotometricIntensity(sun, 100000);
117
+ expect(sun.intensity).toBeCloseTo(1, 6);
118
+ // The unit is not consumed by the write — it selects the conversion and stays as authored.
119
+ expect(sun.intensityUnit).toBe(LuxLightUnit);
120
+ });
121
+
122
+ it('round-trips with the getter for every unit', () => {
123
+ const units: LightUnit[] = [UnitlessLightUnit, LuxLightUnit, CandelaLightUnit, LumenLightUnit];
124
+ for (const unit of units) {
125
+ const light: PhotometricLightLike = { intensity: 0, intensityUnit: unit };
126
+ setLightPhotometricIntensity(light, 1234.5);
127
+ expect(getLightPhotometricIntensity(light)).toBeCloseTo(1234.5, 6);
128
+ }
129
+ });
130
+ });
@@ -0,0 +1,362 @@
1
+ import type { EntityConstruction } from '@flighthq/entity/contract';
2
+ import { createVector3 } from '@flighthq/geometry/contract';
3
+
4
+ import {
5
+ createLightProbe,
6
+ createLightProbeGrid,
7
+ evaluateLightProbeSh,
8
+ initializeLightProbe,
9
+ initializeLightProbeGrid,
10
+ sampleLightProbeGrid,
11
+ setLightProbeShFromColors,
12
+ } from './lightProbe.ts';
13
+ import type { LightProbe, LightProbeGrid } from './types.ts';
14
+ import { LIGHT_PROBE_SH_FLOATS } from './types.ts';
15
+
16
+ // The six axis directions. Every SH band above l=0 sums to exactly zero over this set (the odd bands
17
+ // cancel between opposite poles; 3z^2-1 sums to -1*4 + 2*2 = 0 and x^2-y^2 to 1+1-1-1 = 0), which is
18
+ // what makes the projection round trips below exact rather than approximate.
19
+ const AXIS_DIRECTIONS = new Float32Array([1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1]);
20
+
21
+ // A probe whose first coefficient's red channel is `value` — a scalar tag that survives interpolation,
22
+ // so an interpolated result can be read back as a single number.
23
+ function taggedProbe(value: number, x = 0, y = 0, z = 0): LightProbe {
24
+ const probe = createLightProbe(createVector3(x, y, z));
25
+ probe.shCoefficients[0] = value;
26
+ return probe;
27
+ }
28
+
29
+ // A 2x2x2 grid over the unit cube whose probes are tagged with their own index, so a sample at a known
30
+ // position has a trilinear expectation that can be computed by hand from the eight tags.
31
+ function unitCubeGrid() {
32
+ const probes: LightProbe[] = [];
33
+ for (let i = 0; i < 8; i++) probes.push(taggedProbe(i));
34
+ const bounds = { max: createVector3(1, 1, 1), min: createVector3(0, 0, 0) };
35
+ return createLightProbeGrid(probes, bounds, createVector3(2, 2, 2));
36
+ }
37
+
38
+ function sampleTag(grid: ReturnType<typeof unitCubeGrid>, x: number, y: number, z: number): number {
39
+ const out = new Float32Array(LIGHT_PROBE_SH_FLOATS);
40
+ expect(sampleLightProbeGrid(out, createVector3(x, y, z), grid)).toBe(true);
41
+ return out[0];
42
+ }
43
+
44
+ describe('createLightProbe', () => {
45
+ it('defaults to enabled at the given position with all-zero coefficients', () => {
46
+ const probe = createLightProbe(createVector3(1, 2, 3));
47
+ expect(probe.enabled).toBe(true);
48
+ expect(probe.position.x).toBe(1);
49
+ expect(probe.position.y).toBe(2);
50
+ expect(probe.position.z).toBe(3);
51
+ expect(probe.shCoefficients.length).toBe(LIGHT_PROBE_SH_FLOATS);
52
+ expect(Array.from(probe.shCoefficients).every((v) => v === 0)).toBe(true);
53
+ });
54
+
55
+ it('copies the supplied coefficients rather than aliasing the caller buffer', () => {
56
+ const source = new Float32Array(LIGHT_PROBE_SH_FLOATS);
57
+ source[4] = 9;
58
+ const probe = createLightProbe(createVector3(0, 0, 0), source);
59
+ expect(probe.shCoefficients[4]).toBe(9);
60
+ source[4] = 0;
61
+ expect(probe.shCoefficients[4]).toBe(9);
62
+ expect(probe.shCoefficients).not.toBe(source);
63
+ });
64
+
65
+ it('copies the position rather than aliasing the caller vector', () => {
66
+ const position = createVector3(1, 2, 3);
67
+ const probe = createLightProbe(position);
68
+ position.x = 99;
69
+ expect(probe.position.x).toBe(1);
70
+ });
71
+
72
+ it('throws on a wrong-length coefficient buffer', () => {
73
+ expect(() => createLightProbe(createVector3(0, 0, 0), new Float32Array(9))).toThrow();
74
+ });
75
+ });
76
+
77
+ describe('createLightProbeGrid', () => {
78
+ it('stores bounds, resolution, and probes, enabled by default', () => {
79
+ const grid = unitCubeGrid();
80
+ expect(grid.enabled).toBe(true);
81
+ expect(grid.probes.length).toBe(8);
82
+ expect(grid.resolution.x).toBe(2);
83
+ expect(grid.bounds.max.z).toBe(1);
84
+ });
85
+
86
+ it('throws when the probe count does not match the resolution product', () => {
87
+ const bounds = { max: createVector3(1, 1, 1), min: createVector3(0, 0, 0) };
88
+ expect(() => createLightProbeGrid([taggedProbe(0)], bounds, createVector3(2, 2, 2))).toThrow();
89
+ });
90
+
91
+ it('accepts an empty grid at zero resolution', () => {
92
+ const bounds = { max: createVector3(1, 1, 1), min: createVector3(0, 0, 0) };
93
+ expect(createLightProbeGrid([], bounds, createVector3(0, 0, 0)).probes.length).toBe(0);
94
+ });
95
+ });
96
+
97
+ describe('evaluateLightProbeSh', () => {
98
+ it('scales the l=0 coefficient by its constant basis, independent of the normal', () => {
99
+ const sh = new Float32Array(LIGHT_PROBE_SH_FLOATS);
100
+ sh[0] = 1;
101
+ sh[1] = 2;
102
+ sh[2] = 4;
103
+ const out = new Float32Array(3);
104
+ evaluateLightProbeSh(out, createVector3(1, 0, 0), sh);
105
+ expect(out[0]).toBeCloseTo(0.2820948, 6);
106
+ expect(out[1]).toBeCloseTo(0.5641896, 6);
107
+ expect(out[2]).toBeCloseTo(1.1283792, 6);
108
+
109
+ const other = new Float32Array(3);
110
+ evaluateLightProbeSh(other, createVector3(0, 0, 1), sh);
111
+ expect(other[0]).toBeCloseTo(out[0], 6);
112
+ });
113
+
114
+ it('flips sign with the normal on the l=1 bands, which is what makes them directional', () => {
115
+ const sh = new Float32Array(LIGHT_PROBE_SH_FLOATS);
116
+ sh[9] = 1; // the l=1, m=1 coefficient is index 3 of 9, so float offset 9 — the x lobe.
117
+ const positive = new Float32Array(3);
118
+ const negative = new Float32Array(3);
119
+ evaluateLightProbeSh(positive, createVector3(1, 0, 0), sh);
120
+ evaluateLightProbeSh(negative, createVector3(-1, 0, 0), sh);
121
+ expect(positive[0]).toBeCloseTo(0.4886025, 6);
122
+ expect(negative[0]).toBeCloseTo(-0.4886025, 6);
123
+ // The y and z lobes are orthogonal to an x normal.
124
+ expect(positive[1]).toBe(0);
125
+ });
126
+
127
+ it('normalizes the normal, so magnitude does not change the result', () => {
128
+ const sh = new Float32Array(LIGHT_PROBE_SH_FLOATS);
129
+ sh[9] = 1;
130
+ const unit = new Float32Array(3);
131
+ const long = new Float32Array(3);
132
+ evaluateLightProbeSh(unit, createVector3(1, 0, 0), sh);
133
+ evaluateLightProbeSh(long, createVector3(50, 0, 0), sh);
134
+ expect(long[0]).toBeCloseTo(unit[0], 6);
135
+ });
136
+
137
+ it('writes zeros for a zero-length normal instead of dividing by zero', () => {
138
+ const sh = new Float32Array(LIGHT_PROBE_SH_FLOATS).fill(1);
139
+ const out = new Float32Array(3).fill(7);
140
+ evaluateLightProbeSh(out, createVector3(0, 0, 0), sh);
141
+ expect(Array.from(out)).toEqual([0, 0, 0]);
142
+ });
143
+
144
+ it('is alias-safe when out is the coefficient buffer itself', () => {
145
+ const sh = new Float32Array(LIGHT_PROBE_SH_FLOATS);
146
+ sh[0] = 1;
147
+ sh[1] = 2;
148
+ sh[2] = 4;
149
+ evaluateLightProbeSh(sh, createVector3(1, 0, 0), sh);
150
+ expect(sh[0]).toBeCloseTo(0.2820948, 6);
151
+ expect(sh[1]).toBeCloseTo(0.5641896, 6);
152
+ expect(sh[2]).toBeCloseTo(1.1283792, 6);
153
+ });
154
+ });
155
+
156
+ describe('initializeLightProbe', () => {
157
+ it('fills a construction object with the same defaults createLightProbe produces', () => {
158
+ const out = {} as EntityConstruction<LightProbe>;
159
+ initializeLightProbe(out, createVector3(4, 5, 6));
160
+ expect(out.enabled).toBe(true);
161
+ expect(out.position.y).toBe(5);
162
+ expect(out.shCoefficients.length).toBe(LIGHT_PROBE_SH_FLOATS);
163
+ });
164
+
165
+ it('rejects a wrong-length coefficient buffer at the construction seam', () => {
166
+ const out = {} as EntityConstruction<LightProbe>;
167
+ expect(() => initializeLightProbe(out, createVector3(0, 0, 0), new Float32Array(26))).toThrow(
168
+ /must be 27 floats, got 26/,
169
+ );
170
+ });
171
+ });
172
+
173
+ describe('initializeLightProbeGrid', () => {
174
+ it('fills a construction object with copied bounds and resolution', () => {
175
+ const out = {} as EntityConstruction<LightProbeGrid>;
176
+ const bounds = { max: createVector3(2, 2, 2), min: createVector3(0, 0, 0) };
177
+ initializeLightProbeGrid(out, [taggedProbe(1), taggedProbe(2)], bounds, createVector3(2, 1, 1));
178
+ expect(out.enabled).toBe(true);
179
+ expect(out.probes.length).toBe(2);
180
+ bounds.max.x = 99;
181
+ expect(out.bounds.max.x).toBe(2);
182
+ });
183
+
184
+ it('names both the expected and actual probe count when they disagree', () => {
185
+ const out = {} as EntityConstruction<LightProbeGrid>;
186
+ const bounds = { max: createVector3(1, 1, 1), min: createVector3(0, 0, 0) };
187
+ expect(() => initializeLightProbeGrid(out, [taggedProbe(1)], bounds, createVector3(2, 2, 2))).toThrow(
188
+ /must hold 8 probes for resolution 2x2x2, got 1/,
189
+ );
190
+ });
191
+ });
192
+
193
+ describe('sampleLightProbeGrid', () => {
194
+ it('returns each corner probe exactly at that corner', () => {
195
+ const grid = unitCubeGrid();
196
+ expect(sampleTag(grid, 0, 0, 0)).toBeCloseTo(0, 5);
197
+ expect(sampleTag(grid, 1, 0, 0)).toBeCloseTo(1, 5);
198
+ expect(sampleTag(grid, 0, 1, 0)).toBeCloseTo(2, 5);
199
+ expect(sampleTag(grid, 0, 0, 1)).toBeCloseTo(4, 5);
200
+ expect(sampleTag(grid, 1, 1, 1)).toBeCloseTo(7, 5);
201
+ });
202
+
203
+ it('averages all eight probes at the centre', () => {
204
+ // (0 + 1 + ... + 7) / 8 = 3.5
205
+ expect(sampleTag(unitCubeGrid(), 0.5, 0.5, 0.5)).toBeCloseTo(3.5, 5);
206
+ });
207
+
208
+ it('blends only the two probes along an edge', () => {
209
+ const grid = unitCubeGrid();
210
+ // Midway along the x edge at y=z=0 sits between tags 0 and 1.
211
+ expect(sampleTag(grid, 0.5, 0, 0)).toBeCloseTo(0.5, 5);
212
+ // Three quarters along the same edge weights tag 1 by 0.75.
213
+ expect(sampleTag(grid, 0.75, 0, 0)).toBeCloseTo(0.75, 5);
214
+ // Midway along the z edge sits between tags 0 and 4.
215
+ expect(sampleTag(grid, 0, 0, 0.5)).toBeCloseTo(2, 5);
216
+ });
217
+
218
+ it('clamps a position outside the bounds to the boundary rather than missing', () => {
219
+ const grid = unitCubeGrid();
220
+ expect(sampleTag(grid, -50, -50, -50)).toBeCloseTo(0, 5);
221
+ expect(sampleTag(grid, 50, 50, 50)).toBeCloseTo(7, 5);
222
+ // Clamping is per axis: only x is outside here, so y and z still interpolate.
223
+ expect(sampleTag(grid, 50, 0.5, 0)).toBeCloseTo(2, 5);
224
+ });
225
+
226
+ it('treats an axis of resolution 1 as uniform instead of double-weighting its probe', () => {
227
+ const bounds = { max: createVector3(1, 1, 1), min: createVector3(0, 0, 0) };
228
+ const grid = createLightProbeGrid([taggedProbe(10), taggedProbe(20)], bounds, createVector3(2, 1, 1));
229
+ const out = new Float32Array(LIGHT_PROBE_SH_FLOATS);
230
+ expect(sampleLightProbeGrid(out, createVector3(0.5, 0.5, 0.5), grid)).toBe(true);
231
+ expect(out[0]).toBeCloseTo(15, 5);
232
+ });
233
+
234
+ it('keeps a degenerate INNER axis from pulling in the neighbouring plane it indexes into', () => {
235
+ // Resolution 2x1x2. The degenerate y axis still carries a stride of nx, so its +1 corner is not
236
+ // out of range — it names a real probe in the z=1 plane. That corner weighs exactly 0, which is
237
+ // why it cannot contribute; a resolution-1 axis on the OUTSIDE (2x1x1) would only ever index past
238
+ // the end and so cannot distinguish a zero weight from a bounds check.
239
+ const probes = [taggedProbe(10), taggedProbe(20), taggedProbe(30), taggedProbe(40)];
240
+ const bounds = { max: createVector3(1, 1, 1), min: createVector3(0, 0, 0) };
241
+ const grid = createLightProbeGrid(probes, bounds, createVector3(2, 1, 2));
242
+ const out = new Float32Array(LIGHT_PROBE_SH_FLOATS);
243
+
244
+ expect(sampleLightProbeGrid(out, createVector3(0.5, 0.5, 0), grid)).toBe(true);
245
+ expect(out[0]).toBeCloseTo(15, 5);
246
+
247
+ expect(sampleLightProbeGrid(out, createVector3(0.5, 0.5, 1), grid)).toBe(true);
248
+ expect(out[0]).toBeCloseTo(35, 5);
249
+ });
250
+
251
+ it('drops a disabled probe and renormalizes, so switching one off does not dim its neighbours', () => {
252
+ const bounds = { max: createVector3(1, 1, 1), min: createVector3(0, 0, 0) };
253
+ const probes = [taggedProbe(10), taggedProbe(20)];
254
+ const grid = createLightProbeGrid(probes, bounds, createVector3(2, 1, 1));
255
+ const out = new Float32Array(LIGHT_PROBE_SH_FLOATS);
256
+
257
+ expect(sampleLightProbeGrid(out, createVector3(0.5, 0, 0), grid)).toBe(true);
258
+ expect(out[0]).toBeCloseTo(15, 5);
259
+
260
+ probes[1].enabled = false;
261
+ expect(sampleLightProbeGrid(out, createVector3(0.5, 0, 0), grid)).toBe(true);
262
+ // Renormalized, not weighted-to-black: the surviving probe's value comes back undimmed.
263
+ expect(out[0]).toBeCloseTo(10, 5);
264
+ });
265
+
266
+ it('returns false and leaves out untouched when disabled, empty, or fully disabled', () => {
267
+ const bounds = { max: createVector3(1, 1, 1), min: createVector3(0, 0, 0) };
268
+ const out = new Float32Array(LIGHT_PROBE_SH_FLOATS).fill(-1);
269
+
270
+ const disabled = unitCubeGrid();
271
+ disabled.enabled = false;
272
+ expect(sampleLightProbeGrid(out, createVector3(0, 0, 0), disabled)).toBe(false);
273
+
274
+ const empty = createLightProbeGrid([], bounds, createVector3(0, 0, 0));
275
+ expect(sampleLightProbeGrid(out, createVector3(0, 0, 0), empty)).toBe(false);
276
+
277
+ const allOff = unitCubeGrid();
278
+ for (const probe of allOff.probes) probe.enabled = false;
279
+ expect(sampleLightProbeGrid(out, createVector3(0.5, 0.5, 0.5), allOff)).toBe(false);
280
+
281
+ expect(out[0]).toBe(-1);
282
+ });
283
+
284
+ it('is alias-safe when out is a probe coefficient buffer inside the grid', () => {
285
+ // Aliasing the LAST corner is what actually exercises this. An implementation that wrote into
286
+ // `out` while accumulating would clobber that probe before its own turn to be read, and fold the
287
+ // running partial sum back in as if it were the probe's value. Aliasing the first corner cannot
288
+ // catch that: its contribution is already banked before any write lands on it.
289
+ const last = unitCubeGrid();
290
+ expect(sampleLightProbeGrid(last.probes[7].shCoefficients, createVector3(0.5, 0.5, 0.5), last)).toBe(true);
291
+ expect(last.probes[7].shCoefficients[0]).toBeCloseTo(3.5, 5);
292
+
293
+ const first = unitCubeGrid();
294
+ expect(sampleLightProbeGrid(first.probes[0].shCoefficients, createVector3(0.5, 0.5, 0.5), first)).toBe(true);
295
+ expect(first.probes[0].shCoefficients[0]).toBeCloseTo(3.5, 5);
296
+ });
297
+ });
298
+
299
+ describe('setLightProbeShFromColors', () => {
300
+ it('round-trips a constant color through projection and evaluation', () => {
301
+ // Over the six axis directions every band above l=0 cancels exactly, so reconstruction returns
302
+ // the input color at ANY normal.
303
+ const colors = new Float32Array(18);
304
+ for (let i = 0; i < 6; i++) {
305
+ colors[i * 3] = 2;
306
+ colors[i * 3 + 1] = 3;
307
+ colors[i * 3 + 2] = 4;
308
+ }
309
+ const probe = createLightProbe(createVector3(0, 0, 0));
310
+ setLightProbeShFromColors(probe, colors, AXIS_DIRECTIONS);
311
+
312
+ for (const normal of [createVector3(1, 0, 0), createVector3(0, 1, 0), createVector3(0, 0, -1)]) {
313
+ const out = new Float32Array(3);
314
+ evaluateLightProbeSh(out, normal, probe.shCoefficients);
315
+ expect(out[0]).toBeCloseTo(2, 5);
316
+ expect(out[1]).toBeCloseTo(3, 5);
317
+ expect(out[2]).toBeCloseTo(4, 5);
318
+ }
319
+ });
320
+
321
+ it('round-trips a linear gradient to its exact value on each axis', () => {
322
+ // color(d) = 1 + 0.5 * (d . x). Projected over the six axes this leaves only the l=0 term (1) and
323
+ // the l=1 x term, whose reconstruction at +x contributes exactly 0.5 — every l=2 term cancels.
324
+ const colors = new Float32Array([1.5, 1.5, 1.5, 0.5, 0.5, 0.5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
325
+ const probe = createLightProbe(createVector3(0, 0, 0));
326
+ setLightProbeShFromColors(probe, colors, AXIS_DIRECTIONS);
327
+
328
+ const out = new Float32Array(3);
329
+ evaluateLightProbeSh(out, createVector3(1, 0, 0), probe.shCoefficients);
330
+ expect(out[0]).toBeCloseTo(1.5, 5);
331
+ evaluateLightProbeSh(out, createVector3(-1, 0, 0), probe.shCoefficients);
332
+ expect(out[0]).toBeCloseTo(0.5, 5);
333
+ // The gradient runs along x only, so a y normal sees the mean.
334
+ evaluateLightProbeSh(out, createVector3(0, 1, 0), probe.shCoefficients);
335
+ expect(out[0]).toBeCloseTo(1, 5);
336
+ });
337
+
338
+ it('replaces prior coefficients rather than accumulating into them', () => {
339
+ const colors = new Float32Array(18).fill(1);
340
+ const probe = createLightProbe(createVector3(0, 0, 0));
341
+ setLightProbeShFromColors(probe, colors, AXIS_DIRECTIONS);
342
+ const first = probe.shCoefficients[0];
343
+ setLightProbeShFromColors(probe, colors, AXIS_DIRECTIONS);
344
+ expect(probe.shCoefficients[0]).toBeCloseTo(first, 5);
345
+ });
346
+
347
+ it('zeroes the coefficients when given no samples', () => {
348
+ const probe = createLightProbe(createVector3(0, 0, 0));
349
+ probe.shCoefficients.fill(5);
350
+ setLightProbeShFromColors(probe, new Float32Array(0), new Float32Array(0));
351
+ expect(Array.from(probe.shCoefficients).every((v) => v === 0)).toBe(true);
352
+ });
353
+
354
+ it('skips a zero-length direction instead of producing NaN', () => {
355
+ const colors = new Float32Array([1, 1, 1, 1, 1, 1]);
356
+ const directions = new Float32Array([0, 0, 0, 1, 0, 0]);
357
+ const probe = createLightProbe(createVector3(0, 0, 0));
358
+ setLightProbeShFromColors(probe, colors, directions);
359
+ expect(Array.from(probe.shCoefficients).every((v) => Number.isFinite(v))).toBe(true);
360
+ expect(probe.shCoefficients[0]).not.toBe(0);
361
+ });
362
+ });
@@ -1,7 +1,7 @@
1
1
  import { createVector3 } from '@flighthq/geometry/contract';
2
- import { CandelaLightUnit, PointLightKind, UnitlessLightUnit } from '@flighthq/types/contract';
3
2
 
4
- import { clonePointLight, createPointLight, initializePointLight } from './pointLight';
3
+ import { clonePointLight, createPointLight, initializePointLight } from './pointLight.ts';
4
+ import { CandelaLightUnit, PointLightKind, UnitlessLightUnit } from './types.ts';
5
5
 
6
6
  describe('clonePointLight', () => {
7
7
  it('creates an independent copy with a fresh position vector', () => {