float r) { return length(p - c) - r; } Not explain today about 3D graphics... ! (Camera, Lighting, Ray casting / Ray marching) Sample code: mathShader_8_6.metal
the distance to a shape. • It enables the representing shapes using "distance". • Learned the SDF of a circle and sphere, and their implementations in MSL.
for (float i = 0.0; i < 6.0; i++) { float3 cent = float3(cos(PI * i / 3.0), sin(PI * i / 3.0), 0.0); d = min(d, sphereSDF(p, cent, 0.2)); } → Represents six spheres with a single SDF.
SDF representing one sphere • d2: SDF representing six spheres • a: Cycles from 0.0 to 1.0 over time float a = abs(mod(time, 2.0) - 1.0); Sample code: mathShader_9_2.metal
shapes. // A min function that interpolates the junction for a smooth SDF union float smin(float a, float b, float k) { float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0); return mix(b, a, h) - k * h * (1.0 - h); } (The derivation process of this formula is on p129–130) • d1: A smaller sphere • d2: A larger sphere • k: From left, 0.1, 0.3, 0.5 Sample code: mathShader_9_3.metal