From ddcfca5b8da1a453fcc1fa3f9aa982c116086b85 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 29 Jul 2023 20:37:37 +0200 Subject: [PATCH] libgarbage: fix materials being smoothly interpolated for non-smooth sdf operations --- .../lib/libgarbage_operations.cginc | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/Assets/raymarched/lib/libgarbage_operations.cginc b/Assets/raymarched/lib/libgarbage_operations.cginc index 9ccf72e..a6af79d 100644 --- a/Assets/raymarched/lib/libgarbage_operations.cginc +++ b/Assets/raymarched/lib/libgarbage_operations.cginc @@ -12,6 +12,7 @@ float smax(float a, float b, float k) { Material mixMat(Material a, Material b, float fac) { Material m; + fac = clamp(fac * 1.1 - 0.05, 0, 1); // TODO make this configurable, also possibly use smoothstep m.col = lerp(a.col, b.col, fac); m.gloss = lerp(a.gloss, b.gloss, fac); return m; @@ -22,6 +23,14 @@ Material mixMat(SurfacePoint a, SurfacePoint b) { return mixMat(a.mat, b.mat, fac); } +Material nearestMat(SurfacePoint a, SurfacePoint b) { + if (a.dist < b.dist) { + return a.mat; + } else { + return b.mat; + } +} + // -------------------------------- // base sdf operations // -------------------------------- @@ -37,7 +46,7 @@ float qUnion(float a, float b, float smooth) { SurfacePoint qUnion(SurfacePoint a, SurfacePoint b) { SurfacePoint o; o.dist = min(a.dist, b.dist); - o.mat = mixMat(a, b); + o.mat = nearestMat(a, b); return o; } @@ -81,7 +90,7 @@ float qIntersect(float a, float b, float smooth) { SurfacePoint qIntersect(SurfacePoint a, SurfacePoint b) { SurfacePoint o; o.dist = max(a.dist, b.dist); - o.mat = mixMat(a, b); + o.mat = nearestMat(a, b); return o; } @@ -92,6 +101,10 @@ SurfacePoint qIntersect(SurfacePoint a, SurfacePoint b, float smooth) { return o; } +// -------------------------------- +// single sdf operations +// -------------------------------- + float qRound(float a, float radius) { return a - radius; } @@ -131,7 +144,7 @@ float3 repXYZUnsigned(float3 p, float3 r) { } // repeats space -inline float3 repXZ(float3 p, float x, float z) { +float3 repXZ(float3 p, float x, float z) { float3 o = p; o.x = fmod(abs(p.x) + x / 2.0, x) - x / 2.0; o.x *= sign(p.x); @@ -139,3 +152,10 @@ inline float3 repXZ(float3 p, float x, float z) { o.z *= sign(p.z); return o; } + +float3 repX(float3 p, float x) { + float3 o = p; + o.x = fmod(abs(p.x) + x / 2.0, x) - x / 2.0; + o.x *= sign(p.x); + return o; +}