Class CachedMainLightShadows
Cached static casters for the main light's cascaded shadows: the GPU-driven props' static casters (structures, rocks, cliffs — TerrainGpuRenderer.ShadowCache.cs) are drawn into a persistent copy of URP's cascade atlas only when a cascade changes, and that copy is merged, depth-tested, into URP's own shadow map every frame after URP has drawn the rest.
public static class CachedMainLightShadows
- Inheritance
-
CachedMainLightShadows
- Inherited Members
Remarks
Why a merge, not a replacement. URP keeps its cascade slices and its shadow pass to itself, and every receiver (props, vegetation, GPU terrain, grass, scatter, water, the clouds' shadow reads) samples URP's map with URP's matrices. So URP still draws the shadow map — every live caster: trees and plants (their sway stays), characters, ships, scatter, grass, terrain — and only the batch group's static casters move out of it (TerrainGpuRenderer.Brg.cs leaves them out of the main light's culling while this serves the camera). After URP's pass the cache is merged into the same map with the depth test, so each texel keeps the nearer of the live and the cached depth: exactly what drawing them together gives. Nothing a receiver reads changes.
Windows, not URP's slices (2026-11-03). URP re-fits each cascade to the camera every frame, snapped to its texels. Caching URP's slice itself meant a moving camera redrew every cascade nearly every frame (natively ~1,240 static draws a frame, +2 ms CPU, no saving). Each cascade is now cached over a window: URP's slice at the refresh with its orthographic projection widened by MarginDivisor of its texels on each side (and a quarter of its depth range at each end), at URP's own texel size and rotation. URP's slice in a later frame is the same orthographic grid moved by whole texels (stable cascades), so it maps into the window as a whole-texel shift and a depth offset (TryMap(in Window, in CascadeKey, int, int, out Mapping)), and the merge copies from there. A cascade is redrawn only when URP's slice leaves its window (its tile or its depth range), the slice's rotation, texel size, resolution or bias changes (a sun step, a setting), the atlas changes, or the static casters themselves change (a layout or an instance upload: the camera no longer counts). A redraw culls the casters itself for the whole window, from the camera's position then (TerrainGpuRenderer.RecordShadowCacheCull): its levels chosen there and drawn whole (no crossfade dither, which would freeze mid-fade), so a cached shadow can hold an instance's level from up to a margin away until its window moves on. While caching, the sky lights' directions step on a fixed azimuth/elevation lattice (SunStepDegrees): the sun's direction is a pure function of the shared clock and the lattice is a pure function of the direction, so every client steps at the same world time; between steps the windows hold.
Limits. A sun step redraws every cascade that frame. A redraw costs what every frame cost before (one indirect draw per static command; Unity pays a SetPass for each, whatever their order). Only the props' batch-group casters are cached; MeshRenderers, scatter and the GPU terrain stay live. Needs the instance index to carry the run's start (Vulkan; WebGPU with indirect-first-instance); elsewhere it does nothing.
Fields
CachePass
The cache pass (before URP's shadows) and the merge pass (after them); enqueued by FishCloudsFeature.
public static readonly ScriptableRenderPass CachePass
Field Value
- ScriptableRenderPass
DebugClearDepth
Diagnostic: a raw depth the cache's tiles clear to instead of the far plane (negative: the far plane). With DebugOverwrite a mid value shades every cascade, which shows the merge reads the cache.
public static float DebugClearDepth
Field Value
DebugDrawNothing
Diagnosis (harness shadowCacheDrawNothing): the cache's passes run and set their state, but draw no casters, and the
batch group keeps the static casters in URP's map: what the cache's passes do to a frame besides its casters.
public static bool DebugDrawNothing
Field Value
DebugForceExact
Diagnostic: every frame drawn over exact windows (the refused frame's path), to test that path alone.
public static bool DebugForceExact
Field Value
DebugLiveCull
Diagnosis (harness shadowCacheLiveCull): the cache draws the camera's own culling of the static casters, not its own.
public static bool DebugLiveCull
Field Value
DebugMergeNoDraw
Diagnosis (harness shadowCacheMergeNoDraw, shadowCacheMergeNoState): the merge pass without its draws, or without its depth bias and viewport.
public static bool DebugMergeNoDraw
Field Value
DebugMergeNoState
Diagnosis (harness shadowCacheMergeNoDraw, shadowCacheMergeNoState): the merge pass without its draws, or without its depth bias and viewport.
public static bool DebugMergeNoState
Field Value
DebugOverwrite
Diagnostic: the merge overwrites URP's map with the cache (depth test off), showing only the cached casters.
public static bool DebugOverwrite
Field Value
DebugSkipCull
Diagnosis (harness shadowCacheSkipPass, shadowCacheSkipCull, shadowCacheSkipMerge): one of the cache's passes left out.
public static bool DebugSkipCull
Field Value
DebugSkipMerge
Diagnosis (harness shadowCacheSkipPass, shadowCacheSkipCull, shadowCacheSkipMerge): one of the cache's passes left out.
public static bool DebugSkipMerge
Field Value
DebugSkipPass
Diagnosis (harness shadowCacheSkipPass, shadowCacheSkipCull, shadowCacheSkipMerge): one of the cache's passes left out.
public static bool DebugSkipPass
Field Value
DefaultOn
Whether the cache starts on. The owner chose on in browser players and off natively (2026-11-03); it stays off
everywhere until its merge is right. 2026-11-06: with URP's three cascades in the browser, the cache's image has dark
areas the live map has nowhere (the live map with one cascade has none either), and the merge alone, with nothing
cached (harness shadowCacheDrawNothing), still changed ~8% of the close view's pixels: on WebGPU the merge writes
depth that no caster put there. The editor (Vulkan) merge wrote nothing in the same state (RenderDoc; measured
2026-11-05).
public const bool DefaultOn = false
Field Value
Enabled
The switch (harness setting cachedShadows), on by default in browser players only (DefaultOn). Off: URP
draws every caster every frame.
public static bool Enabled
Field Value
MapFailureNames
Diagnosis: why TryMap last refused, and how often each reason has (MapFailureNames; the browser probe logs them).
public static readonly string[] MapFailureNames
Field Value
- string[]
MapFailures
public static readonly int[] MapFailures
Field Value
- int[]
MapSuccesses
public static int MapSuccesses
Field Value
MarginDivisor
How many of a slice's texels its window adds on each side: a quarter of the resolution (the tile is 1.5× URP's).
public const int MarginDivisor = 4
Field Value
MaxCascades
public const int MaxCascades = 4
Field Value
MergePass
public static readonly ScriptableRenderPass MergePass
Field Value
- ScriptableRenderPass
MergeShader
The shader the merge and the tile clears draw with (FishWorldPassesFeature carries it into builds).
public static Shader MergeShader
Field Value
- Shader
MergeShaderName
public const string MergeShaderName = "Hidden/FishMMO/Shadow Cache Merge"
Field Value
ReadbackBadStarts
The last readback: instances in the cache's args and in the batch group's, over the static commands, and how many readbacks landed.
public static long ReadbackBadStarts
Field Value
ReadbackCacheInstances
The last readback: instances in the cache's args and in the batch group's, over the static commands, and how many readbacks landed.
public static long ReadbackCacheInstances
Field Value
ReadbackLiveInstances
The last readback: instances in the cache's args and in the batch group's, over the static commands, and how many readbacks landed.
public static long ReadbackLiveInstances
Field Value
ReadbackRequested
Diagnostic: set to read back the static commands' instance counts once (both args buffers, probes).
public static bool ReadbackRequested
Field Value
ReadbacksLanded
public static int ReadbacksLanded
Field Value
SkipNames
Diagnosis: frames the cache pass or the merge did not merge, by reason (SkipNames; the browser probe logs them). While the cache is enabled the batch group leaves the static casters out of URP's culling, so every skip but the first four (no main-light shadows at all) is a frame whose structures cast nothing.
public static readonly string[] SkipNames
Field Value
- string[]
Skips
public static readonly int[] Skips
Field Value
- int[]
SunStepDegrees
The sky lights' direction lattice while caching, in degrees of azimuth and elevation.
public static float SunStepDegrees
Field Value
TexelCentreTolerance
How far URP's texel centres may lie from the window's, in texels, for its texels to be copied one for one.
public const double TexelCentreTolerance = 0.1
Field Value
Properties
LastStaticDraws
public static int LastStaticDraws { get; }
Property Value
MergedFrames
Frames the cache was merged into URP's map.
public static int MergedFrames { get; }
Property Value
RefreshFrames
Frames that redrew at least one cascade, cascades redrawn, and the last redraw's static draws (probes).
public static int RefreshFrames { get; }
Property Value
RefreshedCascades
public static int RefreshedCascades { get; }
Property Value
Supported
Whether this device can draw the cached casters: their procedural variants need the run's start in the instance index.
public static bool Supported { get; }
Property Value
Methods
AppliesTo(Camera)
Whether the cache serves this camera's main-light shadows this frame.
public static bool AppliesTo(Camera camera)
Parameters
cameraCamera
Returns
AtlasSize(int, int)
The cache atlas for count cascades of size-texel tiles: two columns, as URP's.
public static Vector2Int AtlasSize(int count, int size)
Parameters
Returns
- Vector2Int
Describe()
The cache's counters as one line (the browser probe and the harness's shadowCacheLog).
public static string Describe()
Returns
LatticeDirection(Vector3, float)
The nearest direction on an azimuth/elevation lattice of stepDegrees (a pure function).
public static Vector3 LatticeDirection(Vector3 towardLight, float stepDegrees)
Parameters
towardLightVector3stepDegreesfloat
Returns
- Vector3
MergeDepthTest(bool)
The merge's depth test: keep the nearer depth to the light, written as on a forward depth buffer (LessEqual), since Unity itself reverses a material's ZTest where the depth buffer is reversed (the casters' own "ZTest LEqual" reaches the GPU as greater-equal there). Passing GreaterEqual under a reversed buffer came out as less-equal, which kept the farther depth: no cached caster ever won, and an empty cache tile erased the live casters. The diagnostic overwrite copies the cache whatever it holds.
public static CompareFunction MergeDepthTest(bool overwrite)
Parameters
overwritebool
Returns
- CompareFunction
OmitsStaticCasters(Camera)
Whether the batch group leaves the static casters out of URP's culling for this camera: the cache serves it, and the last frame merged. Culling comes before the cache pass knows whether it can merge, so a frame after one that could not keeps them in URP's map (drawn twice if this one merges too: the depth test keeps one depth), and only the first frame of a failure lacks them. Before 2026-11-05 every refused frame did.
public static bool OmitsStaticCasters(Camera camera)
Parameters
cameraCamera
Returns
Release()
Releases the cache (and forgets every window), e.g. when switched off.
public static void Release()
StableDirection(Vector3)
A sky light's direction for the light itself: on the lattice while caching, unchanged otherwise.
public static Vector3 StableDirection(Vector3 towardLight)
Parameters
towardLightVector3
Returns
- Vector3
TileOf(int, int, int)
A cascade's tile in that atlas.
public static Vector2Int TileOf(int cascade, int count, int size)
Parameters
Returns
- Vector2Int
TryExactWindowFor(in CascadeKey, int, int, out Window)
A window that is URP's slice itself (no margin, its own view and projection) in the tile at
(tileX, tileY): what a frame the margin windows cannot serve draws, merged by
TryMap(in Window, in CascadeKey, int, int, out Mapping) as any window. False when the slice's projection is not a plain orthographic one.
public static bool TryExactWindowFor(in CachedMainLightShadows.CascadeKey slice, int tileX, int tileY, out CachedMainLightShadows.Window window)
Parameters
sliceCachedMainLightShadows.CascadeKeytileXinttileYintwindowCachedMainLightShadows.Window
Returns
TryMap(in Window, in CascadeKey, int, int, out Mapping)
public static bool TryMap(in CachedMainLightShadows.Window window, in CachedMainLightShadows.CascadeKey slice, int atlasWidth, int atlasHeight, out CachedMainLightShadows.Mapping map)
Parameters
windowCachedMainLightShadows.WindowsliceCachedMainLightShadows.CascadeKeyatlasWidthintatlasHeightintmapCachedMainLightShadows.Mapping
Returns
TryOrthoBox(Matrix4x4, out float, out float, out float, out float, out float, out float)
An orthographic projection's box (Matrix4x4.Ortho's arguments), or false when it is not a plain one.
public static bool TryOrthoBox(Matrix4x4 p, out float left, out float right, out float bottom, out float top, out float near, out float far)
Parameters
Returns
TryWindowFor(in CascadeKey, int, int, int, out Window)
A slice's window at a refresh: its view as it is, its projection widened by margin texels on each
side and by a quarter of its depth range at each end, in the tile at (tileX, tileY).
False when the slice's projection is not a plain orthographic one (then nothing is cached).
public static bool TryWindowFor(in CachedMainLightShadows.CascadeKey slice, int margin, int tileX, int tileY, out CachedMainLightShadows.Window window)
Parameters
sliceCachedMainLightShadows.CascadeKeymargininttileXinttileYintwindowCachedMainLightShadows.Window
Returns
WindowPlanes(in Window, Vector4[])
The cache cull's planes for a window (TerrainGpuRenderer.RecordShadowCacheCull): its four sides, which run along the light, then two that pass everything; inside where dot(n, p) + w ≥ 0.
public static void WindowPlanes(in CachedMainLightShadows.Window window, Vector4[] planes)
Parameters
windowCachedMainLightShadows.WindowplanesVector4[]