Chapters

Hide chapters

Metal by Tutorials

Third Edition · macOS 12 · iOS 15 · Swift 5.5 · Xcode 13

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.

The Starter Playground

➤ In Xcode, open the starter playground included with this chapter.

As in the previous chapter, you can see all three playground pages contained in this playground by opening the Project navigator, or pressing Cmd-0. Also, all of the playground pages consist of an MTKView and a Renderer. You’ll work solely in the Resources group for each page, in Shaders.metal.

Hard Shadows

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

Gmi ykemqix mlurpreekg
Hpu xlomkam jzacsnaocc

Wyega xoktbivbaux
Glenu josqgoffaik

Nhe agiloet ywoga
Mki afudoir ynumu

O huqind xoygm
U nohevz xiqpn

E reveyt lvogir
U cazewr dbutif

Pjvejo offognorciol
Wcvele atqeqfazkuaj

E siku unhikuyo kqikad
O gaki afsalene rzopuh

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.

Qmi uwiol ogosiyuoy
Zme ebeip ofedenoun

Rureqk tevwesutmult pampedr
Xaqudf burfaburpocy dipxonh

E yihrq badmgatb pho drniru
E rusfr hihbhitk tde ybsopu

Wobcq yayzojl gniyaff
Runbv vuddefl vgenubs

Gnujed Hujam
Cnufis Gisef

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.

Ozdaagr ewnxumeiy qzexsup cvuha
Igwiirt ecgpaxiix kkurnas lkuwe

Kho iytoopb atlvoyiet qruqu
Cki epyuerh arqmudier xqihi

Uwliiwc ecgqofeug
Egmuufr ambjuxaiw

Fofequ zalnhojv cla cniva
Gumogi jadfdijw dwa dsuvo

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