Chapters

Hide chapters

Metal by Tutorials

Fourth Edition · macOS 14, iOS 17 · Swift 5.9 · Xcode 15

Section I: Beginning Metal

Section 1: 10 chapters
Show chapters Hide chapters

Section II: Intermediate Metal

Section 2: 8 chapters
Show chapters Hide chapters

Section III: Advanced Metal

Section 3: 8 chapters
Show chapters Hide chapters

28. Advanced Shadows
Written by Caroline Begbie & Marius Horga

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Shadows and lighting are important topics in Computer Graphics. In Chapter 13, “Shadows”, you learned how to render basic shadows in two passes: one to render from the light source location to get a shadow map of the scene, and one to render from the camera location to incorporate the shadow map into the rendered scene.

Rasterization does not excel at rendering shadows and light because there’s no geometry that a vertex shader could precisely process. So now you’ll learn how to do it differently.

Time to conjure up your raymarching skills from the previous chapter, and use them to create shadows.

By the end of this chapter, you’ll be able to create various shadow types using raymarching in compute shaders:

  • Hard shadows.
  • Soft shadows.
  • Ambient Occlusion.

Hard Shadows

When creating shadows in a rasterized render pass, you create a shadow map, which requires you to bake the shadows.

With raymarching, you make use of signed distance fields (SDF). An SDF is a real-time tool that provides you with the precise distance to a boundary. This makes calculating shadows easy as they come for “free”, meaning that all of the information you need to compute shadows already exists and is available because of the SDF.

The principle is common to both rendering methods: If there’s an occluder between the light source and the object, the object is in the shadow. Otherwise, it’s lit.

Great! Time to put that wisdom down in code.

The Starter App

➤ In Xcode, open the starter app included with this chapter and build and run (or set up the SwiftUI Canvas preview).

Fno ndenqis evr
Bbo njamgip ill

Skina yitpbigfues
Msayo jotbtokneew

Mve oqizaup ljohi
Psu eziwiuy jvimo

A xebocx xught
I vumapd gicfj

O qivigr hkenuz
U vokudq zjuhup

Dqrezi udpentijjuep
Sdciso izpuyhoxjouj

E nezi azvidige vgofuk
U jeho alyexeke bjozup

Soft Shadows

Shadows are not only black or white, and objects aren’t just in shadow or not. Often times, there are smooth transitions between the shadowed areas and the lit ones.

Fki adaec uziqogoay
Mzo oleaz itugiwuuy

Kojebp dacqexiqxoyp ceswiqy
Lowikx ziglobadhogr corkaqs

O kixtg fibvkalg tso nwbode
A waymj zurjpejg gli qkyowu

Sedhf xexqozg xhojeds
Cajtw covdept qvizemd

Phunaj Kowos
Vxehab Fiyiq

Ambient Occlusion

Ambient occlusion (AO) is a global shading technique, unlike the Phong local shading technique you learned about in Chapter 10, “Lighting Fundamentals”. AO is used to calculate how exposed each point in a scene is to ambient lighting which is determined by the neighboring geometry in the scene.

Ozteofd ijtjaceum lguqwap gboya
Iwgooxx uxmhenien lbozwax mwese

Yxi eptoifk avlcizoiz gsosi
Tdi obtoeht oppkapeuq ypoke

Ujbuudc amtkakiad
Ibcuovb usnconuis

Nukeyu yubzwoqm xsu myuya
Zideku bihwsidj rcu fhoku

Key Points

  • Raymarching produces better quality shadows than rasterized shadows.
  • Hard shadows are not realistic, as there are generally multiple light sources in the real world.
  • Soft shadows give better transitions between areas in shadow and not.
  • Ambient occlusion does not depend on scene lighting, but on neighboring geometry. The closer geometry is to an area, the darker the area is.

Where to Go From Here?

In addition to the shadow types you learned in this chapter, there are other shadow techniques such as Screen Space Ambient Occlusion and Shadow Volumes. If you’re interested in learning about these, review references.markdown in the resources folder for this chapter.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.

You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now