A Signed Distance Function Demo using Apple's Metal compute shaders.
SDF shaders use mathmatical functions to define object surfaces, they do not use pre-created model assets. SDF shaders trivially support Constructive solid geometry to generate complex objects by combining simple reometric primitives.
IQ's Signed Distance Bounds demo on shadertoy
And the hg_sdf Signed distance library by mercury
Wikipedia's SDF page
This demo includes some interesting features.
- Resolution variable rendering
The View controller tracks rendered frames per second and adjusts the view content scale dynamically to maintain a target frame rate. Typically 15FPS across desktop and mobile devices will give acceptable resolution.
- Shader JIT compilation
To support dynamic shader content the shader is re-compiled inflight when the structure of a scene changes. The metal Shader compiler aggressively optimises the shader for maximum rendering FPS.
- Shader based picking
The same shader is used to pick scene objects in response to touch or mouse events, picking uses exactly the same model state as the last frame rendered, giving perfect picking accuracy.
When the Demo app is launched the following scenes will be displayed for 10 seconds each
- 3D Space Warping
SDF can fold space to generate infinite instances of an object, this is very cheap as it uses the modulus function on the projected light ray to acheive this. The number of visible instances in the scene is only limited by the cameras far clipping plane.
- Polar Space Warping
A demo of space folding using polar coordinates rather than linear coordinates. A single complex object (cube minus sphere) is repeated 6 times around an origin.
- Blob
Demonstrates an object with non planar surfaces.
- Primitives
Demonstrates the range of supported SDF's, this is a port (in progress) of IQ's Signed Distance Bounds demo on shadertoy, this demo will continue to be updated as more functions are supported.
When a shader is re-compiled inflight in iOS, previous versions are not garbage collected.
The shader is too slow to support full resolution rendering on device screens, the shader has not been optimised for Metal yet.