@@ -36,11 +36,21 @@ class MetalMetaballRenderer: MetaballRenderer {
36 func updateTargetView() {
36 func updateTargetView() {
37 let metaballs = dataSource.metaballs
37 let metaballs = dataSource.metaballs
39+ // Re-alloc buffers if frame has changed
39 if previousFrame != targetView.frame {
40 if previousFrame != targetView.frame {
40 previousFrame = targetView.frame
41 previousFrame = targetView.frame
41 updateFrame()
42 updateFrame()
45+ // Render graphics to metal texture
46+ renderToRenderingTexture(metaballs: metaballs)
47+ // Transform metal texture into image
48+ let uiimage = image(texture: renderingTexture)
49+ // Send image to view
50+ targetView.image = uiimage
53+ func renderToRenderingTexture(metaballs metaballs: [Metaball]) {
44 let width = Int(targetView.width)
54 let width = Int(targetView.width)
45 let height = Int(targetView.height)
55 let height = Int(targetView.height)
@@ -79,13 +89,10 @@ class MetalMetaballRenderer: MetaballRenderer {
80 commandBuffer.commit()
90 commandBuffer.commit()
81 commandBuffer.waitUntilCompleted()
91 commandBuffer.waitUntilCompleted()
83 } catch _ {
93 } catch _ {
84 print("Error!")
94 print("Error!")
87− // Transform new metal texture into image
88− targetView.image = image(texture: renderingTexture)
91 func image(texture texture: MTLTexture) -> UIImage {
98 func image(texture texture: MTLTexture) -> UIImage {