cvr-props/Assets/raymarched/distortion.shader

131 lines
3.1 KiB
GLSL

Shader "CrispyPin/Distortion"
{
Properties
{
[Header(Raymarcher Properties)]
_MaxSteps ("Max steps", Int) = 128
_MaxDist ("Max distance", Float) = 128
_SurfDist ("Surface distance threshold", Range(0.0001, 0.05)) = 0.001
}
SubShader
{
Tags { "RenderType"="Opaque" }
Cull Front
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
int _MaxSteps;
float _MaxDist;
float _SurfDist;
#define MAX_STEPS _MaxSteps
#define MAX_DIST _MaxDist
#define SURF_DIST _SurfDist
#define REFLECTIONS 3
#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.5
#define SCENE_SCALE 0.2
#define LIMIT_DEPTH_TO_MESH
#include "lib/libgarbage.cginc"
float3 distort(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;
return p;
}
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;
}
float3 floor_col(float3 p) {
// oscillating warp
// p = rotY(p, length(p.xz)*0.03*sin(_Time.y) + _Time.y*0.1);
// constant movement with warped grid
// p = rotY(p, sin(length(p.xz) * 0.2) * 0.2);
// p.x += fmod(_Time.y, 4);
//
// p.x += smoothstep(0, 0.5, fmod(_Time.y, 1)) * 2;
// p.z += smoothstep(0.5, 1, fmod(_Time.y, 1)) * 2;
return lerp(0.08,
checkers(p - 1, 0.06, 0.12, 2),
smoothstep(64, 0, length(p))
);
}
SurfacePoint main(float3 p) {
float3 twistp = rotY(p, sin(length(p.xz) * 0.2) * 0.2);
p = distort(p);
Material floor = mat(floor_col(p));
p.y += 2.5;
Material black = mat(0.05);
SurfacePoint d;
// d = mSphere(p - float3(0, -1, 0), 0.1);
d = mPlaneY(p, 0, floor);
d = qIntersect(d, mSphere(p, 20, d.mat));
float3 rp = repXZ(p, 2, 2);
float h = sin(twistp.x - rp.x) + 2 + sin((twistp.z - rp.z)* 0.17);
d = qUnion(d, mBox(rp - float3(0, h*0.5, 0), float3(0.5, h, 0.5), black));
return d;
}
float3 lighting(Ray ray) {
float3 sun_dir = normalize(float3(2, 1, -1));
if (ray.missed) {
if (ray.dir.y >= 0) {
return lRenderSky(ray.dir, sun_dir);
} else
{
float3 cam = ray.start;
cam.y += 2.5;
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(distort(surface_pos));
return col * (lSky(float3(0,1,0)) + lSun(float3(0,1,0), sun_dir));
}
}
float3 col = lSun(ray.normal, sun_dir);
col *= lShadow(ray.hit_pos + ray.normal * SURF_DIST, sun_dir, 50);
col += lSky(ray.normal);
// col = clamp(col, 0, 1);
// col = smoothstep(0,1,col);
// col = pow(col, 1.3);
return ray.mat.col * col ;
// return col*0.2;
}
ENDCG
}
}
}