How to code a MTKView that draws multiple shapes

A step by step tutorial on drawing multiple shapes, filled or outlined, in a UIView using Metal.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/09/2023 at 12:14
Last Updated on 10/09/2023 at 13:35
<p>Three flowers that represent Swift, Metal and Xcode with the text "Multiple Shapes" beneath them.</p>

A step by step tutorial on drawing multiple shapes, filled or outlined, 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/multiple-shapes branch.

View Repository

Step One: Setup the MTKView

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

Follow the tutorial below to setup a MTKView within a UIView that draws a polygon which features color interpolation.

How to use color interpolation in a Metal shader

Step Two: Update the Uniforms struct

<p>A screenshot of Xcode showing the PolygonUniforms struct with the updated dataset.</p>

Add the origin to the PolygonUniforms struct as a SIMD2<Float>.

SIMD2<Float> is the equivalent of the float2 in metal.

Step Three: Update the Delegate

<p>A screenshot of Xcode showing the updated MetalUIView+MetalDelegate.swift file. Code available below.</p>

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

The important changes here are:

  • We have made the draw polygon functionality its own function.
  • From within the draw function, we call the draw polygon function multiple times to draw multiple shapes.

Please note that the current methodology uses a single shader.

Step Four: Update the Metal Shader

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

Update the metal shader to the code below.

The key addition here is the addition of the origin coordinate and how the code is adapted to implement the origin coordinate.

Step Four: Test

<p>A screenshot of an iPhone showing an outlined Pentagon above a filled circle. Both outlined and filled shape show a gradient that covers all the RGB colors available.</p>

Run the code on a device and confirm that multiple shapes appear.

As demonstrated above, the code is expected to let you draw both outlined shapes and filled shapes within the same draw function.

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