From 5abb65f6168e4d0c00871ae819278824d547f467 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 30 Jul 2023 16:27:19 +0200 Subject: [PATCH] libgarbage: add capped torus, gyroid sdfs --- .../raymarched/lib/libgarbage_example.shader | 45 +++++++++++++++---- Assets/raymarched/lib/libgarbage_shapes.cginc | 18 +++++++- 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Assets/raymarched/lib/libgarbage_example.shader b/Assets/raymarched/lib/libgarbage_example.shader index fea419d..4ffa564 100644 --- a/Assets/raymarched/lib/libgarbage_example.shader +++ b/Assets/raymarched/lib/libgarbage_example.shader @@ -27,7 +27,7 @@ Shader "CrispyPin/LibGarbageExample" #define MAX_DIST _MaxDist #define SURF_DIST _SurfDist - #define REFLECTIONS 3 + #define REFLECTIONS 1 #define LIGHT_FN lighting #define SCENE_FN main @@ -39,8 +39,8 @@ Shader "CrispyPin/LibGarbageExample" // #define DISABLE_DEPTH // #define DISCARD_ON_MISS // #define USE_WORLD_SPACE - #define STEP_MULTIPLIER 0.8 - #define SCENE_SCALE 0.2 + // #define STEP_MULTIPLIER 0.8 + #define SCENE_SCALE 0.17 #define LIMIT_DEPTH_TO_MESH #include "libgarbage.cginc" @@ -53,8 +53,9 @@ Shader "CrispyPin/LibGarbageExample" float3 floor_col(float3 p) { p = rotY(p, sin(length(p.xz) * 0.2) * 0.2); - p.x += smoothstep(0, 0.5, fmod(_Time.x, 1)) * 2; - p.z += smoothstep(0.5, 1, fmod(_Time.x, 1)) * 2; + float atime = fmod(_Time.y * 0.3, 1); + p.x += smoothstep(0, 0.5, atime) * 2; + p.z += smoothstep(0.5, 1, atime) * 2; return lerp(0.08, checkers(p - 1, 0.06, 0.12, 2), smoothstep(64, 0, length(p)) @@ -74,19 +75,45 @@ Shader "CrispyPin/LibGarbageExample" 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, 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, 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 = 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 = qUnion(d, mPlaneY(p, 0, floor)); - d = qIntersect(d, mSphere(p, 7, d.mat)); // limit floor size for better performance + d = qUnion(d, + qIntersect( + mPlaneY(p, 0, floor), + mSphere(p, 7, floor) + ) // limit floor size for better performance + ); return d; } diff --git a/Assets/raymarched/lib/libgarbage_shapes.cginc b/Assets/raymarched/lib/libgarbage_shapes.cginc index f341934..fd03190 100644 --- a/Assets/raymarched/lib/libgarbage_shapes.cginc +++ b/Assets/raymarched/lib/libgarbage_shapes.cginc @@ -44,6 +44,13 @@ float sdTorus(float3 p, 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) { 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 y = angle * incline - p.y; 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) @@ -86,8 +94,14 @@ float sdLine(float3 p, float3 a, float3 b, float 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; o.dist = d; o.mat = mat;