Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(2d): a light reaches a range of sprite layers, so a lamp in a hut does not light the roof over it

yhh17082654c240
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 2039eebb3..443343686 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -32,7 +32,7 @@ env:
test_hierarchy_links test_entity_activity test_transform_traversal test_ui_tree test_tween_activity test_springbone
test_shader_device test_draw_params test_per_draw_blocks test_texture_fb test_buffer_device
test_compressed_format test_batch_builder test_skeletal_batch_color test_boundary_span test_webgpu_device
- test_clip_depth test_planar_reflection test_sort_key test_retained_text test_sprite_mask_range test_sprite_draw_mode test_shadow_atlas test_particle_shapes test_lod_selection test_view_shading test_mesh_edges test_hdr_format test_probe_store test_occlusion_view test_reflection_store test_tiled_lights
+ test_clip_depth test_planar_reflection test_sort_key test_retained_text test_sprite_mask_range test_sprite_draw_mode test_shadow_atlas test_particle_shapes test_lod_selection test_view_shading test_mesh_edges test_hdr_format test_probe_store test_occlusion_view test_reflection_store test_tiled_lights test_light_layers
test_text_edit test_render_graph test_device_loss test_physics_interpolation
test_bitmap_font test_texture_cache test_material_shader_lifetime test_mesh_recovery test_aot_host test_aot_conformance
test_touch_stream
diff --git a/docs/astro/src/content/docs/graphics/lighting.mdx b/docs/astro/src/content/docs/graphics/lighting.mdx
index 4599aa837..b1a3c6537 100644
--- a/docs/astro/src/content/docs/graphics/lighting.mdx
+++ b/docs/astro/src/content/docs/graphics/lighting.mdx
@@ -98,6 +98,24 @@ What to keep in mind:
- Planar reflections are drawn with the sixteen only: their camera is not the one the tiles were made for.
- 2D lights ride the same set, so a 2D scene with fifty torches is lit too.
+## Which layers a light reaches
+
+A light lights every sprite it reaches, whatever layer the sprite is on. That is wrong for a lamp inside a hut, which has no business lighting the roof drawn over it.
+Give the light a **Layer Min** and **Layer Max** (`layerMin`, `layerMax`) and it lights only the sprites whose `layer` is in that range:
+
+```ts
+// The floor and the furniture are layers 1 to 4, the roof is 7: the lamp reaches everything up to 6.
+world.insert(lamp, Light, { type: 0, color: { r: 1, g: 0.78, b: 0.45, a: 1 }, intensity: 1.5, radius: 330, layerMax: 6 });
+```
+
+The default, -128 to 127, is every layer. The roof still takes the scene's **ambient** light, which is not a range-limited lamp but the sky, so it is dark by night and
+bright by day, and only the lamp's glow is kept off it.
+
+- **Sprites carry the layer**, and only sprites: a lit `Sprite` (the Lit checkbox, or a material built from the Lit or Sway template) is tested against it. A mesh, a tilemap, a particle or a skeleton has no layer to test and is lit as it always was.
+- **A custom lit sprite shader** takes part by calling `applyLighting2DLayer(albedo, N, worldPos, v_lightLayer)` instead of `applyLighting2D`, with `flat in highp float v_lightLayer;` declared. A shader that keeps calling `applyLighting2D` is lit by every light, as before.
+- **Directional lights** take a range too, so a sun can leave an interior layer alone.
+- It costs a comparison per light per pixel, and nothing where no light has a range.
+
## Shaping the falloff
A Point or Spot light fades from its own centre to nothing at `radius`, in a straight
diff --git a/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx b/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx
index 1ce7db7c9..22f7eb38a 100644
--- a/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx
+++ b/docs/astro/src/content/docs/zh-cn/graphics/lighting.mdx
@@ -89,6 +89,22 @@ const addSun = defineSystem([Commands()], (cmds) => {
- 平面反射只用那 16 盏绘制:它的相机不是分块所对应的那个相机。
- 2D 光也走同一个集合,所以有五十支火把的 2D 场景同样被照亮。
+## 灯照到哪些层
+
+一盏灯会照亮它够得着的每一个精灵,不管精灵在哪一层。这对小屋里的灯就不对了:它不该照亮盖在头顶的屋顶。给灯设置 **Layer Min** 和 **Layer Max**(`layerMin`、`layerMax`),它就只照亮 `layer` 在这个范围内的精灵:
+
+```ts
+// 地板和家具在 1 到 4 层,屋顶在 7 层:这盏灯只照到 6 层为止。
+world.insert(lamp, Light, { type: 0, color: { r: 1, g: 0.78, b: 0.45, a: 1 }, intensity: 1.5, radius: 330, layerMax: 6 });
+```
+
+默认的 -128 到 127 是所有层。屋顶仍然会受场景的**环境光**照亮,环境光不是有范围限制的灯,而是天空,所以夜里暗、白天亮,只是灯的光晕不再照到它。
+
+- **只有精灵带层**:受光照的 `Sprite`(勾选 Lit,或用 Lit、Sway 模板建的材质)会按它测试。mesh、瓦片地图、粒子和骨骼没有层可测,照旧受光。
+- **自定义的受光精灵着色器**要参与,就用 `applyLighting2DLayer(albedo, N, worldPos, v_lightLayer)` 代替 `applyLighting2D`,并声明 `flat in highp float v_lightLayer;`。继续调用 `applyLighting2D` 的着色器像以前一样被所有灯照亮。
+- **方向光**也能设范围,所以太阳可以不照某个室内层。
+- 每盏灯每个像素多一次比较;没有灯设范围时没有别的开销。
+
## 让渲染器接受光照
默认情况下,一个 `Sprite` 用**不受光**材质绘制,会忽略每一盏灯。让它受光最简单的方式是
diff --git a/docs/astro/src/data/components.generated.json b/docs/astro/src/data/components.generated.json
index 007a7e1d2..463bff83c 100644
--- a/docs/astro/src/data/components.generated.json
+++ b/docs/astro/src/data/components.generated.json
@@ -2372,6 +2372,20 @@
"doc": "How much of this light its shadows remove (1 = all of it).",
"range": "0–1"
},
+ {
+ "key": "layerMin",
+ "type": "number",
+ "default": "-128",
+ "doc": "The lowest sprite layer this light reaches (default: every layer).",
+ "range": "-128–127, step 1"
+ },
+ {
+ "key": "layerMax",
+ "type": "number",
+ "default": "127",
+ "doc": "The highest sprite layer this light reaches (default: every layer).",
+ "range": "-128–127, step 1"
+ },
{
"key": "cookie",
"type": "Asset<texture>",
diff --git a/sdk/etc/index.api.md b/sdk/etc/index.api.md
index f1c68830c..d4214cf01 100644
--- a/sdk/etc/index.api.md
+++ b/sdk/etc/index.api.md
@@ -5,7 +5,7 @@ Symbols: 115 public · 159 beta · 1917 experimental · 3 deprecated
## ABI_LAYOUT_HASH — const @experimental
```
-"952e9aafee3c2480"
+"11e3c2bb051fdd37"
```
## ANCHOR_AXES — const @experimental
@@ -5242,6 +5242,8 @@ followEnvironmentSun: boolean
innerAngle: number
innerRadius: number
intensity: number
+layerMax: number | undefined
+layerMin: number | undefined
meshShadows: boolean
outerAngle: number
radius: number
diff --git a/sdk/etc/index.native.api.md b/sdk/etc/index.native.api.md
index aebb3058d..21f29f93e 100644
--- a/sdk/etc/index.native.api.md
+++ b/sdk/etc/index.native.api.md
@@ -5,7 +5,7 @@ Symbols: 115 public · 164 beta · 1936 experimental · 3 deprecated
## ABI_LAYOUT_HASH — const @experimental
```
-"952e9aafee3c2480"
+"11e3c2bb051fdd37"
```
## ANCHOR_AXES — const @experimental
@@ -5250,6 +5250,8 @@ followEnvironmentSun: boolean
innerAngle: number
innerRadius: number
intensity: number
+layerMax: number | undefined
+layerMin: number | undefined
meshShadows: boolean
outerAngle: number
radius: number
diff --git a/sdk/etc/index.node.api.md b/sdk/etc/index.node.api.md
index 2bafcaa7c..a41c70b4c 100644
--- a/sdk/etc/index.node.api.md
+++ b/sdk/etc/index.node.api.md
@@ -5237,6 +5237,8 @@ followEnvironmentSun: boolean
innerAngle: number
innerRadius: number
intensity: number
+layerMax: number | undefined
+layerMin: number | undefined
meshShadows: boolean
outerAngle: number
radius: number
diff --git a/sdk/etc/minigame.api.md b/sdk/etc/minigame.api.md
index de6400247..79a4db814 100644
--- a/sdk/etc/minigame.api.md
+++ b/sdk/etc/minigame.api.md
@@ -5237,6 +5237,8 @@ followEnvironmentSun: boolean
innerAngle: number
innerRadius: number
intensity: number
+layerMax: number | undefined
+layerMin: number | undefined
meshShadows: boolean
outerAngle: number
radius: number
diff --git a/sdk/etc/wechat.api.md b/sdk/etc/wechat.api.md
index ce383e62d..cc068c8ee 100644
--- a/sdk/etc/wechat.api.md
+++ b/sdk/etc/wechat.api.md
@@ -5244,6 +5244,8 @@ followEnvironmentSun: boolean
innerAngle: number
innerRadius: number
intensity: number
+layerMax: number | undefined
+layerMin: number | undefined
meshShadows: boolean
outerAngle: number
radius: number
diff --git a/sdk/src/ecs/bridge/ptrAccessors.generated.ts b/sdk/src/ecs/bridge/ptrAccessors.generated.ts
index 50853e5c5..b901a368f 100644
--- a/sdk/src/ecs/bridge/ptrAccessors.generated.ts
+++ b/sdk/src/ecs/bridge/ptrAccessors.generated.ts
@@ -805,6 +805,8 @@ export interface LightPtrData {
innerRadius: number;
falloff: number;
shadowStrength: number;
+ layerMin: number;
+ layerMax: number;
cookie: number;
cookieSize: Vec2;
innerAngle: number;
@@ -836,24 +838,26 @@ export function fillLight(
out.innerRadius = f32[(ptr + 28) >> 2];
out.falloff = f32[(ptr + 32) >> 2];
out.shadowStrength = f32[(ptr + 36) >> 2];
- out.cookie = u32[(ptr + 40) >> 2];
- const cookieSize_ = out.cookieSize; cookieSize_.x = f32[(ptr + 44) >> 2]; cookieSize_.y = f32[((ptr + 44) >> 2) + 1];
- out.innerAngle = f32[(ptr + 52) >> 2];
- out.outerAngle = f32[(ptr + 56) >> 2];
- out.shadowSoftness = f32[(ptr + 60) >> 2];
- out.sourceAngle = f32[(ptr + 64) >> 2];
- out.shadowDistance = f32[(ptr + 68) >> 2];
- out.meshShadows = u8[ptr + 72] !== 0;
- out.shadowExtent = f32[(ptr + 76) >> 2];
- out.followEnvironmentSun = u8[ptr + 80] !== 0;
- out.aimFollowedSun = u8[ptr + 81] !== 0;
- out.environment = u32[(ptr + 84) >> 2];
- out.environmentRotation = f32[(ptr + 88) >> 2];
- out.drawEnvironment = u8[ptr + 92] !== 0;
- out.reflectionScale = f32[(ptr + 96) >> 2];
- out.reflectionSaturation = f32[(ptr + 100) >> 2];
- const bakedTint_ = out.bakedTint; bakedTint_.r = f32[(ptr + 104) >> 2]; bakedTint_.g = f32[((ptr + 104) >> 2) + 1]; bakedTint_.b = f32[((ptr + 104) >> 2) + 2]; bakedTint_.a = f32[((ptr + 104) >> 2) + 3];
- out.enabled = u8[ptr + 120] !== 0;
+ out.layerMin = u32[(ptr + 40) >> 2] | 0;
+ out.layerMax = u32[(ptr + 44) >> 2] | 0;
+ out.cookie = u32[(ptr + 48) >> 2];
+ const cookieSize_ = out.cookieSize; cookieSize_.x = f32[(ptr + 52) >> 2]; cookieSize_.y = f32[((ptr + 52) >> 2) + 1];
+ out.innerAngle = f32[(ptr + 60) >> 2];
+ out.outerAngle = f32[(ptr + 64) >> 2];
+ out.shadowSoftness = f32[(ptr + 68) >> 2];
+ out.sourceAngle = f32[(ptr + 72) >> 2];
+ out.shadowDistance = f32[(ptr + 76) >> 2];
+ out.meshShadows = u8[ptr + 80] !== 0;
+ out.shadowExtent = f32[(ptr + 84) >> 2];
+ out.followEnvironmentSun = u8[ptr + 88] !== 0;
+ out.aimFollowedSun = u8[ptr + 89] !== 0;
+ out.environment = u32[(ptr + 92) >> 2];
+ out.environmentRotation = f32[(ptr + 96) >> 2];
+ out.drawEnvironment = u8[ptr + 100] !== 0;
+ out.reflectionScale = f32[(ptr + 104) >> 2];
+ out.reflectionSaturation = f32[(ptr + 108) >> 2];
+ const bakedTint_ = out.bakedTint; bakedTint_.r = f32[(ptr + 112) >> 2]; bakedTint_.g = f32[((ptr + 112) >> 2) + 1]; bakedTint_.b = f32[((ptr + 112) >> 2) + 2]; bakedTint_.a = f32[((ptr + 112) >> 2) + 3];
+ out.enabled = u8[ptr + 128] !== 0;
}
export function writeLight(
@@ -867,24 +871,26 @@ export function writeLight(
f32[(ptr + 28) >> 2] = data.innerRadius;
f32[(ptr + 32) >> 2] = data.falloff;
f32[(ptr + 36) >> 2] = data.shadowStrength;
- u32[(ptr + 40) >> 2] = data.cookie;
- f32[(ptr + 44) >> 2] = data.cookieSize.x; f32[((ptr + 44) >> 2) + 1] = data.cookieSize.y;
- f32[(ptr + 52) >> 2] = data.innerAngle;
- f32[(ptr + 56) >> 2] = data.outerAngle;
- f32[(ptr + 60) >> 2] = data.shadowSoftness;
- f32[(ptr + 64) >> 2] = data.sourceAngle;
- f32[(ptr + 68) >> 2] = data.shadowDistance;
- u8[ptr + 72] = data.meshShadows ? 1 : 0;
- f32[(ptr + 76) >> 2] = data.shadowExtent;
- u8[ptr + 80] = data.followEnvironmentSun ? 1 : 0;
- u8[ptr + 81] = data.aimFollowedSun ? 1 : 0;
- u32[(ptr + 84) >> 2] = data.environment;
- f32[(ptr + 88) >> 2] = data.environmentRotation;
- u8[ptr + 92] = data.drawEnvironment ? 1 : 0;
- f32[(ptr + 96) >> 2] = data.reflectionScale;
- f32[(ptr + 100) >> 2] = data.reflectionSaturation;
- f32[(ptr + 104) >> 2] = data.bakedTint.r; f32[((ptr + 104) >> 2) + 1] = data.bakedTint.g; f32[((ptr + 104) >> 2) + 2] = data.bakedTint.b; f32[((ptr + 104) >> 2) + 3] = data.bakedTint.a;
- u8[ptr + 120] = data.enabled ? 1 : 0;
+ u32[(ptr + 40) >> 2] = data.layerMin | 0;
+ u32[(ptr + 44) >> 2] = data.layerMax | 0;
+ u32[(ptr + 48) >> 2] = data.cookie;
+ f32[(ptr + 52) >> 2] = data.cookieSize.x; f32[((ptr + 52) >> 2) + 1] = data.cookieSize.y;
+ f32[(ptr + 60) >> 2] = data.innerAngle;
+ f32[(ptr + 64) >> 2] = data.outerAngle;
+ f32[(ptr + 68) >> 2] = data.shadowSoftness;
+ f32[(ptr + 72) >> 2] = data.sourceAngle;
+ f32[(ptr + 76) >> 2] = data.shadowDistance;
+ u8[ptr + 80] = data.meshShadows ? 1 : 0;
+ f32[(ptr + 84) >> 2] = data.shadowExtent;
+ u8[ptr + 88] = data.followEnvironmentSun ? 1 : 0;
+ u8[ptr + 89] = data.aimFollowedSun ? 1 : 0;
+ u32[(ptr + 92) >> 2] = data.environment;
+ f32[(ptr + 96) >> 2] = data.environmentRotation;
+ u8[ptr + 100] = data.drawEnvironment ? 1 : 0;
+ f32[(ptr + 104) >> 2] = data.reflectionScale;
+ f32[(ptr + 108) >> 2] = data.reflectionSaturation;
+ f32[(ptr + 112) >> 2] = data.bakedTint.r; f32[((ptr + 112) >> 2) + 1] = data.bakedTint.g; f32[((ptr + 112) >> 2) + 2] = data.bakedTint.b; f32[((ptr + 112) >> 2) + 3] = data.bakedTint.a;
+ u8[ptr + 128] = data.enabled ? 1 : 0;
}
export function createLightData(): LightPtrData {
@@ -896,6 +902,8 @@ export function createLightData(): LightPtrData {
innerRadius: 0,
falloff: 0,
shadowStrength: 0,
+ layerMin: 0,
+ layerMax: 0,
cookie: 0,
cookieSize: { x: 0, y: 0 },
innerAngle: 0,
diff --git a/sdk/src/ecs/component.generated.ts b/sdk/src/ecs/component.generated.ts
index 45db716c7..881ab8eb3 100644
--- a/sdk/src/ecs/component.generated.ts
+++ b/sdk/src/ecs/component.generated.ts
@@ -15,7 +15,7 @@ import type { AlignContent, AlignItems, AlignSelf, BodyType, CanvasScaleMode, Cl
* getAbiLayoutHash(); BuiltinBridge.connect() compares them and refuses to
* run on mismatch, because mismatched offsets read the wrong heap bytes.
*/
-export const ABI_LAYOUT_HASH = '952e9aafee3c2480';
+export const ABI_LAYOUT_HASH = '11e3c2bb051fdd37';
/**
* One asset-valued field of a component: which field, and what kind of
@@ -466,6 +466,8 @@ export const COMPONENT_META: Record<string, ComponentMetaEntry> = {
innerRadius: 0,
falloff: 1,
shadowStrength: 1,
+ layerMin: -128,
+ layerMax: 127,
cookie: 0,
cookieSize: { x: 0, y: 0 },
innerAngle: 30,
@@ -497,6 +499,8 @@ export const COMPONENT_META: Record<string, ComponentMetaEntry> = {
innerRadius: { min: 0, tooltip: "Radius held at full strength before the falloff starts.", shownWhen: { field: "type", values: [0, 3] } },
falloff: { min: 0, max: 8, tooltip: "Falloff shape: 1 = linear, higher pools near the light, lower reaches further.", shownWhen: { field: "type", values: [0, 3] } },
shadowStrength: { min: 0, max: 1, slider: true, tooltip: "How much of this light its shadows remove (1 = all of it).", shownWhen: { field: "type", values: [0, 1, 3] } },
+ layerMin: { min: -128, max: 127, step: 1, tooltip: "The lowest sprite layer this light reaches (default: every layer).", shownWhen: { field: "type", values: [0, 1, 3] } },
+ layerMax: { min: -128, max: 127, step: 1, tooltip: "The highest sprite layer this light reaches (default: every layer).", shownWhen: { field: "type", values: [0, 1, 3] } },
cookie: { tooltip: "A texture whose alpha shapes this light (a cookie).", shownWhen: { field: "type", values: [0, 3] } },
cookieSize: { min: 0, tooltip: "Cookie size in world units; 0 = the light's own reach.", shownWhen: { field: "type", values: [0, 3] } },
innerAngle: { min: 0, max: 180, unit: "°", advanced: true, shownWhen: { field: "type", values: [3] } },
@@ -1567,6 +1571,8 @@ export interface LightData {
innerRadius: number;
falloff: number;
shadowStrength: number;
+ layerMin?: number;
+ layerMax?: number;
cookie: number;
cookieSize: Vec2;
innerAngle: number;
diff --git a/sdk/src/render/builtinShaders.ts b/sdk/src/render/builtinShaders.ts
index ec3d93cd5..fcaaeb01f 100644
--- a/sdk/src/render/builtinShaders.ts
+++ b/sdk/src/render/builtinShaders.ts
@@ -91,6 +91,7 @@ precision mediump float;
in vec4 v_color;
in vec2 v_texCoord;
in highp vec2 v_worldPos;
+flat in highp float v_lightLayer;
#ifdef MESH_NORMALS
in highp vec3 v_worldNormal;
in highp vec3 v_worldXYZ;
@@ -113,7 +114,7 @@ void main() {
#else
vec3 N = sampleNormal(u_normalMap, v_texCoord);
#endif
- fragColor = vec4(applyLighting2D(base.rgb, N, v_worldPos), base.a);
+ fragColor = vec4(applyLighting2DLayer(base.rgb, N, v_worldPos, v_lightLayer), base.a);
}
#pragma end
@@ -130,7 +131,7 @@ void main() {
#else
let N = sampleNormalGrad(u_normalMap, u_normalMap_s, v.v_texCoord, duvx, duvy);
#endif
- return vec4f(applyLighting2D(base.rgb, N, v.v_worldPos), base.a);
+ return vec4f(applyLighting2DLayer(base.rgb, N, v.v_worldPos, v.v_lightLayer), base.a);
}
#pragma end
`;
@@ -154,6 +155,7 @@ precision mediump float;
in vec4 v_color;
in vec2 v_texCoord;
in highp vec2 v_worldPos;
+flat in highp float v_lightLayer;
uniform sampler2D u_textures[8];
@@ -169,7 +171,7 @@ void main() {
vec4 base = texture(u_textures[0], uv) * v_color * u_tint;
if (uv.x < 0.0 || uv.x > 1.0) base.a = 0.0;
vec3 N = sampleNormal(u_normalMap, uv);
- fragColor = vec4(applyLighting2D(base.rgb, N, v_worldPos), base.a);
+ fragColor = vec4(applyLighting2DLayer(base.rgb, N, v_worldPos, v_lightLayer), base.a);
}
#pragma end
@@ -186,7 +188,7 @@ void main() {
var base = textureSampleGrad(t0, s0, uv, duvx, duvy) * v.v_color * mc.u_tint;
if (uv.x < 0.0 || uv.x > 1.0) { base.a = 0.0; }
let N = sampleNormalGrad(u_normalMap, u_normalMap_s, uv, duvx, duvy);
- return vec4f(applyLighting2D(base.rgb, N, v.v_worldPos), base.a);
+ return vec4f(applyLighting2DLayer(base.rgb, N, v.v_worldPos, v.v_lightLayer), base.a);
}
#pragma end
`;
diff --git a/sdk/src/wasm/ptrLayouts.generated.ts b/sdk/src/wasm/ptrLayouts.generated.ts
index fad44f7f2..14f570e03 100644
--- a/sdk/src/wasm/ptrLayouts.generated.ts
+++ b/sdk/src/wasm/ptrLayouts.generated.ts
@@ -234,24 +234,26 @@ export const PTR_LAYOUTS: Record<string, PtrLayout> = {
{ name: 'innerRadius', type: 'f32', offset: 28 },
{ name: 'falloff', type: 'f32', offset: 32 },
{ name: 'shadowStrength', type: 'f32', offset: 36 },
- { name: 'cookie', type: 'u32', offset: 40 },
- { name: 'cookieSize', type: 'vec2', offset: 44 },
- { name: 'innerAngle', type: 'f32', offset: 52 },
- { name: 'outerAngle', type: 'f32', offset: 56 },
- { name: 'shadowSoftness', type: 'f32', offset: 60 },
- { name: 'sourceAngle', type: 'f32', offset: 64 },
- { name: 'shadowDistance', type: 'f32', offset: 68 },
- { name: 'meshShadows', type: 'bool', offset: 72 },
- { name: 'shadowExtent', type: 'f32', offset: 76 },
- { name: 'followEnvironmentSun', type: 'bool', offset: 80 },
- { name: 'aimFollowedSun', type: 'bool', offset: 81 },
- { name: 'environment', type: 'u32', offset: 84 },
- { name: 'environmentRotation', type: 'f32', offset: 88 },
- { name: 'drawEnvironment', type: 'bool', offset: 92 },
- { name: 'reflectionScale', type: 'f32', offset: 96 },
- { name: 'reflectionSaturation', type: 'f32', offset: 100 },
- { name: 'bakedTint', type: 'color', offset: 104 },
- { name: 'enabled', type: 'bool', offset: 120 },
+ { name: 'layerMin', type: 'i32', offset: 40 },
+ { name: 'layerMax', type: 'i32', offset: 44 },
+ { name: 'cookie', type: 'u32', offset: 48 },
+ { name: 'cookieSize', type: 'vec2', offset: 52 },
+ { name: 'innerAngle', type: 'f32', offset: 60 },
+ { name: 'outerAngle', type: 'f32', offset: 64 },
+ { name: 'shadowSoftness', type: 'f32', offset: 68 },
+ { name: 'sourceAngle', type: 'f32', offset: 72 },
+ { name: 'shadowDistance', type: 'f32', offset: 76 },
+ { name: 'meshShadows', type: 'bool', offset: 80 },
+ { name: 'shadowExtent', type: 'f32', offset: 84 },
+ { name: 'followEnvironmentSun', type: 'bool', offset: 88 },
+ { name: 'aimFollowedSun', type: 'bool', offset: 89 },
+ { name: 'environment', type: 'u32', offset: 92 },
+ { name: 'environmentRotation', type: 'f32', offset: 96 },
+ { name: 'drawEnvironment', type: 'bool', offset: 100 },
+ { name: 'reflectionScale', type: 'f32', offset: 104 },
+ { name: 'reflectionSaturation', type: 'f32', offset: 108 },
+ { name: 'bakedTint', type: 'color', offset: 112 },
+ { name: 'enabled', type: 'bool', offset: 128 },
],
},
LightProbeVolume: {
diff --git a/sdk/src/wasm/wasm.generated.ts b/sdk/src/wasm/wasm.generated.ts
index aaf1f9fae..bfcd71b4f 100644
--- a/sdk/src/wasm/wasm.generated.ts
+++ b/sdk/src/wasm/wasm.generated.ts
@@ -612,6 +612,8 @@ export interface Light {
innerRadius: number;
falloff: number;
shadowStrength: number;
+ layerMin: number;
+ layerMax: number;
cookie: number;
cookieSize: Vec2;
innerAngle: number;
diff --git a/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap b/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
index f867a7e92..69fb10682 100644
--- a/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
+++ b/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
@@ -334,6 +334,34 @@ exports[`builtin FieldMeta (RC9-1 equivalence gate) > matches the golden snapsho
"min": 0,
"tooltip": "Brightness multiplier of the light.",
},
+ "layerMax": {
+ "max": 127,
+ "min": -128,
+ "shownWhen": {
+ "field": "type",
+ "values": [
+ 0,
+ 1,
+ 3,
+ ],
+ },
+ "step": 1,
+ "tooltip": "The highest sprite layer this light reaches (default: every layer).",
+ },
+ "layerMin": {
+ "max": 127,
+ "min": -128,
+ "shownWhen": {
+ "field": "type",
+ "values": [
+ 0,
+ 1,
+ 3,
+ ],
+ },
+ "step": 1,
+ "tooltip": "The lowest sprite layer this light reaches (default: every layer).",
+ },
"meshShadows": {
"shownWhen": {
"field": "type",
diff --git a/sdk/tests/atmosphere-2d.test.ts b/sdk/tests/atmosphere-2d.test.ts
index 00b6bd239..011edf7ba 100644
--- a/sdk/tests/atmosphere-2d.test.ts
+++ b/sdk/tests/atmosphere-2d.test.ts
@@ -4,6 +4,7 @@ import { describe, expect, it } from 'vitest';
import { coversPoint, stepFade } from '../src/render/coverFade';
import { applyRainSheltersToRain, drynessOf, nearestShelters, shelterOf, type Shelter } from '../src/render/rainShelter';
import { RAIN_SHELTER_SLOTS, shelterUniform } from '../src/postprocess/rainShelterSlots';
+import { Light } from '../src/ecs/component';
import type { SpriteData, TransformData } from '../src/ecs/component';
import { applyWeatherToEffects, blendWeather, WeatherDrive, WEATHER_CLEAR, WEATHER_NONE, WEATHER_RAIN } from '../src/postprocess/weather';
import { getEffectDef } from '../src/postprocess/effects';
@@ -182,3 +183,25 @@ describe('the 2D rain', () => {
expect(plain.has('rain')).toBe(true);
});
});
+
+describe('the layers a light reaches', () => {
+ it('is every layer by default, so a scene written before it is lit as it was', () => {
+ expect(Light._default.layerMin).toBe(-128);
+ expect(Light._default.layerMax).toBe(127);
+ });
+
+ it('is told to every Lit sprite template, which reads the layer its vertex wrote and names it to the lighting', () => {
+ for (const id of ['sprite-lit', 'sprite-sway']) {
+ const source = BUILTIN_SHADER_TEMPLATES.find((t) => t.id === id)!.source;
+ expect(source, id).toContain('flat in highp float v_lightLayer;');
+ expect(source, id).toContain('applyLighting2DLayer(base.rgb, N, v_worldPos, v_lightLayer)');
+ expect(source, id).toContain('applyLighting2DLayer(base.rgb, N, v.v_worldPos, v.v_lightLayer)');
+ }
+ });
+
+ it('is not asked of the mesh templates, which have no layer to give', () => {
+ for (const id of ['model', 'toon', 'matcap']) {
+ expect(BUILTIN_SHADER_TEMPLATES.find((t) => t.id === id)!.source, id).not.toContain('v_lightLayer');
+ }
+ });
+});
diff --git a/src/esengine/aot/EngineDigest.generated.h b/src/esengine/aot/EngineDigest.generated.h
index 178ac6b40..7bcd28dfb 100644
--- a/src/esengine/aot/EngineDigest.generated.h
+++ b/src/esengine/aot/EngineDigest.generated.h
@@ -6,7 +6,7 @@
/* One per address width, because the width is IN the digest: a host that
took the other one would refuse every module built for it. */
-#define ES_ENGINE_ABI_DIGEST_32 0x10eb5f4f7fbcdb96ULL
-#define ES_ENGINE_ABI_DIGEST_64 0x239e7f83ac19d562ULL
+#define ES_ENGINE_ABI_DIGEST_32 0x2a2acef3f0ef9d3aULL
+#define ES_ENGINE_ABI_DIGEST_64 0xc74e6f71a7edb276ULL
#define ES_ENGINE_ABI_DIGEST \
(sizeof(es_addr_t) == 8 ? ES_ENGINE_ABI_DIGEST_64 : ES_ENGINE_ABI_DIGEST_32)
diff --git a/src/esengine/bindings/EditorAPI.generated.cpp b/src/esengine/bindings/EditorAPI.generated.cpp
index 9a2e7a8ef..a714d38f2 100644
--- a/src/esengine/bindings/EditorAPI.generated.cpp
+++ b/src/esengine/bindings/EditorAPI.generated.cpp
@@ -2,10 +2,10 @@
* @file EditorAPI.generated.cpp
* @brief Auto-generated editor API with string-dispatched component access
* @details Generated by EHT - DO NOT EDIT
- * @schema_hash 109e1d126ef2ba4b
+ * @schema_hash 4f0846b4dd77f7d5
*/
-#define ES_EDITOR_API_SCHEMA_HASH "109e1d126ef2ba4b"
+#define ES_EDITOR_API_SCHEMA_HASH "4f0846b4dd77f7d5"
#include <emscripten/bind.h>
#include <string>
@@ -345,6 +345,8 @@ std::string editor_getComponentSchema(const std::string& name) {
{"key":"innerRadius","type":"float","group":"Light"},
{"key":"falloff","type":"float","group":"Light"},
{"key":"shadowStrength","type":"float","group":"Light"},
+ {"key":"layerMin","type":"int","group":"Light"},
+ {"key":"layerMax","type":"int","group":"Light"},
{"key":"cookie","type":"asset","group":"Light"},
{"key":"cookieSize.x","type":"float","group":"Light"},
{"key":"cookieSize.y","type":"float","group":"Light"},
@@ -2444,6 +2446,8 @@ bool editor_setInt(Registry& reg, u32 e, const std::string& comp, const std::str
if (!reg.has<esengine::ecs::Light>(entity)) return false;
auto& c = reg.get<esengine::ecs::Light>(entity);
if (field == "type") { c.type = static_cast<i32>(value); }
+ else if (field == "layerMin") { c.layerMin = static_cast<i32>(value); }
+ else if (field == "layerMax") { c.layerMax = static_cast<i32>(value); }
else if (field == "cookie") { c.cookie = resource::TextureHandle(static_cast<u32>(value)); }
else if (field == "environment") { c.environment = resource::EnvironmentHandle(static_cast<u32>(value)); }
else { return false; }
@@ -2729,6 +2733,8 @@ i32 editor_getInt(Registry& reg, u32 e, const std::string& comp, const std::stri
if (!reg.has<esengine::ecs::Light>(entity)) return 0;
const auto& c = reg.get<esengine::ecs::Light>(entity);
if (field == "type") { return static_cast<i32>(c.type); }
+ else if (field == "layerMin") { return static_cast<i32>(c.layerMin); }
+ else if (field == "layerMax") { return static_cast<i32>(c.layerMax); }
else if (field == "cookie") { return static_cast<i32>(c.cookie.id()); }
else if (field == "environment") { return static_cast<i32>(c.environment.id()); }
} else if (comp == "LightProbeVolume") {
diff --git a/src/esengine/bindings/WebBindings.generated.cpp b/src/esengine/bindings/WebBindings.generated.cpp
index a352e6e94..165541bab 100644
--- a/src/esengine/bindings/WebBindings.generated.cpp
+++ b/src/esengine/bindings/WebBindings.generated.cpp
@@ -621,6 +621,8 @@ struct LightJS {
f32 innerRadius;
f32 falloff;
f32 shadowStrength;
+ i32 layerMin;
+ i32 layerMax;
u32 cookie;
glm::vec2 cookieSize;
f32 innerAngle;
@@ -649,6 +651,8 @@ void lightApplyJS(esengine::ecs::Light& c, const LightJS& js) {
c.innerRadius = js.innerRadius;
c.falloff = js.falloff;
c.shadowStrength = js.shadowStrength;
+ c.layerMin = js.layerMin;
+ c.layerMax = js.layerMax;
c.cookie = resource::TextureHandle(js.cookie);
c.cookieSize = js.cookieSize;
c.innerAngle = js.innerAngle;
@@ -684,6 +688,8 @@ LightJS lightToJS(const esengine::ecs::Light& c) {
js.innerRadius = c.innerRadius;
js.falloff = c.falloff;
js.shadowStrength = c.shadowStrength;
+ js.layerMin = c.layerMin;
+ js.layerMax = c.layerMax;
js.cookie = c.cookie.id();
js.cookieSize = c.cookieSize;
js.innerAngle = c.innerAngle;
@@ -1945,6 +1951,8 @@ EMSCRIPTEN_BINDINGS(esengine_components) {
.field("innerRadius", &LightJS::innerRadius)
.field("falloff", &LightJS::falloff)
.field("shadowStrength", &LightJS::shadowStrength)
+ .field("layerMin", &LightJS::layerMin)
+ .field("layerMax", &LightJS::layerMax)
.field("cookie", &LightJS::cookie)
.field("cookieSize", &LightJS::cookieSize)
.field("innerAngle", &LightJS::innerAngle)
@@ -3844,24 +3852,26 @@ static_assert(offsetof(esengine::ecs::Light, radius) == 24, "ABI offset drift: e
static_assert(offsetof(esengine::ecs::Light, innerRadius) == 28, "ABI offset drift: esengine::ecs::Light.innerRadius (EHT expected 28)");
static_assert(offsetof(esengine::ecs::Light, falloff) == 32, "ABI offset drift: esengine::ecs::Light.falloff (EHT expected 32)");
static_assert(offsetof(esengine::ecs::Light, shadowStrength) == 36, "ABI offset drift: esengine::ecs::Light.shadowStrength (EHT expected 36)");
-static_assert(offsetof(esengine::ecs::Light, cookie) == 40, "ABI offset drift: esengine::ecs::Light.cookie (EHT expected 40)");
-static_assert(offsetof(esengine::ecs::Light, cookieSize) == 44, "ABI offset drift: esengine::ecs::Light.cookieSize (EHT expected 44)");
-static_assert(offsetof(esengine::ecs::Light, innerAngle) == 52, "ABI offset drift: esengine::ecs::Light.innerAngle (EHT expected 52)");
-static_assert(offsetof(esengine::ecs::Light, outerAngle) == 56, "ABI offset drift: esengine::ecs::Light.outerAngle (EHT expected 56)");
-static_assert(offsetof(esengine::ecs::Light, shadowSoftness) == 60, "ABI offset drift: esengine::ecs::Light.shadowSoftness (EHT expected 60)");
-static_assert(offsetof(esengine::ecs::Light, sourceAngle) == 64, "ABI offset drift: esengine::ecs::Light.sourceAngle (EHT expected 64)");
-static_assert(offsetof(esengine::ecs::Light, shadowDistance) == 68, "ABI offset drift: esengine::ecs::Light.shadowDistance (EHT expected 68)");
-static_assert(offsetof(esengine::ecs::Light, meshShadows) == 72, "ABI offset drift: esengine::ecs::Light.meshShadows (EHT expected 72)");
-static_assert(offsetof(esengine::ecs::Light, shadowExtent) == 76, "ABI offset drift: esengine::ecs::Light.shadowExtent (EHT expected 76)");
-static_assert(offsetof(esengine::ecs::Light, followEnvironmentSun) == 80, "ABI offset drift: esengine::ecs::Light.followEnvironmentSun (EHT expected 80)");
-static_assert(offsetof(esengine::ecs::Light, aimFollowedSun) == 81, "ABI offset drift: esengine::ecs::Light.aimFollowedSun (EHT expected 81)");
-static_assert(offsetof(esengine::ecs::Light, environment) == 84, "ABI offset drift: esengine::ecs::Light.environment (EHT expected 84)");
-static_assert(offsetof(esengine::ecs::Light, environmentRotation) == 88, "ABI offset drift: esengine::ecs::Light.environmentRotation (EHT expected 88)");
-static_assert(offsetof(esengine::ecs::Light, drawEnvironment) == 92, "ABI offset drift: esengine::ecs::Light.drawEnvironment (EHT expected 92)");
-static_assert(offsetof(esengine::ecs::Light, reflectionScale) == 96, "ABI offset drift: esengine::ecs::Light.reflectionScale (EHT expected 96)");
-static_assert(offsetof(esengine::ecs::Light, reflectionSaturation) == 100, "ABI offset drift: esengine::ecs::Light.reflectionSaturation (EHT expected 100)");
-static_assert(offsetof(esengine::ecs::Light, bakedTint) == 104, "ABI offset drift: esengine::ecs::Light.bakedTint (EHT expected 104)");
-static_assert(offsetof(esengine::ecs::Light, enabled) == 120, "ABI offset drift: esengine::ecs::Light.enabled (EHT expected 120)");
+static_assert(offsetof(esengine::ecs::Light, layerMin) == 40, "ABI offset drift: esengine::ecs::Light.layerMin (EHT expected 40)");
+static_assert(offsetof(esengine::ecs::Light, layerMax) == 44, "ABI offset drift: esengine::ecs::Light.layerMax (EHT expected 44)");
+static_assert(offsetof(esengine::ecs::Light, cookie) == 48, "ABI offset drift: esengine::ecs::Light.cookie (EHT expected 48)");
+static_assert(offsetof(esengine::ecs::Light, cookieSize) == 52, "ABI offset drift: esengine::ecs::Light.cookieSize (EHT expected 52)");
+static_assert(offsetof(esengine::ecs::Light, innerAngle) == 60, "ABI offset drift: esengine::ecs::Light.innerAngle (EHT expected 60)");
+static_assert(offsetof(esengine::ecs::Light, outerAngle) == 64, "ABI offset drift: esengine::ecs::Light.outerAngle (EHT expected 64)");
+static_assert(offsetof(esengine::ecs::Light, shadowSoftness) == 68, "ABI offset drift: esengine::ecs::Light.shadowSoftness (EHT expected 68)");
+static_assert(offsetof(esengine::ecs::Light, sourceAngle) == 72, "ABI offset drift: esengine::ecs::Light.sourceAngle (EHT expected 72)");
+static_assert(offsetof(esengine::ecs::Light, shadowDistance) == 76, "ABI offset drift: esengine::ecs::Light.shadowDistance (EHT expected 76)");
+static_assert(offsetof(esengine::ecs::Light, meshShadows) == 80, "ABI offset drift: esengine::ecs::Light.meshShadows (EHT expected 80)");
+static_assert(offsetof(esengine::ecs::Light, shadowExtent) == 84, "ABI offset drift: esengine::ecs::Light.shadowExtent (EHT expected 84)");
+static_assert(offsetof(esengine::ecs::Light, followEnvironmentSun) == 88, "ABI offset drift: esengine::ecs::Light.followEnvironmentSun (EHT expected 88)");
+static_assert(offsetof(esengine::ecs::Light, aimFollowedSun) == 89, "ABI offset drift: esengine::ecs::Light.aimFollowedSun (EHT expected 89)");
+static_assert(offsetof(esengine::ecs::Light, environment) == 92, "ABI offset drift: esengine::ecs::Light.environment (EHT expected 92)");
+static_assert(offsetof(esengine::ecs::Light, environmentRotation) == 96, "ABI offset drift: esengine::ecs::Light.environmentRotation (EHT expected 96)");
+static_assert(offsetof(esengine::ecs::Light, drawEnvironment) == 100, "ABI offset drift: esengine::ecs::Light.drawEnvironment (EHT expected 100)");
+static_assert(offsetof(esengine::ecs::Light, reflectionScale) == 104, "ABI offset drift: esengine::ecs::Light.reflectionScale (EHT expected 104)");
+static_assert(offsetof(esengine::ecs::Light, reflectionSaturation) == 108, "ABI offset drift: esengine::ecs::Light.reflectionSaturation (EHT expected 108)");
+static_assert(offsetof(esengine::ecs::Light, bakedTint) == 112, "ABI offset drift: esengine::ecs::Light.bakedTint (EHT expected 112)");
+static_assert(offsetof(esengine::ecs::Light, enabled) == 128, "ABI offset drift: esengine::ecs::Light.enabled (EHT expected 128)");
static_assert(offsetof(esengine::ecs::LightProbeVolume, probes) == 0, "ABI offset drift: esengine::ecs::LightProbeVolume.probes (EHT expected 0)");
static_assert(offsetof(esengine::ecs::LightProbeVolume, halfExtents) == 4, "ABI offset drift: esengine::ecs::LightProbeVolume.halfExtents (EHT expected 4)");
static_assert(offsetof(esengine::ecs::LightProbeVolume, spacing) == 16, "ABI offset drift: esengine::ecs::LightProbeVolume.spacing (EHT expected 16)");
@@ -4155,7 +4165,7 @@ static_assert(offsetof(esengine::ecs::Velocity, angular) == 12, "ABI offset drif
// ABI Hash -- runtime handshake against the SDK bundle
// =============================================================================
-static const char* kEsAbiLayoutHash = "952e9aafee3c2480";
+static const char* kEsAbiLayoutHash = "11e3c2bb051fdd37";
std::string esengineGetAbiLayoutHash() {
return std::string(kEsAbiLayoutHash);
diff --git a/src/esengine/data/shaders/batch.esshader b/src/esengine/data/shaders/batch.esshader
index 39a575a89..7d2785830 100644
--- a/src/esengine/data/shaders/batch.esshader
+++ b/src/esengine/data/shaders/batch.esshader
@@ -25,7 +25,9 @@ layout(location = 0) in vec3 a_position;
layout(location = 1) in vec4 a_color;
layout(location = 2) in vec2 a_texCoord;
layout(location = 3) in float a_texIndex;
-#ifdef SDF
+// The batch layout's last lane: how far to grow a text glyph, and for a lit sprite the layer it is on (plus 129; 0 = none), which a
+// light's range of layers is tested against.
+#if defined(SDF) || defined(LIT)
layout(location = 4) in float a_sdfBias;
#endif
@@ -37,6 +39,7 @@ out float v_sdfBias;
#endif
#ifdef LIT
out highp vec2 v_worldPos;
+flat out highp float v_lightLayer;
#endif
void main() {
@@ -49,6 +52,7 @@ void main() {
#endif
#ifdef LIT
v_worldPos = a_position.xy;
+ v_lightLayer = a_sdfBias;
#endif
}
#pragma end
@@ -66,6 +70,7 @@ in float v_sdfBias;
#endif
#ifdef LIT
in highp vec2 v_worldPos;
+flat in highp float v_lightLayer;
#endif
// Up to 8 textures bound per multi-texture batch. GLSL ES 3.00 forbids indexing a
@@ -109,7 +114,7 @@ void main() {
#elif defined(LIT)
// Flat normal; the tinted color is the albedo. Normal maps need a material.
vec4 base = texColor * tint;
- fragColor = vec4(applyLighting2D(base.rgb, vec3(0.0, 0.0, 1.0), v_worldPos), base.a);
+ fragColor = vec4(applyLighting2DLayer(base.rgb, vec3(0.0, 0.0, 1.0), v_worldPos, v_lightLayer), base.a);
#else
#ifdef ALPHA_CLIP
// The SPRITE's alpha, not the tinted result: a mask graphic is routinely
@@ -130,6 +135,8 @@ struct VSIn {
@location(3) a_texIndex : f32,
#ifdef SDF
@location(4) a_sdfBias : f32,
+#elif defined(LIT)
+ @location(4) a_sdfBias : f32,
#endif
};
struct VSOut {
@@ -139,6 +146,7 @@ struct VSOut {
@location(2) v_texIndex : f32,
#ifdef LIT
@location(3) v_worldPos : vec2f,
+ @location(5) @interpolate(flat) v_lightLayer : f32,
#endif
#ifdef SDF
@location(4) v_sdfBias : f32,
@@ -156,6 +164,7 @@ struct VSOut {
#endif
#ifdef LIT
out.v_worldPos = v.a_position.xy;
+ out.v_lightLayer = v.a_sdfBias;
#endif
return out;
}
@@ -186,6 +195,7 @@ struct VSOut {
@location(2) v_texIndex : f32,
#ifdef LIT
@location(3) v_worldPos : vec2f,
+ @location(5) @interpolate(flat) v_lightLayer : f32,
#endif
#ifdef SDF
@location(4) v_sdfBias : f32,
@@ -225,7 +235,7 @@ struct VSOut {
return vec4f(tint.rgb, tint.a * coverage);
#elif defined(LIT)
let base = texColor * tint;
- return vec4f(applyLighting2D(base.rgb, vec3f(0.0, 0.0, 1.0), v.v_worldPos), base.a);
+ return vec4f(applyLighting2DLayer(base.rgb, vec3f(0.0, 0.0, 1.0), v.v_worldPos, v.v_lightLayer), base.a);
#else
#ifdef ALPHA_CLIP
// See the GLSL stage: test the sprite's alpha, not the tinted result.
diff --git a/src/esengine/ecs/components/Light.hpp b/src/esengine/ecs/components/Light.hpp
index bd2a7aa29..38994f8ae 100644
--- a/src/esengine/ecs/components/Light.hpp
+++ b/src/esengine/ecs/components/Light.hpp
@@ -96,6 +96,20 @@ struct Light {
tooltip="How much of this light its shadows remove (1 = all of it).")
f32 shadowStrength{1.0f};
+ /** @brief The lowest sprite layer this light reaches. A sprite on a layer below it is not lit by this light,
+ * which is what keeps a lamp inside a room from lighting the roof drawn over it (a layer above the
+ * lamp's @ref layerMax). The default, with @ref layerMax, is every layer. Ambient lights reach every
+ * layer whatever these say: they are the scene's own sky, not a lamp somebody stands beside. Only
+ * sprites carry a layer to test; a mesh, a tilemap, a particle or a skeleton is lit as it always was. */
+ ES_PROPERTY(min=-128, max=127, step=1, optional, shown_when=type:Point|Directional|Spot,
+ tooltip="The lowest sprite layer this light reaches (default: every layer).")
+ i32 layerMin{-128};
+
+ /** @brief The highest sprite layer this light reaches. See @ref layerMin. */
+ ES_PROPERTY(min=-128, max=127, step=1, optional, shown_when=type:Point|Directional|Spot,
+ tooltip="The highest sprite layer this light reaches (default: every layer).")
+ i32 layerMax{127};
+
/** @brief A texture whose ALPHA is this light's shape — a lantern's glow, a window's
* shaft. Drawn at the light's own place, turned by its Transform, and
* multiplied into what the light gives a pixel; the colour is not read,
diff --git a/src/esengine/renderer/frame/RenderFrame.cpp b/src/esengine/renderer/frame/RenderFrame.cpp
index 0d5285b13..f0022e683 100644
--- a/src/esengine/renderer/frame/RenderFrame.cpp
+++ b/src/esengine/renderer/frame/RenderFrame.cpp
@@ -1636,6 +1636,8 @@ void RenderFrame::collectLights(ecs::Registry& registry) {
gpu.falloff = glm::vec4(std::max(light.innerRadius, 0.0f),
std::max(light.falloff, 0.0f),
std::clamp(light.shadowStrength, 0.0f, 1.0f), 0.0f);
+ // The sprite layers it reaches ride above the tile count the shadow atlas fills in later.
+ gpu.shadowMap.y = LIGHT_TILE_BITS * packLightLayerRange(light.layerMin, light.layerMax);
if (type == ecs::LightType::Directional) {
// z=1 flags directional (no attenuation) in the shader; w carries the aim's third
// component, which only a directional light has a use for — point and spot spend
diff --git a/src/esengine/renderer/plugins/SpritePlugin.cpp b/src/esengine/renderer/plugins/SpritePlugin.cpp
index b09e141e0..55fc884c7 100644
--- a/src/esengine/renderer/plugins/SpritePlugin.cpp
+++ b/src/esengine/renderer/plugins/SpritePlugin.cpp
@@ -12,7 +12,9 @@
#include "../../ecs/components/SpriteMask.hpp"
#include "../../ecs/components/UINode.hpp"
#include "../draw/SpriteDrawResolve.hpp"
+#include "../store/LightConstants.hpp"
+#include <algorithm>
#include <cmath>
namespace esengine {
@@ -184,6 +186,16 @@ void SpritePlugin::collect(RenderCollectContext& collect_ctx) {
vertices[i].position = position + basisX * delta.x + basisY * delta.y;
}
}
+ // A lit sprite hands its layer to the fragment stage in the batch layout's last lane (text spends it on an outline, and a sprite
+ // is never SDF text), where a light's range of layers is tested against it. See SPRITE_LAYER_BIAS in LightConstants.hpp.
+ if (sprite.lit || sprite.material != 0) {
+ const u32 vertexEnd = buffers.vertexBytesUsed(LayoutId::Batch);
+ auto* vertices = reinterpret_cast<BatchVertex*>(buffers.vertexData(LayoutId::Batch));
+ const f32 lane = static_cast<f32>(std::clamp(sprite.layer, -128, 127)) + SPRITE_LAYER_BIAS;
+ for (u32 i = vertexStart / sizeof(BatchVertex); i < vertexEnd / sizeof(BatchVertex); ++i) {
+ vertices[i].sdfBias = lane;
+ }
+ }
}
}
diff --git a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
index 61beca1c4..e30c3c79f 100644
--- a/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
+++ b/src/esengine/renderer/rhi/ShaderEmbeds.generated.hpp
@@ -29,7 +29,9 @@ layout(location = 0) in vec3 a_position;
layout(location = 1) in vec4 a_color;
layout(location = 2) in vec2 a_texCoord;
layout(location = 3) in float a_texIndex;
-#ifdef SDF
+
+
+#if defined(SDF) || defined(LIT)
layout(location = 4) in float a_sdfBias;
#endif
@@ -41,6 +43,7 @@ out float v_sdfBias;
#endif
#ifdef LIT
out highp vec2 v_worldPos;
+flat out highp float v_lightLayer;
#endif
void main() {
@@ -53,6 +56,7 @@ void main() {
#endif
#ifdef LIT
v_worldPos = a_position.xy;
+ v_lightLayer = a_sdfBias;
#endif
}
#pragma end
@@ -70,6 +74,7 @@ in float v_sdfBias;
#endif
#ifdef LIT
in highp vec2 v_worldPos;
+flat in highp float v_lightLayer;
#endif
@@ -113,7 +118,7 @@ void main() {
#elif defined(LIT)
vec4 base = texColor * tint;
- fragColor = vec4(applyLighting2D(base.rgb, vec3(0.0, 0.0, 1.0), v_worldPos), base.a);
+ fragColor = vec4(applyLighting2DLayer(base.rgb, vec3(0.0, 0.0, 1.0), v_worldPos, v_lightLayer), base.a);
#else
#ifdef ALPHA_CLIP
@@ -134,6 +139,8 @@ struct VSIn {
@location(3) a_texIndex : f32,
#ifdef SDF
@location(4) a_sdfBias : f32,
+#elif defined(LIT)
+ @location(4) a_sdfBias : f32,
#endif
};
struct VSOut {
@@ -143,6 +150,7 @@ struct VSOut {
@location(2) v_texIndex : f32,
#ifdef LIT
@location(3) v_worldPos : vec2f,
+ @location(5) @interpolate(flat) v_lightLayer : f32,
#endif
#ifdef SDF
@location(4) v_sdfBias : f32,
@@ -160,6 +168,7 @@ struct VSOut {
#endif
#ifdef LIT
out.v_worldPos = v.a_position.xy;
+ out.v_lightLayer = v.a_sdfBias;
#endif
return out;
}
@@ -190,6 +199,7 @@ struct VSOut {
@location(2) v_texIndex : f32,
#ifdef LIT
@location(3) v_worldPos : vec2f,
+ @location(5) @interpolate(flat) v_lightLayer : f32,
#endif
#ifdef SDF
@location(4) v_sdfBias : f32,
@@ -229,7 +239,7 @@ struct VSOut {
return vec4f(tint.rgb, tint.a * coverage);
#elif defined(LIT)
let base = texColor * tint;
- return vec4f(applyLighting2D(base.rgb, vec3f(0.0, 0.0, 1.0), v.v_worldPos), base.a);
+ return vec4f(applyLighting2DLayer(base.rgb, vec3f(0.0, 0.0, 1.0), v.v_worldPos, v.v_lightLayer), base.a);
#else
#ifdef ALPHA_CLIP
diff --git a/src/esengine/renderer/store/LightConstants.hpp b/src/esengine/renderer/store/LightConstants.hpp
index 6debe84d1..f561758fb 100644
--- a/src/esengine/renderer/store/LightConstants.hpp
+++ b/src/esengine/renderer/store/LightConstants.hpp
@@ -15,6 +15,7 @@
#include "../../core/Types.hpp"
#include "./TiledLights.hpp"
+#include <algorithm>
#include <glm/glm.hpp>
namespace esengine {
@@ -119,6 +120,23 @@ inline constexpr u32 MAX_SHADOW_TILES = 16;
*/
inline constexpr u32 MAX_REFLECTION_PROBES = 15;
+/**
+ * @brief The sprite layers a light reaches ride in `GpuLight::shadowMap.y` above the atlas tile count: `tiles + 8 * range`.
+ * @details The block has no spare lane (it sits at the 16 KB every device guarantees). The range is two bytes, the lowest layer biased up by 128
+ * and the highest counted down from 127, so that ZERO IS EVERY LAYER and a light nobody gave a range is unchanged.
+ */
+inline constexpr f32 LIGHT_TILE_BITS = 8.0f;
+
+/** @brief The range of sprite layers a light reaches, as the number that rides in `shadowMap.y` above the tile count. 0 is every layer. */
+inline f32 packLightLayerRange(i32 layerMin, i32 layerMax) {
+ const i32 lo = std::clamp(layerMin, -128, 127) + 128;
+ const i32 hi = 127 - std::clamp(layerMax, -128, 127);
+ return static_cast<f32>(lo + 256 * hi);
+}
+
+/** @brief What a sprite's layer is written as in the vertex lane the lit fragment reads: the layer plus 129, so 0 is "no layer to test". */
+inline constexpr f32 SPRITE_LAYER_BIAS = 129.0f;
+
/**
* @brief One 2D light, std140-packed (six vec4s, 96 bytes, 16-aligned).
* @details Every lane is spoken for; each field says which. An Ambient light occupies no
@@ -138,9 +156,8 @@ struct GpuLight {
/// directional light's shadow march distance (0 = it casts none); z = a positional
/// light's world height, measured against only by real geometry; w = a spot axis' z.
glm::vec4 shadow{0.0f};
- /// x = first atlas tile, y = how many it owns (0 = no map); z = the tangent of the
- /// angle its source subtends, for the map with no distance to divide by (0 for a
- /// light that stands somewhere); w = its 2D mask channel, -1 for none.
+ /// x = first atlas tile, y = tiles owned (0 = no map) in its low bits and the sprite-layer range above (packLightLayerRange);
+ /// z = the tangent of the angle its source subtends (0 for a light that stands somewhere); w = its 2D mask channel, -1 for none.
glm::vec4 shadowMap{0.0f, 0.0f, 0.0f, -1.0f};
/// How the light ENDS, as opposed to where: x = the radius it holds full strength
/// out to, y = the power its ramp is raised to (1 = linear), z = how much a shadow
diff --git a/src/esengine/renderer/store/LightStore.hpp b/src/esengine/renderer/store/LightStore.hpp
index 33165a8dd..dbde41749 100644
--- a/src/esengine/renderer/store/LightStore.hpp
+++ b/src/esengine/renderer/store/LightStore.hpp
@@ -20,6 +20,7 @@
#include "../rhi/GfxEnums.hpp"
#include "./LightConstants.hpp"
+#include <cmath>
#include <glm/glm.hpp>
namespace esengine {
@@ -106,8 +107,10 @@ public:
if (slot >= count_) return;
// The two the atlas decides, and nothing else: z is the light's own source size,
// written when it was collected and not this pass's to overwrite.
+ // The layer range this light carries above the count is not the atlas's to overwrite either.
+ const f32 range = std::floor(data_.lights[slot].shadowMap.y / LIGHT_TILE_BITS);
data_.lights[slot].shadowMap.x = static_cast<f32>(first);
- data_.lights[slot].shadowMap.y = static_cast<f32>(tiles);
+ data_.lights[slot].shadowMap.y = static_cast<f32>(tiles) + LIGHT_TILE_BITS * range;
dirty_ = true;
}
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index e05ebd4a9..8f1f3a0ca 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -252,6 +252,8 @@ std::string wgslCanonicalVSOut(bool lit) {
" @location(1) v_texCoord : vec2f,\n";
if (lit) {
src += " @location(2) v_worldPos : vec2f,\n";
+ // The layer the sprite is on (plus 129), or 0 where the draw has none to give.
+ src += " @location(8) @interpolate(flat) v_lightLayer : f32,\n";
}
// A mesh that HAS normals hands them on, with the full world position a
// tangent frame needs. v_worldPos stays vec2 so one fragment stage serves
@@ -328,6 +330,9 @@ std::string canonicalVertexStageWGSL(bool lit, const std::string& hook = {}) {
"#ifdef MESH_TANGENTS\n"
" @location(4) a_tangent : vec4f,\n"
"#endif\n"
+ + std::string(lit ? "#if !defined(MESH) && !defined(SKINNED)\n"
+ " @location(4) a_lightLayer : f32,\n"
+ "#endif\n" : "") +
"#ifdef SKINNED\n"
" @location(5) a_joints : vec4u,\n"
" @location(6) a_weights : vec4f,\n"
@@ -427,6 +432,7 @@ std::string canonicalVertexStageWGSL(bool lit, const std::string& hook = {}) {
"#endif\n";
if (lit) {
src += " out.v_worldPos = world.xy;\n";
+ src += "#if !defined(MESH) && !defined(SKINNED) && !defined(PARTICLE)\n out.v_lightLayer = v.a_lightLayer;\n#else\n out.v_lightLayer = 0.0;\n#endif\n";
// Declared by wgslVSOut under the same condition, so filled here —
// a slot left at zero makes every instance read the first probe.
src += " out.v_probeSlot = f32(instanceId);\n";
@@ -522,6 +528,10 @@ std::string canonicalVertexStage(bool lit, const std::string& hook = {}) {
"#ifdef MESH_TANGENTS\n"
"layout(location = 4) in vec4 a_tangent;\n"
"#endif\n"
+ // The batch layout carries a sprite's layer in the lane text spends on its outline (see SPRITE_LAYER_BIAS); a lit batch draw reads it.
+ + std::string(lit ? "#if !defined(MESH) && !defined(SKINNED)\n"
+ "layout(location = 4) in float a_lightLayer;\n"
+ "#endif\n" : "") +
"#ifdef SKINNED\n"
"layout(location = 5) in uvec4 a_joints;\n"
"layout(location = 6) in vec4 a_weights;\n"
@@ -570,6 +580,8 @@ std::string canonicalVertexStage(bool lit, const std::string& hook = {}) {
"#endif\n";
if (lit) {
src += "out highp vec2 v_worldPos;\n";
+ // The layer the sprite is on (plus 129), or 0 where the draw has none to give. Flat: it is a label, not a gradient.
+ src += "flat out highp float v_lightLayer;\n";
// Which run of coefficients THIS instance reads. The fragment stage has
// no instance index, and the draw's own base is the same for every
// instance in a merged run — so the one stage that can say it, says it.
@@ -653,6 +665,7 @@ std::string canonicalVertexStage(bool lit, const std::string& hook = {}) {
"#endif\n";
if (lit) {
src += " v_worldPos = world.xy;\n";
+ src += "#if !defined(MESH) && !defined(SKINNED) && !defined(PARTICLE)\n v_lightLayer = a_lightLayer;\n#else\n v_lightLayer = 0.0;\n#endif\n";
// The instance's own run of probe coefficients — declared by the Lit
// vertex header, so filled under the same condition.
src += " v_probeSlot = float(ES_INSTANCE_ID);\n";
@@ -1260,7 +1273,7 @@ fn findSun() -> Sun {
if (l.posDir.z > 0.5 && l.posDir.z < 1.5 && l.color.a > 0.0) {
s.dir = vec4f(-normalize(vec3f(l.posDir.x, l.posDir.y, l.posDir.w)), 1.0);
s.color = l.color;
- s.tiles = vec2i(i32(l.shadowMap.x), i32(l.shadowMap.y));
+ s.tiles = vec2i(i32(l.shadowMap.x), i32(l.shadowMap.y % 8.0));
return s;
}
}
@@ -2045,6 +2058,17 @@ fn applyLightingPBR(albedo : vec3f, N : vec3f, worldPos : vec3f, V : vec3f, meta
return applyLightingSurface(albedo, N, worldPos, V, metallic, roughness, specular, ao, surfaceExtNone(N));
}
struct LightTerm { lit : vec3f, gloss : vec3f, extra : vec3f };
+// The layer of the sprite being lit, as its vertex wrote it (the layer plus 129; 0 when the surface has none to test), and whether a light's
+// range of layers, which rides in shadowMap.y above the tile count, takes it in. See packLightLayerRange.
+var<private> g_surfaceLayer : f32 = 0.0;
+fn lightReachesLayer(rangeLane : f32) -> bool {
+ if (rangeLane < 8.0 || g_surfaceLayer < 0.5) { return true; }
+ let range = floor(rangeLane / 8.0);
+ let lo = range % 256.0 - 128.0;
+ let hi = 127.0 - floor(range / 256.0);
+ let layer = g_surfaceLayer - 129.0;
+ return layer >= lo && layer <= hi;
+}
fn shadeLight(pd : vec4f, col : vec4f, sp : vec4f, sh : vec4f, sm : vec4f, fo : vec4f, N : vec3f, V : vec3f,
worldPos : vec3f, a : f32, F0 : vec3f, NdotV : f32, specular : f32, roughness : f32,
ext : SurfaceExt, shadow3D : bool) -> LightTerm {
@@ -2052,7 +2076,7 @@ fn shadeLight(pd : vec4f, col : vec4f, sp : vec4f, sh : vec4f, sm : vec4f, fo :
var gloss = vec3f(0.0);
var extra = vec3f(0.0);
// A slot nothing lights from adds exactly nothing (see the GLSL twin).
- if (col.a == 0.0) { return LightTerm(lit, gloss, extra); }
+ if (col.a == 0.0 || !lightReachesLayer(sm.y)) { return LightTerm(lit, gloss, extra); }
{
var L : vec3f;
var atten : f32;
@@ -2084,9 +2108,9 @@ fn shadeLight(pd : vec4f, col : vec4f, sp : vec4f, sh : vec4f, sm : vec4f, fo :
atten *= mix(1.0, shadowFactor2D(sm.w, worldPos.xy), fo.z);
}
// See the GLSL twin: a light the tiles carry has no shadow map, and says so.
- if (shadow3D && sm.y > 0.0 && atten > 0.0) {
+ if (shadow3D && sm.y % 8.0 > 0.0 && atten > 0.0) {
atten *= mix(1.0,
- shadowFactor3D(worldPos, N, L, vec2f(sh.x, sm.z), i32(sm.x), i32(sm.y)),
+ shadowFactor3D(worldPos, N, L, vec2f(sh.x, sm.z), i32(sm.x), i32(sm.y % 8.0)),
fo.z);
}
var ndotl = max(dot(N, L), 0.0);
@@ -2176,6 +2200,11 @@ fn applyLighting2DAO(albedo : vec3f, N : vec3f, worldPos : vec2f, ao : f32) -> v
fn applyLighting2D(albedo : vec3f, N : vec3f, worldPos : vec2f) -> vec3f {
return applyLighting2DAO(albedo, N, worldPos, 1.0);
}
+// The same, for a sprite that knows its layer: layerLane is the varying the vertex stage wrote (v_lightLayer).
+fn applyLighting2DLayer(albedo : vec3f, N : vec3f, worldPos : vec2f, layerLane : f32) -> vec3f {
+ g_surfaceLayer = layerLane;
+ return applyLighting2DAO(albedo, N, worldPos, 1.0);
+}
)";
// Blanks a hand-written `layout(std140) uniform <block> {...};` out of a GLSL
@@ -3261,6 +3290,17 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" return clamp(F * mix(vec3(1.0), tint * 2.0, film.x), vec3(0.0), vec3(1.0));\n"
"}\n"
// The body of the per-light loop, a function so that the lights the tiles carry are shaded by the same code as the sixteen.
+ // The layer of the sprite being lit, as its vertex wrote it (the layer plus 129; 0 when the surface has none to test), and
+ // whether a light's range of layers, which rides in shadowMap.y above the tile count, takes it in. See packLightLayerRange.
+ "highp float g_surfaceLayer = 0.0;\n"
+ "bool lightReachesLayer(in highp float rangeLane) {\n"
+ " if (rangeLane < 8.0 || g_surfaceLayer < 0.5) return true;\n"
+ " highp float range = floor(rangeLane / 8.0);\n"
+ " highp float lo = mod(range, 256.0) - 128.0;\n"
+ " highp float hi = 127.0 - floor(range / 256.0);\n"
+ " highp float layer = g_surfaceLayer - 129.0;\n"
+ " return layer >= lo && layer <= hi;\n"
+ "}\n"
"void shadeLight(in highp vec4 pd, in highp vec4 col, in highp vec4 sp, in highp vec4 sh, in highp vec4 sm,\n"
" in highp vec4 fo, in highp vec3 N, in highp vec3 V, in highp vec3 worldPos,\n"
" in highp float a, in highp vec3 F0, in highp float NdotV, in highp float specular,\n"
@@ -3369,7 +3409,7 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" inout highp vec3 extra, in bool shadow3D) {\n"
// A slot nothing lights from (the block holds sixteen and a scene has one or two): every term below is its colour times
// its intensity, so it adds exactly nothing, but the lobe is worked out for it all the same without this.
- " if (col.a == 0.0) return;\n"
+ " if (col.a == 0.0 || !lightReachesLayer(sm.y)) return;\n"
" highp vec3 L;\n"
" highp float atten;\n"
" highp vec2 target = pd.xy;\n" // shadow-ray aim point (light position by default)
@@ -3408,9 +3448,9 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
// seen from somewhere no light reaching it is standing.
// A light the tiles carry has no shadow map (the sixteen are where the lights that cast are), so its call says so and the compiler
// drops the lookup from that copy of this function: a program holds the whole shadow filter once, not twice.
- " if (shadow3D && sm.y > 0.0 && atten > 0.0) {\n"
+ " if (shadow3D && mod(sm.y, 8.0) > 0.0 && atten > 0.0) {\n"
" atten *= mix(1.0, shadowFactor3D(worldPos, N, L, vec2(sh.x, sm.z),\n"
- " int(sm.x), int(sm.y)), fo.z);\n"
+ " int(sm.x), int(mod(sm.y, 8.0))), fo.z);\n"
" }\n"
" highp float ndotl = max(dot(N, L), 0.0);\n"
// Hair (misc.y above zero; aniso.xyz is the strand): Kajiya-Kay diffuse, which lights a strand by how far light is from along it.
@@ -3495,6 +3535,11 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
"}\n"
"highp vec3 applyLighting2D(highp vec3 albedo, highp vec3 N, highp vec2 worldPos) {\n"
" return applyLighting2DAO(albedo, N, worldPos, 1.0);\n"
+ "}\n"
+ // The same, for a sprite that knows its layer: layerLane is the varying the vertex stage wrote (v_lightLayer).
+ "highp vec3 applyLighting2DLayer(highp vec3 albedo, highp vec3 N, highp vec2 worldPos, highp float layerLane) {\n"
+ " g_surfaceLayer = layerLane;\n"
+ " return applyLighting2DAO(albedo, N, worldPos, 1.0);\n"
"}\n";
// Emitted only for a variant that has the feature, not behind an #ifdef:
// a varying declared in dead text still reads as one to a tool that
@@ -3553,7 +3598,7 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" if (l.posDir.z > 0.5 && l.posDir.z < 1.5 && l.color.a > 0.0) {\n"
" dir = vec4(-normalize(vec3(l.posDir.x, l.posDir.y, l.posDir.w)), 1.0);\n"
" color = l.color;\n"
- " tiles = ivec2(int(l.shadowMap.x), int(l.shadowMap.y));\n"
+ " tiles = ivec2(int(l.shadowMap.x), int(mod(l.shadowMap.y, 8.0)));\n"
" return;\n"
" }\n"
" }\n"
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 7fedf747d..15f489eef 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -406,6 +406,14 @@ target_include_directories(test_tiled_lights PRIVATE
)
add_test(NAME test_tiled_lights COMMAND test_tiled_lights)
+# The range of sprite layers a light reaches, and the lane it rides in. Header-only, so it runs without a device.
+add_executable(test_light_layers renderer/test_light_layers.cpp)
+target_include_directories(test_light_layers PRIVATE
+ ${CMAKE_SOURCE_DIR}/src
+ ${CMAKE_SOURCE_DIR}/third_party/glm
+)
+add_test(NAME test_light_layers COMMAND test_light_layers)
+
# Which baked column a point reflects. Header-only, so it runs without a device —
# the pixel gate says a surface came out the wrong colour, this says whether the
# lookup took the wrong box or let two bakes into one frame.
diff --git a/tests/aot/generated/conformance/handshake.h b/tests/aot/generated/conformance/handshake.h
index e61dc9f7e..e9152eced 100644
--- a/tests/aot/generated/conformance/handshake.h
+++ b/tests/aot/generated/conformance/handshake.h
@@ -1,5 +1,5 @@
/* GENERATED by sdk/tests/aot-conformance.test.ts — do not edit. */
/* The contract half of the handshake. The width half is the harness. */
-#define ES_CONF_EXPECTED_CONTRACT_HASH 0x239e7f83ac19d562ULL
+#define ES_CONF_EXPECTED_CONTRACT_HASH 0xc74e6f71a7edb276ULL
/* And which BUILD the artifact beside this one is. */
#define ES_CONF_EXPECTED_MODULE_CONTRACT 0xc3d0e057d5701a2eULL
diff --git a/tests/aot/generated/conformance/systems_decl.c b/tests/aot/generated/conformance/systems_decl.c
index 26b154cce..b4c73706b 100644
--- a/tests/aot/generated/conformance/systems_decl.c
+++ b/tests/aot/generated/conformance/systems_decl.c
@@ -10,7 +10,7 @@
answer, it is a read of a different field. The address width belongs
to the MACHINE, so it mixes in here rather than in the compiler and a
32-bit artifact cannot load into a 64-bit host. */
-#define ES_ABI_ENGINE_DIGEST 0x239e7f83ac19d562ULL
+#define ES_ABI_ENGINE_DIGEST 0xc74e6f71a7edb276ULL
ES_EXPORT const uint64_t es_abi_hash =
ES_ABI_ENGINE_DIGEST ^ (0x9e3779b97f4a7c15ULL * (uint64_t)sizeof(es_addr_t));
diff --git a/tests/aot/generated/move_system_decl.c b/tests/aot/generated/move_system_decl.c
index a056527a2..62af2fc5e 100644
--- a/tests/aot/generated/move_system_decl.c
+++ b/tests/aot/generated/move_system_decl.c
@@ -10,7 +10,7 @@
answer, it is a read of a different field. The address width belongs
to the MACHINE, so it mixes in here rather than in the compiler and a
32-bit artifact cannot load into a 64-bit host. */
-#define ES_ABI_ENGINE_DIGEST 0x239e7f83ac19d562ULL
+#define ES_ABI_ENGINE_DIGEST 0xc74e6f71a7edb276ULL
ES_EXPORT const uint64_t es_abi_hash =
ES_ABI_ENGINE_DIGEST ^ (0x9e3779b97f4a7c15ULL * (uint64_t)sizeof(es_addr_t));
diff --git a/tests/aot/generated/move_system_hash.h b/tests/aot/generated/move_system_hash.h
index 593e80414..dc8b32bf0 100644
--- a/tests/aot/generated/move_system_hash.h
+++ b/tests/aot/generated/move_system_hash.h
@@ -1,6 +1,6 @@
/* GENERATED by @estella/compiler — do not edit. */
/* The contract half of the handshake. The width half is the harness. */
-#define ES_EXPECTED_CONTRACT_HASH 0x239e7f83ac19d562ULL
+#define ES_EXPECTED_CONTRACT_HASH 0xc74e6f71a7edb276ULL
/* And which BUILD the artifact beside this one is, which the sidecar
written with it has to carry too. */
#define ES_EXPECTED_MODULE_CONTRACT 0x10bf77bb714ea1e3ULL
diff --git a/tests/renderer/test_light_layers.cpp b/tests/renderer/test_light_layers.cpp
new file mode 100644
index 000000000..0a6db8afe
--- /dev/null
+++ b/tests/renderer/test_light_layers.cpp
@@ -0,0 +1,95 @@
+// The range of sprite layers a light reaches rides in GpuLight::shadowMap.y above the tile count, and the shader unpacks it.
+// The contract: zero is every layer, the tile count survives the atlas writing it, a range comes back as its layers.
+// reaches() below is the shader's decode, line for line.
+
+#include "esengine/renderer/store/LightStore.hpp"
+
+#include <cmath>
+#include <cstdio>
+
+using namespace esengine;
+
+static int g_failures = 0;
+#define CHECK(cond, msg) \
+ do { \
+ if (!(cond)) { std::printf("FAIL: %s\n", msg); ++g_failures; } \
+ else { std::printf("ok: %s\n", msg); } \
+ } while (0)
+
+namespace {
+
+/// What lightReachesLayer() in ShaderParser.cpp answers for a light whose shadowMap.y is `lane` and a sprite on `layer` (-1000 = no layer to test).
+bool reaches(f32 lane, i32 layer) {
+ const f32 surface = layer == -1000 ? 0.0f : static_cast<f32>(layer) + SPRITE_LAYER_BIAS;
+ if (lane < LIGHT_TILE_BITS || surface < 0.5f) return true;
+ const f32 range = std::floor(lane / LIGHT_TILE_BITS);
+ const f32 lo = std::fmod(range, 256.0f) - 128.0f;
+ const f32 hi = 127.0f - std::floor(range / 256.0f);
+ const f32 l = surface - SPRITE_LAYER_BIAS;
+ return l >= lo && l <= hi;
+}
+
+f32 laneOf(i32 layerMin, i32 layerMax, u32 tiles = 0) {
+ return LIGHT_TILE_BITS * packLightLayerRange(layerMin, layerMax) + static_cast<f32>(tiles);
+}
+
+} // namespace
+
+int main() {
+ // --- zero is every layer: a light nobody gave a range is as it always was
+ CHECK(packLightLayerRange(-128, 127) == 0.0f, "the default range packs to zero");
+ CHECK(reaches(0.0f, 0) && reaches(0.0f, 127) && reaches(0.0f, -128), "zero reaches every layer");
+ CHECK(reaches(laneOf(-128, 127, 5), 7), "a light that owns shadow tiles but has no range still reaches every layer");
+
+ // --- a range reaches the layers in it and no others
+ {
+ const f32 lane = laneOf(-128, 6);
+ CHECK(reaches(lane, 6) && reaches(lane, 0) && reaches(lane, -128), "a lamp up to layer 6 reaches layers 6, 0 and -128");
+ CHECK(!reaches(lane, 7) && !reaches(lane, 100), "and not the roof on layer 7, nor anything above it");
+ }
+ {
+ const f32 lane = laneOf(3, 5);
+ CHECK(!reaches(lane, 2) && reaches(lane, 3) && reaches(lane, 4) && reaches(lane, 5) && !reaches(lane, 6), "a band of layers 3 to 5 takes those and only those");
+ }
+ CHECK(reaches(laneOf(0, 0), 0) && !reaches(laneOf(0, 0), 1) && !reaches(laneOf(0, 0), -1), "a single layer is a single layer");
+
+ // --- every layer a sprite can be on round-trips through the lane
+ {
+ bool ok = true;
+ for (i32 l = -128; l <= 127; ++l) {
+ if (!reaches(laneOf(l, l), l)) ok = false;
+ if (l > -128 && reaches(laneOf(l, l), l - 1)) ok = false;
+ if (l < 127 && reaches(laneOf(l, l), l + 1)) ok = false;
+ }
+ CHECK(ok, "a range of one layer reaches that layer and neither neighbour, for every layer there is");
+ }
+
+ // --- a surface with no layer to test is lit by every light
+ CHECK(reaches(laneOf(0, 0), -1000), "a mesh, a tilemap or a skeleton carries no layer and is lit by a ranged light as it always was");
+
+ // --- out-of-range authoring is clamped, not wrapped
+ CHECK(packLightLayerRange(-9999, 9999) == 0.0f, "a range wider than a byte is every layer, not a wrapped one");
+ CHECK(reaches(laneOf(500, 500), 127) && !reaches(laneOf(500, 500), 126), "a range above the top clamps to the top layer");
+
+ // --- the lane stays exact in a float: the biggest packed value is below 2^24
+ CHECK(laneOf(127, -128, 7) < 16777216.0f, "the largest lane fits a float exactly");
+
+ // --- the atlas names its tiles without disturbing the range
+ {
+ LightStore store;
+ GpuLight light;
+ light.shadowMap.y = LIGHT_TILE_BITS * packLightLayerRange(-128, 6);
+ store.addLight(light);
+ store.setLightShadowTiles(0, 4, 6);
+ const f32 lane = store.data().lights[0].shadowMap.y;
+ CHECK(std::fmod(lane, LIGHT_TILE_BITS) == 6.0f, "the tile count is the low bits after the atlas writes it");
+ CHECK(store.data().lights[0].shadowMap.x == 4.0f, "and the first tile is as named");
+ CHECK(reaches(lane, 6) && !reaches(lane, 7), "and the range the light carried above it is still there");
+ store.setLightShadowTiles(0, 0, 0);
+ CHECK(std::fmod(store.data().lights[0].shadowMap.y, LIGHT_TILE_BITS) == 0.0f && !reaches(store.data().lights[0].shadowMap.y, 7),
+ "a light whose map is taken away keeps its range");
+ }
+
+ std::printf("%s\n", g_failures == 0 ? "all passed" : "FAILED");
+ return g_failures == 0 ? 0 : 1;
+}
diff --git a/tools/check-corpus-authoring.mjs b/tools/check-corpus-authoring.mjs
index c5d82235c..101f52e9f 100644
--- a/tools/check-corpus-authoring.mjs
+++ b/tools/check-corpus-authoring.mjs
@@ -136,6 +136,9 @@ const DISPOSITION = {
// — owed: a creator-facing component nothing in the shipped corpus authors.
// Each reason names where the coverage belongs. —
BlobShadow: ['owed', 'only the blob-shadow render fixture places one; an example for a phone-scale scene that wants a shadow without a shadow map is owed'],
+ CoverFade: ['owed', 'only the rainforest-2d probe, kept out of the examples, places one; an example of a top-down hut whose roof thins as the player walks in is owed'],
+ CoverFadeTarget: ['owed', 'the tag that goes with CoverFade, and owed with it'],
+ RainShelter: ['owed', 'only the rainforest-2d probe, kept out of the examples, places one; it arrives with the top-down rain example CoverFade is owed'],
StaticBatch: ['owed', 'no corpus scene marks a mesh yet: a marked mesh is merged while the game runs, which a golden project has no frame to certify — the static-batch-on / static-batch-off render scenes, tests/static-batch.test.ts and the static-batch editor check hold it'],
};
diff --git a/tools/check-shader-blocks.mjs b/tools/check-shader-blocks.mjs
index e95b9b8e4..d136d2f73 100644
--- a/tools/check-shader-blocks.mjs
+++ b/tools/check-shader-blocks.mjs
@@ -220,7 +220,7 @@ function helpers(pattern) {
return out;
}
-const glslFns = helpers(/^\s*(?:highp\s+)?(?:void|vec[234]|float|mat[234]|SurfaceExt)\s+(\w+)\s*\(([^)]*)\)\s*\{/gm);
+const glslFns = helpers(/^\s*(?:highp\s+)?(?:void|bool|vec[234]|float|mat[234]|SurfaceExt)\s+(\w+)\s*\(([^)]*)\)\s*\{/gm);
const wgslFns = helpers(/\bfn\s+(\w+)\s*\(([^)]*)\)\s*->/g);
const shape = (counts) => counts.length === 1 ? `${counts[0]} argument(s)`