[2D SHADER] Unlock Dynamic 2D Fog of War Power
Solution
Unity 2021.x - Unity 6.3.x
Published Sun, Mar 15
The challenge involves implementing a dynamic fog of war mechanic in 2D environments where a dark overlay progressively reveals world areas. The primary difficulty lies in selectively modifying a RenderTexture in real-time to create transparent holes based on entity positions while maintaining performance and avoiding texture read/write feedback loops.
- A RenderTexture is created to serve as the persistent visibility mask for the scene.
- The RenderTexture is assigned to a
RawImagecomponent located on aCanvasset to Screen Space - Camera. - A custom
Materialusing a subtraction or reveal shader is assigned to theRawImageto handle color and alpha blending. - Entity world positions are converted into normalized UV coordinates by mapping the
Transformposition to the defined map dimensions. - Normalized coordinates and the reveal radius are passed to your shader using
Material.SetVectorandMaterial.SetFloat. Graphics.Blitis executed to update the RenderTexture, utilizing a temporary buffer to prevent sampling conflicts during the read-write process.
The system operates by continuously clearing or modifying alpha values on the RenderTexture at entity coordinates. By using this persistent buffer, revealed areas remain visible as the player explores the environment.
Additional Tips
- A lower resolution RenderTexture should be used to minimize GPU memory usage and naturally soften reveal edges.
- Bilinear filtering must be enabled on the RenderTexture settings to ensure smooth transitions between revealed and hidden areas.
- The
Canvassorting order should be configured to render above the game world but below the primary UI HUD elements.
TL;DR
Dynamic masking is achieved by rendering reveal shapes onto a RenderTexture via Graphics.Blit. This mask is applied to a full-screen RawImage component, where a custom shader manages the blending logic to punch holes through the fog based on world-space coordinates provided by your script.
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.