Recompute pixelGeometry after resize
This commit is contained in:
parent
c809d18fd2
commit
6bcdc867c4
1 changed files with 27 additions and 14 deletions
|
@ -56,6 +56,14 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
private var pixelPipeline: MTLRenderPipelineState?
|
private var pixelPipeline: MTLRenderPipelineState?
|
||||||
private var marchingSquaresPipeline: 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 parametersBuffer: MTLBuffer?
|
private var parametersBuffer: MTLBuffer?
|
||||||
|
|
||||||
override public init() {
|
override public init() {
|
||||||
|
@ -148,13 +156,29 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func pixelGeometry(forAspectRatio aspectRatio: Float) -> [Vertex] {
|
||||||
|
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))
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
/// MARK: - MTKViewDelegate
|
/// MARK: - MTKViewDelegate
|
||||||
|
|
||||||
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
|
public func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) {
|
||||||
delegate?.renderSize = Size(size: size)
|
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)
|
||||||
|
|
||||||
|
// Reproject with the new aspect ratio.
|
||||||
if let buffer = parametersBuffer {
|
if let buffer = parametersBuffer {
|
||||||
let aspectRatio = Float(size.width / size.height)
|
|
||||||
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: 1.0, left: -aspectRatio, bottom: -1.0, right: aspectRatio, near: 0.0, far: 1.0)
|
||||||
let params = RenderParameters(projection: projectionMatrix)
|
let params = RenderParameters(projection: projectionMatrix)
|
||||||
memcpy(buffer.contents(), [params], MemoryLayout<RenderParameters>.size)
|
memcpy(buffer.contents(), [params], MemoryLayout<RenderParameters>.size)
|
||||||
|
@ -166,17 +190,6 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Two triangles, plus texture coordinates.
|
|
||||||
let points: [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))
|
|
||||||
]
|
|
||||||
|
|
||||||
field.update()
|
field.update()
|
||||||
|
|
||||||
if let buffer = commandQueue.makeCommandBuffer(),
|
if let buffer = commandQueue.makeCommandBuffer(),
|
||||||
|
@ -189,7 +202,7 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
let encoder = buffer.makeRenderCommandEncoder(descriptor: renderPass) {
|
let encoder = buffer.makeRenderCommandEncoder(descriptor: renderPass) {
|
||||||
encoder.label = "Pixel Render"
|
encoder.label = "Pixel Render"
|
||||||
encoder.setRenderPipelineState(pipeline)
|
encoder.setRenderPipelineState(pipeline)
|
||||||
encoder.setVertexBytes(points, length: points.count * MemoryLayout<Vertex>.stride, index: 0)
|
encoder.setVertexBytes(pixelGeometry, length: pixelGeometry.count * MemoryLayout<Vertex>.stride, index: 0)
|
||||||
encoder.setVertexBuffer(parametersBuffer, offset: 0, index: 1)
|
encoder.setVertexBuffer(parametersBuffer, offset: 0, index: 1)
|
||||||
encoder.setFragmentBuffer(field.parametersBuffer, offset: 0, index: 0)
|
encoder.setFragmentBuffer(field.parametersBuffer, offset: 0, index: 0)
|
||||||
encoder.setFragmentBuffer(field.ballBuffer, offset: 0, index: 1)
|
encoder.setFragmentBuffer(field.ballBuffer, offset: 0, index: 1)
|
||||||
|
@ -206,7 +219,7 @@ public class Renderer: NSObject, MTKViewDelegate {
|
||||||
let encoder = buffer.makeRenderCommandEncoder(descriptor: pass) {
|
let encoder = buffer.makeRenderCommandEncoder(descriptor: pass) {
|
||||||
encoder.label = "Marching Squares Render"
|
encoder.label = "Marching Squares Render"
|
||||||
encoder.setRenderPipelineState(pipeline)
|
encoder.setRenderPipelineState(pipeline)
|
||||||
encoder.setVertexBytes(points, length: points.count * MemoryLayout<Vertex>.stride, index: 0)
|
encoder.setVertexBytes(pixelGeometry, length: pixelGeometry.count * MemoryLayout<Vertex>.stride, index: 0)
|
||||||
encoder.setVertexBuffer(parametersBuffer, offset: 0, index: 1)
|
encoder.setVertexBuffer(parametersBuffer, offset: 0, index: 1)
|
||||||
encoder.setTriangleFillMode(.lines)
|
encoder.setTriangleFillMode(.lines)
|
||||||
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
|
encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 6)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue