@ignifx/core 0.5.0 → 0.6.1

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.
package/dist/index.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import { a as CORE_ERROR_MESSAGES, c as THIRD_PARTY_ERROR_PREFIX, i as isIgnifxError, l as isValidErrorCode, n as assertNever, o as CoreErrorCode, r as formatErrorMessage, s as ErrorRange, t as IgnifxError } from "./ignifx-error-R1ma_Lzv.js";
2
2
  import { _ as sign, a as approximately, c as degToRad, d as lerp, f as lerpAngleDegrees, g as repeat, h as radToDeg, i as RAD_TO_DEG, l as deltaAngleDegrees, m as pingPong, n as DEG_TO_RAD, o as clamp, p as moveTowards, r as EPSILON, s as clamp01, t as Color, u as inverseLerp, v as smoothStep, y as wrapAngleDegrees } from "./color-CpvebMIh.js";
3
3
  import { A as linkParent, B as tagNode, C as setMeshSubtreeVisible, D as updateMeshUvs$1, E as updateMeshPositions$1, F as readNodeTag, I as readWorldMatrix, L as reparentKeepingWorld, M as localRotation, N as localScale, O as createNode, P as parentNode, R as setNodeName, S as setMeshRenderOrder, T as updateMeshNormals$1, V as worldMatrixVersion, a as createCapsuleMesh, b as setMeshPickable, c as createMeshFromGeometry, d as createTorusMesh, g as removeMeshFromScene, h as recomputeMeshBounds, i as createBoxMesh, j as localPosition, k as disposeNode, l as createPlaneMesh, m as overrideSubtreeMaterials, n as cloneMeshUnderNode, o as createCylinderMesh, p as disposeMeshTemplate, r as collectSubtreeMeshes, s as createGroundMesh, t as addMeshToScene, u as createSphereMesh, v as setMeshId, w as updateMeshColors$1, x as setMeshReceiveShadows, y as setMeshMaterial, z as setNodeSelfVisible } from "./mesh-CnK_PX46.js";
4
- import { a as setSceneClearColor, c as setSceneFog, l as setSceneImageProcessingOptions, n as installSceneEnvironment, o as setSceneEnvironmentBlur, r as loadSceneEnvironment, s as setSceneEnvironmentRotation, t as TONE_MAPPING_NAMES } from "./environment-_EeJXzjk.js";
4
+ import { a as readSceneImageProcessing, c as setSceneEnvironmentRotation, l as setSceneFog, n as installSceneEnvironment, o as setSceneClearColor, r as loadSceneEnvironment, s as setSceneEnvironmentBlur, t as TONE_MAPPING_NAMES, u as setSceneImageProcessingOptions } from "./environment-CvVC1lPa.js";
5
5
  import { a as defineMaterialPlugin, c as MATERIAL_ALPHA_MODES, d as createStandardMaterialFromProps, f as markMaterialDirty, g as setPbrMetallicRoughness, h as setPbrBaseColor, i as MATERIAL_PLUGIN_SAMPLER_BUDGET, l as createDefaultMaterial, m as setMaterialAlpha, n as MATERIAL_PLUGIN_DEFAULT_PRIORITY, o as isLiteMaterialPluginPoint, r as MATERIAL_PLUGIN_RESERVED_NAMES, t as LITE_MATERIAL_PLUGIN_POINTS, u as createPbrMaterialFromProps } from "./material-plugin-CTaWZXEk.js";
6
6
  import { a as postEffectValuesCodec, i as declaredTextureBinding, n as POST_EFFECT_FUNCTION, o as asJsonArray, r as customEffect, s as asJsonObject, t as POST_EFFECT_BUILTIN_UNIFORMS } from "./post-effect-C42XJFmo.js";
7
7
  import { a as SHADER_BLEND_MODES, c as SHADER_KINDS, d as SURFACE_HOOK_NAMES, f as SURFACE_UNIFORM_BLOCK, i as SHADER_ATTRIBUTE_NAMES, l as SHADER_TEXTURE_FALLBACKS, o as SHADER_CULL_MODES, r as LITE_SYSTEM_UNIFORM_NAMES, s as SHADER_INSTANCING_MODES, t as IGNIFX_UNIFORM_NAMES, u as SHADER_UNIFORM_TYPES } from "./shader-declaration-5q__jycG.js";
