// Various distance metrics. private static float Distance(float2 p) { if (SHAPE == 0) { return(Hlsl.Length(p)); } else { p = Hlsl.Abs(p); } switch (SHAPE) { case 1: return(Hlsl.Max(Hlsl.Length(p), (p.X + p.Y) * 0.7071f + 0.015f)); case 2: return(Hlsl.Max((p.X + p.Y) * 0.7071f, Hlsl.Max(p.X, p.Y))); case 3: return(Hlsl.Pow(Hlsl.Dot(Hlsl.Pow(p, 3), 1), 1.0f / 3.0f)); default: return((p.X + p.Y) * 0.7071f); } }
// The scene's distance function: There'd be faster ways to do this, but it's // more readable this way. Plus, this is a pretty simple scene, so it's // efficient enough. private static float M(Float3 p) { float fl = -p.Z; float obj = Truchet(p.XY); obj = Hlsl.Max(obj, Hlsl.Abs(p.Z) - 0.125f) - Hlsl.SmoothStep(0.03f, 0.25f, -obj) * 0.1f; float studs = 1e5f; const float sc = 0.5f; Float2 q = p.XY + 0.5f * sc; Float2 iq = Hlsl.Floor(q / sc) + 0.5f; q -= iq * sc; if (Mod(iq.X + iq.Y, 2.0f) > 0.5f) { studs = Hlsl.Max(Hlsl.Length(q) - 0.1f * sc - 0.02f, Hlsl.Abs(p.Z) - 0.26f); } objID = fl < obj && fl < studs ? 0 : obj < studs ? 1 : 2; return(Hlsl.Min(Hlsl.Min(fl, obj), studs)); }