@ignifx/core 0.3.1 → 0.5.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.
@@ -1 +0,0 @@
1
- {"version":3,"file":"environment-C_JfuAjj.js","names":[],"sources":["../src/lite/gpu/environment.ts"],"sourcesContent":["import {\n AcesToneMapping,\n loadEnvironment,\n loadSkybox,\n NeutralToneMapping,\n setEnvironmentBlur,\n setEnvironmentRotation,\n setFog,\n setSceneImageProcessing,\n StandardToneMapping,\n} from \"@babylonjs/lite\";\nimport { assertNever } from \"../../errors/ignifx-error.js\";\nimport { degToRad } from \"../../math/math-utils.js\";\nimport type { EnvironmentTextures, SceneContext, ToneMapping } from \"@babylonjs/lite\";\n\n/**\n * Environment half of the Babylon Lite adapter (`docs/architecture/07-rendering.md` §2.5): image\n * based lighting, the skybox, fog, image processing, and the clear colour.\n *\n * Everything here is `@internal`. Loading uploads cube maps, so the module is GPU-only.\n *\n * ## The BRDF LUT is mandatory (verified against `@babylonjs/lite@1.27.0`)\n *\n * `loadEnvironment(scene, url, options)` (`index.d.ts` 6779) takes a **required** `brdfUrl`: Lite\n * decodes the split-sum BRDF lookup table from a pre-baked RGBD PNG rather than computing it, \"for\n * pixel-perfect parity\" with Babylon.js (`lib/_chunks/env-helpers-*.js`, `loadBrdfImage`, which\n * `fetch`es the URL and decodes it with `createImageBitmap`). ignifx ships that PNG as\n * `tests/fixtures/assets/brdf-lut.png`, decoded from Babylon.js's own\n * `_environmentBRDFBase64Texture` (`packages/dev/core/src/Misc/brdfTextureTools.ts`, Apache-2.0).\n * It is 256×256 RGBA8 and 23 KB.\n *\n * ## Fog\n *\n * `SceneContext.fog` is `FogConfig | null` (`index.d.ts` 5350) with a numeric `mode`; Lite exports\n * no named constants for it, so {@link FOG_MODES} maps ignifx's names onto the numbers.\n *\n * **Fog has to be installed with `setFog`, not by assigning the field.** `scene.fog` is what the PBR\n * group builder reads to decide whether a material's program gets the fog block at all\n * (`lib/material/pbr/pbr-renderable.js` 140-159, `PBR_HAS_FOG`), but the block it compiles reads\n * `scene.vFogInfos.x` out of the scene uniform buffer — and the only writer of those slots is\n * `writeFogUbo`, which nothing registers except `setFog`\n * (`lib/scene/scene-ubo-extras.js`: `setFog` is `scene.fog = config` **plus**\n * `_registerSceneUboContributor(scene, writeFogUbo)`, and `_writePassSceneUBO` runs\n * `scene._sceneUboContributors` and nothing else, `lib/frame-graph/render-task.js` 372-379).\n * Lite says as much in `index.d.ts` 10471: \"importing `setFog` is what pulls the fog UBO writer into\n * the bundle\". Assigning `scene.fog` alone therefore compiles the fog block into every PBR pipeline\n * and leaves `vFogInfos` zeroed, so `vFogInfos.x > 0.0` is never true and the scene renders with no\n * fog at all — the cost with none of the effect.\n *\n * A **fresh** config object goes in on every change, because a render task caches its scene UBO on\n * the identity of `scene.fog` (`s[1] === fog`, `lib/frame-graph/render-task.js` 355-371): mutating\n * the installed object in place leaves the cache key equal and the new density or colour never\n * reaches the GPU. Turning fog off writes mode `0`, which is what the already-compiled fog block\n * reads as \"no fog\"; dropping the field back to `null` would only stop the writer and leave the last\n * fog standing. A scene that never had fog keeps its `null` and pays nothing.\n *\n * ## Image processing\n *\n * `setSceneImageProcessing` is **async** (`index.d.ts` 10917) because tone mapping is baked into\n * the PBR shaders at `registerScene` time; changing it recompiles the affected pipelines\n * (`rebuildScenePbrPipelines`). Setting exposure or contrast alone still goes through the same\n * call. Note that `loadEnvironment` **overwrites** all three (`toneMappingEnabled = true`,\n * `exposure = 0.8`, `contrast = 1.2`) as a side effect of installing an environment, which is why\n * the `Environment` component re-applies its own image processing after every install.\n *\n * ## Swapping the installed environment: `scene._envTextures`\n *\n * Lite 1.27.0 declares **no** public way to replace, clear, or read back a registered scene's\n * environment. `index.d.ts` has no `scene.environmentTexture`, no `unloadEnvironment`, and no\n * setter of any kind; `loadEnvironment` is the only installer and it fetches, decodes, and uploads\n * a fresh cube map every call. What it actually installs is one field —\n * `scene._envTextures = textures` (`lib/_chunks/env-helpers-CRI1i-A3.js` 24, and the same line in\n * `lib/loader-hdr/load-hdr.js` 23, `lib/loader-env/load-dds-env.js` 177 and\n * `lib/loader-gltf/gltf-ext-lights-image-based.js` 65) — and every consumer reads that field:\n *\n * - the PBR group builder, which bakes the cube view and sampler into each material's bind group\n * (`lib/material/pbr/pbr-material.js` 12 → `lib/material/pbr/pbr-renderable.js`);\n * - the scene-uniform packer, for `lodGenerationScale` (`lib/frame-graph/scene-uniforms-pack.js` 21);\n * - the env UBO contributor, for the diffuse spherical harmonics (`lib/scene/scene-ubo-extras.js` 24);\n * - the blur contributor, for `lodGenerationOffset` (`lib/scene/set-environment-blur.js` 8);\n * - the render task's scene-UBO cache key, which is keyed on the **object identity** of the field\n * (`lib/frame-graph/render-task.js` 358), so a new object re-uploads the harmonics by itself.\n *\n * Lite's own device-lost recovery replaces the field exactly this way\n * (`lib/loader-env/environment-recovery.js` 16 and 38), so {@link installSceneEnvironment} writes it\n * too. Bind groups are the one consumer that does *not* re-read it: they hold the `GPUTextureView`,\n * so a swap only reaches the specular reflection once the material groups have been rebuilt, which\n * `rebuildSceneRenderables` does (`lib/scene/scene-rebuild.js`, `rebuildEachGroup`). The\n * `Environment` component therefore reports an install as a topology change and lets the render-sync\n * system fire its one coalesced rebuild. This is the only place in ignifx that touches a Lite field\n * `index.d.ts` does not declare, which is what the adapter boundary exists for\n * (`CONSTITUTION.md` §3.4, ADR-0002); it is pinned by\n * `packages/core/test/lite/render/environment.browser.test.ts`, which fails loudly if the field is\n * renamed.\n */\n\n/**\n * The GPU-resident image-based-lighting textures `loadEnvironment` resolves to, re-exported under an\n * ignifx name (`CONSTITUTION.md` §3.4, coding standards §4).\n *\n * @remarks\n * Unstable; excluded from the stability guarantees of `CONSTITUTION.md` Article IV.\n *\n * @public\n */\nexport type LiteEnvironmentTextures = EnvironmentTextures;\n\n/**\n * ignifx fog mode names mapped to the numbers `FogConfig.mode` uses.\n *\n * @internal\n */\nexport const FOG_MODES = {\n /** No fog. */\n none: 0,\n /** Exponential fog. */\n exp: 1,\n /** Squared-exponential fog. */\n exp2: 2,\n /** Linear fog between `start` and `end`. */\n linear: 3,\n} as const;\n\n/**\n * The union of the fog mode names.\n *\n * @internal\n */\nexport type FogMode = keyof typeof FOG_MODES;\n\n/**\n * The tone-mapping curves `docs/architecture/07-rendering.md` §2.5 exposes.\n *\n * @public\n */\nexport const TONE_MAPPING_NAMES = [\"none\", \"standard\", \"aces\", \"neutral\"] as const;\n\n/**\n * The union of the tone-mapping curve names.\n *\n * @internal\n */\nexport type ToneMappingName = (typeof TONE_MAPPING_NAMES)[number];\n\n/**\n * How an ignifx `Environment` component configures image based lighting.\n *\n * @internal\n */\nexport interface EnvironmentLoadOptions {\n /** The `.env` file holding the prefiltered specular cube map and its spherical harmonics. */\n readonly url: string;\n /** The RGBD PNG BRDF lookup table. Required by Lite. */\n readonly brdfUrl: string;\n /** A `.dds` or `.env` skybox. Omit for a flat background. */\n readonly skyboxUrl?: string;\n /** The skybox cube's size, in metres. Lite defaults to 20. */\n readonly skyboxSize?: number;\n /** Skip the skybox entirely. */\n readonly skipSkybox?: boolean;\n /** Skip the reflective ground plane Lite would otherwise add. */\n readonly skipGround?: boolean;\n /** A texture for that ground plane. */\n readonly groundTextureUrl?: string;\n}\n\n/**\n * Loads an `.env` environment and installs it on a scene.\n *\n * @param scene - The scene to light.\n * @param options - The environment, BRDF, and skybox URLs.\n * @returns The GPU-resident environment textures, for materials that need them explicitly.\n *\n * @example\n * ```ts\n * await loadSceneEnvironment(scene, {\n * url: \"environments/studio.env\",\n * brdfUrl: \"environments/brdf-lut.png\",\n * });\n * ```\n *\n * @internal\n */\nexport function loadSceneEnvironment(\n scene: SceneContext,\n options: EnvironmentLoadOptions,\n): Promise<EnvironmentTextures> {\n const liteOptions: {\n brdfUrl: string;\n skyboxUrl?: string;\n skyboxSize?: number;\n skipSkybox?: boolean;\n skipGround?: boolean;\n groundTextureUrl?: string;\n } = { brdfUrl: options.brdfUrl };\n if (options.skyboxUrl !== undefined) {\n liteOptions.skyboxUrl = options.skyboxUrl;\n }\n if (options.skyboxSize !== undefined) {\n liteOptions.skyboxSize = options.skyboxSize;\n }\n if (options.skipSkybox !== undefined) {\n liteOptions.skipSkybox = options.skipSkybox;\n }\n if (options.skipGround !== undefined) {\n liteOptions.skipGround = options.skipGround;\n }\n if (options.groundTextureUrl !== undefined) {\n liteOptions.groundTextureUrl = options.groundTextureUrl;\n }\n return loadEnvironment(scene, options.url, liteOptions);\n}\n\n/**\n * The scene fields Babylon Lite 1.27.0 writes for image-based lighting but does not declare in\n * `index.d.ts`. Adapter-internal Lite mirror (coding standards §5.1); see the module header for the\n * `lib/` lines that prove each one.\n */\ninterface SceneEnvironmentSlot {\n /** What `loadEnvironment` installed, or `undefined` on a scene that has never had one. */\n _envTextures?: EnvironmentTextures | undefined;\n}\n\n/**\n * Views a scene through its undeclared environment slot.\n *\n * @param scene - The scene.\n * @returns The same object, typed so the slot is reachable.\n */\nfunction environmentSlotOf(scene: SceneContext): SceneEnvironmentSlot {\n // Boundary assertion (coding standards §5.2): `SceneContext` is a plain object literal Lite\n // creates in `lib/scene/scene-core.js`, and `_envTextures` is one of its own fields — it is\n // absent from `index.d.ts` because it is not part of Lite's declared surface, not because it is\n // a different object. See the module header for every `lib/` line that reads or writes it.\n // oxlint-disable-next-line typescript/no-unsafe-type-assertion\n return scene as SceneContext & SceneEnvironmentSlot;\n}\n\n/**\n * Reports which environment textures a scene is currently lit by.\n *\n * @param scene - The scene to inspect.\n * @returns The installed textures, or `null` when no environment has ever been installed.\n *\n * @internal\n */\nexport function readSceneEnvironment(scene: SceneContext): EnvironmentTextures | null {\n // The underscore is Lite's, not ignifx's: coding standards §5.1 reserves `_field` for exactly\n // this, an adapter-internal mirror of a Lite field, and the module header proves the name.\n // oxlint-disable-next-line no-underscore-dangle\n return environmentSlotOf(scene)._envTextures ?? null;\n}\n\n/**\n * Points a scene at an already-loaded set of environment textures, without loading anything.\n *\n * @remarks\n * This is how an `Environment` component swaps environments at runtime: the textures belong to the\n * `EnvironmentAsset` that loaded them and stay alive as long as its handle is retained, so\n * switching between two loaded assets uploads nothing and frees nothing.\n *\n * The specular reflection follows only after the scene's material groups are rebuilt\n * (`rebuildRenderables` in `../shadow.ts`): a PBR bind group holds the cube map's `GPUTextureView`,\n * not the slot. The diffuse harmonics and the LOD scale follow immediately, because the render\n * task's scene-UBO cache is keyed on this object's identity.\n *\n * @param scene - The scene to light.\n * @param textures - The textures to light it with.\n * @returns `true` when the scene was lit by something else and now is not — the caller owes a\n * renderable rebuild; `false` when these textures were already installed and nothing was written.\n *\n * @example\n * ```ts\n * if (installSceneEnvironment(scene, asset.lite.textures)) {\n * await rebuildRenderables(scene);\n * }\n * ```\n *\n * @internal\n */\nexport function installSceneEnvironment(scene: SceneContext, textures: EnvironmentTextures): boolean {\n const slot = environmentSlotOf(scene);\n // As in `readSceneEnvironment`: an adapter-internal Lite mirror (coding standards §5.1).\n // oxlint-disable-next-line no-underscore-dangle\n if (slot._envTextures === textures) {\n return false;\n }\n // oxlint-disable-next-line no-underscore-dangle\n slot._envTextures = textures;\n return true;\n}\n\n/**\n * Loads a six-face cube-map skybox from a base URL.\n *\n * @param scene - The scene to give a background.\n * @param baseUrl - The URL prefix each face name is appended to.\n * @param extension - The face file extension, such as `\".jpg\"`.\n * @param size - The skybox cube's size, in metres.\n * @returns A promise that resolves when the faces are uploaded.\n *\n * @internal\n */\nexport function loadSceneSkybox(scene: SceneContext, baseUrl: string, extension: string, size?: number): Promise<void> {\n return loadSkybox(scene, baseUrl, extension, size);\n}\n\n/**\n * Rotates the environment around the world Y axis.\n *\n * @param scene - The scene to rotate the environment of.\n * @param degrees - The rotation, in degrees.\n *\n * @internal\n */\nexport function setSceneEnvironmentRotation(scene: SceneContext, degrees: number): void {\n setEnvironmentRotation(scene, degToRad(degrees));\n}\n\n/**\n * Blurs the environment's specular reflection.\n *\n * @param scene - The scene to change.\n * @param blur - The blur amount, 0 to 1.\n *\n * @internal\n */\nexport function setSceneEnvironmentBlur(scene: SceneContext, blur: number): void {\n setEnvironmentBlur(scene, blur);\n}\n\n/**\n * Sets the colour the scene is cleared to each frame.\n *\n * @param scene - The scene to change.\n * @param r - The linear red component, 0 to 1.\n * @param g - The linear green component, 0 to 1.\n * @param b - The linear blue component, 0 to 1.\n * @param a - The alpha component; only meaningful with a premultiplied canvas alpha mode.\n *\n * @internal\n */\nexport function setSceneClearColor(scene: SceneContext, r: number, g: number, b: number, a: number): void {\n const clear = scene.clearColor;\n clear.r = r;\n clear.g = g;\n clear.b = b;\n clear.a = a;\n}\n\n/**\n * Configures the scene's fog, or turns it off.\n *\n * @param scene - The scene to change.\n * @param mode - The fog falloff, or `\"none\"` to disable it.\n * @param r - The sRGB red component of the fog colour; Lite's fog block decodes it itself.\n * @param g - The sRGB green component.\n * @param b - The sRGB blue component.\n * @param density - The density, for the exponential modes.\n * @param start - Where linear fog begins, in metres.\n * @param end - Where linear fog reaches full strength, in metres.\n *\n * @internal\n */\nexport function setSceneFog(\n scene: SceneContext,\n mode: FogMode,\n r: number,\n g: number,\n b: number,\n density: number,\n start: number,\n end: number,\n): void {\n if (mode === \"none\" && scene.fog === null) {\n // A scene that never had fog pays nothing: no contributor, no fog block in any PBR pipeline.\n return;\n }\n setFog(scene, { mode: FOG_MODES[mode], density, start, end, color: [r, g, b] });\n}\n\n/**\n * Sets exposure, contrast, and the tone-mapping curve.\n *\n * @remarks\n * Asynchronous by nature: tone mapping is compiled into the PBR shaders, so changing it after\n * `registerScene` recompiles the scene's PBR pipelines. Exposure and contrast alone are uniform\n * values, but Lite routes them through the same call.\n *\n * @param scene - The scene to change.\n * @param exposure - The exposure multiplier.\n * @param contrast - The contrast multiplier.\n * @param toneMapping - The curve, or `\"none\"` to leave the colour untouched.\n * @returns A promise that resolves when any affected pipeline has been rebuilt.\n *\n * @internal\n */\nexport function setSceneImageProcessingOptions(\n scene: SceneContext,\n exposure: number,\n contrast: number,\n toneMapping: ToneMappingName,\n): Promise<void> {\n if (toneMapping === \"none\") {\n return setSceneImageProcessing(scene, { exposure, contrast, toneMappingEnabled: false });\n }\n return setSceneImageProcessing(scene, {\n exposure,\n contrast,\n toneMappingEnabled: true,\n toneMapping: toneMappingCurve(toneMapping),\n });\n}\n\n/**\n * Maps an ignifx tone-mapping name onto the Lite curve object.\n *\n * @param name - The curve name; `\"none\"` is handled by the caller.\n * @returns The Lite curve.\n */\nfunction toneMappingCurve(name: Exclude<ToneMappingName, \"none\">): ToneMapping {\n switch (name) {\n case \"standard\":\n return StandardToneMapping;\n case \"aces\":\n return AcesToneMapping;\n case \"neutral\":\n return NeutralToneMapping;\n default:\n return assertNever(name, \"tone mapping\");\n }\n}\n"],"mappings":";;;;;;;;;AAgHA,MAAa,YAAY;;CAEvB,MAAM;;CAEN,KAAK;;CAEL,MAAM;;CAEN,QAAQ;AACV;;;;;;AAcA,MAAa,qBAAqB;CAAC;CAAQ;CAAY;CAAQ;AAAS;;;;;;;;;;;;;;;;;;AAgDxE,SAAgB,qBACd,OACA,SAC8B;CAC9B,MAAM,cAOF,EAAE,SAAS,QAAQ,QAAQ;CAC/B,IAAI,QAAQ,cAAc,KAAA,GACxB,YAAY,YAAY,QAAQ;CAElC,IAAI,QAAQ,eAAe,KAAA,GACzB,YAAY,aAAa,QAAQ;CAEnC,IAAI,QAAQ,eAAe,KAAA,GACzB,YAAY,aAAa,QAAQ;CAEnC,IAAI,QAAQ,eAAe,KAAA,GACzB,YAAY,aAAa,QAAQ;CAEnC,IAAI,QAAQ,qBAAqB,KAAA,GAC/B,YAAY,mBAAmB,QAAQ;CAEzC,OAAO,gBAAgB,OAAO,QAAQ,KAAK,WAAW;AACxD;;;;;;;AAkBA,SAAS,kBAAkB,OAA2C;CAMpE,OAAO;AACT;;;;;;;;;AAUA,SAAgB,qBAAqB,OAAiD;CAIpF,OAAO,kBAAkB,KAAK,CAAC,CAAC,gBAAgB;AAClD;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAgB,wBAAwB,OAAqB,UAAwC;CACnG,MAAM,OAAO,kBAAkB,KAAK;CAGpC,IAAI,KAAK,iBAAiB,UACxB,OAAO;CAGT,KAAK,eAAe;CACpB,OAAO;AACT;;;;;;;;;AAyBA,SAAgB,4BAA4B,OAAqB,SAAuB;CACtF,uBAAuB,OAAO,SAAS,OAAO,CAAC;AACjD;;;;;;;;;AAUA,SAAgB,wBAAwB,OAAqB,MAAoB;CAC/E,mBAAmB,OAAO,IAAI;AAChC;;;;;;;;;;;;AAaA,SAAgB,mBAAmB,OAAqB,GAAW,GAAW,GAAW,GAAiB;CACxG,MAAM,QAAQ,MAAM;CACpB,MAAM,IAAI;CACV,MAAM,IAAI;CACV,MAAM,IAAI;CACV,MAAM,IAAI;AACZ;;;;;;;;;;;;;;;AAgBA,SAAgB,YACd,OACA,MACA,GACA,GACA,GACA,SACA,OACA,KACM;CACN,IAAI,SAAS,UAAU,MAAM,QAAQ,MAEnC;CAEF,OAAO,OAAO;EAAE,MAAM,UAAU;EAAO;EAAS;EAAO;EAAK,OAAO;GAAC;GAAG;GAAG;EAAC;CAAE,CAAC;AAChF;;;;;;;;;;;;;;;;;AAkBA,SAAgB,+BACd,OACA,UACA,UACA,aACe;CACf,IAAI,gBAAgB,QAClB,OAAO,wBAAwB,OAAO;EAAE;EAAU;EAAU,oBAAoB;CAAM,CAAC;CAEzF,OAAO,wBAAwB,OAAO;EACpC;EACA;EACA,oBAAoB;EACpB,aAAa,iBAAiB,WAAW;CAC3C,CAAC;AACH;;;;;;;AAQA,SAAS,iBAAiB,MAAqD;CAC7E,QAAQ,MAAR;EACE,KAAK,YACH,OAAO;EACT,KAAK,QACH,OAAO;EACT,KAAK,WACH,OAAO;EACT,SACE,OAAO,YAAY,MAAM,cAAc;CAC3C;AACF"}
@@ -1,277 +0,0 @@
1
- //#region src/math/math-utils.ts
2
- /**
3
- * Scalar helpers shared by the vector, quaternion and matrix types and by game code.
4
- *
5
- * Angles are **degrees** in every public API unless the name ends in `Rad`/`Radians`
6
- * (coding standards §5.1, ADR-0011). Every function here is a pure scalar function: none of them
7
- * allocate, so they are safe in per-frame code (coding standards §7).
8
- */
9
- /**
10
- * The default tolerance for approximate float comparisons. Chosen for single-precision positions in
11
- * metres: `Float32Array` round-tripping loses roughly 1e-7 of relative precision, so 1e-6 is the
12
- * smallest value that does not report false differences on data that has been through the GPU.
13
- *
14
- * @public
15
- */
16
- const EPSILON = 1e-6;
17
- /**
18
- * Multiplier that converts degrees to radians.
19
- *
20
- * @public
21
- */
22
- const DEG_TO_RAD = Math.PI / 180;
23
- /**
24
- * Multiplier that converts radians to degrees.
25
- *
26
- * @public
27
- */
28
- const RAD_TO_DEG = 180 / Math.PI;
29
- /**
30
- * Converts an angle from degrees to radians.
31
- *
32
- * @param degrees - The angle in degrees.
33
- * @returns The same angle in radians.
34
- *
35
- * @public
36
- */
37
- function degToRad(degrees) {
38
- return degrees * DEG_TO_RAD;
39
- }
40
- /**
41
- * Converts an angle from radians to degrees.
42
- *
43
- * @param radians - The angle in radians.
44
- * @returns The same angle in degrees.
45
- *
46
- * @public
47
- */
48
- function radToDeg(radians) {
49
- return radians * RAD_TO_DEG;
50
- }
51
- /**
52
- * Constrains a value to an inclusive range.
53
- *
54
- * @param value - The value to constrain.
55
- * @param min - The lower bound.
56
- * @param max - The upper bound.
57
- * @returns `min` when `value` is smaller, `max` when it is larger, otherwise `value` unchanged.
58
- * `NaN` propagates.
59
- *
60
- * @example
61
- * ```ts
62
- * clamp(12, 0, 10); // 10
63
- * ```
64
- *
65
- * @public
66
- */
67
- function clamp(value, min, max) {
68
- if (value < min) return min;
69
- if (value > max) return max;
70
- return value;
71
- }
72
- /**
73
- * Constrains a value to the 0–1 range.
74
- *
75
- * @param value - The value to constrain.
76
- * @returns The value clamped into `[0, 1]`.
77
- *
78
- * @public
79
- */
80
- function clamp01(value) {
81
- return clamp(value, 0, 1);
82
- }
83
- /**
84
- * Linearly interpolates between two values. The interpolant is **not** clamped, so values outside
85
- * `[0, 1]` extrapolate; wrap `t` in {@link clamp01} when that is not wanted.
86
- *
87
- * @param a - The value returned at `t === 0`.
88
- * @param b - The value returned at `t === 1`.
89
- * @param t - The interpolant.
90
- * @returns The interpolated value.
91
- *
92
- * @example
93
- * ```ts
94
- * lerp(0, 10, 0.25); // 2.5
95
- * ```
96
- *
97
- * @public
98
- */
99
- function lerp(a, b, t) {
100
- return a + (b - a) * t;
101
- }
102
- /**
103
- * The inverse of {@link lerp}: finds the interpolant that maps `a`–`b` onto `value`.
104
- *
105
- * @param a - The value that maps to 0.
106
- * @param b - The value that maps to 1.
107
- * @param value - The value to locate.
108
- * @returns The interpolant, clamped into `[0, 1]`. Returns 0 when `a` and `b` are equal.
109
- *
110
- * @public
111
- */
112
- function inverseLerp(a, b, value) {
113
- if (a === b) return 0;
114
- return clamp01((value - a) / (b - a));
115
- }
116
- /**
117
- * Smoothly interpolates between two edges with a Hermite curve (the GLSL `smoothstep`), easing in
118
- * and out instead of the straight ramp of {@link lerp}.
119
- *
120
- * @param edge0 - The value below which the result is 0.
121
- * @param edge1 - The value above which the result is 1.
122
- * @param x - The value to map.
123
- * @returns A value in `[0, 1]`. Degenerate edges (`edge0 === edge1`) step from 0 to 1 at the edge.
124
- *
125
- * @example
126
- * ```ts
127
- * smoothStep(0, 1, 0.5); // 0.5, but with zero slope at 0 and 1
128
- * ```
129
- *
130
- * @public
131
- */
132
- function smoothStep(edge0, edge1, x) {
133
- if (edge0 === edge1) return x < edge0 ? 0 : 1;
134
- const t = clamp01((x - edge0) / (edge1 - edge0));
135
- return t * t * (3 - 2 * t);
136
- }
137
- /**
138
- * Moves a value towards a target without overshooting it.
139
- *
140
- * @param current - The value to move.
141
- * @param target - The value to move towards.
142
- * @param maxDelta - The largest step allowed this call; negative values move away from the target.
143
- * @returns The stepped value, exactly `target` once the remaining distance fits in `maxDelta`.
144
- *
145
- * @example
146
- * ```ts
147
- * // frame-rate independent approach at 2 units per second
148
- * health = moveTowards(health, 100, 2 * time.deltaTime);
149
- * ```
150
- *
151
- * @public
152
- */
153
- function moveTowards(current, target, maxDelta) {
154
- const delta = target - current;
155
- if (Math.abs(delta) <= maxDelta) return target;
156
- return current + (delta < 0 ? -maxDelta : maxDelta);
157
- }
158
- /**
159
- * Wraps a value into `[0, length)`, the way a looping animation time behaves. Unlike `%` the result
160
- * is never negative.
161
- *
162
- * @param t - The value to wrap.
163
- * @param length - The positive period to wrap into.
164
- * @returns The wrapped value, clamped into `[0, length]` so float error cannot escape the range.
165
- *
166
- * @example
167
- * ```ts
168
- * repeat(-1, 4); // 3
169
- * ```
170
- *
171
- * @public
172
- */
173
- function repeat(t, length) {
174
- return clamp(t - Math.floor(t / length) * length, 0, length);
175
- }
176
- /**
177
- * Bounces a value back and forth between 0 and `length`, the way a ping-pong animation behaves.
178
- *
179
- * @param t - The value to fold.
180
- * @param length - The positive half-period to fold into.
181
- * @returns A value in `[0, length]` that rises then falls as `t` increases.
182
- *
183
- * @example
184
- * ```ts
185
- * pingPong(5, 4); // 3
186
- * ```
187
- *
188
- * @public
189
- */
190
- function pingPong(t, length) {
191
- return length - Math.abs(repeat(t, length * 2) - length);
192
- }
193
- /**
194
- * Compares two numbers with an absolute tolerance. Use this instead of `===` on anything that has
195
- * been through a matrix, a quaternion or a `Float32Array`.
196
- *
197
- * @param a - The first value.
198
- * @param b - The second value.
199
- * @param epsilon - The largest difference still considered equal. Defaults to {@link EPSILON}.
200
- * @returns `true` when the values differ by no more than `epsilon`. `NaN` is never approximately
201
- * equal to anything, including itself.
202
- *
203
- * @public
204
- */
205
- function approximately(a, b, epsilon = EPSILON) {
206
- return Math.abs(a - b) <= epsilon;
207
- }
208
- /**
209
- * The sign of a value, with zero treated as positive (matching Unity's `Mathf.Sign`, and unlike
210
- * `Math.sign`, which returns 0 and `-0`).
211
- *
212
- * @param value - The value to inspect.
213
- * @returns `-1` for negative values, `1` for positive values and for both `0` and `-0`, and `NaN`
214
- * for `NaN`.
215
- *
216
- * @public
217
- */
218
- function sign(value) {
219
- if (Number.isNaN(value)) return NaN;
220
- return value < 0 ? -1 : 1;
221
- }
222
- /**
223
- * Wraps an angle in degrees into `[-180, 180)`, the range rotations are most readable in.
224
- *
225
- * @param degrees - The angle to wrap.
226
- * @returns The equivalent angle in `[-180, 180)`; exactly `180` wraps to `-180`.
227
- *
228
- * @example
229
- * ```ts
230
- * wrapAngleDegrees(370); // 10
231
- * wrapAngleDegrees(-190); // 170
232
- * ```
233
- *
234
- * @public
235
- */
236
- function wrapAngleDegrees(degrees) {
237
- return repeat(degrees + 180, 360) - 180;
238
- }
239
- /**
240
- * The shortest signed rotation from one angle to another, in degrees.
241
- *
242
- * @param fromDegrees - The starting angle.
243
- * @param toDegrees - The target angle.
244
- * @returns The signed difference in `[-180, 180)`.
245
- *
246
- * @example
247
- * ```ts
248
- * deltaAngleDegrees(350, 10); // 20, not -340
249
- * ```
250
- *
251
- * @public
252
- */
253
- function deltaAngleDegrees(fromDegrees, toDegrees) {
254
- return wrapAngleDegrees(toDegrees - fromDegrees);
255
- }
256
- /**
257
- * Interpolates between two angles in degrees the short way around the circle.
258
- *
259
- * @param fromDegrees - The angle returned at `t === 0`.
260
- * @param toDegrees - The angle approached at `t === 1`.
261
- * @param t - The interpolant; not clamped, matching {@link lerp}.
262
- * @returns The interpolated angle. It is not wrapped, so feeding the result back in is stable.
263
- *
264
- * @example
265
- * ```ts
266
- * lerpAngleDegrees(350, 10, 0.5); // 360
267
- * ```
268
- *
269
- * @public
270
- */
271
- function lerpAngleDegrees(fromDegrees, toDegrees, t) {
272
- return fromDegrees + deltaAngleDegrees(fromDegrees, toDegrees) * t;
273
- }
274
- //#endregion
275
- export { smoothStep as _, clamp as a, deltaAngleDegrees as c, lerpAngleDegrees as d, moveTowards as f, sign as g, repeat as h, approximately as i, inverseLerp as l, radToDeg as m, EPSILON as n, clamp01 as o, pingPong as p, RAD_TO_DEG as r, degToRad as s, DEG_TO_RAD as t, lerp as u, wrapAngleDegrees as v };
276
-
277
- //# sourceMappingURL=math-utils-BcDnL-gi.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"math-utils-BcDnL-gi.js","names":[],"sources":["../src/math/math-utils.ts"],"sourcesContent":["/**\n * Scalar helpers shared by the vector, quaternion and matrix types and by game code.\n *\n * Angles are **degrees** in every public API unless the name ends in `Rad`/`Radians`\n * (coding standards §5.1, ADR-0011). Every function here is a pure scalar function: none of them\n * allocate, so they are safe in per-frame code (coding standards §7).\n */\n\n/**\n * The default tolerance for approximate float comparisons. Chosen for single-precision positions in\n * metres: `Float32Array` round-tripping loses roughly 1e-7 of relative precision, so 1e-6 is the\n * smallest value that does not report false differences on data that has been through the GPU.\n *\n * @public\n */\nexport const EPSILON: number = 1e-6;\n\n/**\n * Multiplier that converts degrees to radians.\n *\n * @public\n */\nexport const DEG_TO_RAD: number = Math.PI / 180;\n\n/**\n * Multiplier that converts radians to degrees.\n *\n * @public\n */\nexport const RAD_TO_DEG: number = 180 / Math.PI;\n\n/**\n * Converts an angle from degrees to radians.\n *\n * @param degrees - The angle in degrees.\n * @returns The same angle in radians.\n *\n * @public\n */\nexport function degToRad(degrees: number): number {\n return degrees * DEG_TO_RAD;\n}\n\n/**\n * Converts an angle from radians to degrees.\n *\n * @param radians - The angle in radians.\n * @returns The same angle in degrees.\n *\n * @public\n */\nexport function radToDeg(radians: number): number {\n return radians * RAD_TO_DEG;\n}\n\n/**\n * Constrains a value to an inclusive range.\n *\n * @param value - The value to constrain.\n * @param min - The lower bound.\n * @param max - The upper bound.\n * @returns `min` when `value` is smaller, `max` when it is larger, otherwise `value` unchanged.\n * `NaN` propagates.\n *\n * @example\n * ```ts\n * clamp(12, 0, 10); // 10\n * ```\n *\n * @public\n */\nexport function clamp(value: number, min: number, max: number): number {\n if (value < min) {\n return min;\n }\n if (value > max) {\n return max;\n }\n return value;\n}\n\n/**\n * Constrains a value to the 0–1 range.\n *\n * @param value - The value to constrain.\n * @returns The value clamped into `[0, 1]`.\n *\n * @public\n */\nexport function clamp01(value: number): number {\n return clamp(value, 0, 1);\n}\n\n/**\n * Linearly interpolates between two values. The interpolant is **not** clamped, so values outside\n * `[0, 1]` extrapolate; wrap `t` in {@link clamp01} when that is not wanted.\n *\n * @param a - The value returned at `t === 0`.\n * @param b - The value returned at `t === 1`.\n * @param t - The interpolant.\n * @returns The interpolated value.\n *\n * @example\n * ```ts\n * lerp(0, 10, 0.25); // 2.5\n * ```\n *\n * @public\n */\nexport function lerp(a: number, b: number, t: number): number {\n return a + (b - a) * t;\n}\n\n/**\n * The inverse of {@link lerp}: finds the interpolant that maps `a`–`b` onto `value`.\n *\n * @param a - The value that maps to 0.\n * @param b - The value that maps to 1.\n * @param value - The value to locate.\n * @returns The interpolant, clamped into `[0, 1]`. Returns 0 when `a` and `b` are equal.\n *\n * @public\n */\nexport function inverseLerp(a: number, b: number, value: number): number {\n if (a === b) {\n return 0;\n }\n return clamp01((value - a) / (b - a));\n}\n\n/**\n * Smoothly interpolates between two edges with a Hermite curve (the GLSL `smoothstep`), easing in\n * and out instead of the straight ramp of {@link lerp}.\n *\n * @param edge0 - The value below which the result is 0.\n * @param edge1 - The value above which the result is 1.\n * @param x - The value to map.\n * @returns A value in `[0, 1]`. Degenerate edges (`edge0 === edge1`) step from 0 to 1 at the edge.\n *\n * @example\n * ```ts\n * smoothStep(0, 1, 0.5); // 0.5, but with zero slope at 0 and 1\n * ```\n *\n * @public\n */\nexport function smoothStep(edge0: number, edge1: number, x: number): number {\n if (edge0 === edge1) {\n return x < edge0 ? 0 : 1;\n }\n const t = clamp01((x - edge0) / (edge1 - edge0));\n return t * t * (3 - 2 * t);\n}\n\n/**\n * Moves a value towards a target without overshooting it.\n *\n * @param current - The value to move.\n * @param target - The value to move towards.\n * @param maxDelta - The largest step allowed this call; negative values move away from the target.\n * @returns The stepped value, exactly `target` once the remaining distance fits in `maxDelta`.\n *\n * @example\n * ```ts\n * // frame-rate independent approach at 2 units per second\n * health = moveTowards(health, 100, 2 * time.deltaTime);\n * ```\n *\n * @public\n */\nexport function moveTowards(current: number, target: number, maxDelta: number): number {\n const delta = target - current;\n if (Math.abs(delta) <= maxDelta) {\n return target;\n }\n return current + (delta < 0 ? -maxDelta : maxDelta);\n}\n\n/**\n * Wraps a value into `[0, length)`, the way a looping animation time behaves. Unlike `%` the result\n * is never negative.\n *\n * @param t - The value to wrap.\n * @param length - The positive period to wrap into.\n * @returns The wrapped value, clamped into `[0, length]` so float error cannot escape the range.\n *\n * @example\n * ```ts\n * repeat(-1, 4); // 3\n * ```\n *\n * @public\n */\nexport function repeat(t: number, length: number): number {\n return clamp(t - Math.floor(t / length) * length, 0, length);\n}\n\n/**\n * Bounces a value back and forth between 0 and `length`, the way a ping-pong animation behaves.\n *\n * @param t - The value to fold.\n * @param length - The positive half-period to fold into.\n * @returns A value in `[0, length]` that rises then falls as `t` increases.\n *\n * @example\n * ```ts\n * pingPong(5, 4); // 3\n * ```\n *\n * @public\n */\nexport function pingPong(t: number, length: number): number {\n return length - Math.abs(repeat(t, length * 2) - length);\n}\n\n/**\n * Compares two numbers with an absolute tolerance. Use this instead of `===` on anything that has\n * been through a matrix, a quaternion or a `Float32Array`.\n *\n * @param a - The first value.\n * @param b - The second value.\n * @param epsilon - The largest difference still considered equal. Defaults to {@link EPSILON}.\n * @returns `true` when the values differ by no more than `epsilon`. `NaN` is never approximately\n * equal to anything, including itself.\n *\n * @public\n */\nexport function approximately(a: number, b: number, epsilon: number = EPSILON): boolean {\n return Math.abs(a - b) <= epsilon;\n}\n\n/**\n * The sign of a value, with zero treated as positive (matching Unity's `Mathf.Sign`, and unlike\n * `Math.sign`, which returns 0 and `-0`).\n *\n * @param value - The value to inspect.\n * @returns `-1` for negative values, `1` for positive values and for both `0` and `-0`, and `NaN`\n * for `NaN`.\n *\n * @public\n */\nexport function sign(value: number): number {\n if (Number.isNaN(value)) {\n return Number.NaN;\n }\n return value < 0 ? -1 : 1;\n}\n\n/**\n * Wraps an angle in degrees into `[-180, 180)`, the range rotations are most readable in.\n *\n * @param degrees - The angle to wrap.\n * @returns The equivalent angle in `[-180, 180)`; exactly `180` wraps to `-180`.\n *\n * @example\n * ```ts\n * wrapAngleDegrees(370); // 10\n * wrapAngleDegrees(-190); // 170\n * ```\n *\n * @public\n */\nexport function wrapAngleDegrees(degrees: number): number {\n return repeat(degrees + 180, 360) - 180;\n}\n\n/**\n * The shortest signed rotation from one angle to another, in degrees.\n *\n * @param fromDegrees - The starting angle.\n * @param toDegrees - The target angle.\n * @returns The signed difference in `[-180, 180)`.\n *\n * @example\n * ```ts\n * deltaAngleDegrees(350, 10); // 20, not -340\n * ```\n *\n * @public\n */\nexport function deltaAngleDegrees(fromDegrees: number, toDegrees: number): number {\n return wrapAngleDegrees(toDegrees - fromDegrees);\n}\n\n/**\n * Interpolates between two angles in degrees the short way around the circle.\n *\n * @param fromDegrees - The angle returned at `t === 0`.\n * @param toDegrees - The angle approached at `t === 1`.\n * @param t - The interpolant; not clamped, matching {@link lerp}.\n * @returns The interpolated angle. It is not wrapped, so feeding the result back in is stable.\n *\n * @example\n * ```ts\n * lerpAngleDegrees(350, 10, 0.5); // 360\n * ```\n *\n * @public\n */\nexport function lerpAngleDegrees(fromDegrees: number, toDegrees: number, t: number): number {\n return fromDegrees + deltaAngleDegrees(fromDegrees, toDegrees) * t;\n}\n"],"mappings":";;;;;;;;;;;;;;;AAeA,MAAa,UAAkB;;;;;;AAO/B,MAAa,aAAqB,KAAK,KAAK;;;;;;AAO5C,MAAa,aAAqB,MAAM,KAAK;;;;;;;;;AAU7C,SAAgB,SAAS,SAAyB;CAChD,OAAO,UAAU;AACnB;;;;;;;;;AAUA,SAAgB,SAAS,SAAyB;CAChD,OAAO,UAAU;AACnB;;;;;;;;;;;;;;;;;AAkBA,SAAgB,MAAM,OAAe,KAAa,KAAqB;CACrE,IAAI,QAAQ,KACV,OAAO;CAET,IAAI,QAAQ,KACV,OAAO;CAET,OAAO;AACT;;;;;;;;;AAUA,SAAgB,QAAQ,OAAuB;CAC7C,OAAO,MAAM,OAAO,GAAG,CAAC;AAC1B;;;;;;;;;;;;;;;;;AAkBA,SAAgB,KAAK,GAAW,GAAW,GAAmB;CAC5D,OAAO,KAAK,IAAI,KAAK;AACvB;;;;;;;;;;;AAYA,SAAgB,YAAY,GAAW,GAAW,OAAuB;CACvE,IAAI,MAAM,GACR,OAAO;CAET,OAAO,SAAS,QAAQ,MAAM,IAAI,EAAE;AACtC;;;;;;;;;;;;;;;;;AAkBA,SAAgB,WAAW,OAAe,OAAe,GAAmB;CAC1E,IAAI,UAAU,OACZ,OAAO,IAAI,QAAQ,IAAI;CAEzB,MAAM,IAAI,SAAS,IAAI,UAAU,QAAQ,MAAM;CAC/C,OAAO,IAAI,KAAK,IAAI,IAAI;AAC1B;;;;;;;;;;;;;;;;;AAkBA,SAAgB,YAAY,SAAiB,QAAgB,UAA0B;CACrF,MAAM,QAAQ,SAAS;CACvB,IAAI,KAAK,IAAI,KAAK,KAAK,UACrB,OAAO;CAET,OAAO,WAAW,QAAQ,IAAI,CAAC,WAAW;AAC5C;;;;;;;;;;;;;;;;AAiBA,SAAgB,OAAO,GAAW,QAAwB;CACxD,OAAO,MAAM,IAAI,KAAK,MAAM,IAAI,MAAM,IAAI,QAAQ,GAAG,MAAM;AAC7D;;;;;;;;;;;;;;;AAgBA,SAAgB,SAAS,GAAW,QAAwB;CAC1D,OAAO,SAAS,KAAK,IAAI,OAAO,GAAG,SAAS,CAAC,IAAI,MAAM;AACzD;;;;;;;;;;;;;AAcA,SAAgB,cAAc,GAAW,GAAW,UAAkB,SAAkB;CACtF,OAAO,KAAK,IAAI,IAAI,CAAC,KAAK;AAC5B;;;;;;;;;;;AAYA,SAAgB,KAAK,OAAuB;CAC1C,IAAI,OAAO,MAAM,KAAK,GACpB,OAAO;CAET,OAAO,QAAQ,IAAI,KAAK;AAC1B;;;;;;;;;;;;;;;AAgBA,SAAgB,iBAAiB,SAAyB;CACxD,OAAO,OAAO,UAAU,KAAK,GAAG,IAAI;AACtC;;;;;;;;;;;;;;;AAgBA,SAAgB,kBAAkB,aAAqB,WAA2B;CAChF,OAAO,iBAAiB,YAAY,WAAW;AACjD;;;;;;;;;;;;;;;;AAiBA,SAAgB,iBAAiB,aAAqB,WAAmB,GAAmB;CAC1F,OAAO,cAAc,kBAAkB,aAAa,SAAS,IAAI;AACnE"}