8
- import { acquireTexture, addTask, addToScene, buildFrameGraphTask, captureScreenshot, cloneTransformNode, createBloomPostProcessTask, createCopyToTextureTask, createCsmDirectionalShadowGenerator, createDirectionalLight, createEngine, createEsmDirectionalShadowGenerator, createFontFromBuffer, createFreeCamera, createGpuPicker, createHemisphericLight, createImageProcessingTask, createMeshFromData, createNullEngine, createPcfDirectionalShadowGenerator, createPcfSpotlightShadowGenerator, createPointLight, createRenderTarget, createRenderTask, createSceneContext, createSmaaPostProcessTask, createSpotLight, createStorageBuffer, createTexture2DFromPixels, disableOrthographicCamera, disposeEngine, disposePicker, disposeScene, disposeStorageBuffer, enableBoneControl, enableDeviceLostSceneRecovery, enableMaterialPlugins, enableMaterialStencil, enableOrthographicCamera, enablePbrLightmap, enableStandardSkeleton, forceWebGpuDeviceLossForTesting, getEffectiveAspectRatio, getFrameGraph, getProjectionMatrix, getRenderTaskGpuTimings, getViewMatrix, getViewProjectionMatrix, isShaderMaterial, loadTexture2D, onBeforeRender, pickAsync, pickWithRay, rebuildSceneRenderables, registerScene, registerSceneWithShadowSupport, releaseTexture, removeFromScene, resizeEngine, resolveCameraViewport, setGpuTimingEnabled, setShadowTaskCasterMeshes, setSubtreeVisible, setSurfaceSize, startEngine, stopEngine, updateStorageBuffer, updateTexture2DFromPixels } from "@babylonjs/lite";
8
+ import { acquireTexture, addTask, addToScene, buildFrameGraphTask, captureScreenshot, cloneTransformNode, createBloomPostProcessTask, createCopyToTextureTask, createCsmDirectionalShadowGenerator, createDirectionalLight, createEngine, createEsmDirectionalShadowGenerator, createFontFromBuffer, createFreeCamera, createGpuPicker, createHemisphericLight, createImageProcessingTask, createMeshFromData, createNullEngine, createPcfDirectionalShadowGenerator, createPcfSpotlightShadowGenerator, createPointLight, createRenderTarget, createRenderTask, createSceneContext, createSmaaPostProcessTask, createSpotLight, createStorageBuffer, createTexture2DFromPixels, disableOrthographicCamera, disposeEngine, disposePicker, disposeScene, disposeStorageBuffer, enableBoneControl, enableDeviceLostSceneRecovery, enableMaterialPlugins, enableMaterialStencil, enableOrthographicCamera, enablePbrLightmap, enableSkeletonShadows, enableStandardSkeleton, forceWebGpuDeviceLossForTesting, getEffectiveAspectRatio, getFrameGraph, getProjectionMatrix, getRenderTaskGpuTimings, getViewMatrix, getViewProjectionMatrix, isShaderMaterial, loadTexture2D, onBeforeRender, pickAsync, pickWithRay, rebuildSceneRenderables, registerScene, registerSceneWithShadowSupport, releaseTexture, removeFromScene, resizeEngine, resolveCameraViewport, setGpuTimingEnabled, setShadowTaskCasterMeshes, setSubtreeVisible, setSurfaceSize, startEngine, stopEngine, updateStorageBuffer, updateTexture2DFromPixels } from "@babylonjs/lite";
9
9
  //#region src/signal/signal.ts
10
10
  /** One attached handler. `handler === null` marks the entry dead and awaiting compaction. */
11
11
  var Connection = class {
@@ -7829,7 +7829,7 @@ function environmentSchema() {
7829
7829
  }),
7830
7830
  toneMapping: enumOf(TONE_MAPPING_NAMES, "none", { tooltip: "The tone-mapping curve." })
7831
7831
  }, { tooltip: "Exposure, contrast, and tone mapping. Recompiles PBR pipelines when it changes." }),
7832
- clearColor: optional(color("#000000"), { tooltip: "The colour the scene is cleared to, in sRGB; null leaves the rendering.clearColor setting." })
7832
+ clearColor: optional(color("#000000"), { tooltip: "The sRGB colour the scene is cleared to, through imageProcessing; null leaves the rendering.clearColor setting." })
7833
7833
  });
7834
7834
  }
7835
7835
  //#endregion
@@ -7872,7 +7872,7 @@ let surfaceLayer = null;
7872
7872
  * @internal
7873
7873
  */
