About the Metal by Tutorials category

The official forum for all discussions about the book Metal by Tutorials, available here: https://store.raywenderlich.com/products/metal-by-tutorials

question
i have been thinking about buying this book
but it seems that the code is outdated (its last update is iOS 13)
how much of the book’s code still work on iOS 14 ?

Hi @kvebeeck!

Not all the code works, but most of it does. There are some changes in the few places we use playgrounds, and the SpriteKit rendering in Chapter 22 doesn’t work.

Where the code doesn’t work, I have answered here in this forum, and if you need something answered I generally get to it within twenty-four hours.

There are also a few problems with some MacBooks that don’t have a discrete GPU.

If you want to wait, however, we’ll very likely start working on updating around March, which means probably May - June release.

I believe the code is now public: GitHub - raywenderlich/met-materials at editions/2.0

I am new in Swift and Metal, so i like ‘Metal by Tutorials’ because it’s written in easy English - i am NOT a native.

I have a question about storyboard environment in Xcode12.

I try ‘3DTransforms.playground’ in Xcode12 but it cannot work with a error message like:
Fatal error: Unexpectedly found nil while unwrapping an Optional value: file N3DTransforms_Sources/Utility.swift, line 21

We can read
line20: let path = Bundle.main.path(forResource: “Shaders”, ofType: “metal”)
line21: let source = try String(contentsOfFile: path!, encoding: .utf8)
in ‘Utility.swift’.

But this nice code works fine in Xcode11.
I guess Xcode12 cannot find ‘Shaders.metal’ in ‘Resources’ folder.

How to fix it?

Sorry, i fix it like the following:

    /* xcode12 cannot find 'Shaders.metal' in Resources folder.
        instead, we pass its code to  'shader' directly. h.inomata
    let path = Bundle.main.path(forResource: "Shaders", ofType: "metal")
    let source = try String(contentsOfFile: path!, encoding: .utf8)
     */
    let shader = """
    #include <metal_stdlib>
    using namespace metal;
    struct VertexOut {
      float4 position [[position]];
      float point_size [[point_size]];
    };

    vertex VertexOut vertex_main(constant float3 *vertices [[buffer(0)]],
                                 uint id [[vertex_id]])
    {
      VertexOut vertex_out {
        .position = float4(vertices[id], 1),
        .point_size = 20.0
      };
      return vertex_out;
    }

    fragment float4 fragment_main(constant float4 &color [[buffer(0)]])
    {
      return color;
    }
    """
    library = try device.makeLibrary(source: shader, options: nil)

It is OK?

Hi @hiroaki-inomata and welcome to the forums :blush:. I’m glad you are able to understand the writing!

Apple recently changed the way to make libraries in playgrounds. It looks as if your solution will work.

See also Chapter 4 error - #2 by caroline

1 Like

Hi @caroline

Thank you for your quick response!

public func createLibrary() → MTLLibrary {
device.makeDefaultLibrary()!
}

I have confirmed that the above fix works well :smile:

1 Like

Hi)
What year did you mean when you talked about the upcoming release?

Ugh. It got delayed. I should never make forecasts :unamused:.
I have been working on an update over the last few months so I have every expectation that 2022 will be the year :grin:

1 Like