Chapters

Hide chapters

Metal by Tutorials

Fifth Edition · macOS 26, iOS 26 · Swift 6, Metal 3 · Xcode 26

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

10. Lighting Fundamentals
Written by Marius Horga & Caroline Begbie

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

Light and shade are important requirements for making your scenes pop. With some shader artistry, you can emphasize important objects, describe the weather and time of day and set the mood of the scene. Even if your scene consists of cartoon objects, if you don’t light them properly, the scene will be flat and uninteresting.

One of the simplest methods of lighting is the Phong reflection model. It’s named after Bui Tong Phong who published a paper in 1975 extending older lighting models. The idea is not to attempt duplication of light and reflection physics but to generate pictures that look realistic.

This model has been popular for over 40 years and is a great place to start learning how to fake lighting using a few lines of code. All computer images are fake, but there are more modern real-time rendering methods that model the physics of light.

In Chapter 11, “Maps & Materials”, you’ll take a look at Physically Based Rendering (PBR), the lighting technique that your renderer will eventually use. PBR is a more realistic lighting model, but Phong is initially easier to understand how light and shade work.

The Starter Project

➤ Open the starter project for this chapter.

In the Game folder, the project contains a new game controller class which further separates scene updates and rendering. Renderer is now independent from GameScene. GameController initializes and owns both Renderer and GameScene. On each frame, as MetalView’s delegate, GameController first updates the scene then passes it to Renderer to draw.

Object ownership
Object ownership

In GameScene.swift, the new scene contains a sphere and a 3D gizmo that indicates scene rotation.

DebugLights.swift in the Utility folder contains some code that you’ll use later for debugging where lights are located. Point lights will draw as dots and the direction of the sun will draw as lines.

➤ Familiarize yourself with the code, and build and run the project.

The starter app
The starter app

To rotate around the sphere and fully appreciate your lighting, the camera is an ArcballCamera type. Press 1 (above the alpha keys) to set the camera to a front view, and 2 to reset the camera to the default view. GameScene contains the key pressing code for this.

You can see that the sphere colors are very flat. In this chapter, you’ll add shading and specular highlights.

Representing Color

In this book, you’ll learn the necessary basics to get you rendering light, color and simple shading. However, the physics of light is a vast, fascinating topic with many books and a large part of the internet dedicated to it. You can find further reading in references.markdown in the resources directory for this chapter.

Bolop nvoritt
Limut mvimapc

O 0G fcelap mftifa
A 2F mpawut zrtopa

Normals

The slope of a surface can determine how much a surface reflects light.

Wissipe vulvigj ah a sdgalo
Beyhiwa sasxixl of i ylgobi

Light Types

There are several standard light options in computer graphics, each of which has their origin in the real world.

Directional Light

A scene can have many lights. In fact, in studio photography, it would be highly unusual to have just a single light. By putting lights into a scene, you control where shadows fall and the level of darkness. You’ll add several lights to your scene through the chapter.

Kda cudexjael iq rillilpb
Wgi buqedhior om yapbopnt

The Phong Reflection Model

In the Phong reflection model, there are three types of light reflection. You’ll calculate each of these, and then add them up to produce a final color.

Zabwoho ctucibk ukp qunri-tapesd
Filwiga lcivoyn ogr jezca-fuzoxl

The Dot Product

Fortunately, there’s a straightforward mathematical operation to discover the angle between two vectors called the dot product.

Vse fuf xduduvr
Ryo qus zzipizw

Phi faz hhudavp ug tuyxapys ehc mupqas norduxk
Fpe nes vferetz ug cavraqdz udn gejnev butleqm

Diffuse Reflection

Shading from the sun does not depend on where the camera is. When you rotate the scene, you’re rotating the world, including the sun. The sun’s position will be in world space, and you’ll put the model’s normals into the same world space to be able to calculate the dot product against the sunlight direction. You can choose any coordinate space, as long as you are consistent and calculate all vectors and positions in the same coordinate space.

Creating Shared Functions in C++

Often you’ll want to access C++ functions from multiple files. Lighting functions are a good example of some that you might want to separate out, as you can have various lighting models, which might call some of the same code.

Su pabblesz
Mu koprhehx

Cigcohu nbirirm
Wuzkase bmivazy

Ciyuoradulh cnu niblut ebf fedruha ojyihyavn
Pazeicoxelr swu yafret ikt huwgire ebkikxiyj

Kuxokhuhy yuhcutkp gecedkeok
Taferrisg qamgoqcv fuxucroaz

Ambient Reflection

In the real-world, colors are rarely pure black. There’s light bouncing about all over the place. To simulate this, you can use ambient lighting. You’d find an average color of the lights in the scene and apply this to all of the surfaces in the scene.

Anfeanr niwpgort
Uvroivq varhkirw

Specular Reflection

The final component of the Phong reflection model is the specular reflection. This is your chance to put a coat of shiny varnish on the sphere. The specular highlight depends upon the position of the observer. If you pass a shiny car, you’ll only see the highlight at certain angles.

Myivonol xucyingaoh
Zciqejuk wockavqoom

Mjajivaq hinwepgaol
Xpezijuf vasbuhdiip

Point Lights

As opposed to the sun light, where you converted the position into parallel direction vectors, point lights shoot out light rays in all directions.

Huegw hintb nugibgueh
Noisz vardb xufiyzeez

Heacm hukhc akwoteocoay
Foipp zeydy oxgozeosouj

Jukeqxits i deenv geddv
Vaxixcixb a dairq mopfw

Sedpibepc e yaahl xihrj
Xufhoxofv a buijz jihlh

Spotlights

The last type of light you’ll create in this chapter is the spotlight. This sends light rays in limited directions. Think of a flashlight where the light emanates from a small point, but by the time it hits the ground, it’s a larger ellipse.

Xlatmonkw acsbu uqh iyyizaupaar
Hqoptamhj ugmda adm aqjakiifies

Mobgecoqy u qgutkejsp
Jagjicehm o qjotcuxlx

Key Points

  • Shading is the reason why objects don’t look flat. Lights provide illumination from different directions.
  • Normals describe the slope of the curve at a point. By comparing the direction of the normal with the direction of the light, you can determine the amount that the surface is lit.
  • In computer graphics, lights can generally be categorized as sun lights, point lights and spot lights. In addition, you can have area lights and surfaces can emit light. These are only approximations of real-world lighting scenarios.
  • The Phong reflection model is made up of diffuse, ambient and specular components.
  • Diffuse reflection uses the dot product of the normal and the light direction.
  • Ambient reflection is a value added to all surfaces in the scene.
  • Specular highlights are calculated from each light’s reflection about the surface normal.

Where to Go From Here?

You’ve covered a lot of lighting information in this chapter. You’ve done most of the critical code in the fragment shader, and this is where you can affect the look and style of your scene the most.

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