7874
7874
  async function loadShaderSupport() {
7875
- const loaded = shaderLayer ?? await import("./shader-material-CqkEsKQ2.js").then((n) => n.n);
7875
+ const loaded = shaderLayer ?? await import("./shader-material-fOI9Upt3.js").then((n) => n.n);
7876
7876
  shaderLayer = loaded;
7877
7877
  await loaded.loadShaderMaterialAdapter();
7878
7878
  return loaded;
@@ -7895,7 +7895,7 @@ function shaderSupport() {
7895
7895
  * @internal
7896
7896
  */
7897
7897
  async function loadSurfaceShaderSupport() {
7898
- const loaded = surfaceLayer ?? await import("./surface-shader-B3gtg_8Q.js");
7898
+ const loaded = surfaceLayer ?? await import("./surface-shader-fJ2MOez0.js");
7899
7899
  surfaceLayer = loaded;
7900
7900
  return loaded;
7901
7901
  }
@@ -10271,6 +10271,175 @@ function readTaskTimings(engine) {
10271
10271
  };
10272
10272
  }
10273
10273
  //#endregion
10274
+ //#region src/render/image-processing-color.ts
10275
+ /** Lite's gamma: a plain 2.2 power, not the piecewise sRGB curve. */
10276
+ const GAMMA = 2.2;
10277
+ /**
10278
+ * Whether image processing leaves an encoded colour unchanged.
10279
+ *
10280
+ * @param state - The image processing.
10281
+ * @returns `true` for exposure 1, contrast 1 and no curve, where decode and encode cancel out.
10282
+ *
10283
+ * @internal
10284
+ */
10285
+ function isIdentityImageProcessing(state) {
10286
+ return state.exposure === 1 && state.contrast === 1 && state.curve === "none";
10287
+ }
10288
+ /**
10289
+ * Whether two image-processing states are the same.
10290
+ *
10291
+ * @param left - One state.
10292
+ * @param right - The other.
10293
+ * @returns `true` when every field matches.
10294
+ *
10295
+ * @internal
10296
+ */
10297
+ function sameImageProcessing(left, right) {
10298
+ return left.exposure === right.exposure && left.contrast === right.contrast && left.curve === right.curve;
10299
+ }
10300
+ /**
10301
+ * Applies Lite's PBR image processing to an sRGB colour, as a fully fogged fragment of that colour
10302
+ * would receive it.
10303
+ *
10304
+ * @param r - The sRGB red component, 0 to 1.
10305
+ * @param g - The sRGB green component, 0 to 1.
10306
+ * @param b - The sRGB blue component, 0 to 1.
10307
+ * @param a - The alpha, passed through.
10308
+ * @param state - The image processing to apply.
10309
+ * @param out - Receives the processed sRGB colour.
10310
+ * @returns `out`.
10311
+ *
10312
+ * @internal
10313
+ */
10314
+ function imageProcessedColor(r, g, b, a, state, out) {
10315
+ out.a = a;
10316
+ if (isIdentityImageProcessing(state)) {
10317
+ out.r = r;
10318
+ out.g = g;
10319
+ out.b = b;
10320
+ return out;
10321
+ }
10322
+ out.r = decode(r) * state.exposure;
10323
+ out.g = decode(g) * state.exposure;
10324
+ out.b = decode(b) * state.exposure;
10325
+ applyCurve(state.curve, out);
10326
+ out.r = finish(out.r, state.contrast);
10327
+ out.g = finish(out.g, state.contrast);
10328
+ out.b = finish(out.b, state.contrast);
10329
+ return out;
10330
+ }
10331
+ /**
10332
+ * Lite's fog-colour decode.
10333
+ *
10334
+ * @param value - The encoded channel.
10335
+ * @returns The linear channel.
10336
+ */
10337
+ function decode(value) {
10338
+ return Math.max(value, 0) ** GAMMA;
10339
+ }
10340
+ /**
10341
+ * Encodes, clamps and applies contrast, the steps every PBR fragment ends with.
10342
+ *
10343
+ * @param value - The tone-mapped linear channel.
10344
+ * @param contrast - The contrast multiplier.
10345
+ * @returns The presented channel, 0 to 1.
10346
+ */
10347
+ function finish(value, contrast) {
10348
+ const encoded = Math.min(Math.max(Math.max(value, 0) ** (1 / GAMMA), 0), 1);
10349
+ const high = encoded * encoded * (3 - 2 * encoded);
10350
+ const contrasted = contrast < 1 ? .5 + (encoded - .5) * contrast : encoded + (high - encoded) * (contrast - 1);
10351
+ return Math.max(contrasted, 0);
10352
+ }
10353
+ /**
10354
+ * Applies a tone-mapping curve in place, to an already exposed linear colour.
10355
+ *
10356
+ * @param curve - The curve.
10357
+ * @param color - The colour; overwritten.
10358
+ */
10359
+ function applyCurve(curve, color) {
10360
+ switch (curve) {
10361
+ case "none": return;
10362
+ case "standard":
10363
+ color.r = standard(color.r);
10364
+ color.g = standard(color.g);
10365
+ color.b = standard(color.b);
10366
+ return;
10367
+ case "aces":
10368
+ aces(color);
10369
+ return;
10370
+ case "neutral":
10371
+ neutral(color);
10372
+ return;
10373
+ default: assertNever(curve, "tone mapping");
10374
+ }
10375
+ }
10376
+ /**
10377
+ * Lite's `StandardToneMapping` (`lib/material/pbr/tone-mapping.js`).
10378
+ *
10379
+ * @param value - The exposed channel.
10380
+ * @returns The tone-mapped channel.
10381
+ */
10382
+ function standard(value) {
10383
+ return 1 - 2 ** (-1.590579 * value);
10384
+ }
10385
+ /**
10386
+ * Lite's `AcesToneMapping`, the fitted RRT + ODT (`lib/material/pbr/pbr-aces-wgsl.js`).
10387
+ *
10388
+ * @param color - The exposed colour; overwritten.
10389
+ */
10390
+ function aces(color) {
10391
+ const r = .59719 * color.r + .35458 * color.g + .04823 * color.b;
10392
+ const g = .076 * color.r + .90834 * color.g + .01566 * color.b;
10393
+ const b = .0284 * color.r + .13383 * color.g + .83777 * color.b;
10394
+ const fr = rrtAndOdtFit(r);
10395
+ const fg = rrtAndOdtFit(g);
10396
+ const fb = rrtAndOdtFit(b);
10397
+ color.r = saturate(1.60475 * fr - .53108 * fg - .07367 * fb);
10398
+ color.g = saturate(-.10208 * fr + 1.10813 * fg - .00605 * fb);
10399
+ color.b = saturate(-.00327 * fr - .07276 * fg + 1.07602 * fb);
10400
+ }
10401
+ /**
10402
+ * The rational fit inside ACES.
10403
+ *
10404
+ * @param value - One channel in the ACES working space.
10405
+ * @returns The fitted channel.
10406
+ */
10407
+ function rrtAndOdtFit(value) {
10408
+ return (value * (value + .0245786) - 90537e-9) / (value * (.983729 * value + .432951) + .238081);
10409
+ }
10410
+ /**
10411
+ * Lite's `NeutralToneMapping`, Khronos PBR Neutral (`lib/material/pbr/pbr-neutral-wgsl.js`).
10412
+ *
10413
+ * @param color - The exposed colour; overwritten.
10414
+ */
10415
+ function neutral(color) {
10416
+ const startCompression = .76;
10417
+ const desaturation = .15;
10418
+ const lowest = Math.min(color.r, color.g, color.b);
10419
+ const offset = lowest < .08 ? lowest - 6.25 * lowest * lowest : .04;
10420
+ color.r -= offset;
10421
+ color.g -= offset;
10422
+ color.b -= offset;
10423
+ const peak = Math.max(color.r, color.g, color.b);
10424
+ if (peak < startCompression) return;
10425
+ const d = .24;
10426
+ const newPeak = 1 - d * d / (peak + d - startCompression);
10427
+ const scale = newPeak / peak;
10428
+ const blend = 1 - 1 / (desaturation * (peak - newPeak) + 1);
10429
+ color.r = color.r * scale + (newPeak - color.r * scale) * blend;
10430
+ color.g = color.g * scale + (newPeak - color.g * scale) * blend;
10431
+ color.b = color.b * scale + (newPeak - color.b * scale) * blend;
10432
+ }
10433
+ /**
10434
+ * Clamps to `[0, 1]`.
10435
+ *
10436
+ * @param value - The value.
10437
+ * @returns The clamped value.
10438
+ */
10439
+ function saturate(value) {
10440
+ return Math.min(Math.max(value, 0), 1);
10441
+ }
10442
+ //#endregion
10274
10443
  //#region src/render/rendering-settings.ts
10275
10444
  /**
10276
10445
  * The `rendering` project settings section (`docs/architecture/04-extensions.md` §5,
@@ -10541,6 +10710,8 @@ var RendererImpl = class {
10541
10710
  needsRenderableRebuild = false;
10542
10711
  /** `true` once a `castShadows` or caster-set change needs the caster lists rebuilt this frame. */
10543
10712
  needsCasterRebuild = false;
10713
+ /** `true` once a light gained or lost a shadow generator, so the next rebuild needs fresh layouts. */
10714
+ needsShadowLayoutReset = false;
10544
10715
  /** `true` once a camera was added, removed, enabled, disabled, or re-prioritised. */
10545
10716
  needsCameraSelect = true;
10546
10717
  /** `true` once the world has been told it has no enabled camera, so `IGX-0706` is logged once. */
@@ -10571,6 +10742,32 @@ var RendererImpl = class {
10571
10742
  #profileTasks = false;
10572
10743
  #isSceneRegistered = false;
10573
10744
  #isLoopRunning = false;
10745
+ /** The clear colour as authored, in sRGB, before image processing. */
10746
+ #authoredClear = {
10747
+ r: 0,
10748
+ g: 0,
10749
+ b: 0,
10750
+ a: 1
10751
+ };
10752
+ /** The image processing the clear colour was last written with. */
10753
+ #clearProcessing = {
10754
+ exposure: 1,
10755
+ contrast: 1,
10756
+ curve: "none"
10757
+ };
10758
+ /** Scratch for {@link RendererImpl.refreshClearColor}'s per-frame read. */
10759
+ #processingScratch = {
10760
+ exposure: 1,
10761
+ contrast: 1,
10762
+ curve: "none"
10763
+ };
10764
+ /** Scratch for the processed clear colour. */
10765
+ #processedClear = {
10766
+ r: 0,
10767
+ g: 0,
10768
+ b: 0,
10769
+ a: 1
10770
+ };
10574
10771
  /**
10575
10772
  * Creates the rendering service.
10576
10773
  *
@@ -10968,18 +11165,52 @@ var RendererImpl = class {
10968
11165
  * ignifx default is opaque black (`src/render/rendering-settings.ts`), so Lite's grey is only
10969
11166
  * ever seen by code that bypasses `createApp`.
10970
11167
  *
10971
- * Colours are stored in sRGB in every settings section and every component field, and the clear
10972
- * reaches the swapchain without tone mapping or image processing, so an sRGB `0.5` presents as
10973
- * byte `128`. The value is written unchanged on the default surface and decoded only for an
10974
- * `*-srgb` surface view (`rendering.srgb: true`), which encodes it again on write
10975
- * (`setSceneClearColor`).
11168
+ * Colours are stored in sRGB in every settings section and every component field. The clear goes
11169
+ * through the same exposure, curve and contrast as every PBR fragment, applied here on the CPU,
11170
+ * so a fully fogged floor and a clear colour of the fog's colour present as the same bytes. With
11171
+ * no image processing (exposure 1, contrast 1, curve `"none"`) an sRGB `0.5` presents as byte
11172
+ * `128`; under ACES it presents as byte `104`. The value is decoded only for an `*-srgb` surface
11173
+ * view (`rendering.srgb: true`), which encodes it again on write (`setSceneClearColor`).
10976
11174
  *
10977
11175
  * @param value - The sRGB colour, `0` to `1` per channel.
10978
11176
  *
10979
11177
  * @internal
10980
11178
  */
