How to draw a polygon in Metal

Oscar de la Hera Gomez
Three flowers that represent Swift, Metal and XCode side by side with the text "Polygon" beneath them.

A step by step tutorial on drawing a shape (i.e. a triangle, square, pentagon or circle), either filled or outlined, in a UIView using Metal.

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/polygon branch.

git clone git@github.com:delasign/swift-starter-project.git

Step One: Setup the MTKView

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

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

Step Two: Create the Uniforms

A screenshot of Xcode showing the Poylgon Uniforms struct that defines the dataset that will be passed down to the metal shader.

Under Models/Metal create a new file called PolygonUniforms.swift and paste in the code below.

This struct represents the data structure that will be passed from the UIView to the Metal shader.

Step Three: Update the Metal View UI File

A screenshot of Xcode showing the MetalUIView+UI.swift file. It highlights the lines that need to be removed and the location of the changes that need to be made to the vertex and fragment function names.

Remove the vertices and vertex buffer code from the MetalUIView+UI.swift file.

We do not need this code as in this tutorial we will be defining the position of the vertices in the shader file.

Subsequently, change the sample_vertex_main and sample_fragment_main to polygon_vertex_main and polygon_fragment_main.

This is updated in Step Five.

Step Four: Update the Metal View Delegate File

A screenshot of XCode showing the MetalUIView+MetalDelegate.swift file. The code for this file is found below.

Update the MetalUIView+MetalDelegate.swift file to the code below.

This code makes use of buffers that are passed to the metal shader and defines the number of vertices in the drawPrimitives function.

Step Five: Update the Metal Shader

A screenshot of XCode showing the PolygonMetalShader.metal file. The code is available below.

Update the metal shader to the code found below.

This code makes use of the Uniforms passed in on Step Four and applies the aspect ratio to make sure that the circle is drawn correctly.

Please note that we renamed SampleMetalShader.metal to PolygonMetalShader.metal, and have also renamed the vertex and fragment functions.

For more information on how all of this works, please consult the article linked below.

Step Six: Test

A screenshot of an iPhone showing a circle filled in.

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.

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.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details