implement-fog-of-war

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Implement Fog of War

实现Fog of War(战争迷雾)

Build fog of war as a shared perception system with a restrained presentation layer.
将fog of war构建为带有克制表现层的共享感知系统。

Preserve the architecture

保留架构设计

Keep these responsibilities separate:
  1. Let deterministic CPU perception own ranges, obstacle intersections, and gameplay visibility.
  2. Encode the player's angular visible distances into one fixed-size lookup texture.
  3. Let one full-screen shader turn that lookup into soft radial and wall-aware fog.
  4. Let gameplay consume perception results directly; never infer combat truth from fog pixels.
Read mechanics.md before implementing or changing the algorithm. It records the proven obstacle model, shader composition, calibrated values, state rules, enemy behavior, lighting constraints, and telemetry.
请将以下职责分离:
  1. 由确定性CPU感知模块负责范围计算、障碍物相交检测以及游戏内可见性判断。
  2. 将玩家的角度可见距离编码为一张固定尺寸的lookup texture(查找纹理)。
  3. 通过一个全屏shader将该查找纹理转换为支持径向柔化和墙体识别的雾效。
  4. 游戏逻辑直接调用感知结果;绝不要从雾效像素中推断战斗相关的真实状态。
在实现或修改算法前,请阅读mechanics.md。该文档记录了已验证的障碍物模型、shader组成、校准值、状态规则、敌人行为、光照约束以及遥测数据。

Inspect before editing

编辑前检查

  • Locate the authoritative player position, camera, obstacles, scene lifecycle, frame loop, targeting, enemy perception, and render layers.
  • Confirm the gameplay ground model. The proven corner-unprojection method assumes a locally horizontal plane; use authoritative surface or terrain reconstruction when elevation varies across the visible area.
  • Find existing scene fog, post-processing, renderer creation, review modes, menus, captures, transitions, and disposal paths.
  • Read the current tests and recent fog/perception commits before changing constants or ownership.
  • Confirm whether obstacle state changes at runtime. Reuse the same active obstacle collection used by navigation or collision when its geometry is suitable for sight.
  • Check the working tree early and preserve unrelated changes.
  • 定位权威的玩家位置、相机、障碍物、场景生命周期、帧循环、瞄准系统、敌人感知模块以及渲染层。
  • 确认游戏地面模型。已验证的角点反投影方法假设地面为局部水平面;当可见区域内存在海拔变化时,请使用权威的表面或地形重建方案。
  • 查找现有场景雾效、后处理、渲染器创建、审查模式、菜单、捕获功能、过渡效果以及资源销毁路径。
  • 在修改常量或模块归属前,阅读当前测试用例以及近期与雾效/感知相关的提交记录。
  • 确认障碍物状态是否会在运行时变化。当导航或碰撞系统使用的活跃障碍物集合的几何形状适用于视线检测时,请复用该集合。
  • 尽早检查工作目录,保留无关修改。

Implement in dependency order

按依赖顺序实现

1. Define perception truth

1. 定义感知真实状态

  • Represent each sight blocker with a stable footprint, vertical span, and active state.
  • Intersect a 3D sight segment or ray with both the horizontal footprint and vertical span.
  • Ignore inactive blockers and cover below the eye-to-target sight line.
  • Expose:
    • point-to-point perception for enemies, targeting, and attacks;
    • a deterministic angular distance fill for the fog lookup.
  • Use explicit player and enemy ranges. Allow them to differ when the encounter design needs enemies to acquire beyond the player's reveal edge.
  • 用稳定的占地面积、垂直范围和激活状态表示每个视线阻挡物。
  • 将3D视线线段或射线与水平占地面积和垂直范围进行相交检测。
  • 忽略未激活的阻挡物以及视线下方的遮蔽物。
  • 对外提供:
    • 供敌人、瞄准和攻击系统使用的点对点感知功能;
    • 用于雾效查找的确定性角度距离填充功能。
  • 使用明确的玩家和敌人视野范围。当关卡设计要求敌人的感知范围超出玩家的迷雾揭露边界时,允许两者范围不同。