10981
11179
  applyClearColor(value) {
10982
- setSceneClearColor(this.scene, value.r, value.g, value.b, value.a);
11180
+ const authored = this.#authoredClear;
11181
+ authored.r = value.r;
11182
+ authored.g = value.g;
11183
+ authored.b = value.b;
11184
+ authored.a = value.a;
11185
+ this.#writeClearColor(readSceneImageProcessing(this.scene, this.#processingScratch));
11186
+ }
11187
+ /**
11188
+ * Re-applies the clear colour when the scene's image processing changed since it was written.
11189
+ *
11190
+ * @remarks
11191
+ * The `PreRender` system calls it every frame, after the `Environment` has written its image
11192
+ * processing. Lite's `loadEnvironment` changes image processing too, which is why the scene is
11193
+ * read rather than the component.
11194
+ *
11195
+ * @internal
11196
+ */
11197
+ refreshClearColor() {
11198
+ const current = readSceneImageProcessing(this.scene, this.#processingScratch);
11199
+ if (!sameImageProcessing(current, this.#clearProcessing)) this.#writeClearColor(current);
11200
+ }
11201
+ /**
11202
+ * Writes the authored clear colour, processed with `processing`, onto the scene.
11203
+ *
11204
+ * @param processing - The scene's current image processing.
11205
+ */
11206
+ #writeClearColor(processing) {
11207
+ const applied = this.#clearProcessing;
11208
+ applied.exposure = processing.exposure;
11209
+ applied.contrast = processing.contrast;
11210
+ applied.curve = processing.curve;
11211
+ const authored = this.#authoredClear;
11212
+ const out = imageProcessedColor(authored.r, authored.g, authored.b, authored.a, processing, this.#processedClear);
11213
+ setSceneClearColor(this.scene, out.r, out.g, out.b, out.a);
10983
11214
  }
10984
11215
  /**
10985
11216
  * Reconciles the world with the Lite scene once, before the scene is registered.
@@ -12024,6 +12255,45 @@ function rebuildRenderables(scene) {
12024
12255
  return rebuildSceneRenderables(scene);
12025
12256
  }
12026
12257
  /**
12258
+ * How many times the scene's materials have been rebuilt.
12259
+ *
12260
+ * @remarks
12261
+ * An ESM generator keeps the caster views it built for a caster array until it is handed a
12262
+ * different array, whatever happens to the materials (`ensureEsmShadowTaskState`,
12263
+ * `lib/shadow/esm-directional-shadow-generator.js` 121-126; PCF and CSM compare each material's
12264
+ * `_csmGen` instead). So a surface shader switched on or off went on drawing the old shape into an
12265
+ * ESM map. The render sync system re-supplies the caster lists whenever this moves.
12266
+ *
12267
+ * @param scene - The scene.
12268
+ * @returns The counter; `0` before anything was built.
12269
+ *
12270
+ * @internal
12271
+ */
12272
+ function readMaterialEpoch(scene) {
12273
+ return scene._materialEpoch ?? 0;
12274
+ }
12275
+ /**
12276
+ * Makes the next renderable rebuild create fresh bind-group layouts for every material.
12277
+ *
12278
+ * @remarks
12279
+ * Call it before {@link rebuildRenderables} whenever a light gained or lost a shadow generator.
12280
+ * Lite caches a material's bindings, shadow bind-group layout included, under a key that does not
12281
+ * name the shadow maps (`getOrCreatePbrBindings`, `lib/material/pbr/pbr-pipeline.js` 36). So after
12282
+ * the number of shadow-casting lights changes, a rebuild reuses a layout sized for the old number
12283
+ * and builds a bind group for the new one, and WebGPU rejects every frame ("Number of entries (3)
12284
+ * did not match the expected number of entries (6)"). Clearing the cache costs one pipeline
12285
+ * rebuild per material, only on a frame whose shadow casters changed.
12286
+ *
12287
+ * @param engine - The engine whose caches to clear.
12288
+ *
12289
+ * @internal
12290
+ */
12291
+ function forgetMaterialBindingLayouts(engine) {
12292
+ const resets = engine;
12293
+ resets._pbrCleanup?.();
12294
+ resets._standardCleanup?.();
12295
+ }
12296
+ /**
12027
12297
  * Releases a shadow generator's GPU resources and unlinks it from its light.
12028
12298
  *
12029
12299
  * @remarks
@@ -12109,7 +12379,115 @@ function toSpotConfig(settings) {
12109
12379
  return config;
12110
12380
  }
12111
12381
  //#endregion
12382
+ //#region src/lite/shadow-caster-shapes.ts
12383
+ /**
12384
+ * Tracks the bone poses and shader uniforms of one shadow generator's casters.
12385
+ *
12386
+ * @remarks
12387
+ * Call {@link ShadowCasterShapes.watch} whenever the generator starts drawing a new caster list,
12388
+ * then {@link ShadowCasterShapes.hasChanged} once per frame. PCF and CSM build a new shadow task
12389
+ * state whenever a caster's material changes, and the render sync system hands ESM a new caster list
12390
+ * when one does, so watching per state follows material swaps too. Only `watch` allocates.
12391
+ *
12392
+ * Morph targets would need the same treatment, but ignifx has no morph-target support yet.
12393
+ *
12394
+ * @internal
12395
+ */
12396
+ var ShadowCasterShapes = class {
12397
+ /** The skinned casters. */
12398
+ #skinned = [];
12399
+ /** Each skinned caster's bone matrices when the map was last drawn, by the same index. */
12400
+ #poses = [];
12401
+ /** The shader materials the casters draw their shadows with. */
12402
+ #shaders = [];
12403
+ /** Each shader material's uniform and resource versions, summed, by the same index. */
12404
+ #shaderVersions = [];
12405
+ /**
12406
+ * Starts tracking a new caster list.
12407
+ *
12408
+ * @param casters - The meshes the generator draws.
12409
+ */
12410
+ watch(casters) {
12411
+ this.#skinned.length = 0;
12412
+ this.#poses.length = 0;
12413
+ this.#shaders.length = 0;
12414
+ this.#shaderVersions.length = 0;
12415
+ for (const mesh of casters) {
12416
+ const bones = mesh.skeleton?.boneMatrices;
12417
+ if (bones !== void 0) {
12418
+ this.#skinned.push(mesh);
12419
+ this.#poses.push(bones.slice());
12420
+ }
12421
+ const shader = shadowShaderMaterial(mesh);
12422
+ if (shader !== null && !this.#shaders.includes(shader)) {
12423
+ this.#shaders.push(shader);
12424
+ this.#shaderVersions.push(shaderVersion(shader));
12425
+ }
12426
+ }
12427
+ }
12428
+ /**
12429
+ * Whether any tracked caster changed shape since the last call (or since {@link watch}).
12430
+ *
12431
+ * @returns `true` when a bone pose or a shader material's uniforms changed.
12432
+ */
12433
+ hasChanged() {
12434
+ let changed = false;
12435
+ for (let index = 0; index < this.#skinned.length; index += 1) {
12436
+ const bones = this.#skinned[index]?.skeleton?.boneMatrices;
12437
+ const pose = this.#poses[index];
12438
+ if (bones === void 0 || pose === void 0 || hasSameValues(pose, bones)) continue;
12439
+ if (pose.length === bones.length) pose.set(bones);
12440
+ else this.#poses[index] = bones.slice();
12441
+ changed = true;
12442
+ }
12443
+ for (let index = 0; index < this.#shaders.length; index += 1) {
12444
+ const shader = this.#shaders[index];
12445
+ if (shader === void 0) continue;
12446
+ const version = shaderVersion(shader);
12447
+ if (version !== this.#shaderVersions[index]) {
12448
+ this.#shaderVersions[index] = version;
12449
+ changed = true;
12450
+ }
12451
+ }
12452
+ return changed;
12453
+ }
12454
+ };
12455
+ /**
12456
+ * The shader material a caster's shadow is drawn with, if it is one.
12457
+ *
12458
+ * @param mesh - A caster.
12459
+ * @returns The material, or `null` when the caster's shadow is not drawn by a shader material.
12460
+ */
12461
+ function shadowShaderMaterial(mesh) {
12462
+ let material = mesh.material;
12463
+ while (material?._shadowCasterMaterial !== void 0) material = material._shadowCasterMaterial;
12464
+ return material?._buildGroup?._materialFamily === "shader" ? material : null;
12465
+ }
12466
+ /**
12467
+ * A number that grows whenever a shader material's uniforms or bound resources change.
12468
+ *
12469
+ * @param material - A shader material.
12470
+ * @returns The sum of its two version counters; both only ever grow.
12471
+ */
12472
+ function shaderVersion(material) {
12473
+ return (material._uniformVersion ?? 0) + (material._resourceVersion ?? 0);
12474
+ }
12475
+ /**
12476
+ * Whether two arrays hold the same values.
12477
+ *
12478
+ * @param a - One array.
12479
+ * @param b - The other.
12480
+ * @returns `true` when both have the same length and elements.
12481
+ */
12482
+ function hasSameValues(a, b) {
12483
+ if (a.length !== b.length) return false;
12484
+ for (let index = 0; index < a.length; index += 1) if (a[index] !== b[index]) return false;
12485
+ return true;
12486
+ }
12487
+ //#endregion
12112
12488
  //#region src/lite/gpu/shadow-generator.ts
12489
+ /** What Lite's caster gate reads as "never drawn" (`lib/shadow/pcf-shadow-task-hooks.js` 90). */
12490
+ const NEVER_DRAWN = -1;
12113
12491
  /**
12114
12492
  * Builds the shadow generator a light's declared settings ask for.
12115
12493
  *
@@ -12130,7 +12508,9 @@ function toSpotConfig(settings) {
12130
12508
  */
12131
12509
  function createShadowGeneratorForLight(engine, light, settings) {
12132
12510
  const generator = light.lightType === "spot" ? createPcfSpotlightShadowGenerator(engine, light, toSpotConfig(settings)) : createDirectionalGenerator(engine, light, settings);
12511
+ enableSkeletonShadows(generator);
12133
12512
  recordReplacedShadowTaskStates(generator);
12513
+ redrawChangedCasterShapes(generator);
12134
12514
  return generator;
12135
12515
  }
12136
12516
  /**
@@ -12138,7 +12518,7 @@ function createShadowGeneratorForLight(engine, light, settings) {
12138
12518
  * can draw (see the module header for the frame this prevents).
12139
12519
  *
12140
12520
  * @remarks
12141
- * Lite's own `enableDeformableShadowCasters` wraps the same hook the same way
12521
+ * Lite's own `enableDeformableShadowBounds` wraps the same hook the same way
12142
12522
  * (`lib/shadow/deformable-shadow-casters.js` 98-104). A state built for a *new* caster array is
12143
12523
  * left alone, because the shadow task records that one itself.
12144
12524
  *
@@ -12150,12 +12530,68 @@ function recordReplacedShadowTaskStates(generator) {
12150
12530
  if (ensure === void 0) return;
12151
12531
  hooks._ensureShadowTaskState = (engine, scene, casterMeshes) => {
12152
12532
  const previous = hooks._shadowTaskState;
12533
+ const previousCasters = previous?._casterMeshes;
12153
12534
  const state = ensure(engine, scene, casterMeshes);
12154
- if (previous !== void 0 && state !== previous && state._casterMeshes === previous._casterMeshes) state._task.record();
12535
+ if (state !== previous && casterMeshes.length === 0) stopMirroringEmptyStates(scene, state);
12536
+ if (previous !== void 0 && state !== previous && state._casterMeshes === previousCasters) state._task.record();
12155
12537
  return state;
12156
12538
  };
12157
12539
  }
12158
12540
  /**
12541
+ * Makes a generator redraw its map in any frame where a caster changed shape without moving: a new
12542
+ * bone pose, or a shader material whose uniforms changed (see the module header).
12543
+ *
12544
+ * @remarks
12545
+ * It forgets the caster version the map was drawn at, which is what Lite's own gate compares
12546
+ * against, so the pass draws as if it never had. A pose change is found by comparing bone matrices,
12547
+ * not by asking whether an animator ran: a paused animator, or a skinned model with no animator at
12548
+ * all, costs no redraw, while bone control and every other writer is still caught.
12549
+ *
12550
+ * @param generator - A generator from one of Lite's four factories.
12551
+ */
12552
+ function redrawChangedCasterShapes(generator) {
12553
+ const hooks = generator;
12554
+ const render = hooks._renderShadowMap;
12555
+ if (render === void 0) return;
12556
+ const shapes = new ShadowCasterShapes();
12557
+ let watched = null;
12558
+ hooks._renderShadowMap = (engine, state) => {
12559
+ if (state !== watched) {
12560
+ watched = state;
12561
+ shapes.watch(state._casterMeshes);
12562
+ } else if (shapes.hasChanged()) state._lastCasterVersion = NEVER_DRAWN;
12563
+ return render(engine, state);
12564
+ };
12565
+ }
12566
+ /**
12567
+ * Keeps a shadow task state built for no casters from drawing the scene's renderables.
12568
+ *
12569
+ * @remarks
12570
+ * Each render task records against an empty renderable list, and its mirroring flag is cleared
12571
+ * afterwards so a later change to the scene's renderables does not copy them in either. The pass
12572
+ * then only clears the map. See the module header.
12573
+ *
12574
+ * @param scene - The scene the state belongs to.
12575
+ * @param state - A state whose caster list is empty.
12576
+ */
12577
+ function stopMirroringEmptyStates(scene, state) {
12578
+ const renderables = scene;
12579
+ const tasks = state._tasks ?? [state._task];
12580
+ for (const task of tasks) {
12581
+ const record = task.record.bind(task);
12582
+ task.record = () => {
12583
+ const all = renderables._renderables;
12584
+ renderables._renderables = [];
12585
+ try {
12586
+ record();
12587
+ } finally {
12588
+ renderables._renderables = all;
12589
+ }
12590
+ task._af = false;
12591
+ };
12592
+ }
12593
+ }
12594
+ /**
12159
12595
  * Builds the directional generator for a technique.
12160
12596
  *
12161
12597
  * @param engine - The engine that owns the shadow map.
@@ -12505,6 +12941,7 @@ var Light = class Light extends Component {
12505
12941
  return true;
12506
12942
  }
12507
12943
  this.#generator = attachShadows(renderer.engine, light, this.shadows);
12944
+ renderer.needsShadowLayoutReset = true;
12508
12945
  return true;
12509
12946
  }
12510
12947
  /**
@@ -12545,7 +12982,10 @@ var Light = class Light extends Component {
12545
12982
  const generator = this.#generator;
12546
12983
  const light = this.#light;
12547
12984
  this.#generator = null;
12548
- if (generator !== null) detachShadows(renderer.scene, light, generator);
12985
+ if (generator !== null) {
12986
+ detachShadows(renderer.scene, light, generator);
12987
+ renderer.needsShadowLayoutReset = true;
12988
+ }
12549
12989
  }
12550
12990
  /**
12551
12991
  * Removes the light and its generator from the scene.
@@ -13949,7 +14389,7 @@ let effectLayer = null;
13949
14389
  * @internal
13950
14390
  */
13951
14391
  async function loadPostEffectSupport() {
13952
- const [compiler, effects] = await Promise.all([compilerLayer ?? import("./post-effect-compiler-DTlkT1si.js"), effectLayer ?? import("./effect-task-CMbuO6Kb.js")]);
14392
+ const [compiler, effects] = await Promise.all([compilerLayer ?? import("./post-effect-compiler-Cq6cshjL.js"), effectLayer ?? import("./effect-task-CMbuO6Kb.js")]);
13953
14393
  compilerLayer = compiler;
13954
14394
  effectLayer = effects;
13955
14395
  }
@@ -16563,6 +17003,8 @@ var RenderSyncSystem = class {
16563
17003
  /** A rebuild asked for while one was already running; it is run once the in-flight one lands. */
16564
17004
  #isRebuildPending = false;
16565
17005
  #areCastersDirty = false;
17006
+ /** The scene's material epoch when the ESM caster lists were last handed over. */
17007
+ #materialEpoch = 0;
16566
17008
  /**
16567
17009
  * Creates the system.
16568
17010
  *
@@ -16630,8 +17072,10 @@ var RenderSyncSystem = class {
16630
17072
  topologyChanged = changed || topologyChanged;
16631
17073
  castersChanged = changed || castersChanged;
16632
17074
  }
17075
+ castersChanged = this.#hasEsmMaterialChanged(lights) || castersChanged;
16633
17076
  this.#selectCamera(world, cameras);
16634
17077
  topologyChanged = this.#applyEnvironment(world) || topologyChanged;
17078
+ renderer.refreshClearColor();
16635
17079
  this.#applyStacks(world);
16636
17080
  this.#areCastersDirty = this.#areCastersDirty || castersChanged;
16637
17081
  if (this.#isRebuildDue && !topologyChanged) {
@@ -16771,6 +17215,25 @@ var RenderSyncSystem = class {
16771
17215
  }
16772
17216
  }
16773
17217
  /**
17218
+ * Whether a material was rebuilt since the caster lists were last handed over, while an ESM light
17219
+ * is casting.
17220
+ *
17221
+ * @remarks
17222
+ * An ESM generator only rebuilds its caster views for a new caster array (`readMaterialEpoch`), so
17223
+ * a fresh array is what makes it draw a rebuilt material's new shape. PCF and CSM notice material
17224
+ * changes themselves, which is why nothing is re-supplied without an ESM light.
17225
+ *
17226
+ * @param lights - The world's lights.
17227
+ * @returns `true` when the caster lists should be rebuilt.
17228
+ */
17229
+ #hasEsmMaterialChanged(lights) {
17230
+ const epoch = readMaterialEpoch(this.#renderer.scene);
17231
+ if (epoch === this.#materialEpoch) return false;
17232
+ this.#materialEpoch = epoch;
17233
+ for (let index = 0; index < lights.length; index += 1) if (isEsmCaster(lights[index])) return true;
17234
+ return false;
17235
+ }
17236
+ /**
16774
17237
  * Collects one renderer's casters into the full list and, unless it draws with a shader
16775
17238
  * material, into the ESM list too. A shader-material caster under an ESM light is reported once
16776
17239
  * with `IGX-0724` (Babylon Lite 1.27.0 has no ESM view for the shader family).
@@ -16864,6 +17327,10 @@ var RenderSyncSystem = class {
16864
17327
  do {
16865
17328
  if (renderer.isHeadless || !renderer.isSceneRegistered) return;
16866
17329
  try {
17330
+ if (renderer.needsShadowLayoutReset) {
17331
+ renderer.needsShadowLayoutReset = false;
17332
+ forgetMaterialBindingLayouts(renderer.engine);
17333
+ }
16867
17334
  await rebuildRenderables(renderer.scene);
16868
17335
  } catch (error) {
16869
17336
  renderer.app.onError.emit({
@@ -28923,12 +29390,32 @@ function applyComponentInit(component, schema, init, typeName) {
28923
29390
  issues: issues.length
28924
29391
  } });
28925
29392
  const target = component;
28926
- for (const name of Object.keys(schema)) {
29393
+ for (const [name, field] of Object.entries(schema)) {
28927
29394
  const value = values[name];
28928
- if (value !== void 0) target[name] = value;
29395
+ if (value !== void 0) target[name] = withRecordDefaults(field, value);
28929
29396
  }
28930
29397
  }
28931
29398
  /**
29399
+ * Fills the fields a partial `record` value leaves out with their schema defaults, at any depth, as
29400
+ * reading a scene file does.
29401
+ *
29402
+ * @param field - The field the value is for.
29403
+ * @param value - The caller's validated value.
29404
+ * @returns A fresh record for a `record` field (or an `optional` one holding a record), otherwise
29405
+ * `value` itself.
29406
+ */
29407
+ function withRecordDefaults(field, value) {
29408
+ const spec = field.spec;
29409
+ if (spec.kind === "optional") return value === null ? value : withRecordDefaults(spec.inner, value);
29410
+ if (spec.kind !== "record" || !isPlainObject(value)) return value;
29411
+ const merged = {};
29412
+ for (const [name, inner] of Object.entries(spec.fields)) {
29413
+ const given = value[name];
29414
+ merged[name] = given === void 0 ? inner.createDefault() : withRecordDefaults(inner, given);
29415
+ }
29416
+ return merged;
29417
+ }
29418
+ /**
28932
29419
  * Builds the "used after destroy" error.
28933
29420
  *
28934
29421
  * @param uid - The destroyed entity's identifier.