2023-07-24 08:42:25 +02:00
|
|
|
#ifndef LIBGARBAGE_INCLUDED
|
|
|
|
#define LIBGARBAGE_INCLUDED
|
|
|
|
#include "UnityCG.cginc"
|
|
|
|
|
|
|
|
/*
|
2023-07-24 18:15:12 +02:00
|
|
|
## defines with no defaults
|
|
|
|
|
|
|
|
# default is object space
|
|
|
|
USE_WORLD_SPACE
|
|
|
|
|
|
|
|
# renders missed rays as transparent
|
|
|
|
DISCARD_ON_MISS
|
|
|
|
|
|
|
|
# use if the shape of the scene is significnatly different from the shape of the material space
|
|
|
|
# will use SCENE_FN only for the final step, and DISTANCE_FN for raymarching
|
|
|
|
# DISTANCE_FN must return a float
|
|
|
|
SEPARATE_MATERIAL_AND_DIST_FUNCTIONS
|
|
|
|
|
|
|
|
# don't write depth (will have the depth defined by the original mesh)
|
|
|
|
DISABLE_DEPTH
|
|
|
|
|
2023-07-25 12:34:08 +02:00
|
|
|
# meant for frontface culled meshes, makes depth no further than the mesh edges
|
|
|
|
LIMIT_DEPTH_TO_MESH
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
START_RAYS_IN_BOX // TODO: implement
|
|
|
|
START_RAYS_IN_SPHERE // TODO: implement
|
|
|
|
*/
|
|
|
|
|
2023-07-26 18:25:45 +02:00
|
|
|
// sky used as background unless DISCARD_ON_MISS is set, and used in reflections
|
|
|
|
// TODO: implement
|
|
|
|
#ifndef SKY_FN
|
|
|
|
#define SKY_FN default_sky
|
|
|
|
#endif
|
2023-07-24 08:42:25 +02:00
|
|
|
|
|
|
|
// scene sdf that includes material data
|
2023-07-24 18:15:12 +02:00
|
|
|
#ifndef SCENE_FN
|
|
|
|
#define SCENE_FN default_material_sdf
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SEPARATE_MATERIAL_AND_DIST_FUNCTIONS
|
|
|
|
// scene sdf with only distance
|
|
|
|
#ifndef DISTANCE_FN
|
|
|
|
#define DISTANCE_FN default_distance_sdf
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define DISTANCE_FN material_fn_as_dist
|
2023-07-24 08:42:25 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// calculates a color from a Ray
|
|
|
|
#ifndef LIGHT_FN
|
|
|
|
#define LIGHT_FN default_lighting
|
|
|
|
#endif
|
|
|
|
|
2023-07-25 12:34:08 +02:00
|
|
|
// set to less than one if there are artifacts in distorted sdfs
|
|
|
|
#ifndef STEP_MULTIPLIER
|
|
|
|
#define STEP_MULTIPLIER 1
|
|
|
|
#endif
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
// max ray steps, from last bounce (reset on each reflection)
|
|
|
|
#ifndef MAX_STEPS
|
|
|
|
#define MAX_STEPS 128
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// max ray length, from last bounce (length is reset on each reflection)
|
|
|
|
#ifndef MAX_DIST
|
|
|
|
#define MAX_DIST 128
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// the largest surface distance that is counted as a hit
|
|
|
|
#ifndef SURF_DIST
|
|
|
|
#define SURF_DIST 0.001
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef REFLECTIONS
|
|
|
|
#define REFLECTIONS 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SCENE_SCALE
|
|
|
|
#define SCENE_SCALE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct AppData {
|
|
|
|
float4 vertex : POSITION;
|
2023-07-25 12:35:36 +02:00
|
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
2023-07-23 18:31:33 +02:00
|
|
|
};
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
struct V2F {
|
|
|
|
float4 vertex : SV_POSITION;
|
|
|
|
float3 cam_pos : TEXCOORD0;
|
|
|
|
float3 hit_pos : TEXCOORD1;
|
2023-07-25 12:35:36 +02:00
|
|
|
UNITY_VERTEX_OUTPUT_STEREO
|
2023-07-23 18:31:33 +02:00
|
|
|
};
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
struct FragOut {
|
|
|
|
fixed3 col : SV_Target;
|
|
|
|
#ifndef DISABLE_DEPTH
|
|
|
|
float depth : SV_Depth;
|
|
|
|
#endif
|
2023-07-23 18:31:33 +02:00
|
|
|
};
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
struct Material {
|
|
|
|
float3 col;
|
|
|
|
float gloss;
|
|
|
|
};
|
2023-07-29 16:41:25 +02:00
|
|
|
|
2023-07-24 14:47:24 +02:00
|
|
|
#define DEFAULT_MAT {float3(1, 1, 1), 0}
|
2024-08-10 10:45:04 +02:00
|
|
|
Material mat(float3 col = float3(1, 1, 1), float gloss = 0) {
|
2023-07-24 14:47:24 +02:00
|
|
|
Material m;
|
|
|
|
m.col = col;
|
|
|
|
m.gloss = gloss;
|
|
|
|
return m;
|
|
|
|
}
|
2023-07-29 16:41:25 +02:00
|
|
|
Material mat(float r, float g, float b) {
|
|
|
|
return mat(float3(r, g, b));
|
|
|
|
}
|
2024-08-10 10:45:04 +02:00
|
|
|
Material mat(float r, float g, float b, float gloss) {
|
|
|
|
return mat(float3(r, g, b), gloss);
|
|
|
|
}
|
2023-07-24 08:42:25 +02:00
|
|
|
|
|
|
|
struct SurfacePoint {
|
|
|
|
float dist;
|
|
|
|
Material mat;
|
|
|
|
};
|
|
|
|
|
|
|
|
//used for lighting a point
|
|
|
|
struct Ray {
|
|
|
|
float dist;
|
|
|
|
int steps;
|
|
|
|
Material mat;
|
|
|
|
float3 start;
|
|
|
|
float3 dir;
|
|
|
|
float3 hit_pos;
|
|
|
|
float3 normal;
|
|
|
|
bool missed;
|
|
|
|
float min_dist; // smallest distance encountered along the path (only useful for misses; shadow calculations)
|
|
|
|
};
|
|
|
|
|
2023-08-08 20:22:50 +02:00
|
|
|
#define UP float3(0, 1, 0)
|
2023-07-24 08:42:25 +02:00
|
|
|
|
|
|
|
float DISTANCE_FN(float3 p);
|
2023-07-24 18:15:12 +02:00
|
|
|
SurfacePoint SCENE_FN(float3 p);
|
2023-07-24 08:42:25 +02:00
|
|
|
float3 LIGHT_FN(Ray ray);
|
2023-07-30 20:23:29 +02:00
|
|
|
float3 SKY_FN(float3 d);
|
2023-07-24 08:42:25 +02:00
|
|
|
Ray cast_ray(float3 p, float3 d, float startDist = 0);
|
|
|
|
|
2023-07-24 17:14:06 +02:00
|
|
|
#include "libgarbage_shapes.cginc"
|
|
|
|
#include "libgarbage_operations.cginc"
|
|
|
|
#include "libgarbage_lighting.cginc"
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
V2F vert (AppData v) {
|
|
|
|
V2F o;
|
2023-07-25 12:35:36 +02:00
|
|
|
UNITY_SETUP_INSTANCE_ID(v);
|
|
|
|
UNITY_INITIALIZE_OUTPUT(V2F, o);
|
|
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
|
2023-07-24 08:42:25 +02:00
|
|
|
o.vertex = UnityObjectToClipPos(v.vertex);
|
|
|
|
#ifdef USE_WORLD_SPACE
|
|
|
|
o.cam_pos = _WorldSpaceCameraPos;
|
|
|
|
o.hit_pos = mul(unity_ObjectToWorld, v.vertex);
|
|
|
|
#else
|
|
|
|
o.cam_pos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1));
|
|
|
|
o.hit_pos = v.vertex;
|
|
|
|
#endif
|
|
|
|
return o;
|
2023-07-23 18:31:33 +02:00
|
|
|
}
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
FragOut frag (V2F i) {
|
|
|
|
float ray_len = 0;
|
|
|
|
float3 ray_dir = normalize(i.hit_pos - i.cam_pos);
|
|
|
|
|
2023-07-26 18:25:45 +02:00
|
|
|
float3 ray_origin = i.cam_pos / SCENE_SCALE;
|
2023-07-24 08:42:25 +02:00
|
|
|
Ray ray;
|
|
|
|
|
2023-08-08 20:22:50 +02:00
|
|
|
float3 col_acc = 1;
|
2023-07-24 08:42:25 +02:00
|
|
|
float3 first_hit;
|
2023-07-26 18:25:45 +02:00
|
|
|
bool inside_shape;
|
2023-07-24 08:42:25 +02:00
|
|
|
|
2023-07-25 12:34:08 +02:00
|
|
|
int ray_num = 0;
|
|
|
|
for (; ray_num < REFLECTIONS + 1;) {
|
2023-07-24 16:28:02 +02:00
|
|
|
ray = cast_ray(ray_origin, ray_dir);
|
2023-07-24 08:42:25 +02:00
|
|
|
if (ray_num == 0) { // before any bounces
|
|
|
|
first_hit = ray.hit_pos;
|
2023-07-26 18:25:45 +02:00
|
|
|
inside_shape = ray.steps == 0;
|
2023-07-24 08:42:25 +02:00
|
|
|
}
|
2024-08-11 13:33:20 +02:00
|
|
|
float3 light = clamp(LIGHT_FN(ray), 0, 1);
|
2023-08-08 20:22:50 +02:00
|
|
|
col_acc *= ray.mat.col * lerp(light, 1, ray.mat.gloss);
|
2023-07-25 12:34:08 +02:00
|
|
|
ray_num++;
|
2023-07-24 08:42:25 +02:00
|
|
|
if (ray.missed || ray.mat.gloss < 0.01) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ray_dir = reflect(ray_dir, ray.normal);
|
2023-07-29 16:41:25 +02:00
|
|
|
ray_origin = ray.hit_pos + ray_dir * SURF_DIST * 2;
|
2023-07-24 08:42:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DISCARD_ON_MISS
|
2023-07-25 12:34:08 +02:00
|
|
|
if (ray.missed && ray_num == 1) discard;
|
2023-07-24 08:42:25 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
FragOut o;
|
2023-08-08 20:22:50 +02:00
|
|
|
// something something sRGB, colour space is annoying
|
|
|
|
col_acc = pow(col_acc, 2.2);
|
|
|
|
o.col = clamp(col_acc, 0, 1);
|
2023-07-24 08:42:25 +02:00
|
|
|
|
|
|
|
#ifndef DISABLE_DEPTH
|
2023-07-24 16:28:02 +02:00
|
|
|
float3 depth_point = first_hit * SCENE_SCALE;
|
2023-07-25 12:34:08 +02:00
|
|
|
if (ray.missed && ray_num == 1)
|
2023-07-24 14:47:24 +02:00
|
|
|
depth_point = i.hit_pos;
|
2023-07-25 12:34:08 +02:00
|
|
|
|
|
|
|
#ifdef LIMIT_DEPTH_TO_MESH
|
2023-07-29 17:17:31 +02:00
|
|
|
if (length(i.cam_pos - first_hit * SCENE_SCALE) > length(i.cam_pos - i.hit_pos))
|
2023-07-25 12:34:08 +02:00
|
|
|
depth_point = i.hit_pos;
|
|
|
|
#endif
|
|
|
|
|
2023-07-26 18:25:45 +02:00
|
|
|
if (inside_shape) {
|
|
|
|
depth_point = i.cam_pos + normalize(i.hit_pos - i.cam_pos) * 0.01;
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:28:02 +02:00
|
|
|
#ifdef USE_WORLD_SPACE
|
|
|
|
float4 clip_pos = mul(UNITY_MATRIX_VP, float4(depth_point, 1));
|
|
|
|
#else
|
|
|
|
float4 clip_pos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(depth_point, 1)));
|
|
|
|
#endif
|
2023-07-24 08:42:25 +02:00
|
|
|
o.depth = clip_pos.z / clip_pos.w;
|
|
|
|
#ifndef UNITY_REVERSED_Z // basically only OpenGL (unity editor on linux)
|
|
|
|
o.depth = o.depth * 0.5 + 0.5; // remap -1 to 1 range to 0.0 to 1.0
|
|
|
|
#endif
|
|
|
|
#endif
|
2023-07-23 18:31:33 +02:00
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
inline float3 get_normal(float3 pos, float surface_distance, float epsilon = 0.001) {
|
|
|
|
// if epsilon is smaller than 0.001, there are often artifacts
|
|
|
|
const float2 e = float2(epsilon, 0);
|
|
|
|
float3 n = surface_distance - float3(
|
|
|
|
DISTANCE_FN(pos - e.xyy),
|
|
|
|
DISTANCE_FN(pos - e.yxy),
|
|
|
|
DISTANCE_FN(pos - e.yyx));
|
|
|
|
return normalize(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ray cast_ray(float3 start, float3 dir, float start_len) {
|
|
|
|
float ray_len = start_len;
|
2023-07-23 18:31:33 +02:00
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
Ray ray;
|
|
|
|
ray.dir = dir;
|
|
|
|
ray.start = start;
|
|
|
|
ray.min_dist = 100100100; // budget "infinity"
|
|
|
|
ray.missed = true;
|
2023-07-23 18:31:33 +02:00
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
for (int i = 0; i < MAX_STEPS; i++) {
|
|
|
|
ray.hit_pos = start + ray_len * dir;
|
|
|
|
ray.dist = DISTANCE_FN(ray.hit_pos);
|
|
|
|
if (ray.dist < SURF_DIST) {
|
|
|
|
ray.missed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ray.min_dist = min(ray.min_dist, ray.dist);
|
2023-07-25 12:34:08 +02:00
|
|
|
ray_len += ray.dist * STEP_MULTIPLIER;
|
2023-07-24 08:42:25 +02:00
|
|
|
if (ray_len > MAX_DIST) break;
|
|
|
|
}
|
2023-07-23 18:31:33 +02:00
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
ray.steps = i;
|
|
|
|
if (ray.missed) {
|
|
|
|
ray.normal = float3(0, 0, 0);
|
2023-07-24 16:28:02 +02:00
|
|
|
ray.mat = mat();
|
2023-07-24 08:42:25 +02:00
|
|
|
} else {
|
|
|
|
ray.normal = get_normal(ray.hit_pos, ray.dist);
|
2023-07-24 18:15:12 +02:00
|
|
|
ray.mat = SCENE_FN(ray.hit_pos).mat;
|
2023-07-24 08:42:25 +02:00
|
|
|
}
|
|
|
|
return ray;
|
|
|
|
}
|
2023-07-23 18:31:33 +02:00
|
|
|
|
2023-07-26 18:25:45 +02:00
|
|
|
float3 default_sky(float3 dir) {
|
|
|
|
return lRenderSky(dir, normalize(float3(2, 1, 0)));
|
|
|
|
}
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
float default_distance_sdf(float3 p) {
|
|
|
|
return sdSphere(p, 0.5);
|
|
|
|
}
|
2023-07-23 18:31:33 +02:00
|
|
|
|
2023-07-24 18:15:12 +02:00
|
|
|
float material_fn_as_dist(float3 p) {
|
|
|
|
return SCENE_FN(p).dist;
|
|
|
|
}
|
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
SurfacePoint default_material_sdf(float3 p) {
|
|
|
|
return mSphere(p, 0.5);
|
|
|
|
}
|
2023-07-23 18:31:33 +02:00
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
float3 default_lighting(Ray ray) {
|
2023-07-30 12:26:54 +02:00
|
|
|
float3 sun_dir = normalize(float3(1, 0.5, -0.5));
|
2023-07-24 08:42:25 +02:00
|
|
|
if (ray.missed)
|
2023-07-30 20:23:29 +02:00
|
|
|
return SKY_FN(ray.dir);
|
2023-07-30 12:26:54 +02:00
|
|
|
float3 light = lSun(ray.normal, sun_dir);
|
|
|
|
light *= lShadow(ray.hit_pos + ray.normal * SURF_DIST, sun_dir, 50);
|
2024-08-11 13:33:20 +02:00
|
|
|
light += lSky(ray.normal);
|
2023-08-08 20:22:50 +02:00
|
|
|
return light;
|
2023-07-24 08:42:25 +02:00
|
|
|
}
|
2023-07-23 18:31:33 +02:00
|
|
|
|
2023-07-24 08:42:25 +02:00
|
|
|
#endif // LIBGARBAGE_INCLUDED
|