2. Build the visibility lookup

2. 构建可见性查找纹理

  • Allocate the distance array, byte pixels, and data texture once.
  • Cast a fixed number of evenly spaced horizontal rays around the player's eye point.
  • Normalize hit distances by the player vision radius and quantize them into a one-row red-channel texture.
  • Use linear filtering, horizontal repeat wrapping, no mipmaps, and a bounded mobile/desktop ray budget.
  • Refresh after meaningful player motion or a short maximum interval. Do not allocate in the frame loop.
  • 一次性分配距离数组、字节像素和data texture(数据纹理)。
  • 在玩家眼睛位置周围投射固定数量、均匀分布的水平射线。
  • 用玩家视野半径对命中距离进行归一化处理,并将其量化为单行红通道纹理。
  • 使用线性过滤、水平重复环绕、无mipmaps(多级纹理),并设置移动端/桌面端的射线预算上限。
  • 在玩家发生有效移动或达到最短刷新间隔时更新纹理。不要在帧循环中进行内存分配。

3. Render one soft overlay

3. 渲染柔化覆盖层

  • Use one clip-space plane and one transparent shader material.
  • Reconstruct each fragment's ground-plane position from the four camera-corner intersections.
  • Combine:
    • a clear inner radius and soft outer radial falloff;
    • a feathered wall boundary sampled from the angular lookup.
  • Smooth obstacle visibility across neighboring angles. Weight wall fog below the outer radial fog so blockers read as atmosphere, not opaque wedges.
  • Combine radial and obstacle fog with
    max
    , then cap final opacity.
  • Disable depth test, depth write, and tone mapping; render after the world.
  • Do not introduce a second renderer, render target, composer, or per-ray meshes.
  • 使用一个裁剪空间平面和一个透明shader材质。
  • 通过四个相机角点的相交结果重建每个片段的地面位置。
  • 融合以下效果:
    • 清晰的内圈半径和柔化的外圈径向衰减;
    • 从角度查找纹理采样得到的羽化墙体边界。
  • 平滑相邻角度间的障碍物可见性。降低墙体雾效的权重,使其低于外圈径向雾效,让阻挡物看起来像大气效果而非不透明楔形区域。
  • 使用
    max
    函数融合径向雾效和墙体雾效,然后限制最终不透明度。
  • 禁用深度测试、深度写入和色调映射;在场景渲染完成后再渲染雾效。
  • 不要引入第二个渲染器、渲染目标、合成器或每条射线对应的网格。

4. Integrate gameplay

4. 集成游戏逻辑

  • Keep simulation presence separate from presentation visibility.
  • Hide unrevealed enemy renderables through layers or a presentation-only mechanism; do not toggle the actor root if root visibility also controls lifecycle or simulation.
  • Exclude unrevealed enemies from target lock, aim selection, nearest-target search, HUD counts, and player hit eligibility.
  • Hide telegraphs that would leak an unseen enemy.
  • Let enemy acquisition and attacks use their own range plus the same line-of-sight truth.
  • Clear an active target lock as soon as perception invalidates it.
  • Cache perception within a simulation phase and refresh after movement when downstream systems need current positions.
  • 将模拟存在状态与表现层可见性分离。
  • 通过图层或仅作用于表现层的机制隐藏未被揭露的敌人渲染对象;如果角色根节点的可见性同时控制生命周期或模拟逻辑,则不要切换其可见性。
  • 将未被揭露的敌人排除在目标锁定、瞄准选择、最近目标搜索、HUD计数以及玩家可攻击目标之外。
  • 隐藏会泄露未被发现敌人存在的预警提示。
  • 让敌人的感知和攻击系统使用自身的范围以及相同的视线真实状态。
  • 一旦感知系统判定目标不可见,立即清除当前的目标锁定。
  • 在模拟阶段缓存感知结果,当下游系统需要当前位置时,在移动后刷新缓存。

5. Integrate state and lifecycle

