By now, you might have all noticed I am in love with
Swift. I am also an avid user of
Xcode playgrounds. This week we will take our
Metal code and put it into a playground. Huzzah, for
Metal prototyping in the playgrounds!
Let’s start by creating a new
Xcode playground for
OS X. Once created, click the
Show the Assistant editor button and also press
Command + 1 to show the
Project navigator as well. Your playground should look like this:
First thing we do is create Shaders.metal under the Resources folder in the
Project navigator with the same source code from the previous episode of this series. Next, we create MathUtils.swift and MetalView.swift under the Sources folder. The only change we make in
MathUtils.swift is we create a handy initializer for the
MetalView.swift we need to make more changes though. First, we need to make the class public as we will call it from outside the
Sources folder. Consequently, the initializer and the drawRect(:) method need to become
public as well. Also, we will create a second initializer so we can create a
MetalView with a given frame:
Next, we need to make a rather curious change because as you notice, creating a
fails with this error message:
Since the playground does not have a default
filepath that we could use, we will need to create our own:
Notice that we tell the playground to find a path where a resource named
Shaders of type
metal exists. Next, we convert that file into a large
String and then we create the library from this source.
Finally, we go to the playground’s main page and create a new
MetalView with a given frame. Then we tell the playground to present us the live view:
If you are showing the
Timeline in the
Assistant editor you should have a similar view:
The source code is posted on Github as usual.
Until next time!