kodeco.com Forums

iOS 8 Metal Tutorial with Swift: Getting Started

Learn how to use Apple's new low-level and high performance 3D graphics API in this iOS 8 Metal tutorial.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/2318-ios-8-metal-tutorial-with-swift-getting-started

Updated with Swift 2.2

https://github.com/rivertea/HelloMetal

Hello, thanks for this amazing tutorial it’s really helpful to start learning about metal :slight_smile:
Also I need a little help for this question :

  1. How to draw multiple triangles instead of one ?
  2. How to draw a point ?

Thank you :wink:

I got the error Use of unresolved identifier ‘drawable’. I noticed in the swift 2.2 version above the line “let drawable = metalLayer.nextDrawable()!” is there but above its not written…

Hi,

I tried to convert this to Xcode 8 and Swit 3, some of the functions will be converted automatically and some issues I found by myself, but the following Code is impossible to fix for me:

if let renderEncoder = renderEncoderOpt { 
            renderEncoder.setRenderPipelineState(pipelineState)
            renderEncoder.setVertexBuffer(vertexBuffer, offset: 0, atIndex: 0)
            renderEncoder.drawPrimitives(.Triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1)
            renderEncoder.endEncoding()
        }

The compiler always reports me:

Initializer for conditional binding must have Optional type, not ‘MTLRenderCommandEncoder’

It complains about the line if let renderEncoder = renderEncoderOpt { . Can someone help me to fix this issue?

Thanks in advance.

Try this… Swift 3 doesn’t return an Optional like it used to…

let renderEncoderOpt = commandBuffer.makeRenderCommandEncoder(descriptor: renderPassDescriptor) renderEncoderOpt.setRenderPipelineState(pipelineState) renderEncoderOpt.setVertexBuffer(vertexBuffer, offset: 0, at: 0) renderEncoderOpt.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3, instanceCount: 1) renderEncoderOpt.endEncoding()

Hi,

it works. Thanks.