5. 集成状态与生命周期

  • Enable fog only during active gameplay.
  • Disable it for menus, inventory, review/capture modes, paused or non-playing states, and stage transitions unless the product explicitly requires otherwise.
  • Disable it before any early-render branch so a previous frame cannot leak into another state.
  • Keep atmospheric scene fog separate from fog of war.
  • Dispose the overlay geometry, material, and lookup texture on teardown.
  • Publish compact telemetry for ray budget, enabled state, ranges, visible enemies, target visibility, line of sight, and measured distance.
  • 仅在活跃游戏过程中启用雾效。
  • 在菜单、背包、审查/捕获模式、暂停或非游戏状态以及关卡过渡时禁用雾效,除非产品有明确要求。
  • 在任何提前渲染分支前禁用雾效,避免上一帧内容泄露到其他状态中。
  • 将场景大气雾效与fog of war(战争迷雾)分离。
  • 在销毁时释放覆盖层的几何体、材质和查找纹理。
  • 发布精简的遥测数据,包括射线预算、启用状态、视野范围、可见敌人数量、目标可见性、视线状态以及测量距离。

Tune in the right order

按正确顺序调优

  1. Validate obstacle geometry and eye height.
  2. Validate player and enemy ranges.
  3. Set the fully clear inner radius.
  4. Set the outer radial falloff and maximum opacity.
  5. Set wall-edge softness.
  6. Add angular smoothing until spokes disappear.
  7. Reduce obstacle darkness until walls feel like occlusion rather than black rays.
  8. Adjust ray budgets only after visual correctness and measured performance.
Do not hide bad obstacle data with extra blur. Do not make the entire scene darker to compensate for weak visibility boundaries.
  1. 验证障碍物几何形状和眼睛高度。
  2. 验证玩家和敌人的视野范围。
  3. 设置完全清晰的内圈半径。
  4. 设置外圈径向衰减和最大不透明度。
  5. 设置墙体边缘柔化程度。
  6. 添加角度平滑效果,直到放射状线条消失。
  7. 降低障碍物的暗度,直到墙体看起来是遮挡效果而非黑色射线。
  8. 仅在视觉效果正确且性能达标后调整射线预算。
不要用额外模糊来掩盖劣质的障碍物数据。不要为了弥补薄弱的可见性边界而调暗整个场景。

Validate

验证

Read validation.md before claiming completion. Run:
  • pure perception and opacity tests;
  • structural render-budget contracts;
  • integration tests for targeting, layers, lighting, and state;
  • project lint and build;
  • real desktop and mobile browser checks in the Codex browser.
Use normal gameplay for visual verification when review modes intentionally disable fog. Judge motion, camera movement, wall edges, gates, seams, and menu transitions—not only a still frame.
在宣称完成前,请阅读validation.md。运行以下测试:
  • 纯感知和不透明度测试;
  • 结构化渲染预算约束测试;
  • 瞄准、图层、光照和状态的集成测试;
  • 项目代码检查和构建;
  • 在Codex浏览器中进行真实桌面端和移动端浏览器测试。
当审查模式故意禁用雾效时,使用正常游戏流程进行视觉验证。评估运动效果、相机移动、墙体边缘、门、接缝以及菜单过渡——不要仅评估静态帧。

Protect the proven qualities

维护已验证的特性

  • Keep the inner play area readable.
  • Keep the maximum darkness restrained.
  • Keep walls soft and free of visible radial spokes.
  • Keep the angle seam invisible.
  • Keep dynamic gates synchronized with sight.
  • Keep enemy behavior fair even when enemy vision exceeds player reveal.
  • Keep one authoritative visibility model and one bounded overlay.
  • 保持游戏内圈区域清晰可读。
  • 控制最大暗度,避免过度黑暗。
  • 保持墙体边缘柔化,无可见放射状线条。
  • 确保角度接缝不可见。
  • 保持动态门与视线同步。
  • 即使敌人视野超出玩家的迷雾揭露范围,也要保证敌人行为公平合理。
  • 维护单一权威可见性模型和单一有界覆盖层。