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.

docs: 2D Lighting & Shadows guide (en + zh)

esengine629c07dd7f15
diff --git a/docs/astro/astro.config.mjs b/docs/astro/astro.config.mjs
index 90298b7a8..8a49cdb6b 100644
--- a/docs/astro/astro.config.mjs
+++ b/docs/astro/astro.config.mjs
@@ -69,6 +69,7 @@ export default defineConfig({
{ label: 'Particles', translations: { 'zh-CN': '粒子' }, slug: 'guides/particle' },
{ label: 'Post-processing', translations: { 'zh-CN': '后处理' }, slug: 'guides/postprocess' },
{ label: 'Materials & Shaders', translations: { 'zh-CN': '材质与着色器' }, slug: 'guides/material' },
+ { label: '2D Lighting & Shadows', translations: { 'zh-CN': '2D 光照与阴影' }, slug: 'guides/lighting' },
],
},
{
diff --git a/docs/astro/src/content/docs/guides/lighting.mdx b/docs/astro/src/content/docs/guides/lighting.mdx
new file mode 100644
index 000000000..c1a3ec560
--- /dev/null
+++ b/docs/astro/src/content/docs/guides/lighting.mdx
@@ -0,0 +1,174 @@
+---
+title: 2D Lighting & Shadows
+description: Light a 2D scene with directional, point, spot, and ambient Light2D sources — color and intensity, normal-mapped surfaces, and soft shadows cast by ShadowCaster2D — all through the Lit-2D material domain.
+---
+
+import { Aside } from '@astrojs/starlight/components';
+
+Estella lights 2D scenes with **`Light2D`** sources and **`ShadowCaster2D`** occluders.
+A light is just an entity with a `Light2D` component; a renderer receives light only
+when it's drawn with a **Lit-2D material**. This keeps lighting opt-in and cheap — most
+sprites stay unlit, and the ones you want lit get a lit material.
+
+<Aside type="note">
+ Lighting is built into the renderer (no plugin to add). `Light2D` and
+ `ShadowCaster2D` are built-in components — attach them from code or in the editor's
+ Details panel like any other.
+</Aside>
+
+## Adding a light
+
+Give an entity a `Light2D` component and pick a type:
+
+```typescript
+import { defineSystem, Commands, Transform, Light2D, Light2DType } from 'esengine';
+
+const addSun = defineSystem([Commands()], (cmds) => {
+ cmds.spawn()
+ .insert(Transform, { position: { x: 0, y: 0, z: 0 } })
+ .insert(Light2D, {
+ type: Light2DType.Directional,
+ color: { r: 1, g: 0.95, b: 0.8, a: 1 },
+ intensity: 1.2,
+ direction: { x: -1, y: -1 },
+ });
+});
+```
+
+`Light2D` fields (with defaults):
+
+| Field | Default | Description |
+|---|---|---|
+| `type` | `Point` | `Light2DType`: `Point`, `Directional`, `Ambient`, or `Spot`. |
+| `color` | `{1,1,1,1}` | Light color, RGBA 0..1. |
+| `intensity` | `1` | Brightness multiplier. |
+| `radius` | `200` | Falloff reach in world units (Point / Spot). |
+| `direction` | `{0,0}` | Aim direction (Directional / Spot). |
+| `innerAngle` | `30` | Spot cone inner angle, degrees (full brightness inside). |
+| `outerAngle` | `45` | Spot cone outer angle, degrees (falls to zero by here). |
+| `shadowSoftness` | `0` | Shadow edge softness (light-source size); `0` = hard edge. |
+| `shadowDistance` | `0` | Directional shadow reach; `0` = no directional shadow. |
+| `enabled` | `true` | Turn the light off without removing it. |
+
+## Light types
+
+| Type | Behavior |
+|---|---|
+| **Directional** | A uniform light from one `direction`, like the sun — position doesn't matter, only the aim. |
+| **Point** | Radiates from the entity's position and falls off to nothing at `radius`. |
+| **Spot** | A cone from the position along `direction`, full-bright within `innerAngle` and fading to the edge at `outerAngle`, out to `radius`. |
+| **Ambient** | A flat base light added everywhere, regardless of position — use a low `intensity` so shaded areas aren't pure black. |
+
+```typescript
+// A warm point light
+.insert(Light2D, { type: Light2DType.Point, radius: 320, intensity: 1.5,
+ color: { r: 1, g: 0.7, b: 0.3, a: 1 } });
+
+// A flashlight cone
+.insert(Light2D, { type: Light2DType.Spot, direction: { x: 0, y: -1 },
+ innerAngle: 18, outerAngle: 34, radius: 500, intensity: 2 });
+
+// A dim ambient fill so nothing is fully black
+.insert(Light2D, { type: Light2DType.Ambient, intensity: 0.25 });
+```
+
+## Making a renderer receive light
+
+By default a `Sprite` draws with an **unlit** material and ignores every light. To make
+it respond, give it a material whose shader declares the **Lit-2D domain**. In an
+`.esshader`, that's one pragma plus a call to the injected lighting helper:
+
+```glsl
+#pragma shader "MyLit"
+#pragma version 300 es
+#pragma domain Lit2D
+
+#pragma vertex
+// … pass the world-space position out as v_worldPos …
+#pragma end
+
+#pragma fragment
+precision mediump float;
+in vec4 v_color;
+in vec2 v_texCoord;
+in highp vec2 v_worldPos;
+uniform sampler2D u_textures[8];
+out vec4 fragColor;
+
+// #pragma domain Lit2D makes the engine inject the LightConstants block and the
+// es_applyLighting2D() helper — you just call it with your base color and a normal.
+void main() {
+ vec4 base = texture(u_textures[0], v_texCoord) * v_color;
+ vec3 N = vec3(0.0, 0.0, 1.0); // flat normal, facing the camera
+ vec3 lit = es_applyLighting2D(base.rgb, N, v_worldPos);
+ fragColor = vec4(lit, base.a);
+}
+#pragma end
+```
+
+Compile it and assign the material to the renderer exactly as in
+[Materials & Shaders](/docs/guides/material/):
+
+```typescript
+import { Material, defineSystem, Query, Mut, Sprite } from 'esengine';
+
+const litMat = Material.create({ shader: Material.compileShader(myLitSource) });
+
+const applyLit = defineSystem([Query(Mut(Sprite))], (q) => {
+ for (const [, sprite] of q) sprite.material = litMat;
+});
+```
+
+You can also build a lit material visually — set the **Material Graph**'s domain to
+`Lit2D` — see the [material graph](/docs/guides/material/) section.
+
+<Aside type="tip">
+ Every renderer that takes a `material` — `Sprite`, `SpineAnimation`,
+ `ParticleEmitter`, and tilemap layers — can be lit the same way: give it a Lit-2D
+ material.
+</Aside>
+
+### Normal maps
+
+The `N` you pass to `es_applyLighting2D` is the surface normal. Passing a constant
+`vec3(0, 0, 1)` gives flat, evenly-lit surfaces. For per-pixel relief, sample a
+**normal map** in the shader and use that as `N` instead — the same base color then
+catches highlights and shadow from each light's direction. Add the normal texture as a
+`#pragma param … texture` on your lit material (see [Materials & Shaders](/docs/guides/material/)).
+
+## Casting shadows
+
+Add a **`ShadowCaster2D`** to an entity to make it block light and cast a shadow:
+
+```typescript
+import { ShadowCaster2D } from 'esengine';
+
+cmds.spawn()
+ .insert(Transform, { position: { x: 100, y: 0, z: 0 } })
+ .insert(Sprite, { /* … a lit material … */ })
+ .insert(ShadowCaster2D, { size: { x: 64, y: 64 } });
+```
+
+| `ShadowCaster2D` field | Default | Description |
+|---|---|---|
+| `size` | `{32,32}` | The occluder's size in world units. |
+| `enabled` | `true` | Toggle shadow casting. |
+
+Shadow **quality** is controlled on the light, not the caster:
+
+- **`shadowSoftness`** — the light-source size. `0` is a crisp hard edge; larger values
+ give a soft penumbra (a bigger, closer light casts softer shadows).
+- **`shadowDistance`** — for a **Directional** light, how far its shadows extend; `0`
+ leaves a directional light shadow-free.
+
+## In the editor
+
+- **Add a light** via **Create… → Light**, or add a `Light2D` component to an existing
+ entity in the Details panel.
+- Lights are drawn as **gizmos** in edit mode — an icon plus the reach circle and the
+ direction / cone — so you can aim and size them by eye. See [The Editor](/docs/guides/editor/).
+- Tune `color` (a color picker), `intensity`, `radius`, and the spot angles live in
+ Details; the results update in the viewport immediately.
+- All of a light's visual fields (`color`, `intensity`, `radius`, the angles, and the
+ shadow settings) are **animatable** — key them in the [Sequencer](/docs/guides/timeline/)
+ for flickering torches, day/night cycles, or a pulsing beacon.
diff --git a/docs/astro/src/content/docs/zh-cn/guides/lighting.mdx b/docs/astro/src/content/docs/zh-cn/guides/lighting.mdx
new file mode 100644
index 000000000..8d1706444
--- /dev/null
+++ b/docs/astro/src/content/docs/zh-cn/guides/lighting.mdx
@@ -0,0 +1,164 @@
+---
+title: 2D 光照与阴影
+description: 用方向光、点光、聚光和环境光 Light2D 光源照亮 2D 场景——颜色与强度、法线贴图表面、以及 ShadowCaster2D 投出的柔和阴影,全部经由 Lit-2D 材质域实现。
+---
+
+import { Aside } from '@astrojs/starlight/components';
+
+Estella 用 **`Light2D`** 光源和 **`ShadowCaster2D`** 遮挡体照亮 2D 场景。一个光源就是一个带
+`Light2D` 组件的实体;而一个渲染器只有在用 **Lit-2D 材质**绘制时才会被照亮。这让光照是可选的、
+且开销低——大多数精灵保持不受光,你想照亮的那些才配一个受光材质。
+
+<Aside type="note">
+ 光照内建于渲染器(无需加插件)。`Light2D` 和 `ShadowCaster2D` 是内建组件——像其他组件一样,
+ 从代码或在编辑器 Details 面板里挂上它们。
+</Aside>
+
+## 添加光源
+
+给一个实体加 `Light2D` 组件并选一个类型:
+
+```typescript
+import { defineSystem, Commands, Transform, Light2D, Light2DType } from 'esengine';
+
+const addSun = defineSystem([Commands()], (cmds) => {
+ cmds.spawn()
+ .insert(Transform, { position: { x: 0, y: 0, z: 0 } })
+ .insert(Light2D, {
+ type: Light2DType.Directional,
+ color: { r: 1, g: 0.95, b: 0.8, a: 1 },
+ intensity: 1.2,
+ direction: { x: -1, y: -1 },
+ });
+});
+```
+
+`Light2D` 字段(含默认值):
+
+| 字段 | 默认 | 说明 |
+|---|---|---|
+| `type` | `Point` | `Light2DType`:`Point`、`Directional`、`Ambient` 或 `Spot`。 |
+| `color` | `{1,1,1,1}` | 光颜色,RGBA 0..1。 |
+| `intensity` | `1` | 亮度倍数。 |
+| `radius` | `200` | 衰减半径,世界单位(Point / Spot)。 |
+| `direction` | `{0,0}` | 朝向(Directional / Spot)。 |
+| `innerAngle` | `30` | 聚光锥内角,度(内部全亮)。 |
+| `outerAngle` | `45` | 聚光锥外角,度(到这里衰减为零)。 |
+| `shadowSoftness` | `0` | 阴影边缘柔和度(光源尺寸);`0` = 硬边。 |
+| `shadowDistance` | `0` | 方向光阴影延伸距离;`0` = 无方向光阴影。 |
+| `enabled` | `true` | 不移除也能关掉光源。 |
+
+## 光源类型
+
+| 类型 | 行为 |
+|---|---|
+| **Directional(方向光)** | 来自单一 `direction` 的均匀光,像太阳——位置无关,只看朝向。 |
+| **Point(点光)** | 从实体位置向外辐射,到 `radius` 处衰减为零。 |
+| **Spot(聚光)** | 从位置沿 `direction` 的一个锥,在 `innerAngle` 内全亮、到 `outerAngle` 衰减到边缘,直到 `radius`。 |
+| **Ambient(环境光)** | 处处叠加的平坦底光,与位置无关——用较低的 `intensity`,让阴影区不至于纯黑。 |
+
+```typescript
+// 一盏暖色点光
+.insert(Light2D, { type: Light2DType.Point, radius: 320, intensity: 1.5,
+ color: { r: 1, g: 0.7, b: 0.3, a: 1 } });
+
+// 一束手电锥
+.insert(Light2D, { type: Light2DType.Spot, direction: { x: 0, y: -1 },
+ innerAngle: 18, outerAngle: 34, radius: 500, intensity: 2 });
+
+// 一点微弱环境光,让画面不至于全黑
+.insert(Light2D, { type: Light2DType.Ambient, intensity: 0.25 });
+```
+
+## 让渲染器接受光照
+
+默认情况下,一个 `Sprite` 用**不受光**材质绘制,会忽略每一盏灯。要让它响应光照,给它一个着色器
+声明了 **Lit-2D 域**的材质。在 `.esshader` 里,就是一个 pragma 加一次对注入的光照 helper 的调用:
+
+```glsl
+#pragma shader "MyLit"
+#pragma version 300 es
+#pragma domain Lit2D
+
+#pragma vertex
+// …把世界空间位置作为 v_worldPos 输出…
+#pragma end
+
+#pragma fragment
+precision mediump float;
+in vec4 v_color;
+in vec2 v_texCoord;
+in highp vec2 v_worldPos;
+uniform sampler2D u_textures[8];
+out vec4 fragColor;
+
+// #pragma domain Lit2D 让引擎注入 LightConstants 块和 es_applyLighting2D() helper——
+// 你只需用你的基础色和一个法线调用它。
+void main() {
+ vec4 base = texture(u_textures[0], v_texCoord) * v_color;
+ vec3 N = vec3(0.0, 0.0, 1.0); // 平法线,朝向相机
+ vec3 lit = es_applyLighting2D(base.rgb, N, v_worldPos);
+ fragColor = vec4(lit, base.a);
+}
+#pragma end
+```
+
+编译它并把材质赋给渲染器,方式与[材质与着色器](/docs/zh-cn/guides/material/)里完全一致:
+
+```typescript
+import { Material, defineSystem, Query, Mut, Sprite } from 'esengine';
+
+const litMat = Material.create({ shader: Material.compileShader(myLitSource) });
+
+const applyLit = defineSystem([Query(Mut(Sprite))], (q) => {
+ for (const [, sprite] of q) sprite.material = litMat;
+});
+```
+
+你也能可视化地做一个受光材质——把 **材质图** 的域设为 `Lit2D`——见
+[材质图](/docs/zh-cn/guides/material/)一节。
+
+<Aside type="tip">
+ 每个接受 `material` 的渲染器——`Sprite`、`SpineAnimation`、`ParticleEmitter` 和瓦片图层——
+ 都能用同样方式受光:给它一个 Lit-2D 材质。
+</Aside>
+
+### 法线贴图
+
+你传给 `es_applyLighting2D` 的 `N` 是表面法线。传一个常量 `vec3(0, 0, 1)` 得到平坦、均匀受光的
+表面。要有逐像素的凹凸感,就在着色器里采样一张**法线贴图**并用它作 `N`——同一张基础色便会随每盏
+灯的方向产生高光和阴影。把法线纹理作为一个 `#pragma param … texture` 加到你的受光材质上
+(见[材质与着色器](/docs/zh-cn/guides/material/))。
+
+## 投射阴影
+
+给一个实体加 **`ShadowCaster2D`**,让它遮挡光并投出阴影:
+
+```typescript
+import { ShadowCaster2D } from 'esengine';
+
+cmds.spawn()
+ .insert(Transform, { position: { x: 100, y: 0, z: 0 } })
+ .insert(Sprite, { /* …一个受光材质… */ })
+ .insert(ShadowCaster2D, { size: { x: 64, y: 64 } });
+```
+
+| `ShadowCaster2D` 字段 | 默认 | 说明 |
+|---|---|---|
+| `size` | `{32,32}` | 遮挡体尺寸,世界单位。 |
+| `enabled` | `true` | 开关阴影投射。 |
+
+阴影**质量**在光源上控制,而非遮挡体:
+
+- **`shadowSoftness`** — 光源尺寸。`0` 是锐利硬边;更大的值给出柔和半影(更大、更近的光投出更软的
+ 阴影)。
+- **`shadowDistance`** — 对 **Directional** 光,阴影延伸多远;`0` 让方向光不产生阴影。
+
+## 在编辑器里
+
+- 通过 **创建… → 光** 添加光源,或在 Details 面板里给已有实体加一个 `Light2D` 组件。
+- 光源在编辑模式下画成 **gizmo**——图标加上其范围圈和朝向/锥——所以你能凭眼睛瞄准和调尺寸。见
+ [编辑器](/docs/zh-cn/guides/editor/)。
+- 在 Details 里实时调 `color`(取色器)、`intensity`、`radius` 和聚光角度;结果在视口里即时更新。
+- 一盏灯的全部视觉字段(`color`、`intensity`、`radius`、各角度、阴影设置)都**可动画**——在
+ [Sequencer](/docs/zh-cn/guides/timeline/)里给它们打关键帧,做闪烁的火把、昼夜循环或脉动的信标。