t1k:unity:rendering:fake-shadow
| Field | Value |
|---|---|
| Module | rendering |
| Version | 5.39.1 |
| Effort | low |
| Tools | — |
Keywords: blob shadow, fake shadow, FakeShadowRTManager, shadow render texture, stylized shadow, top-down shadow camera
How to invoke
Section titled “How to invoke”/t1k:unity:rendering:fake-shadowFakeShadow
Section titled “FakeShadow”A “fake” shadow technique for mobile: a second orthographic camera renders a shadowLayer-masked silhouette pass into a RenderTexture, which a generated quad then displays via a shader — not real-time shadow mapping, no Light.shadows, no shadow atlas. Source: The1Studio/Mike.UnityCommon FakeShadow/ — full annotated source: references/source.md.
Why this technique
Section titled “Why this technique”Real-time shadow mapping is expensive on mobile (shadow atlas, cascades, per-light shadow maps). This trick captures a top-down silhouette of shadow-caster objects once per frame into a small RT and paints it flat — cost is one extra orthographic camera pass over a culling-masked layer, not a shadow-map render per shadow-casting light. Good fit for: stylized blob/contact shadows under characters, isometric/top-down games, mobile titles already avoiding real shadows for perf. Not a fit for: shadows that need to follow arbitrary light direction/angle, or multi-light shadow scenarios — this only ever mirrors mainCamera’s orthographic framing.
Quick usage
Section titled “Quick usage”- Put your shadow-caster geometry (silhouette shapes, not full-detail meshes) on a dedicated layer.
- Add
FakeShadowRTManagerto a GameObject, assignmainCameraandshadowShader(both required —CreateResources()throwsInvalidOperationExceptionif either is null), setshadowLayerto that layer. - Tune
rtResolution(capture-texture size),shadowColor/enableBlur/blurRadius(appearance),displayTarget/displayLocalPosition/displayDepthScale/displayVerticalOverscan(where the display quad sits and how much bigger than the capture frustum it renders). - Everything auto-builds in
Awakeat Play mode start and tears down inOnDestroy— no manual init/dispose calls needed for the happy path. Use theSet*public methods (SetShadowColor,SetBlurEnabled,SetSortingLayer, etc.) for runtime changes.
Hard rules
Section titled “Hard rules”- Hard-throws, not silent-skip, on missing refs.
mainCamera == nullorshadowShader == nullatCreateResources()time throwsInvalidOperationException— this fires fromAwake()in Play mode, so a misconfigured prefab fails loud and immediately rather than silently rendering nothing. - The generated camera/quad only exist in Play mode.
Awake()checks!Application.isPlayingfirst and callsDestroyGeneratedObjects()instead of building anything — dropping this component into a scene shows nothing in the Editor outside Play mode. Don’t expect a Scene-view preview. - Hard dependency on Sirenix Odin Inspector (
[ValueDropdown(nameof(GetSortingLayerNames))]). This is NOT decoupled behind a seam in the source repo — if your project followsskills/t1k-library-decoupling/references/third-party-decoupling.md(theonekit-core) for library code, either accept Odin as a dependency for this component or replace the attribute with a plain string field before porting it in. - The shadow camera continuously mirrors
mainCamera’s framing, everyLateUpdate. Position, rotation,orthographicSize, andaspectare re-synced every frame (SyncCamera()), not cached once — if you swapmainCamerato a different camera reference at runtime via a field write (not through a setter, since there isn’t one formainCamera), the shadow camera picks it up on the very nextLateUpdateautomatically. There is noSetMainCameramethod; assign the serialized field directly, or expose your own. - The GC-sweep cleanup pattern (
FakeShadowRTGeneratedMarker+ scene-wideFindObjectsByType).DestroyGeneratedObjects()doesn’t rely on the manager’s owncameraGO/displayGOfields — it scans EVERYFakeShadowRTGeneratedMarkerin the scene (including inactive) and destroys any whoseOwner == this. This survives cases where the manager’s direct references were lost (recompile, duplicate-without-child-refs) but costs a scene-wideFindObjectsByTypecall — don’t callReleaseResources()/CreateResources()in a hot per-frame loop; it’s meant for lifecycle boundaries (enable/disable/destroy), not continuous rebuilding. Set*methods each trigger their own immediate re-sync — no batching. UnlikeSpriteTextRenderer’s next-frame-coalesced rebuild, callingSetShadowColorthenSetBlurEnabledthenSetBlurRadiusback-to-back runsUpdateMaterialProperties()three times in the same frame. Harmless for occasional calls; if you’re driving many properties from a single runtime event, write the backing fields directly and callSyncRuntimeState()-equivalent logic once, or accept the minor redundant work.displayVerticalOverscanis a Range-clamped[1, 1.2]deliberately — the display quad is sized slightly larger than the shadow camera’s actual capture frustum so RT-edge clamp artifacts (fromTextureWrapMode.Clamp) sit just outside the visible display area, not at its border.cameraDepthcontrols render ORDER relative to the main camera, not physical depth or distance. Default-10f— lower depth renders first. Don’t confuse it withdisplayDepthScale(aVector3.zscale on the display quad’s local scale) which is a completely different, unrelated field.- The shader itself (
shadowShader) is not included in this reference — only the C# driving it. It must expose_MainTex,_ShadowColor,_BlurRadius,_ShadowCameraPosition,_ShadowCameraRight,_ShadowCameraUp,_ShadowCameraHalfExtents, and an_ENABLEBLUR_ONkeyword. Port or author a matching shader before this component will render anything visible beyond a flat quad. - The silhouette RT is read as alpha, often through its MIP chain. A stale frame, a stale mip, or uncleared alpha becomes a black trail, then a black map on device.
- A one-frame editor capture cannot show it. Test 100+ frames with automatic camera rendering (not
Camera.Render()) and moving casters. - Clear the RT explicitly:
ClearRenderTarget(true, true, Color.clear)inbeginCameraRenderingfor that camera. Do not trust only the camera SolidColor clear on tile-based GPUs. - Build mips with
CommandBuffer.GenerateMipsexecuted fromendCameraRendering, never an immediateRenderTexture.GenerateMips(), which runs before the pipeline submits the camera. - Recreate and clear the RT when
IsCreated()is false (pause/resume, context loss). Validate at startup:clearFlags == SolidColorandbackgroundColor.a == 0, else throw. - Mike.UnityCommon’s RT sets
depthStencilFormat = D16_UNorm_S8_UInt. A rewrite with depth bits 0 is an untested device difference (status: unproven as the cause; fix committed in RoyalBounce a236a07, device run pending). - A missing
_ShadowTexdefaults to the opaque “black” (alpha 1) and shades the whole map. - Test traps:
WaitForEndOfFramethrows in batchmode;GetPixelafterGraphics.CopyTexturereads stale CPU data (useReadPixels); keep sample points inside the camera view.
- A one-frame editor capture cannot show it. Test 100+ frames with automatic camera rendering (not
- When a project needs more than this. Mike.UnityCommon’s hard silhouette is the baseline. RoyalBounce wrote its own soft rig (
Scripts/Gameplay/Shadow/FakeShadowRig.cs+SoftFakeShadow.shader) for soft blur/AO. Start from Mike.UnityCommonFakeShadowRTManagerand only add what it lacks.
Full source + line-by-line reference
Section titled “Full source + line-by-line reference”references/source.md — every field, the full lifecycle table, CreateResources/ReleaseResources/SyncRuntimeState walkthroughs, and the marker-based cleanup idiom, annotated against the actual Mike.UnityCommon source.
Reference-only: this skill documents third-party repo code for reuse/porting decisions. It does not ship the .cs files into this project — copy FakeShadowRTManager.cs/FakeShadowRTGeneratedMarker.cs from the source repo (respecting its own license/versioning) when you integrate it, and author/port the matching shadowShader separately (not included in the source repo slice this skill was written from).
Visual change notice
Section titled “Visual change notice”A visible change made with this skill (UI, sprites, 3D, shaders, VFX, animation, lighting, scene layout; audio excluded) follows core rule visual-change-discord-notice:
- Capture OLD before the first edit and NEW after the last (screen:
ScreenCapturein Play mode pert1k-unity-editor-cli, windowed or Xvfb only because rendering is needed; 3D, shader, VFX: same-scene frame grab; 2D asset: the file). - Return the paths and a draft Discord post to the orchestrator, which posts; the PR links the post or carries
visual-notice-waiver: <reason>. Logic only: “no visible change”. No capture: “screenshots pending:”, never a fake image.