How to use color interpolation in a Metal shader

A step by step tutorial on drawing a filled or outlined shape that interpolates color values between vertices in a UIView using Metal.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/09/2023 at 09:30
Last Updated on 10/09/2023 at 11:31
<p>Three flowers that represent Swift, Metal and XCode side by side. Beneath them sits the text "Color Interpolation."</p>

A step by step tutorial on drawing a filled or outlined shape that interpolates color values between vertices in a UIView using Metal.

SubscribeCheck out our Apple Metal Resources Guide

We recommend that you clone our Open Source Swift Starter Project, checking out the main branch and carrying out the steps below. The changes can be found on the tutorial/metal/interpolation branch.

git clone git@github.com:delasign/swift-starter-project.git
View Repository
<p>A triangle that has different colors on its edges (i.e. vertices). The diagram shows how the triangle calculates the colors within the triangle depending on the color of the vertices.</p>

The tutorial outlined below supplies a position and color for each vertex, and the render pipeline uses that data to render the triangle, interpolating color values between the colors specified for the triangle’s vertices.

This behavior is considered out-of-the-box and automatic. To learn more consult the guide below.

Using a Render Pipeline to Render Primitives

Step One: Setup the MTKView

<p>A screenshot of an iPhone showing a black background with a red triangle that was produced with metal.</p>

Follow the tutorial below to setup a MTKView within a UIView that shows a polygon.

How to draw a polygon in Metal

Step Two: Update the Metal Shader

<p>A screenshot of Xcode showing the updated metal shader. Code is available below.</p>

Update the metal shader to the code found below.

This metal shader:

  • Determine the color based on the angular position of the vertex to create a color wheel.
  • Passes data from the swift file to the metal shader using a methodology explained in a link below.
  • Passes data from the vertex function to the fragment function using a methodology explained in a link below.

Please note that we renamed the Metal shader to ColorWheelPolygonMetalShader.metal.

How to pass variables or data to a Metal Shader in XCodeHow to pass data from the vertex to the fragment function in Metal

Step Three: Test

<p>A screenshot of an iPhone showing a circle with all the RGB colors that are available.</p>

Run the code on a device and confirm that the shader works as expected.

Looking to learn more about things you can do with Swift, Metal and XCode ?

Search our blog to find educational content on learning how to use Swift, Metal and XCode.

Search our Blog

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe