First pass at a marching squares idea
This commit is contained in:
parent
d6b8d97b49
commit
09e9bf1f1a
2 changed files with 70 additions and 0 deletions
|
@ -46,6 +46,8 @@
|
||||||
C0FF7CBC216B002E0081B781 /* PreferencesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C091616F1F3F5AE6009C4263 /* PreferencesViewController.swift */; };
|
C0FF7CBC216B002E0081B781 /* PreferencesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C091616F1F3F5AE6009C4263 /* PreferencesViewController.swift */; };
|
||||||
C0FF7CBF216B00570081B781 /* MetalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C0FF7CBD216B00570081B781 /* MetalKit.framework */; };
|
C0FF7CBF216B00570081B781 /* MetalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C0FF7CBD216B00570081B781 /* MetalKit.framework */; };
|
||||||
C0FF7CC0216B00570081B781 /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C0FF7CBE216B00570081B781 /* Metal.framework */; };
|
C0FF7CC0216B00570081B781 /* Metal.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C0FF7CBE216B00570081B781 /* Metal.framework */; };
|
||||||
|
C0FF7CC2217020820081B781 /* MarchingSquares.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0FF7CC1217020820081B781 /* MarchingSquares.swift */; };
|
||||||
|
C0FF7CC3217020820081B781 /* MarchingSquares.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0FF7CC1217020820081B781 /* MarchingSquares.swift */; };
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
/* Begin PBXContainerItemProxy section */
|
/* Begin PBXContainerItemProxy section */
|
||||||
|
@ -105,6 +107,7 @@
|
||||||
C0FF7CB1216AFD890081B781 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
C0FF7CB1216AFD890081B781 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||||
C0FF7CBD216B00570081B781 /* MetalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MetalKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/MetalKit.framework; sourceTree = DEVELOPER_DIR; };
|
C0FF7CBD216B00570081B781 /* MetalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MetalKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/MetalKit.framework; sourceTree = DEVELOPER_DIR; };
|
||||||
C0FF7CBE216B00570081B781 /* Metal.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Metal.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Metal.framework; sourceTree = DEVELOPER_DIR; };
|
C0FF7CBE216B00570081B781 /* Metal.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Metal.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS12.0.sdk/System/Library/Frameworks/Metal.framework; sourceTree = DEVELOPER_DIR; };
|
||||||
|
C0FF7CC1217020820081B781 /* MarchingSquares.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarchingSquares.swift; sourceTree = "<group>"; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
|
|
||||||
/* Begin PBXFrameworksBuildPhase section */
|
/* Begin PBXFrameworksBuildPhase section */
|
||||||
|
@ -203,6 +206,7 @@
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
C0BBE3A31F2E81C700E68524 /* Metaballs.swift */,
|
C0BBE3A31F2E81C700E68524 /* Metaballs.swift */,
|
||||||
|
C0FF7CC1217020820081B781 /* MarchingSquares.swift */,
|
||||||
C0CE7BFF1F362C3F001516B6 /* Geometry.swift */,
|
C0CE7BFF1F362C3F001516B6 /* Geometry.swift */,
|
||||||
C0DF1D781F3789DC0038B0A0 /* Memory.swift */,
|
C0DF1D781F3789DC0038B0A0 /* Memory.swift */,
|
||||||
C0FF7C9C216A6DE00081B781 /* Math.swift */,
|
C0FF7C9C216A6DE00081B781 /* Math.swift */,
|
||||||
|
@ -433,6 +437,7 @@
|
||||||
C0FF7C992168062C0081B781 /* Shaders.metal in Sources */,
|
C0FF7C992168062C0081B781 /* Shaders.metal in Sources */,
|
||||||
C0FF7C9A2168062C0081B781 /* Preferences.swift in Sources */,
|
C0FF7C9A2168062C0081B781 /* Preferences.swift in Sources */,
|
||||||
C0FF7C9E216A6DE00081B781 /* Math.swift in Sources */,
|
C0FF7C9E216A6DE00081B781 /* Math.swift in Sources */,
|
||||||
|
C0FF7CC3217020820081B781 /* MarchingSquares.swift in Sources */,
|
||||||
C0FF7C972168062C0081B781 /* Memory.swift in Sources */,
|
C0FF7C972168062C0081B781 /* Memory.swift in Sources */,
|
||||||
C0FF7C9B2168062C0081B781 /* PreferencesViewController.swift in Sources */,
|
C0FF7C9B2168062C0081B781 /* PreferencesViewController.swift in Sources */,
|
||||||
C0B906E91F455D1A00B5F89B /* MetaballsSaverView.swift in Sources */,
|
C0B906E91F455D1A00B5F89B /* MetaballsSaverView.swift in Sources */,
|
||||||
|
@ -456,6 +461,7 @@
|
||||||
C0FF7C932168062B0081B781 /* Preferences.swift in Sources */,
|
C0FF7C932168062B0081B781 /* Preferences.swift in Sources */,
|
||||||
C0BBE36B1F2E816500E68524 /* AppDelegate.swift in Sources */,
|
C0BBE36B1F2E816500E68524 /* AppDelegate.swift in Sources */,
|
||||||
C0FF7C9D216A6DE00081B781 /* Math.swift in Sources */,
|
C0FF7C9D216A6DE00081B781 /* Math.swift in Sources */,
|
||||||
|
C0FF7CC2217020820081B781 /* MarchingSquares.swift in Sources */,
|
||||||
);
|
);
|
||||||
runOnlyForDeploymentPostprocessing = 0;
|
runOnlyForDeploymentPostprocessing = 0;
|
||||||
};
|
};
|
||||||
|
|
64
MetaballsKit/MarchingSquares.swift
Normal file
64
MetaballsKit/MarchingSquares.swift
Normal file
|
@ -0,0 +1,64 @@
|
||||||
|
//
|
||||||
|
// 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?
|
||||||
|
|
||||||
|
init(field: Field) {
|
||||||
|
self.field = field
|
||||||
|
sampleGridSize = Size(16)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setupMetal(withDevice device: MTLDevice) {
|
||||||
|
let xSamples = Int(field.size.x / sampleGridSize.x)
|
||||||
|
let ySamples = Int(field.size.y / sampleGridSize.y)
|
||||||
|
|
||||||
|
let samplesDesc = MTLTextureDescriptor()
|
||||||
|
samplesDesc.textureType = .type2D
|
||||||
|
samplesDesc.width = xSamples
|
||||||
|
samplesDesc.height = ySamples
|
||||||
|
samplesDesc.pixelFormat = .depth32Float
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sampleField() {
|
||||||
|
guard let samples = samples else { return }
|
||||||
|
|
||||||
|
let xSamples = Int(field.size.x / sampleGridSize.x)
|
||||||
|
let ySamples = Int(field.size.y / sampleGridSize.y)
|
||||||
|
for xSample in 0..<xSamples {
|
||||||
|
let x = Float(xSample * Int(sampleGridSize.x))
|
||||||
|
for ySample in 0..<ySamples {
|
||||||
|
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)
|
||||||
|
let bytesPerRow = samples.width * MemoryLayout<Float>.stride
|
||||||
|
samples.replace(region: region, mipmapLevel: 0, withBytes: sample, bytesPerRow: bytesPerRow)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue