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(render): soft + directional 2D shadows (T3)

yhhecc6c0bfad27
diff --git a/desktop/public/scenes/mat-lit-shadow-dir.esscene b/desktop/public/scenes/mat-lit-shadow-dir.esscene
new file mode 100644
index 000000000..f1e768503
--- /dev/null
+++ b/desktop/public/scenes/mat-lit-shadow-dir.esscene
@@ -0,0 +1,67 @@
+{
+ "version": "1.0",
+ "name": "Main",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 10 } } },
+ { "type": "Camera", "data": { "projectionType": 1, "orthoSize": 300, "isActive": true, "priority": 0 } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 1,
+ "name": "Lit",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 0 } } },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": { "x": 400, "y": 400 },
+ "color": { "r": 1, "g": 1, "b": 1, "a": 1 },
+ "material": "/scenes/mat-lit/lit.esmaterial"
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 2,
+ "name": "GreenSun",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 0 } } },
+ {
+ "type": "Light2D",
+ "data": {
+ "type": 1,
+ "color": { "r": 0, "g": 1, "b": 0, "a": 1 },
+ "intensity": 1,
+ "direction": { "x": 1, "y": 0 },
+ "shadowDistance": 600,
+ "shadowSoftness": 0
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 3,
+ "name": "Wall",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 0 } } },
+ { "type": "ShadowCaster2D", "data": { "size": { "x": 40, "y": 120 }, "enabled": true } }
+ ],
+ "visible": true
+ }
+ ]
+}
diff --git a/desktop/public/scenes/mat-lit-shadow-dir.textures.json b/desktop/public/scenes/mat-lit-shadow-dir.textures.json
new file mode 100644
index 000000000..0967ef424
--- /dev/null
+++ b/desktop/public/scenes/mat-lit-shadow-dir.textures.json
@@ -0,0 +1 @@
+{}
diff --git a/desktop/public/scenes/mat-lit-shadow-hard.esscene b/desktop/public/scenes/mat-lit-shadow-hard.esscene
new file mode 100644
index 000000000..3e53f5bbf
--- /dev/null
+++ b/desktop/public/scenes/mat-lit-shadow-hard.esscene
@@ -0,0 +1,66 @@
+{
+ "version": "1.0",
+ "name": "Main",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 10 } } },
+ { "type": "Camera", "data": { "projectionType": 1, "orthoSize": 300, "isActive": true, "priority": 0 } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 1,
+ "name": "Lit",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 0 } } },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": { "x": 400, "y": 400 },
+ "color": { "r": 1, "g": 1, "b": 1, "a": 1 },
+ "material": "/scenes/mat-lit/lit.esmaterial"
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 2,
+ "name": "GreenPointLight",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": -150, "y": 0, "z": 0 } } },
+ {
+ "type": "Light2D",
+ "data": {
+ "type": 0,
+ "color": { "r": 0, "g": 1, "b": 0, "a": 1 },
+ "intensity": 1,
+ "radius": 800,
+ "shadowSoftness": 0
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 3,
+ "name": "Wall",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 0 } } },
+ { "type": "ShadowCaster2D", "data": { "size": { "x": 40, "y": 120 }, "enabled": true } }
+ ],
+ "visible": true
+ }
+ ]
+}
diff --git a/desktop/public/scenes/mat-lit-shadow-hard.textures.json b/desktop/public/scenes/mat-lit-shadow-hard.textures.json
new file mode 100644
index 000000000..0967ef424
--- /dev/null
+++ b/desktop/public/scenes/mat-lit-shadow-hard.textures.json
@@ -0,0 +1 @@
+{}
diff --git a/desktop/public/scenes/mat-lit-shadow-soft.esscene b/desktop/public/scenes/mat-lit-shadow-soft.esscene
new file mode 100644
index 000000000..1c474c399
--- /dev/null
+++ b/desktop/public/scenes/mat-lit-shadow-soft.esscene
@@ -0,0 +1,66 @@
+{
+ "version": "1.0",
+ "name": "Main",
+ "entities": [
+ {
+ "id": 0,
+ "name": "Camera",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 10 } } },
+ { "type": "Camera", "data": { "projectionType": 1, "orthoSize": 300, "isActive": true, "priority": 0 } }
+ ],
+ "visible": true
+ },
+ {
+ "id": 1,
+ "name": "Lit",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 0 } } },
+ {
+ "type": "Sprite",
+ "data": {
+ "size": { "x": 400, "y": 400 },
+ "color": { "r": 1, "g": 1, "b": 1, "a": 1 },
+ "material": "/scenes/mat-lit/lit.esmaterial"
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 2,
+ "name": "GreenPointLight",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": -150, "y": 0, "z": 0 } } },
+ {
+ "type": "Light2D",
+ "data": {
+ "type": 0,
+ "color": { "r": 0, "g": 1, "b": 0, "a": 1 },
+ "intensity": 1,
+ "radius": 800,
+ "shadowSoftness": 50
+ }
+ }
+ ],
+ "visible": true
+ },
+ {
+ "id": 3,
+ "name": "Wall",
+ "parent": null,
+ "children": [],
+ "components": [
+ { "type": "Transform", "data": { "position": { "x": 0, "y": 0, "z": 0 } } },
+ { "type": "ShadowCaster2D", "data": { "size": { "x": 40, "y": 120 }, "enabled": true } }
+ ],
+ "visible": true
+ }
+ ]
+}
diff --git a/desktop/public/scenes/mat-lit-shadow-soft.textures.json b/desktop/public/scenes/mat-lit-shadow-soft.textures.json
new file mode 100644
index 000000000..0967ef424
--- /dev/null
+++ b/desktop/public/scenes/mat-lit-shadow-soft.textures.json
@@ -0,0 +1 @@
+{}
diff --git a/sdk/src/component.generated.ts b/sdk/src/component.generated.ts
index c5c3aafc9..a95445fb5 100644
--- a/sdk/src/component.generated.ts
+++ b/sdk/src/component.generated.ts
@@ -13,7 +13,7 @@ import type { FieldMeta } from './component';
* getAbiLayoutHash(); BuiltinBridge.connect() compares them and refuses to
* run on mismatch, because mismatched offsets read the wrong heap bytes.
*/
-export const ABI_LAYOUT_HASH = '7ac6cfeb8c0c1963';
+export const ABI_LAYOUT_HASH = 'b77fc377a67f3228';
export interface AssetFieldMeta {
field: string;
@@ -198,12 +198,14 @@ export const COMPONENT_META: Record<string, ComponentMetaEntry> = {
direction: { x: 0, y: 0 },
innerAngle: 30,
outerAngle: 45,
+ shadowSoftness: 0,
+ shadowDistance: 0,
enabled: true,
},
assetFields: [],
entityFields: [],
colorFields: ['color'],
- animatableFields: ['color.r', 'color.g', 'color.b', 'color.a', 'intensity', 'radius', 'innerAngle', 'outerAngle'],
+ animatableFields: ['color.r', 'color.g', 'color.b', 'color.a', 'intensity', 'radius', 'innerAngle', 'outerAngle', 'shadowSoftness', 'shadowDistance'],
fields: {
type: { tooltip: "Point, Directional, Ambient, or Spot." },
intensity: { min: 0, tooltip: "Brightness multiplier of the light." },
@@ -211,6 +213,8 @@ export const COMPONENT_META: Record<string, ComponentMetaEntry> = {
direction: { tooltip: "Aim direction (Directional / Spot).", advanced: true },
innerAngle: { min: 0, max: 180, unit: "°", advanced: true },
outerAngle: { min: 0, max: 180, unit: "°", advanced: true },
+ shadowSoftness: { min: 0, tooltip: "Shadow softness (light-source size); 0 = hard edge." },
+ shadowDistance: { min: 0, tooltip: "Directional shadow distance; 0 = no directional shadow.", advanced: true },
},
},
Parent: {
diff --git a/sdk/src/component.ts b/sdk/src/component.ts
index 2833c8721..c3a599403 100644
--- a/sdk/src/component.ts
+++ b/sdk/src/component.ts
@@ -571,6 +571,8 @@ export interface Light2DData {
direction: Vec2;
innerAngle: number;
outerAngle: number;
+ shadowSoftness: number;
+ shadowDistance: number;
enabled: boolean;
}
diff --git a/sdk/src/ecs/ptrAccessors.generated.ts b/sdk/src/ecs/ptrAccessors.generated.ts
index b20ba2aa0..57232bd4b 100644
--- a/sdk/src/ecs/ptrAccessors.generated.ts
+++ b/sdk/src/ecs/ptrAccessors.generated.ts
@@ -413,6 +413,8 @@ export interface Light2DPtrData {
direction: Vec2;
innerAngle: number;
outerAngle: number;
+ shadowSoftness: number;
+ shadowDistance: number;
enabled: boolean;
}
@@ -427,7 +429,9 @@ export function fillLight2D(
const direction_ = out.direction; direction_.x = f32[(ptr + 28) >> 2]; direction_.y = f32[((ptr + 28) >> 2) + 1];
out.innerAngle = f32[(ptr + 36) >> 2];
out.outerAngle = f32[(ptr + 40) >> 2];
- out.enabled = u8[ptr + 44] !== 0;
+ out.shadowSoftness = f32[(ptr + 44) >> 2];
+ out.shadowDistance = f32[(ptr + 48) >> 2];
+ out.enabled = u8[ptr + 52] !== 0;
}
export function writeLight2D(
@@ -441,7 +445,9 @@ export function writeLight2D(
f32[(ptr + 28) >> 2] = data.direction.x; f32[((ptr + 28) >> 2) + 1] = data.direction.y;
f32[(ptr + 36) >> 2] = data.innerAngle;
f32[(ptr + 40) >> 2] = data.outerAngle;
- u8[ptr + 44] = data.enabled ? 1 : 0;
+ f32[(ptr + 44) >> 2] = data.shadowSoftness;
+ f32[(ptr + 48) >> 2] = data.shadowDistance;
+ u8[ptr + 52] = data.enabled ? 1 : 0;
}
export function createLight2DData(): Light2DPtrData {
@@ -453,6 +459,8 @@ export function createLight2DData(): Light2DPtrData {
direction: { x: 0, y: 0 },
innerAngle: 0,
outerAngle: 0,
+ shadowSoftness: 0,
+ shadowDistance: 0,
enabled: false,
};
}
diff --git a/sdk/src/ptrLayouts.generated.ts b/sdk/src/ptrLayouts.generated.ts
index d6a46bb35..513d3bbf4 100644
--- a/sdk/src/ptrLayouts.generated.ts
+++ b/sdk/src/ptrLayouts.generated.ts
@@ -127,7 +127,9 @@ export const PTR_LAYOUTS: Record<string, PtrLayout> = {
{ name: 'direction', type: 'vec2', offset: 28 },
{ name: 'innerAngle', type: 'f32', offset: 36 },
{ name: 'outerAngle', type: 'f32', offset: 40 },
- { name: 'enabled', type: 'bool', offset: 44 },
+ { name: 'shadowSoftness', type: 'f32', offset: 44 },
+ { name: 'shadowDistance', type: 'f32', offset: 48 },
+ { name: 'enabled', type: 'bool', offset: 52 },
],
},
ParticleEmitter: {
diff --git a/sdk/src/wasm.generated.ts b/sdk/src/wasm.generated.ts
index cb07af144..17684cf0e 100644
--- a/sdk/src/wasm.generated.ts
+++ b/sdk/src/wasm.generated.ts
@@ -243,6 +243,8 @@ export interface Light2D {
direction: Vec2;
innerAngle: number;
outerAngle: number;
+ shadowSoftness: number;
+ shadowDistance: number;
enabled: boolean;
}
diff --git a/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap b/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
index 2cde7f0ce..3950c14f1 100644
--- a/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
+++ b/sdk/tests/__snapshots__/component-fieldmeta-golden.test.ts.snap
@@ -127,6 +127,15 @@ exports[`builtin FieldMeta (RC9-1 equivalence gate) > matches the golden snapsho
"min": 0,
"tooltip": "Falloff reach in world units (Point / Spot).",
},
+ "shadowDistance": {
+ "advanced": true,
+ "min": 0,
+ "tooltip": "Directional shadow distance; 0 = no directional shadow.",
+ },
+ "shadowSoftness": {
+ "min": 0,
+ "tooltip": "Shadow softness (light-source size); 0 = hard edge.",
+ },
"type": {
"enum": [
{
diff --git a/src/esengine/bindings/EditorAPI.generated.cpp b/src/esengine/bindings/EditorAPI.generated.cpp
index 28dcf194c..46b6af222 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 15f44ddaf4bd00ac
+ * @schema_hash 97c7defd8d289607
*/
-#define ES_EDITOR_API_SCHEMA_HASH "15f44ddaf4bd00ac"
+#define ES_EDITOR_API_SCHEMA_HASH "97c7defd8d289607"
#include <emscripten/bind.h>
#include <string>
@@ -206,6 +206,8 @@ std::string editor_getComponentSchema(const std::string& name) {
{"key":"direction.y","type":"float","group":"Light2D"},
{"key":"innerAngle","type":"float","group":"Light2D"},
{"key":"outerAngle","type":"float","group":"Light2D"},
+ {"key":"shadowSoftness","type":"float","group":"Light2D"},
+ {"key":"shadowDistance","type":"float","group":"Light2D"},
{"key":"enabled","type":"bool","group":"Light2D"}
])JSON";
} else if (name == "Parent") {
@@ -800,6 +802,8 @@ bool editor_setFloat(Registry& reg, u32 e, const std::string& comp, const std::s
else if (field == "direction.y") { c.direction.y = value; }
else if (field == "innerAngle") { c.innerAngle = value; }
else if (field == "outerAngle") { c.outerAngle = value; }
+ else if (field == "shadowSoftness") { c.shadowSoftness = value; }
+ else if (field == "shadowDistance") { c.shadowDistance = value; }
else { return false; }
return true;
} else if (comp == "ParticleEmitter") {
@@ -1117,6 +1121,8 @@ f32 editor_getFloat(Registry& reg, u32 e, const std::string& comp, const std::st
else if (field == "direction.y") { return c.direction.y; }
else if (field == "innerAngle") { return c.innerAngle; }
else if (field == "outerAngle") { return c.outerAngle; }
+ else if (field == "shadowSoftness") { return c.shadowSoftness; }
+ else if (field == "shadowDistance") { return c.shadowDistance; }
} else if (comp == "ParticleEmitter") {
if (!reg.has<esengine::ecs::ParticleEmitter>(entity)) return 0.0f;
const auto& c = reg.get<esengine::ecs::ParticleEmitter>(entity);
diff --git a/src/esengine/bindings/WebBindings.generated.cpp b/src/esengine/bindings/WebBindings.generated.cpp
index 298d27c95..bfc10316e 100644
--- a/src/esengine/bindings/WebBindings.generated.cpp
+++ b/src/esengine/bindings/WebBindings.generated.cpp
@@ -900,6 +900,8 @@ EMSCRIPTEN_BINDINGS(esengine_components) {
.field("direction", &esengine::ecs::Light2D::direction)
.field("innerAngle", &esengine::ecs::Light2D::innerAngle)
.field("outerAngle", &esengine::ecs::Light2D::outerAngle)
+ .field("shadowSoftness", &esengine::ecs::Light2D::shadowSoftness)
+ .field("shadowDistance", &esengine::ecs::Light2D::shadowDistance)
.field("enabled", &esengine::ecs::Light2D::enabled);
value_object<ParentJS>("Parent")
@@ -1788,7 +1790,9 @@ static_assert(offsetof(esengine::ecs::Light2D, radius) == 24, "ABI offset drift:
static_assert(offsetof(esengine::ecs::Light2D, direction) == 28, "ABI offset drift: esengine::ecs::Light2D.direction (EHT expected 28)");
static_assert(offsetof(esengine::ecs::Light2D, innerAngle) == 36, "ABI offset drift: esengine::ecs::Light2D.innerAngle (EHT expected 36)");
static_assert(offsetof(esengine::ecs::Light2D, outerAngle) == 40, "ABI offset drift: esengine::ecs::Light2D.outerAngle (EHT expected 40)");
-static_assert(offsetof(esengine::ecs::Light2D, enabled) == 44, "ABI offset drift: esengine::ecs::Light2D.enabled (EHT expected 44)");
+static_assert(offsetof(esengine::ecs::Light2D, shadowSoftness) == 44, "ABI offset drift: esengine::ecs::Light2D.shadowSoftness (EHT expected 44)");
+static_assert(offsetof(esengine::ecs::Light2D, shadowDistance) == 48, "ABI offset drift: esengine::ecs::Light2D.shadowDistance (EHT expected 48)");
+static_assert(offsetof(esengine::ecs::Light2D, enabled) == 52, "ABI offset drift: esengine::ecs::Light2D.enabled (EHT expected 52)");
static_assert(offsetof(esengine::ecs::ParticleEmitter, rate) == 0, "ABI offset drift: esengine::ecs::ParticleEmitter.rate (EHT expected 0)");
static_assert(offsetof(esengine::ecs::ParticleEmitter, burstCount) == 4, "ABI offset drift: esengine::ecs::ParticleEmitter.burstCount (EHT expected 4)");
static_assert(offsetof(esengine::ecs::ParticleEmitter, burstInterval) == 8, "ABI offset drift: esengine::ecs::ParticleEmitter.burstInterval (EHT expected 8)");
@@ -1923,7 +1927,7 @@ static_assert(offsetof(esengine::ecs::Velocity, angular) == 12, "ABI offset drif
// ABI Hash -- runtime handshake against the SDK bundle
// =============================================================================
-static const char* kEsAbiLayoutHash = "7ac6cfeb8c0c1963";
+static const char* kEsAbiLayoutHash = "b77fc377a67f3228";
std::string esengineGetAbiLayoutHash() {
return std::string(kEsAbiLayoutHash);
diff --git a/src/esengine/ecs/components/Light2D.hpp b/src/esengine/ecs/components/Light2D.hpp
index 059ba1f92..64d9c2964 100644
--- a/src/esengine/ecs/components/Light2D.hpp
+++ b/src/esengine/ecs/components/Light2D.hpp
@@ -77,6 +77,18 @@ struct Light2D {
ES_PROPERTY(animatable, min=0, max=180, unit="°", advanced)
f32 outerAngle{45.0f};
+ /** @brief Shadow penumbra softness = light-source half-extent in world units. 0 = hard-edged
+ * shadow (default; identical to no softening). Larger values widen the penumbra the way
+ * a bigger area light does. Applies to every light type that casts a shadow. */
+ ES_PROPERTY(animatable, min=0, tooltip="Shadow softness (light-source size); 0 = hard edge.")
+ f32 shadowSoftness{0.0f};
+
+ /** @brief Directional-light shadow reach in world units: how far back toward the light a
+ * fragment searches for an occluder. 0 = a Directional light casts no shadow (default).
+ * Ignored by Point/Spot, which shadow along the segment to the light position. */
+ ES_PROPERTY(animatable, min=0, advanced, tooltip="Directional shadow distance; 0 = no directional shadow.")
+ f32 shadowDistance{0.0f};
+
/** @brief Disabled lights are skipped during collection. */
ES_PROPERTY()
bool enabled{true};
diff --git a/src/esengine/renderer/LightConstants.hpp b/src/esengine/renderer/LightConstants.hpp
index bbd3fe8f8..81447501b 100644
--- a/src/esengine/renderer/LightConstants.hpp
+++ b/src/esengine/renderer/LightConstants.hpp
@@ -40,25 +40,29 @@ inline constexpr u32 MAX_LIGHTS_2D = 16;
inline constexpr u32 MAX_OCCLUDERS_2D = 8;
/**
- * @brief One 2D light, std140-packed (three vec4s, 48 bytes, 16-aligned).
+ * @brief One 2D light, std140-packed (four vec4s, 64 bytes, 16-aligned).
* @details posDir: xy = world position (point/spot) or direction (directional); z = type
* (0 = point, 1 = directional, 2 = spot); w = falloff radius (point/spot, world units).
* color: rgb = light color, a = intensity.
* spot: xy = normalized cone axis, z = cos(innerHalfAngle), w = cos(outerHalfAngle)
* (spot only; zero for other types). Ambient lights are folded into
* LightConstants::ambient instead of occupying a slot.
+ * shadow: x = penumbra softness (light-source half-extent in world units; 0 = hard,
+ * backward-compatible); y = directional shadow march distance (world units; 0 = a
+ * directional light casts no shadow). zw reserved (0). Read by es_shadowFactor2D.
*/
struct GpuLight2D {
glm::vec4 posDir{0.0f};
glm::vec4 color{0.0f};
glm::vec4 spot{0.0f};
+ glm::vec4 shadow{0.0f};
};
/**
* @brief CPU mirror of the GLSL LightConstants block (std140).
* @details ambient: rgb = summed ambient color, a = active light count (informational).
- * std140 array-of-struct stride is 32 (each GpuLight2D is already 16-aligned), so
- * lights start at offset 16 and the whole block is 16 + 32*MAX_LIGHTS_2D bytes.
+ * std140 array-of-struct stride is 64 (each GpuLight2D is four 16-aligned vec4s), so
+ * lights start at offset 16 and the lights array spans 64*MAX_LIGHTS_2D bytes.
*/
struct LightConstants {
glm::vec4 ambient{0.0f};
@@ -66,13 +70,14 @@ struct LightConstants {
/// x = active occluder count; yzw unused. Appended after `lights` so existing std140
/// offsets (ambient, lights) are unchanged — old Lit2D shaders keep reading the same bytes.
glm::vec4 occluderCount{0.0f};
- /// World-space AABBs: (minX, minY, maxX, maxY). A point/spot light is shadowed at a fragment
- /// when the fragment→light segment crosses any box.
+ /// World-space AABBs: (minX, minY, maxX, maxY). A light is shadowed at a fragment when the
+ /// fragment→light segment (or, for directional, the fragment→far-along-light-dir segment)
+ /// crosses any box.
glm::vec4 occluders[MAX_OCCLUDERS_2D];
};
-static_assert(sizeof(GpuLight2D) == 48, "GpuLight2D must be std140-tight (three vec4s)");
-static_assert(sizeof(LightConstants) == 16 + 48 * MAX_LIGHTS_2D + 16 + 16 * MAX_OCCLUDERS_2D,
+static_assert(sizeof(GpuLight2D) == 64, "GpuLight2D must be std140-tight (four vec4s)");
+static_assert(sizeof(LightConstants) == 16 + 64 * MAX_LIGHTS_2D + 16 + 16 * MAX_OCCLUDERS_2D,
"LightConstants must match the std140 GLSL block layout");
} // namespace esengine
diff --git a/src/esengine/renderer/RenderFrame.cpp b/src/esengine/renderer/RenderFrame.cpp
index 3336dfba8..458939101 100644
--- a/src/esengine/renderer/RenderFrame.cpp
+++ b/src/esengine/renderer/RenderFrame.cpp
@@ -428,6 +428,10 @@ void RenderFrame::collectLights(ecs::Registry& registry) {
GpuLight2D gpu;
gpu.color = glm::vec4(rgb, light.intensity);
+ // shadow.x = penumbra softness (all types); shadow.y = directional march distance (only the
+ // directional branch of es_shadowFactor2D reads it; 0 keeps directional shadows off).
+ gpu.shadow = glm::vec4(std::max(light.shadowSoftness, 0.0f),
+ std::max(light.shadowDistance, 0.0f), 0.0f, 0.0f);
if (type == ecs::Light2DType::Directional) {
// Direction in the 2D plane; z=1 flags directional (no attenuation) in the shader.
gpu.posDir = glm::vec4(light.direction.x, light.direction.y, 1.0f, 0.0f);
diff --git a/src/esengine/resource/ShaderParser.cpp b/src/esengine/resource/ShaderParser.cpp
index 1b1983911..5cd846d6b 100644
--- a/src/esengine/resource/ShaderParser.cpp
+++ b/src/esengine/resource/ShaderParser.cpp
@@ -426,10 +426,10 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
// carry explicit highp for the same reason MaterialConstants does: a fragment shader has no
// default float precision until its `precision` line, which follows this injected header.
// The light-array size and packing here MUST match renderer/LightConstants.hpp (MAX_LIGHTS_2D,
- // GpuLight2D = two vec4s).
+ // GpuLight2D = four vec4s).
if (stage == ShaderStage::Fragment && parsed.domain == "Lit2D") {
static const char* kLit2DHeader =
- "struct es_Light2D { highp vec4 posDir; highp vec4 color; highp vec4 spot; };\n"
+ "struct es_Light2D { highp vec4 posDir; highp vec4 color; highp vec4 spot; highp vec4 shadow; };\n"
"layout(std140) uniform LightConstants {\n"
" highp vec4 u_ambient;\n"
" es_Light2D u_lights[16];\n"
@@ -466,23 +466,48 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" }\n"
" return 1.0;\n"
"}\n"
- // 0.0 if any occluder blocks the fragment from the light, else 1.0. No occluders
- // (count 0) -> always 1.0, so the shadow test is inert until the render path feeds boxes.
- "highp float es_shadowFactor2D(in highp vec2 worldPos, in highp vec2 lightPos) {\n"
+ // Soft 2D shadows: averages occlusion of K rays cast from the fragment toward points
+ // spread across the light's apparent size (softness = source half-extent, world units).
+ // softness 0 collapses to the single hard-edged centre ray — bit-identical to the prior
+ // behaviour; larger softness widens the penumbra the way an area light's shadow does.
+ // `target` is the point the rays aim at — the light position for point/spot, or a far
+ // point along the light direction for directional — so one primitive shadows every type.
+ // No occluders (count 0) -> always 1.0, inert until the render path feeds boxes.
+ "highp float es_shadowFactor2D(in highp vec2 worldPos, in highp vec2 target, in highp float softness) {\n"
" int n = int(u_occluderCount.x);\n"
- " for (int i = 0; i < 8; ++i) {\n"
- " if (i >= n) break;\n"
- " if (es_segHitsBox(worldPos, lightPos, u_occluders[i]) > 0.5) return 0.0;\n"
+ " if (n <= 0) return 1.0;\n"
+ " if (softness < 1e-4) {\n"
+ " for (int i = 0; i < 8; ++i) {\n"
+ " if (i >= n) break;\n"
+ " if (es_segHitsBox(worldPos, target, u_occluders[i]) > 0.5) return 0.0;\n"
+ " }\n"
+ " return 1.0;\n"
" }\n"
- " return 1.0;\n"
+ " highp vec2 dir = target - worldPos;\n"
+ " highp float dl = length(dir);\n"
+ " highp vec2 perp = (dl > 1e-4) ? vec2(-dir.y, dir.x) / dl : vec2(1.0, 0.0);\n"
+ " const int K = 5;\n"
+ " highp float blocked = 0.0;\n"
+ " for (int s = 0; s < K; ++s) {\n"
+ " highp float t = float(s) / float(K - 1) * 2.0 - 1.0;\n"
+ " highp vec2 tp = target + perp * (t * softness);\n"
+ " for (int i = 0; i < 8; ++i) {\n"
+ " if (i >= n) break;\n"
+ " if (es_segHitsBox(worldPos, tp, u_occluders[i]) > 0.5) { blocked += 1.0; break; }\n"
+ " }\n"
+ " }\n"
+ " return 1.0 - blocked / float(K);\n"
"}\n"
"highp vec3 es_applyLighting2D(highp vec3 albedo, highp vec3 N, highp vec2 worldPos) {\n"
" highp vec3 lit = u_ambient.rgb;\n"
" for (int i = 0; i < 16; ++i) {\n"
" highp vec4 pd = u_lights[i].posDir;\n"
" highp vec4 col = u_lights[i].color;\n"
+ " highp vec4 sh = u_lights[i].shadow;\n" // x = penumbra softness, y = directional distance
" highp vec3 L;\n"
" highp float atten;\n"
+ " highp vec2 target = pd.xy;\n" // shadow-ray aim point (light position by default)
+ " bool castShadow = true;\n"
" if (pd.z < 0.5) {\n"
" highp vec2 d = pd.xy - worldPos;\n"
" highp float dist = length(d);\n"
@@ -491,6 +516,11 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" } else if (pd.z < 1.5) {\n"
" atten = 1.0;\n"
" L = normalize(vec3(-pd.xy, 1.0));\n"
+ // Directional rays are parallel: aim at a far point toward the light source, opt in via
+ // a positive march distance, and require a real direction (a zeroed one casts nothing).
+ " highp vec2 toLight = (dot(pd.xy, pd.xy) > 1e-8) ? normalize(-pd.xy) : vec2(0.0);\n"
+ " castShadow = sh.y > 0.0 && dot(toLight, toLight) > 0.5;\n"
+ " target = worldPos + toLight * sh.y;\n"
" } else {\n"
" highp vec4 sp = u_lights[i].spot;\n"
" highp vec2 d = pd.xy - worldPos;\n"
@@ -500,8 +530,10 @@ ShaderParser::AssembledStage ShaderParser::assembleStageEx(const ParsedShader& p
" highp vec2 toFrag = (dist > 0.0001) ? (-d / dist) : sp.xy;\n"
" atten *= smoothstep(sp.w, sp.z, dot(sp.xy, toFrag));\n"
" }\n"
- " if (pd.z < 0.5 || pd.z >= 1.5) {\n" // point/spot cast shadows; directional skipped
- " atten *= es_shadowFactor2D(worldPos, pd.xy);\n"
+ // Only pay for the shadow test when the light actually reaches this fragment (skips the
+ // zeroed/inactive slots and unlit fragments — cheaper than the old unconditional call).
+ " if (castShadow && col.a > 0.0 && atten > 0.0) {\n"
+ " atten *= es_shadowFactor2D(worldPos, target, sh.x);\n"
" }\n"
" highp float ndotl = max(dot(N, L), 0.0);\n"
" lit += col.rgb * (col.a * ndotl * atten);\n"