168 lines
5.5 KiB
Swift
168 lines
5.5 KiB
Swift
//
|
|
// MarchingSquares.swift
|
|
// Metaballs
|
|
//
|
|
// Created by Eryn Wells on 10/11/18.
|
|
// Copyright © 2018 Eryn Wells. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import Metal
|
|
|
|
class MarchingSquares {
|
|
private var field: Field
|
|
private var sampleGridSize: Size
|
|
|
|
/// Samples of the field's current state.
|
|
private(set) var samples: MTLTexture?
|
|
/// Indexes of geometry to render.
|
|
private(set) var indexes: MTLTexture?
|
|
|
|
private(set) var gridGeometry: MTLBuffer?
|
|
|
|
private var xSamples: Int {
|
|
return Int(field.size.x / sampleGridSize.x)
|
|
}
|
|
|
|
private var ySamples: Int {
|
|
return Int(field.size.y / sampleGridSize.y)
|
|
}
|
|
|
|
private var xGridlinesCount: Int {
|
|
let xSamples = Int(field.size.x / sampleGridSize.x)
|
|
return xSamples - 1
|
|
}
|
|
|
|
private var yGridlinesCount: Int {
|
|
let ySamples = Int(field.size.y / sampleGridSize.y)
|
|
return ySamples - 1
|
|
}
|
|
|
|
var gridVertexCount: Int {
|
|
return xGridlinesCount * 2 + yGridlinesCount * 2
|
|
}
|
|
|
|
init(field: Field) {
|
|
self.field = field
|
|
sampleGridSize = Size(16)
|
|
}
|
|
|
|
func setupMetal(withDevice device: MTLDevice) {
|
|
guard xSamples > 1 && ySamples > 1 else {
|
|
return
|
|
}
|
|
|
|
let samplesDesc = MTLTextureDescriptor()
|
|
samplesDesc.textureType = .type2D
|
|
samplesDesc.width = xSamples
|
|
samplesDesc.height = ySamples
|
|
samplesDesc.pixelFormat = .r32Float
|
|
samples = device.makeTexture(descriptor: samplesDesc)
|
|
|
|
let indexesDesc = MTLTextureDescriptor()
|
|
indexesDesc.textureType = .type2D
|
|
indexesDesc.width = xSamples - 1
|
|
indexesDesc.height = ySamples - 1
|
|
indexesDesc.pixelFormat = .a8Unorm
|
|
indexes = device.makeTexture(descriptor: indexesDesc)
|
|
|
|
let gridGeometryLength = MemoryLayout<Vertex>.stride * gridVertexCount
|
|
gridGeometry = device.makeBuffer(length: gridGeometryLength, options: .storageModeShared)
|
|
populateGridGeometryBuffer()
|
|
}
|
|
|
|
func fieldDidResize() {
|
|
guard let gridGeometry = gridGeometry else {
|
|
return
|
|
}
|
|
let gridGeometryLength = MemoryLayout<Vertex>.stride * gridVertexCount
|
|
self.gridGeometry = gridGeometry.device.makeBuffer(length: gridGeometryLength, options: .storageModeShared)
|
|
populateGridGeometryBuffer()
|
|
}
|
|
|
|
private func populateGridGeometryBuffer() {
|
|
guard let gridGeometry = gridGeometry else {
|
|
return
|
|
}
|
|
|
|
print("Rebuilding gridlines")
|
|
|
|
var vertices = [Vertex]()
|
|
|
|
let fieldSizeX = Float(field.size.x)
|
|
let fieldSizeY = Float(field.size.y)
|
|
|
|
for x in 1..<xSamples {
|
|
let xCoord = Float(x * Int(sampleGridSize.x))
|
|
vertices.append(Vertex(position: Float2(xCoord, 0), textureCoordinate: Float2()))
|
|
vertices.append(Vertex(position: Float2(xCoord, fieldSizeY), textureCoordinate: Float2()))
|
|
}
|
|
|
|
for y in 1..<ySamples {
|
|
let yCoord = Float(y * Int(sampleGridSize.y))
|
|
vertices.append(Vertex(position: Float2(0, yCoord), textureCoordinate: Float2()))
|
|
vertices.append(Vertex(position: Float2(fieldSizeX, yCoord), textureCoordinate: Float2()))
|
|
}
|
|
|
|
memcpy(gridGeometry.contents(), vertices, MemoryLayout<Vertex>.stride * vertices.count)
|
|
}
|
|
|
|
func sampleField() {
|
|
guard let samples = samples else { return }
|
|
|
|
let bytesPerRow = samples.width * MemoryLayout<Float>.stride
|
|
|
|
for xSample in 0..<samples.width {
|
|
let x = Float(xSample * Int(sampleGridSize.x))
|
|
for ySample in 0..<samples.height {
|
|
let y = Float(ySample * Int(sampleGridSize.y))
|
|
let sample = [field.sample(at: Float2(x: x, y: y))]
|
|
|
|
let origin = MTLOrigin(x: xSample, y: ySample, z: 0)
|
|
let size = MTLSize(width: 1, height: 1, depth: 1)
|
|
let region = MTLRegion(origin: origin, size: size)
|
|
samples.replace(region: region, mipmapLevel: 0, withBytes: sample, bytesPerRow: bytesPerRow)
|
|
}
|
|
}
|
|
}
|
|
|
|
func populateIndexes() {
|
|
guard let indexes = indexes else { return }
|
|
|
|
let bytesPerRow = indexes.width * MemoryLayout<UInt8>.stride
|
|
|
|
for x in 0..<indexes.width {
|
|
for y in 0..<indexes.height {
|
|
guard let samples = getSampleBlock(x: x, y: y) else {
|
|
continue
|
|
}
|
|
|
|
let index = (samples[0] > 1.0 ? 0b1000 : 0) +
|
|
(samples[1] > 1.0 ? 0b0100 : 0) +
|
|
(samples[2] > 1.0 ? 0b0001 : 0) +
|
|
(samples[3] > 1.0 ? 0b0010 : 0)
|
|
|
|
let origin = MTLOrigin(x: x, y: y, z: 0)
|
|
let size = MTLSize(width: 1, height: 1, depth: 1)
|
|
let region = MTLRegion(origin: origin, size: size)
|
|
let indexArr = [index]
|
|
indexes.replace(region: region, mipmapLevel: 0, withBytes: indexArr, bytesPerRow: bytesPerRow)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func getSampleBlock(x: Int, y: Int) -> [Float]? {
|
|
guard let samples = samples else {
|
|
return nil
|
|
}
|
|
|
|
var block: [Float] = [0, 0, 0, 0]
|
|
let bytesPerRow = samples.width * MemoryLayout<Float>.stride
|
|
let origin = MTLOrigin(x: x, y: y, z: 0)
|
|
let size = MTLSize(width: 2, height: 2, depth: 1)
|
|
let region = MTLRegion(origin: origin, size: size)
|
|
samples.getBytes(&block, bytesPerRow: bytesPerRow, from: region, mipmapLevel: 0)
|
|
|
|
return block
|
|
}
|
|
}
|