Skip to content

t1k:unity:rendering:fake-shadow

FieldValue
Modulerendering
Version5.39.1
Effortlow
Tools—

Keywords: blob shadow, fake shadow, FakeShadowRTManager, shadow render texture, stylized shadow, top-down shadow camera

/t1k:unity:rendering:fake-shadow

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.

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.

  1. Put your shadow-caster geometry (silhouette shapes, not full-detail meshes) on a dedicated layer.
  2. Add FakeShadowRTManager to a GameObject, assign mainCamera and shadowShader (both required — CreateResources() throws InvalidOperationException if either is null), set shadowLayer to that layer.
  3. 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).
  4. Everything auto-builds in Awake at Play mode start and tears down in OnDestroy — no manual init/dispose calls needed for the happy path. Use the Set* public methods (SetShadowColor, SetBlurEnabled, SetSortingLayer, etc.) for runtime changes.
  • Hard-throws, not silent-skip, on missing refs. mainCamera == null or shadowShader == null at CreateResources() time throws InvalidOperationException — this fires from Awake() 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.isPlaying first and calls DestroyGeneratedObjects() 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 follows skills/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, every LateUpdate. Position, rotation, orthographicSize, and aspect are re-synced every frame (SyncCamera()), not cached once — if you swap mainCamera to a different camera reference at runtime via a field write (not through a setter, since there isn’t one for mainCamera), the shadow camera picks it up on the very next LateUpdate automatically. There is no SetMainCamera method; assign the serialized field directly, or expose your own.
  • The GC-sweep cleanup pattern (FakeShadowRTGeneratedMarker + scene-wide FindObjectsByType). DestroyGeneratedObjects() doesn’t rely on the manager’s own cameraGO/displayGO fields — it scans EVERY FakeShadowRTGeneratedMarker in the scene (including inactive) and destroys any whose Owner == this. This survives cases where the manager’s direct references were lost (recompile, duplicate-without-child-refs) but costs a scene-wide FindObjectsByType call — don’t call ReleaseResources()/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. Unlike SpriteTextRenderer’s next-frame-coalesced rebuild, calling SetShadowColor then SetBlurEnabled then SetBlurRadius back-to-back runs UpdateMaterialProperties() 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 call SyncRuntimeState()-equivalent logic once, or accept the minor redundant work.
  • displayVerticalOverscan is 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 (from TextureWrapMode.Clamp) sit just outside the visible display area, not at its border.
  • cameraDepth controls render ORDER relative to the main camera, not physical depth or distance. Default -10f — lower depth renders first. Don’t confuse it with displayDepthScale (a Vector3.z scale 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_ON keyword. 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) in beginCameraRendering for that camera. Do not trust only the camera SolidColor clear on tile-based GPUs.
    • Build mips with CommandBuffer.GenerateMips executed from endCameraRendering, never an immediate RenderTexture.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 == SolidColor and backgroundColor.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 _ShadowTex defaults to the opaque “black” (alpha 1) and shades the whole map.
    • Test traps: WaitForEndOfFrame throws in batchmode; GetPixel after Graphics.CopyTexture reads stale CPU data (use ReadPixels); keep sample points inside the camera view.
  • 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.UnityCommon FakeShadowRTManager and only add what it lacks.

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).

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: ScreenCapture in Play mode per t1k-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.