libgarbage: add sdfs: hex prism, cylinder, infinite cylinder, helix, line

This commit is contained in:
Crispy 2023-07-29 16:43:23 +02:00
parent a19707240a
commit 633680777a

View file

@ -49,5 +49,47 @@ float sdPlaneY(float3 p, float height) {
}
_MAT_VARIANT1(mPlaneY, sdPlaneY, float, height)
float sdHexPrism(float3 p, float width, float height) {
const float3 k = float3(-0.8660254, 0.5, 0.57735);
p = abs(p);
p.xz -= 2.0 * min(dot(k.xy, p.xz), 0) * k.xy;
float2 d = float2(length(p.xz - float2(clamp(p.x,-k.z * width, k.z * width), width)) * sign(p.z - width), p.y - height);
return min(max(d.x, d.y), 0) + length(max(d, 0));
}
_MAT_VARIANT2(mHexPrism, sdHexPrism, float, width, float, height)
float sdInfCylinder(float3 p, float3 c) {
return length(p.xz - c.xy) - c.z;
}
_MAT_VARIANT1(mInfCylinder, sdInfCylinder, float3, c)
float sdCylinder(float3 p, float r, float h) {
float2 d = abs(float2(length(p.xz), p.y)) - float2(r, h);
return min(max(d.x, d.y), 0) + length(max(d, 0));
}
_MAT_VARIANT2(mCylinder, sdCylinder, float, r, float, h)
float sdHelix(float3 p, float r1, float r2, float incline) {
float x2 = length(p.xz) - r1; // vertical plane
float angle = atan2(p.z, p.x); // angle around y axis
float y = angle * incline - p.y;
y = fmod(y, UNITY_PI * incline * 2) + UNITY_PI * incline;
return length(float2(x2, y)) - r2;
}
_MAT_VARIANT3(mHelix, sdHelix, float, r1, float, r2, float, incline)
float sdLine(float3 p, float3 a, float3 b, float r) {
float3 pa = p - a;
float3 ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0, 1);
return length(p- a - (b-a) * h) - r;
}
_MAT_VARIANT3(mLine, sdLine, float3, a, float3, b, float3, r)
SurfacePoint mDummy(float d, Material mat = DEFAULT_MAT) {
SurfacePoint o;
o.dist = d;
o.mat = mat;
return o;
}