Updated some comments and added readme+license
First commit after migrating to framework from prototype app.
iOS Framework that renders signed distance fields in Augmented Reality defined by Metal Function Pointers via a multi-pass "conemarching" algorithm.
Sample project included. An earlier prototype with lots of fun fractal stuff exists at this repo before I refactored into a framework.
var arSDFView: ARSDFView?
override func viewDidLoad() {
let cubeSizeUniform = Float3Uniform(name: "Cube Size",
range: CGSize(width: 0.1, height: 5.0),
value: simd_float3(0.5, 0.5, 0.5))
let distanceField = DistanceField(kernelName: "cube",
uniforms: [cubeSizeUniform])
arSDFView = ARSDFView(frame: self.view.frame)
self.view.addSubview(arSDFView!)
arSDFView?.start(distanceFields: [distanceField])
super.viewDidLoad()
}
Define a Metal function with the [[visible]] attribute that returns a float and has input parameters: (float3 position, float time, DistanceFieldParams params).
Optional: In Swift, define uniforms. Available types are FloatUniform, Float2Uniform, Float3Uniform and Float4Uniform.
Create a DistanceField object with the kernel name and an optional array of uniforms (max 4 at the moment).
The device compatibility with Metal Function Pointers is unclear. It runs on iPad Pro (3rd gen) but not on iPhone 12, so I suspect it's limited to M1 devices. This limitation can be removed by making the function tables optional (future work).
ARSDFView conforms to ARManagerDelegate which ARManager fufuils each frame with ARData containing rgb color, smoothed depth, and the view+projection matrices. This data is passed to the Conemarcher.
The Conemarcher renders the distance field progressively over a number of passes, doubling the resolution each pass. Rays are advanced as far as the cone guarantees there won't be an intersection, filling in the details at higher resolutions.
The final pass removes the cone constraint and performs regular raymarching. The depth texture from ARSession is tested against the depth of the projected ray in order to rule out unnecessary computation.
Internally, ARSDFView uses Metal Function Pointers, which allows the ability to write a single raymarch algorithm that is agnostic to the distance field it renders. The advantages of abstracting the renderer over writing in each shader is invaluable to code duplication. This does create restrictions however with input uniforms being a fixed struct.
Conemarching technique developed by fulcrum demo team http://www.fulcrum-demo.org/wp-content/uploads/2012/04/Cone_Marching_Mandelbox_by_Seven_Fulcrum_LongVersion.pdf
Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0)