From 3399bbcd53551787baa3cc3289d215c425995889 Mon Sep 17 00:00:00 2001 From: Eryn Wells Date: Sat, 13 Oct 2018 21:29:38 -0700 Subject: [PATCH] Fix the projection stuff Geometry also in window coordinates, just to make things easier... --- MetaballsKit/Math.swift | 2 +- MetaballsKit/Renderer.swift | 37 ++++++++++++++++++------------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/MetaballsKit/Math.swift b/MetaballsKit/Math.swift index 57a3640..f0465b7 100644 --- a/MetaballsKit/Math.swift +++ b/MetaballsKit/Math.swift @@ -70,7 +70,7 @@ extension Matrix4x4 { Float4(0.0, 0.0, -2.0 / (far - near), -(far + near) / (far - near)), Float4(0.0, 0.0, 0.0, 1.0) ] - return Matrix4x4(rows) + return Matrix4x4(rows: rows) } } diff --git a/MetaballsKit/Renderer.swift b/MetaballsKit/Renderer.swift index 4239119..6e70ae2 100644 --- a/MetaballsKit/Renderer.swift +++ b/MetaballsKit/Renderer.swift @@ -56,14 +56,7 @@ public class Renderer: NSObject, MTKViewDelegate { private var pixelPipeline: MTLRenderPipelineState? private var marchingSquaresPipeline: MTLRenderPipelineState? - private var pixelGeometry: [Vertex] = [ - Vertex(position: Float2(x: 1, y: -1), textureCoordinate: Float2(x: 1, y: 0)), - Vertex(position: Float2(x: -1, y: -1), textureCoordinate: Float2(x: 0, y: 0)), - Vertex(position: Float2(x: -1, y: 1), textureCoordinate: Float2(x: 0, y: 1)), - Vertex(position: Float2(x: 1, y: -1), textureCoordinate: Float2(x: 1, y: 0)), - Vertex(position: Float2(x: -1, y: 1), textureCoordinate: Float2(x: 0, y: 1)), - Vertex(position: Float2(x: 1, y: 1), textureCoordinate: Float2(x: 1, y: 1)) - ] + private var pixelGeometry: [Vertex]? private var parametersBuffer: MTLBuffer? private var inFlightSemaphore: DispatchSemaphore @@ -160,14 +153,17 @@ public class Renderer: NSObject, MTKViewDelegate { } } - private func pixelGeometry(forAspectRatio aspectRatio: Float) -> [Vertex] { + private func pixelGeometry(forViewSize size: CGSize) -> [Vertex] { + let w = Float(size.width) + let h = Float(size.height) return [ - Vertex(position: Float2(x: aspectRatio, y: -1), textureCoordinate: Float2(x: 1, y: 0)), - Vertex(position: Float2(x: -aspectRatio, y: -1), textureCoordinate: Float2(x: 0, y: 0)), - Vertex(position: Float2(x: -aspectRatio, y: 1), textureCoordinate: Float2(x: 0, y: 1)), - Vertex(position: Float2(x: aspectRatio, y: -1), textureCoordinate: Float2(x: 1, y: 0)), - Vertex(position: Float2(x: -aspectRatio, y: 1), textureCoordinate: Float2(x: 0, y: 1)), - Vertex(position: Float2(x: aspectRatio, y: 1), textureCoordinate: Float2(x: 1, y: 1)) + Vertex(position: Float2(x: w, y: h), textureCoordinate: Float2(x: 1, y: 1)), + Vertex(position: Float2(x: 0, y: h), textureCoordinate: Float2(x: 0, y: 1)), + Vertex(position: Float2(x: 0, y: 0), textureCoordinate: Float2(x: 0, y: 0)), + + Vertex(position: Float2(x: w, y: h), textureCoordinate: Float2(x: 1, y: 1)), + Vertex(position: Float2(x: 0, y: 0), textureCoordinate: Float2(x: 0, y: 0)), + Vertex(position: Float2(x: w, y: 0), textureCoordinate: Float2(x: 1, y: 0)) ] } @@ -176,14 +172,12 @@ public class Renderer: NSObject, MTKViewDelegate { public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) { delegate?.renderSize = Size(size: size) - let aspectRatio = Float(size.width / size.height) - // Generate a new surface to draw the pixel version on - pixelGeometry = pixelGeometry(forAspectRatio: aspectRatio) + pixelGeometry = pixelGeometry(forViewSize: size) // Reproject with the new aspect ratio. if let buffer = parametersBuffer { - let projectionMatrix = Matrix4x4.orthographicProjection(top: 1.0, left: -aspectRatio, bottom: -1.0, right: aspectRatio, near: 0.0, far: 1.0) + let projectionMatrix = Matrix4x4.orthographicProjection(top: 0, left: 0, bottom: Float(size.height), right: Float(size.width), near: -1.0, far: 1.0) let params = RenderParameters(projection: projectionMatrix) memcpy(buffer.contents(), [params], MemoryLayout.size) } @@ -209,6 +203,11 @@ public class Renderer: NSObject, MTKViewDelegate { field.update() + if self.pixelGeometry == nil { + self.pixelGeometry = self.pixelGeometry(forViewSize: view.drawableSize) + } + let pixelGeometry = self.pixelGeometry! + if let renderPass = view.currentRenderPassDescriptor { // Render the per-pixel metaballs if let pipeline = pixelPipeline,