7float sphereSDF(
const float3 position,
const float3 centre,
const float radius)
9 return magnitude(centre - position) - radius;
12float boxSDF(
const float3 position,
const float3 centre,
const float3 dimensions)
14 float3 q = absolute(centre - position) - dimensions;
15 float length = magnitude((float3)(max(q.x, 0.0f), max(q.y, 0.0f), max(q.z, 0.0f)));
16 return length + min(max(q.x, max(q.y, q.z)), 0.0f);
19float opUnion(
const float d1,
const float d2)
24float opSubtraction(
const float d1,
const float d2)
29float opIntersection(
const float d1,
const float d2)
34float opSmoothUnion(
const float d1,
const float d2,
const float k)
36 float h = max(k - f_abs(d1 - d2), 0.0f);
37 return min(d1, d2) - h * h * 0.25f / k;
40float opPolynomialSmoothUnion(
const float a,
const float b,
const float k)
42 float h = max(k - f_abs(a - b), 0.0f) / k;
43 return min(a, b) - h * h * k * (1.0f / 4.0f);
46float opSmoothSubtraction(
const float d1,
const float d2,
const float k)
48 float h = max(k - f_abs(-d1 - d2), 0.0f);
49 return max(-d1, d2) + h * h * 0.25f / k;
52float opSmoothIntersection(
const float d1,
const float d2,
const float k)
54 float h = max(k - f_abs(d1 - d2), 0.0f);
55 return max(d1, d2) + h * h * 0.25 / k;
59bool isWithinBoundingSphere(
const float3 position,
const float3 sphereCentre,
const float sphereRadius)
61 return magnitude(sphereCentre - position) - sphereRadius <= 0;