Inside refreshVertexBuffer, you call LiquidFun.particleCountForSystem to get the number of particles in the system, and store the result in particleCount. Here you add two new properties, particleCount to keep track of how many particles you have, and vertexBuffer to store the MTLBuffer Metal requires to access the vertex positions.
VertexBuffer = device.newBufferWithBytes(positions, length: bufferSize, options: nil) Let bufferSize = sizeof(Float) * particleCount * 2 Let positions = LiquidFun.particlePositionsForSystem(particleSystem) ParticleCount = Int(LiquidFun.particleCountForSystem(particleSystem)) Still in ViewController.swift, add the following properties and new method: Metal needs this information to know where to render your particles on the screen. The next step is to prepare a buffer that contains the positions of each particle in your LiquidFun world. You call createMetalLayer from viewDidLoad to ensure your Metal layer is set up along with the view. Next, you create a CAMetalLayer with default properties and add it as a sublayer to your current view’s main layer. Inside createMetalLayer, you store a reference to an MTLDevice, which you’ll use later to create the other Metal objects that you’ll need. Now replace printParticleInfo() in viewDidLoad with a call to this new method: As mentioned earlier, the iOS Simulator does not support Metal at the time of writing this tutorial, and Xcode won’t even let you compile Metal code without a valid target. Note: If you get a compiler error at this point, make sure you set the app to target your Metal-compatible iOS device. To keep this part short, I’ll breeze through the basic setup of Metal and focus only on new concepts that aren’t in the other Metal tutorials on our site. Getting Startedįirst, make sure you have a copy of the project from Part 1, either by going through the first tutorial or by downloading the finished project.īefore proceeding with Metal, I recommend going through the Introduction to Metal Tutorial if you haven’t already. You’ll also get to move them around in a simulated physics world for some water-splashing fun.Īfter all, you didn’t name your project LiquidMetal for nothing.
In this second part of the series, you’ll learn how to render your LiquidFun particles onscreen using projection transformations, uniform data and shaders in Metal. In the first part of the series, you learned how to integrate LiquidFun with Swift and used that knowledge to create an invisible liquid particle system. Welcome back to our 2-part tutorial series that teaches you how to use LiquidFun with Metal and Swift!
Update : Updated for Xcode 6.3 / Swift 1.2.