libgarbage: add capped torus, gyroid sdfs

This commit is contained in:
Crispy 2023-07-30 16:27:19 +02:00
parent 8ca3dc6e53
commit 5abb65f616
2 changed files with 52 additions and 11 deletions

View file

@ -27,7 +27,7 @@ Shader "CrispyPin/LibGarbageExample"
#define MAX_DIST _MaxDist #define MAX_DIST _MaxDist
#define SURF_DIST _SurfDist #define SURF_DIST _SurfDist
#define REFLECTIONS 3 #define REFLECTIONS 1
#define LIGHT_FN lighting #define LIGHT_FN lighting
#define SCENE_FN main #define SCENE_FN main
@ -39,8 +39,8 @@ Shader "CrispyPin/LibGarbageExample"
// #define DISABLE_DEPTH // #define DISABLE_DEPTH
// #define DISCARD_ON_MISS // #define DISCARD_ON_MISS
// #define USE_WORLD_SPACE // #define USE_WORLD_SPACE
#define STEP_MULTIPLIER 0.8 // #define STEP_MULTIPLIER 0.8
#define SCENE_SCALE 0.2 #define SCENE_SCALE 0.17
#define LIMIT_DEPTH_TO_MESH #define LIMIT_DEPTH_TO_MESH
#include "libgarbage.cginc" #include "libgarbage.cginc"
@ -53,8 +53,9 @@ Shader "CrispyPin/LibGarbageExample"
float3 floor_col(float3 p) { float3 floor_col(float3 p) {
p = rotY(p, sin(length(p.xz) * 0.2) * 0.2); p = rotY(p, sin(length(p.xz) * 0.2) * 0.2);
p.x += smoothstep(0, 0.5, fmod(_Time.x, 1)) * 2; float atime = fmod(_Time.y * 0.3, 1);
p.z += smoothstep(0.5, 1, fmod(_Time.x, 1)) * 2; p.x += smoothstep(0, 0.5, atime) * 2;
p.z += smoothstep(0.5, 1, atime) * 2;
return lerp(0.08, return lerp(0.08,
checkers(p - 1, 0.06, 0.12, 2), checkers(p - 1, 0.06, 0.12, 2),
smoothstep(64, 0, length(p)) smoothstep(64, 0, length(p))
@ -74,19 +75,45 @@ Shader "CrispyPin/LibGarbageExample"
SurfacePoint d; SurfacePoint d;
d =/* qUnion(d, */ mSphere(p - float3(-2, 1, -2), 0.5, mat1)/* ) */; d = mSphere(p - float3(-2, 1, -2), 0.5, mat1);
d = qUnion(d, mTorus(p - float3(0, 1, -2), 0.4, 0.1, mat1)); d = qUnion(d, mTorus(p - float3(0, 1, -2), 0.4, 0.1, mat1));
d = qUnion(d, mLine(p, float3(1.5, 1.5, -2), float3(2.5, 0.5, -2), 0.2, mat1)); d = qUnion(d, mLine(p, float3(1.5, 1.5, -2), float3(2.5, 0.5, -2), 0.2, mat1));
d = qUnion(d, mBox(p - float3(-2, 1, 0), float3(0.5, 0.5, 0.8), mat1)); d = qUnion(d, mBox(p - float3(-2, 1, 0), float3(0.5, 0.5, 0.8), mat1));
d = qUnion(d, mHexPrism(p - float3(0, 1, 0), 0.5, 0.2, mat1)); d = qUnion(d, mHexPrism(p - float3(0, 1, 0), 0.5, 0.2, mat1));
d = qUnion(d, qIntersect(mHelix(rotY(p - float3(2, -1, 0), _Time.y), 0.5, 0.2, 0.13, helix_glossy), mBox(p - float3(2, 1, 0), 1.8, mat1),0.05)); d = qUnion(d,
qIntersect(
mHelix(rotY(p - float3(2, -1, 0), _Time.y), 0.5, 0.2, 0.13, helix_glossy),
mBox(p - float3(2, 1, 0), 1.8, mat1),
0.05
)
);
d = qUnion(d, mCylinder(p - float3(-2, 1, 2), 0.4, 0.5, mat1)); d = qUnion(d, mCylinder(p - float3(-2, 1, 2), 0.4, 0.5, mat1));
d = qRound(d, 0.05 * sin(_Time.y)); d = qRound(d, 0.05 * sin(_Time.y));
float a = (sin(_Time.y)/2 + 0.5) * UNITY_PI;
d = qUnion(d, mCappedTorus(p - float3(0, 1, 2), float2(sin(a), cos(a)), 0.4, 0.1, mat1));
d = qUnion(d,
mFromDist(
qIntersect(
qRound(sdBox(p - float3(2, 1, 2), 1), 0.005),
sdGyroid(p, 12, sin(_Time.y) * UNITY_PI * 0.5, 0.2),
0.01
),
helix_glossy
)
);
// d.mat = mat1; // d.mat = mat1;
d = qUnion(d, mPlaneY(p, 0, floor)); d = qUnion(d,
d = qIntersect(d, mSphere(p, 7, d.mat)); // limit floor size for better performance qIntersect(
mPlaneY(p, 0, floor),
mSphere(p, 7, floor)
) // limit floor size for better performance
);
return d; return d;
} }

View file

@ -44,6 +44,13 @@ float sdTorus(float3 p, float radius, float thickness) {
} }
_MAT_VARIANT2(mTorus, sdTorus, float, radius, float, thickness) _MAT_VARIANT2(mTorus, sdTorus, float, radius, float, thickness)
float sdCappedTorus(float3 p, float2 sc, float r1, float r2) {
p.x = abs(p.x);
float k = (sc.y * p.x > sc.x * p.z) ? dot(p.xz, sc) : length(p.xz);
return sqrt(dot(p, p) + r1 * r1 - 2.0 * r1 * k) - r2;
}
_MAT_VARIANT3(mCappedTorus, sdCappedTorus, float2, sc, float, r1, float, r2)
float sdPlaneY(float3 p, float height) { float sdPlaneY(float3 p, float height) {
return p.y - height; return p.y - height;
} }
@ -74,7 +81,8 @@ float sdHelix(float3 p, float r1, float r2, float incline) {
float angle = atan2(p.z, p.x); // angle around y axis float angle = atan2(p.z, p.x); // angle around y axis
float y = angle * incline - p.y; float y = angle * incline - p.y;
y = fmod(y, UNITY_PI * incline * 2) + UNITY_PI * incline; y = fmod(y, UNITY_PI * incline * 2) + UNITY_PI * incline;
return length(float2(x2, y)) - r2; float dist = length(float2(x2, y)) - r2;
return dist * 0.8;
} }
_MAT_VARIANT3(mHelix, sdHelix, float, r1, float, r2, float, incline) _MAT_VARIANT3(mHelix, sdHelix, float, r1, float, r2, float, incline)
@ -86,8 +94,14 @@ float sdLine(float3 p, float3 a, float3 b, float r) {
} }
_MAT_VARIANT3(mLine, sdLine, float3, a, float3, b, float3, r) _MAT_VARIANT3(mLine, sdLine, float3, a, float3, b, float3, r)
float sdGyroid(float3 p, float scale, float bias, float thickness) {
float dist = abs(dot(sin(p * scale), cos(p.zxy * scale)) + bias) - thickness;
dist *= .58 / scale;
return dist;
}
_MAT_VARIANT3(mGyroid, sdGyroid, float, scale, float, bias, float, thickness)
SurfacePoint mDummy(float d, Material mat = DEFAULT_MAT) { SurfacePoint mFromDist(float d, Material mat = DEFAULT_MAT) {
SurfacePoint o; SurfacePoint o;
o.dist = d; o.dist = d;
o.mat = mat; o.mat = mat;