cvr-props/Assets/raymarched/lib/libgarbage_example.shader

159 lines
4.5 KiB
Text
Raw Normal View History

2023-07-23 18:31:33 +02:00
Shader "CrispyPin/LibGarbageExample"
{
Properties
{
[Header(Raymarcher Properties)]
2023-09-22 21:24:21 +02:00
_MaxSteps ("Max steps", Integer) = 128
2023-07-24 08:42:25 +02:00
_MaxDist ("Max distance", Float) = 128
_SurfDist ("Surface distance threshold", Range(0.0001, 0.05)) = 0.001
2023-07-23 18:31:33 +02:00
}
SubShader
{
Tags { "RenderType"="Opaque" }
2023-07-24 08:42:25 +02:00
Cull Front
2023-07-23 18:31:33 +02:00
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
2023-07-24 08:42:25 +02:00
int _MaxSteps;
float _MaxDist;
float _SurfDist;
#define MAX_STEPS _MaxSteps
#define MAX_DIST _MaxDist
#define SURF_DIST _SurfDist
2023-07-23 18:31:33 +02:00
#define REFLECTIONS 2
#define LIGHT_FN lighting
#define SCENE_FN main
// #define SEPARATE_MATERIAL_AND_DIST_FUNCTIONS
// #define SCENE_FN separate_mat
// #define DISTANCE_FN separate_dist
// #define DISABLE_DEPTH
// #define DISCARD_ON_MISS
// #define USE_WORLD_SPACE
// #define STEP_MULTIPLIER 0.8
#define SCENE_SCALE (1/6.0)
2023-07-29 20:53:35 +02:00
#define LIMIT_DEPTH_TO_MESH
2023-07-24 08:42:25 +02:00
#include "libgarbage.cginc"
2023-07-23 18:31:33 +02:00
2023-07-29 20:53:35 +02:00
float3 checkers(float3 p, float3 a, float3 b, float2 size) {
float2 q = p.xz / size;
q = int2(abs(q) + 0.5);
int s = ((q.x + q.y) % 2);
return s * a + (1 - s) * b;
2023-07-24 14:47:24 +02:00
}
2023-07-29 20:53:35 +02:00
float3 floor_col(float3 p) {
2023-07-30 12:26:54 +02:00
p = rotY(p, sin(length(p.xz) * 0.2) * 0.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.42,
checkers(p - 1, 0.35, 0.45, 1),
2023-07-29 20:53:35 +02:00
smoothstep(64, 0, length(p))
);
}
SurfacePoint minimal_floor(float3 p, float3 a, float3 b) {
float3 pmin = min(a, b);
float3 pmax = max(a, b);
float3 center = (pmin + pmax) / 2;
center.y = -0.05;
float3 size = pmax - pmin;
size.y = 0.1;
SurfacePoint f = mBox(p - center, size, mat(floor_col(p)));
// f = qUnion(f, mSphere(p - center, 0.3, mat(1,0,0)));
// f = qUnion(f, mSphere(p - a, 0.3, mat(0,1,0)));
// f = qUnion(f, mSphere(p - b, 0.3, mat(0,0,1)));
return f;
}
2023-07-29 20:53:35 +02:00
SurfacePoint main(float3 p) {
Material floor = mat(floor_col(p));
2023-07-30 17:17:54 +02:00
p.y += 0.5 / SCENE_SCALE;
2023-07-29 20:53:35 +02:00
SurfacePoint d;
d = mSphere(p - float3(-2, 1, -2), 0.5);
d = qUnion(d, mTorus(p - float3(0, 1, -2), 0.4, 0.1));
// float3 line_a = float3(1.5, 1.5, -2);
float3 line_a = float3(2 - cos(_Time.z)*0.5, 1.5, -2 - sin(_Time.z)*0.5);
float3 line_b = float3(2 + cos(_Time.z)*0.3, 0.5, -2 + sin(_Time.z)*0.3);
d = qUnion(d, mLine(p, line_a, line_b, 0.2));
d = qUnion(d, mBox(p - float3(-2, 1, 0), float3(0.5, 0.5, 0.8)));
d = qUnion(d, mHexPrism(p - float3(0, 1, 0), 0.5, 0.2));
d = qUnion(d,
qIntersect(
mHelix(rotY(p - float3(2, -1, 0), _Time.y), 0.5, 0.2, 0.13),
mBox(p - float3(2, 1.5, 0), 2),
0.05
)
);
2023-07-29 20:53:35 +02:00
d = qUnion(d, mCylinder(p - float3(-2, 1, 2), 0.4, 0.5));
2023-07-29 20:53:35 +02:00
d = qRound(d, 0.05 * sin(_Time.y));
float torus_angle = (sin(_Time.y)/2 + 0.5) * UNITY_PI;
d = qUnion(d, mCappedTorus(p - float3(0, 1, 2), float2(sin(torus_angle), cos(torus_angle)), 0.4, 0.1));
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
)
)
);
d.mat = mat(float3(0.35, 0.8, 1), 1);
float3 p2 = p - float3(0, 2, sin(_Time.x * 5) * 3);
d = qSub(d, mBox(p2, float3(5.6, 3.6, .25)), 0.02);
d = qUnion(d, qRound(mBox(p2 - float3(0, -1.9, 0), float3(6, .2, .2), mat(0.8)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(0, 1.9, 0), float3(6, .2, .2), mat(0.8)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(2.9, 0, 0), float3(.2, 4, .2), mat(0.8)), 0.01));
d = qUnion(d, qRound(mBox(p2 - float3(-2.9, 0, 0), float3(.2, 4, .2), mat(0.8)), 0.01));
d = qUnion(d, minimal_floor(p, float3(-11.5, 0, -3.5), float3(3.5, 0, 7.5)));
2023-07-29 20:53:35 +02:00
return d;
}
float3 lighting(Ray ray) {
2023-07-29 20:53:35 +02:00
float3 sun_dir = normalize(float3(2, 1, -1));
if (ray.missed) {
if (ray.dir.y >= 0) {
return lRenderSky(ray.dir, sun_dir);
2023-07-30 12:26:54 +02:00
} else {
2023-07-29 20:53:35 +02:00
float3 cam = ray.start;
2023-07-30 17:17:54 +02:00
cam.y += 0.5 / SCENE_SCALE;
2023-07-29 20:53:35 +02:00
float3 dir = ray.dir;
float3 surface_pos = float3(
cam.x - cam.y / (dir.y / dir.x),
0,
cam.z - cam.y / (dir.y / dir.z)
);
float col = floor_col(surface_pos);
return col * (lSky(float3(0,1,0)) + lSun(float3(0,1,0), sun_dir));
}
}
float3 light = lSun(ray.normal, sun_dir);
light *= lShadow(ray.hit_pos + ray.normal * SURF_DIST, sun_dir, 50);
light += lSky(ray.normal);
return light;
}
2023-07-23 18:31:33 +02:00
ENDCG
}
}
}