How to draw an outlined shape with a line width in Metal

Setting the thickness of a stroke or the line width is not available in Metal, instead you must draw triangles to produce an outline of your shape.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 10/04/2023 at 11:30
Last Updated on 10/09/2023 at 17:31
<p>Three flowers that represent Swift, Metal and Xcode with the text "Line Width in Metal" beneath it.</p>

Setting the thickness of a stroke or the line width is not available in Metal, instead you must draw triangles to produce an outline of your shape.

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/line-thickness branch.

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

Step One: Setup the Project

<p>A screenshot of an iPhone that shows a pentagon and a circle each with different colors. This demonstrates that different shaders are being used.</p>

Follow the tutorial below to setup a MTKView within a UIView that draws multiple shapes, with multiple shaders.

How to use multiple shaders to draw multiple shapes within a MTKView

Step Two: Create the OutlinedPolygonUniforms struct

<p>A screenshot of Xcode showing the struct of the data that we will pass onto the metal shader. Code available below.</p>

Create a new file called OutlinedPolygonUniforms.swift and paste in the code below.

This file is responsible for defining the struct for the data that is passed in from Swift to the Metal shader.

How to pass variables or data to a Metal Shader in Xcode

Step Three: Create the Shader

<p>A screenshot of Xcode showing the shader that we developed to produce an outlined shape with a variable line thickness. Code available below.</p>

Create a new Metal file called LineThicknessColorWheelPolygonMetalShader.metal and paste in the code below.

This shader is responsible for drawing the shape with a variable line width.

The line width is calculated from the center.

Please note that this shader makes use of data from the Swift file and passes data from vertex shader to the fragment function. To learn more about this, consult the articles below.

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 Four: Update the Delegate

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

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

Key changes include:

  • The addition of the drawOutlinedPolygon function.
  • Calling the drawOutlinedPolygon function from the draw function.

Please note that this functionality draws multiple shapes, each of which uses a different vertex and fragment shader function. To learn more consult the article below.

How to use multiple shaders to draw multiple shapes within a MTKView

This function also passes data from this file to the metal shader, to learn how to do this consult the article below.

How to pass variables or data to a Metal Shader in XCode

Step Five: Test

<p>A screenshot of an iPhone showing two shapes drawn in metal. One is filled and the other is outlined with a stroke that we set in code. This proves the functionality works.</p>

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

One must be outlined and the other, filled.

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