Compare commits

...

5 commits

3 changed files with 67 additions and 13 deletions

View file

@ -26,6 +26,11 @@ START_RAYS_IN_BOX // TODO: implement
START_RAYS_IN_SPHERE // TODO: implement
*/
// 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
// scene sdf that includes material data
#ifndef SCENE_FN
@ -103,6 +108,7 @@ struct Material {
float3 col;
float gloss;
};
#define DEFAULT_MAT {float3(1, 1, 1), 0}
Material mat(float3 col = float3(1, 1,1 ), float gloss = 0) {
Material m;
@ -110,6 +116,9 @@ Material mat(float3 col = float3(1, 1,1 ), float gloss = 0) {
m.gloss = gloss;
return m;
}
Material mat(float r, float g, float b) {
return mat(float3(r, g, b));
}
struct SurfacePoint {
float dist;
@ -159,15 +168,14 @@ FragOut frag (V2F i) {
float ray_len = 0;
float3 ray_dir = normalize(i.hit_pos - i.cam_pos);
float3 ray_origin = i.cam_pos;
ray_origin /= SCENE_SCALE;
SurfacePoint point_data;
float3 ray_origin = i.cam_pos / SCENE_SCALE;
Ray ray;
float3 col;
float color_used = 0;// what amount of the final color has been calculated
float prev_gloss = 0;
float3 first_hit;
bool inside_shape;
int ray_num = 0;
for (; ray_num < REFLECTIONS + 1;) {
@ -175,10 +183,7 @@ FragOut frag (V2F i) {
if (ray_num == 0) { // before any bounces
col = LIGHT_FN(ray);
first_hit = ray.hit_pos;
if (ray.steps == 0) {
// discard;
// TODO: sky fn
}
inside_shape = ray.steps == 0;
} else {
float col_amount = color_used + ((1 - prev_gloss) * (1 - color_used));
col = lerp(LIGHT_FN(ray), col, col_amount);
@ -190,7 +195,7 @@ FragOut frag (V2F i) {
}
prev_gloss = ray.mat.gloss;
ray_dir = reflect(ray_dir, ray.normal);
ray_origin = ray.hit_pos + ray_dir * 0.01;
ray_origin = ray.hit_pos + ray_dir * SURF_DIST * 2;
}
#ifdef DISCARD_ON_MISS
@ -198,7 +203,7 @@ FragOut frag (V2F i) {
#endif
FragOut o;
o.col = col;
o.col = clamp(col, 0, 1);
#ifndef DISABLE_DEPTH
float3 depth_point = first_hit * SCENE_SCALE;
@ -206,10 +211,14 @@ FragOut frag (V2F i) {
depth_point = i.hit_pos;
#ifdef LIMIT_DEPTH_TO_MESH
if (length(ray.start - ray.hit_pos) > length(i.cam_pos - i.hit_pos))
if (length(i.cam_pos - first_hit * SCENE_SCALE) > length(i.cam_pos - i.hit_pos))
depth_point = i.hit_pos;
#endif
if (inside_shape) {
depth_point = i.cam_pos + normalize(i.hit_pos - i.cam_pos) * 0.01;
}
#ifdef USE_WORLD_SPACE
float4 clip_pos = mul(UNITY_MATRIX_VP, float4(depth_point, 1));
#else
@ -265,6 +274,10 @@ Ray cast_ray(float3 start, float3 dir, float start_len) {
return ray;
}
float3 default_sky(float3 dir) {
return lRenderSky(dir, normalize(float3(2, 1, 0)));
}
float default_distance_sdf(float3 p) {
return sdSphere(p, 0.5);
}
@ -286,5 +299,4 @@ float3 default_lighting(Ray ray) {
return col;
}
#endif // LIBGARBAGE_INCLUDED

View file

@ -18,11 +18,11 @@ float3 lRenderSky(float3 ray_dir, float3 sun_dir) {
}
//calculate sky light
float3 lSky(float3 normal, float3 sky_col = float3(0.5, 0.8, 0.9)) {
float3 lSky(float3 normal, float3 sky_col = float3(0.1, 0.16, 0.18)) {
return sky_col * (0.5 + 0.5 * normal.y);
}
float lSun(float3 normal, float3 sun_dir, float3 sun_col = float3(7, 5.5, 3)) {
float lSun(float3 normal, float3 sun_dir, float3 sun_col = float3(1, 0.78, 0.43)) {
return sun_col * max(dot(normal, sun_dir), 0);
}

View file

@ -49,5 +49,47 @@ float sdPlaneY(float3 p, float height) {
}
_MAT_VARIANT1(mPlaneY, sdPlaneY, float, height)
float sdHexPrism(float3 p, float width, float height) {
const float3 k = float3(-0.8660254, 0.5, 0.57735);
p = abs(p);
p.xz -= 2.0 * min(dot(k.xy, p.xz), 0) * k.xy;
float2 d = float2(length(p.xz - float2(clamp(p.x,-k.z * width, k.z * width), width)) * sign(p.z - width), p.y - height);
return min(max(d.x, d.y), 0) + length(max(d, 0));
}
_MAT_VARIANT2(mHexPrism, sdHexPrism, float, width, float, height)
float sdInfCylinder(float3 p, float3 c) {
return length(p.xz - c.xy) - c.z;
}
_MAT_VARIANT1(mInfCylinder, sdInfCylinder, float3, c)
float sdCylinder(float3 p, float r, float h) {
float2 d = abs(float2(length(p.xz), p.y)) - float2(r, h);
return min(max(d.x, d.y), 0) + length(max(d, 0));
}
_MAT_VARIANT2(mCylinder, sdCylinder, float, r, float, h)
float sdHelix(float3 p, float r1, float r2, 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;
return length(float2(x2, y)) - r2;
}
_MAT_VARIANT3(mHelix, sdHelix, float, r1, float, r2, float, incline)
float sdLine(float3 p, float3 a, float3 b, float r) {
float3 pa = p - a;
float3 ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0, 1);
return length(p- a - (b-a) * h) - r;
}
_MAT_VARIANT3(mLine, sdLine, float3, a, float3, b, float3, r)
SurfacePoint mDummy(float d, Material mat = DEFAULT_MAT) {
SurfacePoint o;
o.dist = d;
o.mat = mat;
return o;
}