2D Soft Shadows with SpriteKit and Metal

Hi!

I'd like to share an implementation of 2D soft shadows using SpriteKit and custom Metal rendering.

GitHub Repo

The demo app runs on iOS and Mac Catalyst. The soft shadow implementation is based on Scott Lembcke's algorithm.

Pipeline

The app uses MTKView to drive the rendering loop at the desired frame rate. Each frame:

  • SpriteKit renders the scene into a Metal texture using SKRenderer.
  • The CPU sends each light's properties and the relevant shape edges to a Metal vertex shader.
  • The vertex shader projects shadow geometry from each edge.
  • A fragment shader calculates the shadow opacity at each pixel, producing a soft shadow mask for each light.
  • A final fragment shader combines the SpriteKit texture, lights, and shadow masks to produce the displayed image.

The SwiftUI controls update variables inside the SpriteKit scene through @Observable. The rendering loop consumes the new values on its next cycle.

Let me know if you have any feedback!

2D Soft Shadows with SpriteKit and Metal
 
 
Q