libgarbage: add square helix

This commit is contained in:
Crispy 2023-07-31 20:53:38 +02:00
parent 9bf8c0962b
commit 9befad0a59
2 changed files with 29 additions and 10 deletions

View file

@ -86,11 +86,28 @@ 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;
// 2d circle
float dist = length(float2(x2, y)) - r2; float dist = length(float2(x2, y)) - r2;
return dist * 0.8; return dist * 0.8;
} }
_MAT_VARIANT3(mHelix, sdHelix, float, float, float) _MAT_VARIANT3(mHelix, sdHelix, float, float, float)
// box = (width, height, rotation)
float sdHelixSquare(float3 p, float r1, float3 box, 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;
// rotated 2d box
float2 p2 = float2(x2, y);
p2 = mul(float2x2(cos(box.z), -sin(box.z), sin(box.z), cos(box.z)), p2);
float2 d = abs(p2) - box.xy;
float dist = length(max(d, 0)) + min(max(d.x, d.y), 0);
return dist * 0.8;
}
_MAT_VARIANT3(mHelixSquare, sdHelixSquare, float, float3, float)
float sdLine(float3 p, float3 a, float3 b, float r) { float sdLine(float3 p, float3 a, float3 b, float r) {
float3 pa = p - a; float3 pa = p - a;
float3 ba = b - a; float3 ba = b - a;
@ -106,6 +123,7 @@ float sdGyroid(float3 p, float scale, float bias, float thickness) {
} }
_MAT_VARIANT3(mGyroid, sdGyroid, float, float, float) _MAT_VARIANT3(mGyroid, sdGyroid, float, float, float)
SurfacePoint mFromDist(float d, Material mat = DEFAULT_MAT) { SurfacePoint mFromDist(float d, Material mat = DEFAULT_MAT) {
SurfacePoint o; SurfacePoint o;
o.dist = d; o.dist = d;

View file

@ -19,7 +19,7 @@ Shader "CrispyPin/Nut"
#pragma vertex vert #pragma vertex vert
#pragma fragment frag #pragma fragment frag
#define PLAYER_COUNT 6 #define PLAYER_COUNT 4
float4 _PlayerRightHandPositions[PLAYER_COUNT]; float4 _PlayerRightHandPositions[PLAYER_COUNT];
float4 _PlayerLeftHandPositions[PLAYER_COUNT]; float4 _PlayerLeftHandPositions[PLAYER_COUNT];
@ -30,7 +30,7 @@ Shader "CrispyPin/Nut"
#define MAX_DIST _MaxDist #define MAX_DIST _MaxDist
#define SURF_DIST _SurfDist #define SURF_DIST _SurfDist
#define REFLECTIONS 2 #define REFLECTIONS 3
#define SCENE_FN main #define SCENE_FN main
#define LIGHT_FN lighting #define LIGHT_FN lighting
@ -76,7 +76,7 @@ Shader "CrispyPin/Nut"
float height = 0.1; float height = 0.1;
float inner_radius = 0.18; float inner_radius = 0.18;
float thread = 0.013; float thread = 0.013;
// float thread = 0.01 + sin(_Time.x * 8) * 0.01 + 0.01; float3 thread_box = float3(thread, thread, UNITY_PI/4);
SurfacePoint d; SurfacePoint d;
float3 rp = rotY(p, _Time.y); float3 rp = rotY(p, _Time.y);
@ -84,12 +84,12 @@ Shader "CrispyPin/Nut"
// nut // nut
d = mRoundedHex(rp, girth, height); d = mRoundedHex(rp, girth, height);
d = qSub(d, mInfCylinder(rp, float3(0, 0, inner_radius)), 0.02); d = qSub(d, mInfCylinder(rp, float3(0, 0, inner_radius)), 0.02);
d = qSub(d, mHelix(rp - float3(0, -1, 0), inner_radius, thread, thread/2), 0.003); d = qSub(d, mHelixSquare(rp - float3(0, -1, 0), inner_radius, thread_box, thread/2), 0.003);
// bolt // bolt
SurfacePoint bolt_thread = mInfCylinder(rp, float3(0, 0, inner_radius + 0.3)); SurfacePoint bolt_thread = mInfCylinder(rp, float3(0, 0, inner_radius + 0.3));
bolt_thread = qSub(bolt_thread, mInfCylinder(rp, float3(0, 0, inner_radius)), 0.02); bolt_thread = qSub(bolt_thread, mInfCylinder(rp, float3(0, 0, inner_radius)), 0.02);
bolt_thread = qSub(bolt_thread, mHelix(rp - float3(0, -1, 0), inner_radius, thread, thread/2), 0.03); bolt_thread = qSub(bolt_thread, mHelixSquare(rp - float3(0, -1, 0), inner_radius, thread_box, thread/2), 0.03);
// d = qUnion(d, bolt_thread); // d = qUnion(d, bolt_thread);
@ -98,9 +98,10 @@ Shader "CrispyPin/Nut"
bp.y -= h; bp.y -= h;
SurfacePoint bolt = qSub(mCylinder(bp, inner_radius+thread*2, 0.2), bolt_thread); SurfacePoint bolt = qSub(mCylinder(bp, inner_radius+thread*2, 0.2), bolt_thread);
bolt = qUnion(bolt, mRoundedHex(bp - float3(0, 0.13, 0), girth, height)); bolt = qUnion(bolt, mRoundedHex(bp - float3(0, 0.13, 0), girth, height));
bolt = qUnion(bolt, mCylinder(bp - float3(0, -0.17, 0), inner_radius , 0.1), 0.05); // Material metal1 = mat(float3(0.5, 0.3, 0.1), 1);
Material metal1 = mat(float3(0.5, 0.3, 0.1), 1); // Material metal2 = mat(float3(0.3, 0.3, 0.5), 1);
Material metal2 = mat(float3(0.3, 0.3, 0.5), 1); Material metal1 = mat(0.3, 1);
Material metal2 = mat(0.3, 1);
d.mat = metal1; d.mat = metal1;
bolt.mat = metal2; bolt.mat = metal2;
@ -116,7 +117,7 @@ Shader "CrispyPin/Nut"
// d = qUnion(d, mPlaneY(p, -0.5, mat(floor_col, 0)), 0.01); // d = qUnion(d, mPlaneY(p, -0.5, mat(floor_col, 0)), 0.01);
// d = qIntersect(d, mPlaneY(-p, 0.6, mat(floor_col, 0)), 0.01); // d = qIntersect(d, mPlaneY(-p, 0.6, mat(floor_col, 0)), 0.01);
d = qUnion(d, mBox(p - float3(0, -0.6, 0), float3(6, 0.05, 6), mat(floor(p), 0))); d = qUnion(d, mBox(p - float3(0, -0.5 / SCENE_SCALE, 0), float3(6, 0.05, 6), mat(floor(p), 0)));
[unroll] [unroll]
for (int i = 0; i < PLAYER_COUNT; i++) { for (int i = 0; i < PLAYER_COUNT; i++) {
@ -146,7 +147,7 @@ Shader "CrispyPin/Nut"
} }
else { else {
float3 cam = ray.start; float3 cam = ray.start;
cam.y += 0.6 - 0.0275; cam.y += 0.5 / SCENE_SCALE;
float3 dir = ray.dir; float3 dir = ray.dir;
float3 surface_pos = float3 ( float3 surface_pos = float3 (
cam.x - cam.y / (dir.y / dir.x), cam.x - cam.y / (dir.y / dir.x),