libgarbage: fix materials being smoothly interpolated for non-smooth sdf operations

This commit is contained in:
Crispy 2023-07-29 20:37:37 +02:00
parent ffdaf36ebc
commit ddcfca5b8d

View file

@ -12,6 +12,7 @@ float smax(float a, float b, float k) {
Material mixMat(Material a, Material b, float fac) { Material mixMat(Material a, Material b, float fac) {
Material m; 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.col = lerp(a.col, b.col, fac);
m.gloss = lerp(a.gloss, b.gloss, fac); m.gloss = lerp(a.gloss, b.gloss, fac);
return m; return m;
@ -22,6 +23,14 @@ Material mixMat(SurfacePoint a, SurfacePoint b) {
return mixMat(a.mat, b.mat, fac); 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 // base sdf operations
// -------------------------------- // --------------------------------
@ -37,7 +46,7 @@ float qUnion(float a, float b, float smooth) {
SurfacePoint qUnion(SurfacePoint a, SurfacePoint b) { SurfacePoint qUnion(SurfacePoint a, SurfacePoint b) {
SurfacePoint o; SurfacePoint o;
o.dist = min(a.dist, b.dist); o.dist = min(a.dist, b.dist);
o.mat = mixMat(a, b); o.mat = nearestMat(a, b);
return o; return o;
} }
@ -81,7 +90,7 @@ float qIntersect(float a, float b, float smooth) {
SurfacePoint qIntersect(SurfacePoint a, SurfacePoint b) { SurfacePoint qIntersect(SurfacePoint a, SurfacePoint b) {
SurfacePoint o; SurfacePoint o;
o.dist = max(a.dist, b.dist); o.dist = max(a.dist, b.dist);
o.mat = mixMat(a, b); o.mat = nearestMat(a, b);
return o; return o;
} }
@ -92,6 +101,10 @@ SurfacePoint qIntersect(SurfacePoint a, SurfacePoint b, float smooth) {
return o; return o;
} }
// --------------------------------
// single sdf operations
// --------------------------------
float qRound(float a, float radius) { float qRound(float a, float radius) {
return a - radius; return a - radius;
} }
@ -131,7 +144,7 @@ float3 repXYZUnsigned(float3 p, float3 r) {
} }
// repeats space // repeats space
inline float3 repXZ(float3 p, float x, float z) { float3 repXZ(float3 p, float x, float z) {
float3 o = p; float3 o = p;
o.x = fmod(abs(p.x) + x / 2.0, x) - x / 2.0; o.x = fmod(abs(p.x) + x / 2.0, x) - x / 2.0;
o.x *= sign(p.x); o.x *= sign(p.x);
@ -139,3 +152,10 @@ inline float3 repXZ(float3 p, float x, float z) {
o.z *= sign(p.z); o.z *= sign(p.z);
return o; 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;
}