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

17. Particle Systems
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

One of the many ways to create art and present science in code is by making use of particles. A particle is a tiny graphical object that carries basic information about itself such as color, position, life, speed and direction of movement.

Nothing explains a visual effect better than an image showing what you’ll be able to achieve at the end of this chapter:

Particle systems
Particle systems

Particle systems are widely used in:

  • Video games and animation: hair, cloth, fur.
  • Modeling of natural phenomena: fire, smoke, water, snow.
  • Scientific simulations: galaxy collisions, cellular mitosis, fluid turbulence.

Note: William Reeves is credited as being the “father” of particle systems. While at Lucasfilm, Reeves created the Genesis Effect in 1982 while working on the movie Star Trek II: The Wrath of Khan. Later, he joined Pixar Animation Studios where he’s still creating amazing animations using particles.

In a moment, you’ll get your feet wet trying out one such practical application: fireworks. But first, what exactly is a particle?

Particle

Newtonian dynamics describe the relationship between any small body — a particle — and the forces acting upon it, as well as its motion in response to those forces. Newton’s three laws of motion define the relationship between them. The first two laws define motion as a result of either inertia or force interference upon the particle’s current state of motion (stationary or moving). You’ll be working with them in this chapter. The third law, however, defines motion as a reaction of two or more particles interacting with each other. You’ll work with this law in Chapter 18, “Particle Behavior”.

A fourth law, if you wish, is the law of life. It’s not one of the Newtonian motion laws, but it does indeed apply to particles. Particles are born. They move and interact with the environment, and then they die.

Particle life
Particle life

You need a particle system to create fireworks. But first, you need to define a particle that has — at a minimum — a position, direction, speed, color and life. What makes a particle system cohesive, though, are emitters.

Emitter

An emitter is nothing more than a particle generator — in other words, a source of particles. You can make your particle system more exciting by having several emitters shooting out particles from different positions.

The Starter Project

➤ In Xcode, open the starter project for this chapter, then build and run it.

Rgi mluqrar zkuqaln
Gma ryetriz lrixarq

Creating a Particle and Emitter

➤ In the Shaders folder, open Common.h, and add a new structure:

The Compute Pipeline State Object

➤ In the Render Passes folder, open Pipelines.swift and add this to PipelineStates:

The Fireworks Pass

➤ Open Fireworks.swift, and add the pipeline states and initializer:

Clearing the Screen

The clear screen kernel function will run on every pixel in the drawable texture. The texture has a width and height, and is therefore a two dimensional grid.

Hcokorbi jsaizul xe kkuvy
Lkixatki clueruh fi ygeng

Dispatching the Particle Buffer

Now that you’ve cleared the screen, you’ll set up a new encoder to dispatch the particle buffer to the GPU.

Particle Dynamics

Particle dynamics makes heavy use of Newton’s laws of motion. Particles are considered to be small objects approximated as point masses. Since volume is not something that characterizes particles, scaling or rotational motion will not be considered. Particles will, however, make use of translation motion so they’ll always need to have a position.

Implementing Particle Physics

➤ Open Fireworks.metal and replace fireworks with:

Yizukozft!
Wovexoshd!

Particle Systems

The fireworks particle system was tailor-made for fireworks. But, particle systems can be very complex with many different options for particle movement, colors and sizes. In the Particles folder, the Emitter class in the starter project is a simple example of a generic particle system where you can create many different types of particles using a particle descriptor.

Qfekudg ujih dedi
Ysigarw olec loqi

Resetting the Scene

To add this new, more generic particle system, remove your fireworks simulation from Renderer.

Bonuv qtiyufm
Fafos cgasopc

Updating the Particle Structure

With a more complex particle system, you need to store more particle properties.

Rendering a Particle System

You’ll attach a texture to snow particles to improve the realism of your rendering. To render textured particles, as well as having a compute kernel to update the particles, you’ll also have to perform a render pass.

The Vertex and Fragment Functions

All right, it’s time to configure the shader functions.

Configuring Particle Effects

The particle computing and rendering structure is complete. All you have to do now is configure an emitter for snow particles.

O jvey qecdohfo yrgcag
O jjim nejdilya sngxed

Fire

Brrr… that snow is so cold. Let’s build a fire to warm you up!

Wace esm lfos
Copo unr rceb

Key Points

  • Particle emitters emit particles. These particles carry information about themselves, such as position, velocity and color.
  • Particle attributes can vary over time. A particle may have a life and decay after a certain amount of time.
  • As each particle in a particle system has the same attributes, the GPU is a good fit for updating them in parallel.
  • Particle systems, depending on given attributes, can simulate physics or fluid systems, or even hair and grass systems.

Where to Go From Here?

You’ve only just begun playing with particles. There are many more particle characteristics you could include in your particle system:

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