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

91 lines
2.6 KiB
Text
Raw Normal View History

2023-07-23 18:31:33 +02:00
Shader "CrispyPin/LibGarbageExample"
{
Properties
{
[Header(Raymarcher Properties)]
2023-07-24 08:42:25 +02:00
_MaxSteps ("Max steps", Int) = 128
_MaxDist ("Max distance", Float) = 128
2023-07-23 18:31:33 +02:00
_SurfDist ("Surface distance threshold", Range(0.00001, 0.05)) = 0.001
}
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;
#define MAX_STEPS _MaxSteps
float _MaxDist;
#define MAX_DIST _MaxDist
float _SurfDist;
#define SURF_DIST _SurfDist
2023-07-23 18:31:33 +02:00
#define REFLECTIONS 3
2023-07-24 14:47:24 +02:00
#define DISTANCE_FN main_dist
#define MATERIAL_FN main_mat
#define LIGHT_FN lighting
// #define DISABLE_DEPTH
// #define DISCARD_ON_MISS
#define USE_WORLD_SPACE
#define SCENE_SCALE 0.05
2023-07-24 08:42:25 +02:00
#include "libgarbage.cginc"
2023-07-23 18:31:33 +02:00
2023-07-24 14:47:24 +02:00
float main_dist(float3 p) {
float d = sdPlaneY(p, 0);
d = qIntersect(d, sdSphere(p, 9), 0.5);
d = qUnion(d, sdSphere(p - float3(0, 2, 0), 2));
d = qUnion(d, sdTorus(rotX(p, _Time * 40 + UNITY_PI / 2), 5, 0.5), 0.5);
d = qUnion(d, sdTorus(rotZ(p, _Time * 40 + UNITY_PI / 2), 5, 0.5), 0.5);
d = qUnion(d, sdTorus(rotX(p, _Time * 40), 5, 0.5), 0.5);
d = qUnion(d, sdTorus(rotZ(p, _Time * 40), 5, 0.5), 0.5);
// small spheres
float3 p2 = abs(rotY(p, -20 * _Time)) - float3(1.5, sin(_Time.y * 5) + 1, 1.5);
d = qUnion(d, sdSphere(p2, 0.7), 0.2);
return d;
2023-07-24 14:47:24 +02:00
}
SurfacePoint main_mat(float3 p) {
Material grass = mat(float3(0.001, 0.1, 0.001), 0.3);
Material dirt = mat(float3(0.1, 0.04, 0.01), 0);
Material metal = mat(0.1, 1);
Material blue = mat(float3(0.05, 0.1, 0.2), 0);
SurfacePoint d = mPlaneY(p, 0, grass);
d = qIntersect(d, mSphere(p, 9, dirt), 0.5);
d = qUnion(d, mSphere(p - float3(0, 2, 0), 2, metal));
d = qUnion(d, mTorus(rotX(p, _Time * 40 + UNITY_PI / 2), 5, 0.5, blue), 0.5);
d = qUnion(d, mTorus(rotZ(p, _Time * 40 + UNITY_PI / 2), 5, 0.5, blue), 0.5);
d = qUnion(d, mTorus(rotX(p, _Time * 40), 5, 0.5, blue), 0.5);
d = qUnion(d, mTorus(rotZ(p, _Time * 40), 5, 0.5, blue), 0.5);
// small spheres
float3 p2 = abs(rotY(p, -20 * _Time)) - float3(1.5, sin(_Time.y * 5) + 1, 1.5);
d = qUnion(d, mSphere(p2, 0.7, metal), 0.2);
return d;
2023-07-24 14:47:24 +02:00
}
float3 lighting(Ray ray) {
if (ray.missed)
return lRenderSky(ray.dir, normalize(float3(4,2,1)));
// return float3(0.15, 0.25, 0.3);
float3 sun_dir = normalize(float3(4, 2, 1));
float3 col = 0;
col = ray.mat.col * lSun(ray.normal, sun_dir);
col *= lShadow(ray.hit_pos + ray.normal * SURF_DIST, sun_dir, 50);
col += ray.mat.col * lSky(ray.normal);
return col;
}
2023-07-23 18:31:33 +02:00
ENDCG
}
}
}