From b9159d9e7ced55f0422295d8c36da075f9bbd3be Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 24 Jul 2023 08:42:25 +0200 Subject: [PATCH 1/7] shell of new raymarching lib --- Assets/raymarched/Raymarching.unity | 2 +- Assets/raymarched/lib/GarbageExample.mat | 4 +- Assets/raymarched/lib/libgarbage.cginc | 266 ++++++++++++++---- Assets/raymarched/lib/libgarbage_end.cginc | 1 - .../raymarched/lib/libgarbage_example.shader | 27 +- Assets/raymarched/lib/libgarbage_shapes.cginc | 40 +++ ...ginc.meta => libgarbage_shapes.cginc.meta} | 2 +- 7 files changed, 269 insertions(+), 73 deletions(-) delete mode 100644 Assets/raymarched/lib/libgarbage_end.cginc create mode 100644 Assets/raymarched/lib/libgarbage_shapes.cginc rename Assets/raymarched/lib/{libgarbage_end.cginc.meta => libgarbage_shapes.cginc.meta} (80%) diff --git a/Assets/raymarched/Raymarching.unity b/Assets/raymarched/Raymarching.unity index df53584..24fb023 100644 --- a/Assets/raymarched/Raymarching.unity +++ b/Assets/raymarched/Raymarching.unity @@ -529,7 +529,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!65 &683266144 BoxCollider: m_ObjectHideFlags: 0 diff --git a/Assets/raymarched/lib/GarbageExample.mat b/Assets/raymarched/lib/GarbageExample.mat index 27e3981..66e737e 100644 --- a/Assets/raymarched/lib/GarbageExample.mat +++ b/Assets/raymarched/lib/GarbageExample.mat @@ -63,8 +63,8 @@ Material: - _GlossMapScale: 1 - _Glossiness: 0.5 - _GlossyReflections: 1 - - _MaxDist: 256 - - _MaxSteps: 256 + - _MaxDist: 128 + - _MaxSteps: 128 - _Metallic: 0 - _Mode: 0 - _OcclusionStrength: 1 diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index 17ba790..804ca3b 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -1,61 +1,229 @@ -struct appdata -{ - float4 vertex : POSITION; -}; +#ifndef LIBGARBAGE_INCLUDED +#define LIBGARBAGE_INCLUDED +#include "UnityCG.cginc" -struct v2f -{ - float4 vertex : SV_POSITION; - float3 vCamPos : TEXCOORD1; - float3 vHitPos : TEXCOORD2; -}; +/* +# defines with no defaults +USE_WORLD_SPACE +DISCARD_ON_MISS +USE_MATERIALS_IN_RAYMARCH // TODO: implement, this is easier to work with but more expensive +DISABLE_DEPTH +START_RAYS_IN_BOX // TODO: implement +START_RAYS_IN_SPHERE // TODO: implement +*/ -struct fragOut -{ - fixed4 col : SV_Target; - // float depth : SV_Depth; -}; - -v2f vert (appdata v) -{ - v2f o; - o.vertex = UnityObjectToClipPos(v.vertex); -#ifdef USE_WORLD_SPACE - o.vCamPos = _WorldSpaceCameraPos; - o.vHitPos = mul(unity_ObjectToWorld, v.vertex); -#else - o.vCamPos = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1)); - o.vHitPos = v.vertex; +// scene sdf with only distance +#ifndef DISTANCE_FN +#define DISTANCE_FN default_distance_sdf #endif - return o; -} -fragOut frag (v2f i) -{ - fragOut o; - o.col = 1; - o.col.r = 0; +// scene sdf that includes material data +#ifndef MATERIAL_FN +#define MATERIAL_FN default_material_sdf +#endif + +// calculates a color from a Ray +#ifndef LIGHT_FN +#define LIGHT_FN default_lighting +#endif + +// 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 + +// TODO: implement +#ifndef REFLECTIONS +#define REFLECTIONS 0 +#endif + +// TODO: implement +#ifndef SCENE_SCALE +#define SCENE_SCALE 1 +#endif + +// TODO: implement +#ifndef AMBIENT_OCCLUSION_STEPS +#define AMBIENT_OCCLUSION_STEPS 4 +#endif + + +struct AppData { + float4 vertex : POSITION; +}; + +struct V2F { + float4 vertex : SV_POSITION; + float3 cam_pos : TEXCOORD0; + float3 hit_pos : TEXCOORD1; +}; + +struct FragOut { + fixed3 col : SV_Target; +#ifndef DISABLE_DEPTH + float depth : SV_Depth; +#endif +}; + +struct Material { + float3 col; + float gloss; +}; +#define DEFAULT_MAT {float3(0.2, 0.2, 0.2), 0} + +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) +}; + +#include "libgarbage_shapes.cginc" + +float DISTANCE_FN(float3 p); +SurfacePoint MATERIAL_FN(float3 p); +float3 LIGHT_FN(Ray ray); +Ray cast_ray(float3 p, float3 d, float startDist = 0); + +V2F vert (AppData v) { + V2F o; + 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; } -// #define SOME_MAGIC(main_fn) float3 main1()\ -// main_fn \ +FragOut frag (V2F i) { + float ray_len = 0; + float3 ray_dir = normalize(i.hit_pos - i.cam_pos); -#define SOME_MAGIC(PASS, function_definition) float3 main##PASS() function_definition + float3 last_bounce = i.cam_pos; + SurfacePoint point_data; + Ray ray; -#define DO_MAGIC MAIN_FN(1) + float3 col; + float color_used = 0;// what amount of the final color has been calculated + float prev_gloss = 0; + float3 first_hit; -// #define SECOND_PASS 1 -// #define AA MAIN_FN -// #define BB MAIN_FN + for (int ray_num = 0; ray_num < REFLECTIONS + 1; ray_num++) { + ray = cast_ray(last_bounce, ray_dir); + if (ray_num == 0) { // before any bounces + col = LIGHT_FN(ray); + first_hit = ray.hit_pos; + } else { + float col_amount = color_used + ((1 - prev_gloss) * (1 - color_used)); + col = lerp(LIGHT_FN(ray), col, col_amount); + color_used = col_amount; + } + if (ray.missed || ray.mat.gloss < 0.01) { + break; + } + prev_gloss = ray.mat.gloss; + ray_dir = reflect(ray_dir, ray.normal); + last_bounce = ray.hit_pos + ray_dir * 0.01; + } + + #ifdef DISCARD_ON_MISS + if (ray.missed && ray_num == 0) discard; + #endif + + FragOut o; + o.col = col; + + #ifndef DISABLE_DEPTH + float4 clip_pos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(first_hit, 1))); + 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 + return o; +} + +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; + + Ray ray; + ray.dir = dir; + ray.start = start; + ray.min_dist = 100100100; // budget "infinity" + ray.missed = true; + + 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); + ray_len += ray.dist; + if (ray_len > MAX_DIST) break; + } + + ray.steps = i; + if (ray.missed) { + ray.normal = float3(0, 0, 0); + } else { + ray.normal = get_normal(ray.hit_pos, ray.dist); + ray.mat = MATERIAL_FN(ray.hit_pos).mat; + } + return ray; +} + +float default_distance_sdf(float3 p) { + return sdSphere(p, 0.5); +} + +SurfacePoint default_material_sdf(float3 p) { + return mSphere(p, 0.5); +} + +float3 default_lighting(Ray ray) { + float3 sun_dir = normalize(float3(1, 0.5, 0)); + if (ray.missed) + return 0.1; + float3 col = 0; + col = ray.mat.col * dot(ray.normal, sun_dir); + return col; +} -// float3 main2() \ -// main_fn - -// \ -// float3 main2() \ -// main_fn\ - - -// #define SECOND_PASS \ +#endif // LIBGARBAGE_INCLUDED diff --git a/Assets/raymarched/lib/libgarbage_end.cginc b/Assets/raymarched/lib/libgarbage_end.cginc deleted file mode 100644 index ee953f0..0000000 --- a/Assets/raymarched/lib/libgarbage_end.cginc +++ /dev/null @@ -1 +0,0 @@ -#define DO_MAGIC(p) MAIN_FN(p) \ No newline at end of file diff --git a/Assets/raymarched/lib/libgarbage_example.shader b/Assets/raymarched/lib/libgarbage_example.shader index 84e10d7..2062328 100644 --- a/Assets/raymarched/lib/libgarbage_example.shader +++ b/Assets/raymarched/lib/libgarbage_example.shader @@ -2,17 +2,16 @@ Shader "CrispyPin/LibGarbageExample" { Properties { - [Header(Raymarcher Properties)] - _MaxSteps ("Max steps", Int) = 256 - _MaxDist ("Max distance", Float) = 256 + _MaxSteps ("Max steps", Int) = 128 + _MaxDist ("Max distance", Float) = 128 _SurfDist ("Surface distance threshold", Range(0.00001, 0.05)) = 0.001 } SubShader { Tags { "RenderType"="Opaque" } - Cull Off + Cull Front LOD 100 Pass @@ -21,23 +20,13 @@ Shader "CrispyPin/LibGarbageExample" #pragma vertex vert #pragma fragment frag + int _MaxSteps; + #define MAX_STEPS _MaxSteps + + // #define DISABLE_DEPTH + // #define DISCARD_ON_MISS #include "libgarbage.cginc" - // #define MAIN_FN(PASS) float3 main##PASS ()\ - - #define MAIN_FN(PASS) SOME_MAGIC(PASS,\ - {\ - return float3(1,0,0);\ - }\ - ) - // #include "libgarbage_end.cginc" - // #define DO_MAGIC(p) MAIN_FN(p) - DO_MAGIC - - // {return 0;} - // MAIN_FN(1) {return 0;} - - ENDCG } } diff --git a/Assets/raymarched/lib/libgarbage_shapes.cginc b/Assets/raymarched/lib/libgarbage_shapes.cginc new file mode 100644 index 0000000..996315d --- /dev/null +++ b/Assets/raymarched/lib/libgarbage_shapes.cginc @@ -0,0 +1,40 @@ + +#define _MAT_VARIANT1(NEW_NAME, BASE_FN, TYPE_1, ARG_1) \ + SurfacePoint NEW_NAME(float3 p, TYPE_1 ARG_1, Material mat = DEFAULT_MAT) {\ + SurfacePoint o;\ + o.dist = BASE_FN(p, ARG_1);\ + o.mat = mat;\ + return o;\ + } + +#define _MAT_VARIANT2(NEW_NAME, BASE_FN, TYPE_1, ARG_1, TYPE_2, ARG_2) \ + SurfacePoint NEW_NAME(float3 p, TYPE_1 ARG_1, TYPE_2 ARG_2, Material mat = DEFAULT_MAT) {\ + SurfacePoint o;\ + o.dist = BASE_FN(p, ARG_1, ARG_2);\ + o.mat = mat;\ + return o;\ + } + +#define _MAT_VARIANT3(NEW_NAME, BASE_FN, TYPE_1, ARG_1, TYPE_2, ARG_2, TYPE_3, ARG_3) \ + SurfacePoint NEW_NAME(float3 p, TYPE_1 ARG_1, TYPE_2 ARG_2, TYPE_3 ARG_3, Material mat = DEFAULT_MAT) {\ + SurfacePoint o;\ + o.dist = BASE_FN(p, ARG_1, ARG_2, ARG_3);\ + o.mat = mat;\ + return o;\ + } + +// +// Most of these are taken from https://iquilezles.org/articles/distfunctions/ +// Thank you Inigo Quilez <3 + +float sdBox(float3 p, float3 size) { + float3 q = abs(p) - size / 2.0; + return length(max(q, 0)) + min(max(q.x, max(q.y, q.z)), 0); +} +_MAT_VARIANT1(mBox, sdBox, float3, size) + +float sdSphere(float3 p, float radius) { + return length(p) - radius; +} +_MAT_VARIANT1(mSphere, sdSphere, float, radius) + diff --git a/Assets/raymarched/lib/libgarbage_end.cginc.meta b/Assets/raymarched/lib/libgarbage_shapes.cginc.meta similarity index 80% rename from Assets/raymarched/lib/libgarbage_end.cginc.meta rename to Assets/raymarched/lib/libgarbage_shapes.cginc.meta index 549f561..967350e 100644 --- a/Assets/raymarched/lib/libgarbage_end.cginc.meta +++ b/Assets/raymarched/lib/libgarbage_shapes.cginc.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 26203816190e7e521a12959d895d68a3 +guid: 4d08637fa3f7e141190ffd1bdbca2c19 ShaderImporter: externalObjects: {} defaultTextures: [] From e5f8d6b6b33e289a45784e9e9c0c63c9559947be Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 24 Jul 2023 14:47:24 +0200 Subject: [PATCH 2/7] libgarbage: add most operations --- Assets/raymarched/Raymarching.unity | 6 +- Assets/raymarched/lib/GarbageExample.mat | 2 +- Assets/raymarched/lib/libgarbage.cginc | 14 +- .../raymarched/lib/libgarbage_example.shader | 18 +- .../lib/libgarbage_operations.cginc | 164 ++++++++++++++++++ .../lib/libgarbage_operations.cginc.meta | 9 + 6 files changed, 205 insertions(+), 8 deletions(-) create mode 100644 Assets/raymarched/lib/libgarbage_operations.cginc create mode 100644 Assets/raymarched/lib/libgarbage_operations.cginc.meta diff --git a/Assets/raymarched/Raymarching.unity b/Assets/raymarched/Raymarching.unity index 24fb023..471d849 100644 --- a/Assets/raymarched/Raymarching.unity +++ b/Assets/raymarched/Raymarching.unity @@ -598,8 +598,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 683266143} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -0.754, y: -0.24626623, z: 0.587} - m_LocalScale: {x: 1, y: 1, z: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 3, y: 3, z: 3} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 7 @@ -773,7 +773,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 1 + m_IsActive: 0 --- !u!4 &779234489 Transform: m_ObjectHideFlags: 0 diff --git a/Assets/raymarched/lib/GarbageExample.mat b/Assets/raymarched/lib/GarbageExample.mat index 66e737e..909f5bc 100644 --- a/Assets/raymarched/lib/GarbageExample.mat +++ b/Assets/raymarched/lib/GarbageExample.mat @@ -64,7 +64,7 @@ Material: - _Glossiness: 0.5 - _GlossyReflections: 1 - _MaxDist: 128 - - _MaxSteps: 128 + - _MaxSteps: 512 - _Metallic: 0 - _Mode: 0 - _OcclusionStrength: 1 diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index 804ca3b..85fe0ce 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -79,7 +79,13 @@ struct Material { float3 col; float gloss; }; -#define DEFAULT_MAT {float3(0.2, 0.2, 0.2), 0} +#define DEFAULT_MAT {float3(1, 1, 1), 0} +Material mat(float3 col = float3(1, 1,1 ), float gloss = 0) { + Material m; + m.col = col; + m.gloss = gloss; + return m; +} struct SurfacePoint { float dist; @@ -100,6 +106,7 @@ struct Ray { }; #include "libgarbage_shapes.cginc" +#include "libgarbage_operations.cginc" float DISTANCE_FN(float3 p); SurfacePoint MATERIAL_FN(float3 p); @@ -158,7 +165,10 @@ FragOut frag (V2F i) { o.col = col; #ifndef DISABLE_DEPTH - float4 clip_pos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(first_hit, 1))); + float3 depth_point = first_hit; + if (ray.missed) + depth_point = i.hit_pos; + float4 clip_pos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(depth_point, 1))); 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 diff --git a/Assets/raymarched/lib/libgarbage_example.shader b/Assets/raymarched/lib/libgarbage_example.shader index 2062328..dd008dc 100644 --- a/Assets/raymarched/lib/libgarbage_example.shader +++ b/Assets/raymarched/lib/libgarbage_example.shader @@ -23,10 +23,24 @@ Shader "CrispyPin/LibGarbageExample" int _MaxSteps; #define MAX_STEPS _MaxSteps - // #define DISABLE_DEPTH - // #define DISCARD_ON_MISS + #define REFLECTIONS 1 + #define DISTANCE_FN main_dist + #define MATERIAL_FN main_mat + #define DISABLE_DEPTH + #define DISCARD_ON_MISS + #define USE_WORLD_SPACE #include "libgarbage.cginc" + float main_dist(float3 p) { + p = repXYZ(p, 2); + return qSub(sdSphere(p, 0.5), sdSphere(p - 0.3, 0.2), 0.05); + } + + SurfacePoint main_mat(float3 p) { + float3 p2 = repXYZ(p, 2); + return mSphere(p2, 0.5, mat(clamp(abs(p), 0, 1), 0.1)); + } + ENDCG } } diff --git a/Assets/raymarched/lib/libgarbage_operations.cginc b/Assets/raymarched/lib/libgarbage_operations.cginc new file mode 100644 index 0000000..a39ec96 --- /dev/null +++ b/Assets/raymarched/lib/libgarbage_operations.cginc @@ -0,0 +1,164 @@ +// soft min of a and b with smoothing factor k +float smin(float a, float b, float k) { + float h = max(k - abs(a-b), 0) / k; + return min(a, b) - h * h * h * k * (1 / 6.0); +} + +// soft max of a and b with smoothing factor k +float smax(float a, float b, float k) { + float h = max(k - abs(a - b), 0) / k; + return max(a, b) + h * h * h * k * (1 / 6.0); +} + +Material mixMat(Material a, Material b, float fac) { + Material m; + m.col = lerp(a.col, b.col, fac); + m.gloss = lerp(a.gloss, b.gloss, fac); + return m; +} + +Material mixMat(SurfacePoint a, SurfacePoint b) { + float fac = clamp(a.dist/(a.dist + b.dist), 0, 1); + return mixMat(a.mat, b.mat, fac); +} + +// -------------------------------- +// base sdf operations +// -------------------------------- + +float qUnion(float a, float b) { + return min(a, b); +} + +float qUnion(float a, float b, float smooth) { + return smin(a, b, smooth); +} + +SurfacePoint qUnion(SurfacePoint a, SurfacePoint b) { + SurfacePoint o; + o.dist = min(a.dist, b.dist); + o.mat = mixMat(a, b); + return o; +} + +SurfacePoint qUnion(SurfacePoint a, SurfacePoint b, float smooth) { + SurfacePoint o; + o.dist = smin(a.dist, b.dist, smooth); + o.mat = mixMat(a, b); + return o; +} + +float qSub(float a, float b) { + return max(a, -b); +} + +float qSub(float a, float b, float smooth) { + return smax(a, -b, smooth); +} + +SurfacePoint qSub(SurfacePoint a, SurfacePoint b) { + SurfacePoint o; + o.dist = max(a.dist, -b.dist); + o.mat = a.mat; + return o; +} + +SurfacePoint qSub(SurfacePoint a, SurfacePoint b, float smooth) { + SurfacePoint o; + o.dist = smax(a.dist, -b.dist, smooth); + o.mat = a.mat; + return o; +} + +float qIntersect(float a, float b) { + return max(a, b); +} + +float qIntersect(float a, float b, float smooth) { + return smax(a, b, smooth); +} + +SurfacePoint qIntersect(SurfacePoint a, SurfacePoint b) { + SurfacePoint o; + o.dist = max(a.dist, b.dist); + o.mat = mixMat(a, b); + return o; +} + +SurfacePoint qIntersect(SurfacePoint a, SurfacePoint b, float smooth) { + SurfacePoint o; + o.dist = smax(a.dist, b.dist, smooth); + o.mat = mixMat(a, b); + return o; +} + +float qRound(float a, float radius) { + return a - radius; +} + +SurfacePoint qRound(SurfacePoint a, float radius) { + a.dist -= radius; + return a; +} + +// -------------------------------- +// spatial warping +// -------------------------------- + +float3 rotX(float3 p, float a) { + return mul(float3x3(1, 0, 0, 0, cos(a), -sin(a), 0, sin(a), cos(a)), p); +} + +float3 rotY(float3 p, float a) { + return mul(float3x3(cos(a), 0, sin(a), 0, 1, 0, -sin(a), 0, cos(a)), p); +} + +float3 rotZ(float3 p, float a) { + return mul(float3x3(cos(a), -sin(a), 0, sin(a), cos(a), 0, 0, 0, 1), p); +} + +// repeats space every r units, centered on the origin +float3 repXYZ(float3 p, float3 r) { + float3 o = p; + o = fmod(abs(p + r/2.0), r) - r/2.0; + o *= sign(o); + return o; +} + +// repeats space every r units, centered on the origin, no sign +float3 repXYZUnsigned(float3 p, float3 r) { + return fmod(abs(p + r / 2.0), r) - r / 2.0; +} + +// repeats space +inline float3 repXZ(float3 p, float x, float z) { + float3 o = p; + o.x = fmod(abs(p.x) + x / 2.0, x) - x / 2.0; + o.x *= sign(p.x); + o.z = fmod(abs(p.z) + z / 2.0, z) - z / 2.0; + o.z *= sign(p.z); + return o; +} + +// -------------------------------- +// misc +// -------------------------------- + +float3 HSV(float h, float s, float v) { + h *= 6; + float c = s * v; + float x = c * (1 - abs(fmod(h, 2) - 1)); + float m = v - c; + c += m; + x += m; + + float3 colors[6] = { + float3(c, x, m), + float3(x, c, m), + float3(m, c, x), + float3(m, x, c), + float3(x, m, c), + float3(c, m, x)}; + + return colors[int(h)]; +} diff --git a/Assets/raymarched/lib/libgarbage_operations.cginc.meta b/Assets/raymarched/lib/libgarbage_operations.cginc.meta new file mode 100644 index 0000000..01f65b5 --- /dev/null +++ b/Assets/raymarched/lib/libgarbage_operations.cginc.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 6f298ca37830a108492fa68448a84676 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: From a2fb224be5944fd8c29c4ada9cf580e9364c663f Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 24 Jul 2023 16:28:02 +0200 Subject: [PATCH 3/7] libgarbage: implement scene scaling & fix depth --- Assets/raymarched/Raymarching.unity | 6 ++-- Assets/raymarched/lib/libgarbage.cginc | 20 ++++++----- .../raymarched/lib/libgarbage_example.shader | 35 ++++++++++++++++--- Assets/raymarched/lib/libgarbage_shapes.cginc | 13 +++++++ 4 files changed, 58 insertions(+), 16 deletions(-) diff --git a/Assets/raymarched/Raymarching.unity b/Assets/raymarched/Raymarching.unity index 471d849..9d30701 100644 --- a/Assets/raymarched/Raymarching.unity +++ b/Assets/raymarched/Raymarching.unity @@ -598,8 +598,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 683266143} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0, z: 0} - m_LocalScale: {x: 3, y: 3, z: 3} + m_LocalPosition: {x: 0, y: 0.26, z: -0.407} + m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 7 @@ -1516,7 +1516,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!65 &1485771012 BoxCollider: m_ObjectHideFlags: 0 diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index 85fe0ce..febeb75 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -42,12 +42,10 @@ START_RAYS_IN_SPHERE // TODO: implement #define SURF_DIST 0.001 #endif -// TODO: implement #ifndef REFLECTIONS #define REFLECTIONS 0 #endif -// TODO: implement #ifndef SCENE_SCALE #define SCENE_SCALE 1 #endif @@ -130,7 +128,8 @@ FragOut frag (V2F i) { float ray_len = 0; float3 ray_dir = normalize(i.hit_pos - i.cam_pos); - float3 last_bounce = i.cam_pos; + float3 ray_origin = i.cam_pos; + ray_origin /= SCENE_SCALE; SurfacePoint point_data; Ray ray; @@ -140,7 +139,7 @@ FragOut frag (V2F i) { float3 first_hit; for (int ray_num = 0; ray_num < REFLECTIONS + 1; ray_num++) { - ray = cast_ray(last_bounce, ray_dir); + ray = cast_ray(ray_origin, ray_dir); if (ray_num == 0) { // before any bounces col = LIGHT_FN(ray); first_hit = ray.hit_pos; @@ -154,7 +153,7 @@ FragOut frag (V2F i) { } prev_gloss = ray.mat.gloss; ray_dir = reflect(ray_dir, ray.normal); - last_bounce = ray.hit_pos + ray_dir * 0.01; + ray_origin = ray.hit_pos + ray_dir * 0.01; } #ifdef DISCARD_ON_MISS @@ -165,10 +164,14 @@ FragOut frag (V2F i) { o.col = col; #ifndef DISABLE_DEPTH - float3 depth_point = first_hit; - if (ray.missed) + float3 depth_point = first_hit * SCENE_SCALE; + if (ray.missed && ray_num == 0) depth_point = i.hit_pos; - float4 clip_pos = mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, float4(depth_point, 1))); + #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 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 @@ -211,6 +214,7 @@ Ray cast_ray(float3 start, float3 dir, float start_len) { ray.steps = i; if (ray.missed) { ray.normal = float3(0, 0, 0); + ray.mat = mat(); } else { ray.normal = get_normal(ray.hit_pos, ray.dist); ray.mat = MATERIAL_FN(ray.hit_pos).mat; diff --git a/Assets/raymarched/lib/libgarbage_example.shader b/Assets/raymarched/lib/libgarbage_example.shader index dd008dc..c97e0c2 100644 --- a/Assets/raymarched/lib/libgarbage_example.shader +++ b/Assets/raymarched/lib/libgarbage_example.shader @@ -22,18 +22,34 @@ Shader "CrispyPin/LibGarbageExample" int _MaxSteps; #define MAX_STEPS _MaxSteps + float _MaxDist; + #define MAX_DIST _MaxDist + float _SurfDist; + #define SURF_DIST _SurfDist #define REFLECTIONS 1 #define DISTANCE_FN main_dist #define MATERIAL_FN main_mat - #define DISABLE_DEPTH - #define DISCARD_ON_MISS - #define USE_WORLD_SPACE + #define LIGHT_FN lighting + // #define DISABLE_DEPTH + // #define DISCARD_ON_MISS + // #define USE_WORLD_SPACE + #define SCENE_SCALE 0.05 + #include "libgarbage.cginc" float main_dist(float3 p) { - p = repXYZ(p, 2); - return qSub(sdSphere(p, 0.5), sdSphere(p - 0.3, 0.2), 0.05); + 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; } SurfacePoint main_mat(float3 p) { @@ -41,6 +57,15 @@ Shader "CrispyPin/LibGarbageExample" return mSphere(p2, 0.5, mat(clamp(abs(p), 0, 1), 0.1)); } + float3 lighting(Ray ray) { + float3 sun_dir = normalize(float3(1, 0.5, 0)); + if (ray.missed) + return float3(0.15, 0.25, 0.3); + float3 col = 0; + col = ray.mat.col * clamp(dot(ray.normal, sun_dir),0.01,1); + return col; + } + ENDCG } } diff --git a/Assets/raymarched/lib/libgarbage_shapes.cginc b/Assets/raymarched/lib/libgarbage_shapes.cginc index 996315d..506180e 100644 --- a/Assets/raymarched/lib/libgarbage_shapes.cginc +++ b/Assets/raymarched/lib/libgarbage_shapes.cginc @@ -38,3 +38,16 @@ float sdSphere(float3 p, float radius) { } _MAT_VARIANT1(mSphere, sdSphere, float, radius) +float sdTorus(float3 p, float radius, float thickness) { + float2 q = float2(length(p.xz) - radius, p.y); + return length(q) - thickness; +} +_MAT_VARIANT2(mTorus, sdTorus, float, radius, float, thickness) + +float sdPlaneY(float3 p, float height) { + return p.y - height; +} +_MAT_VARIANT1(mPlaneY, sdPlaneY, float, height) + + + From eadf1193f646320f0c044c668f2e3e8c1170f1c2 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 24 Jul 2023 17:14:06 +0200 Subject: [PATCH 4/7] libgarbage: add some common lighting operations --- Assets/raymarched/Raymarching.unity | 99 ++++++++++++++++++- Assets/raymarched/lib/GarbageExample.mat | 2 +- Assets/raymarched/lib/libgarbage.cginc | 6 +- .../raymarched/lib/libgarbage_example.shader | 32 ++++-- .../raymarched/lib/libgarbage_lighting.cginc | 63 ++++++++++++ .../lib/libgarbage_lighting.cginc.meta | 9 ++ .../lib/libgarbage_operations.cginc | 23 ----- 7 files changed, 198 insertions(+), 36 deletions(-) create mode 100644 Assets/raymarched/lib/libgarbage_lighting.cginc create mode 100644 Assets/raymarched/lib/libgarbage_lighting.cginc.meta diff --git a/Assets/raymarched/Raymarching.unity b/Assets/raymarched/Raymarching.unity index 9d30701..d7510e8 100644 --- a/Assets/raymarched/Raymarching.unity +++ b/Assets/raymarched/Raymarching.unity @@ -599,7 +599,7 @@ Transform: m_GameObject: {fileID: 683266143} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0.26, z: -0.407} - m_LocalScale: {x: 1, y: 1, z: 1} + m_LocalScale: {x: 10, y: 10, z: 10} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 7 @@ -1288,6 +1288,99 @@ MeshFilter: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 950930976} m_Mesh: {fileID: 4393975565123819156, guid: 955b9c52fc0be3217a2074e1e2169b9d, type: 3} +--- !u!1 &1274159448 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1274159452} + - component: {fileID: 1274159451} + - component: {fileID: 1274159450} + - component: {fileID: 1274159449} + m_Layer: 0 + m_Name: Cube (2) + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!65 &1274159449 +BoxCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1274159448} + m_Material: {fileID: 0} + m_IsTrigger: 1 + m_Enabled: 1 + serializedVersion: 2 + m_Size: {x: 1, y: 1, z: 1} + m_Center: {x: 0, y: 0, z: 0} +--- !u!23 &1274159450 +MeshRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1274159448} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_MotionVectors: 1 + m_LightProbeUsage: 1 + m_ReflectionProbeUsage: 1 + m_RayTracingMode: 2 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: c5afce7a29c155681939e1ad4f96dc18, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 +--- !u!33 &1274159451 +MeshFilter: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1274159448} + m_Mesh: {fileID: -5495902117074765545, guid: 85702488cb92b9182a44437d143f9fc4, type: 3} +--- !u!4 &1274159452 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1274159448} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 1, y: 0.26, z: -0.407} + m_LocalScale: {x: 0.05, y: 0.05, z: 0.05} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 8 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1450495540 GameObject: m_ObjectHideFlags: 0 @@ -1585,8 +1678,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1485771011} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -0.452, y: 0.221, z: -0.984} - m_LocalScale: {x: 1, y: 1, z: 1} + m_LocalPosition: {x: 0.429, y: 0.221, z: -1.243} + m_LocalScale: {x: 19.2, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 4 diff --git a/Assets/raymarched/lib/GarbageExample.mat b/Assets/raymarched/lib/GarbageExample.mat index 909f5bc..bc8027f 100644 --- a/Assets/raymarched/lib/GarbageExample.mat +++ b/Assets/raymarched/lib/GarbageExample.mat @@ -63,7 +63,7 @@ Material: - _GlossMapScale: 1 - _Glossiness: 0.5 - _GlossyReflections: 1 - - _MaxDist: 128 + - _MaxDist: 512 - _MaxSteps: 512 - _Metallic: 0 - _Mode: 0 diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index febeb75..169ca38 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -103,14 +103,16 @@ struct Ray { float min_dist; // smallest distance encountered along the path (only useful for misses; shadow calculations) }; -#include "libgarbage_shapes.cginc" -#include "libgarbage_operations.cginc" float DISTANCE_FN(float3 p); SurfacePoint MATERIAL_FN(float3 p); float3 LIGHT_FN(Ray ray); Ray cast_ray(float3 p, float3 d, float startDist = 0); +#include "libgarbage_shapes.cginc" +#include "libgarbage_operations.cginc" +#include "libgarbage_lighting.cginc" + V2F vert (AppData v) { V2F o; o.vertex = UnityObjectToClipPos(v.vertex); diff --git a/Assets/raymarched/lib/libgarbage_example.shader b/Assets/raymarched/lib/libgarbage_example.shader index c97e0c2..a2bcde2 100644 --- a/Assets/raymarched/lib/libgarbage_example.shader +++ b/Assets/raymarched/lib/libgarbage_example.shader @@ -27,13 +27,13 @@ Shader "CrispyPin/LibGarbageExample" float _SurfDist; #define SURF_DIST _SurfDist - #define REFLECTIONS 1 + #define REFLECTIONS 3 #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 USE_WORLD_SPACE #define SCENE_SCALE 0.05 #include "libgarbage.cginc" @@ -53,16 +53,34 @@ Shader "CrispyPin/LibGarbageExample" } SurfacePoint main_mat(float3 p) { - float3 p2 = repXYZ(p, 2); - return mSphere(p2, 0.5, mat(clamp(abs(p), 0, 1), 0.1)); + 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; } float3 lighting(Ray ray) { - float3 sun_dir = normalize(float3(1, 0.5, 0)); if (ray.missed) - return float3(0.15, 0.25, 0.3); + 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 * clamp(dot(ray.normal, sun_dir),0.01,1); + 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; } diff --git a/Assets/raymarched/lib/libgarbage_lighting.cginc b/Assets/raymarched/lib/libgarbage_lighting.cginc new file mode 100644 index 0000000..09328e5 --- /dev/null +++ b/Assets/raymarched/lib/libgarbage_lighting.cginc @@ -0,0 +1,63 @@ + +// -------------------------------- +// common lighting operations +// -------------------------------- + +float3 lRenderSun(float3 ray_dir, float3 sun_dir) { + float alignment = min(acos(dot(ray_dir, sun_dir)), 1); + float sun_amount = smax(min(0.03 / alignment, 5) - 0.06, 0, 0.15); + return sun_amount* float3(0.8, 0.4, 0.1); +} + +// a basic procedural sky +float3 lRenderSky(float3 ray_dir, float3 sun_dir) { + float3 rendered_sun = lRenderSun(ray_dir, sun_dir); + // float3 rendered_sun = max(0, pow(dot(ray_dir, sun_dir) + 0.4, 10)-28) * float3(0.8, 0.4, 0); + return float3(0.7, 0.75, 0.8) - abs(ray_dir.y) * 0.5 + rendered_sun; + // return rendered_sun; +} + +//calculate sky light +float3 lSky(float3 normal, float3 sky_col = float3(0.5, 0.8, 0.9)) { + return sky_col * (0.5 + 0.5 * normal.y); +} + +float lSun(float3 normal, float3 sun_dir, float3 sun_col = float3(7, 5.5, 3)) { + return sun_col * max(dot(normal, sun_dir), 0); +} + +// soft shadows +float lShadow(float3 p, float3 sun_dir, float sharpness = 8) { + float shadow = 1; + for (float ray_len = 0.001; ray_len < MAX_DIST / 2.0;) + { + float dist = DISTANCE_FN(p + sun_dir * ray_len); + if (dist < SURF_DIST) return 0; + shadow = min(shadow, sharpness * dist / ray_len); + ray_len += dist; + } + return shadow; +} + +// -------------------------------- +// misc +// -------------------------------- + +float3 HSV(float h, float s, float v) { + h *= 6; + float c = s * v; + float x = c * (1 - abs(fmod(h, 2) - 1)); + float m = v - c; + c += m; + x += m; + + float3 colors[6] = { + float3(c, x, m), + float3(x, c, m), + float3(m, c, x), + float3(m, x, c), + float3(x, m, c), + float3(c, m, x)}; + + return colors[int(h)]; +} diff --git a/Assets/raymarched/lib/libgarbage_lighting.cginc.meta b/Assets/raymarched/lib/libgarbage_lighting.cginc.meta new file mode 100644 index 0000000..d82b508 --- /dev/null +++ b/Assets/raymarched/lib/libgarbage_lighting.cginc.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: ad884ac058b95a125963d424efaab135 +ShaderImporter: + externalObjects: {} + defaultTextures: [] + nonModifiableTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/raymarched/lib/libgarbage_operations.cginc b/Assets/raymarched/lib/libgarbage_operations.cginc index a39ec96..9ccf72e 100644 --- a/Assets/raymarched/lib/libgarbage_operations.cginc +++ b/Assets/raymarched/lib/libgarbage_operations.cginc @@ -139,26 +139,3 @@ inline float3 repXZ(float3 p, float x, float z) { o.z *= sign(p.z); return o; } - -// -------------------------------- -// misc -// -------------------------------- - -float3 HSV(float h, float s, float v) { - h *= 6; - float c = s * v; - float x = c * (1 - abs(fmod(h, 2) - 1)); - float m = v - c; - c += m; - x += m; - - float3 colors[6] = { - float3(c, x, m), - float3(x, c, m), - float3(m, c, x), - float3(m, x, c), - float3(x, m, c), - float3(c, m, x)}; - - return colors[int(h)]; -} From 86a3bad9925253686c98b9ec2c56b10d3b9fa76c Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 24 Jul 2023 18:15:12 +0200 Subject: [PATCH 5/7] libgarbage: change default to be only using one sdf function, with materials, since it seems to get optimised out anyway --- Assets/raymarched/Raymarching.unity | 8 ++-- Assets/raymarched/lib/libgarbage.cginc | 46 +++++++++++++------ .../raymarched/lib/libgarbage_example.shader | 45 +++++++++--------- 3 files changed, 59 insertions(+), 40 deletions(-) diff --git a/Assets/raymarched/Raymarching.unity b/Assets/raymarched/Raymarching.unity index d7510e8..35aef40 100644 --- a/Assets/raymarched/Raymarching.unity +++ b/Assets/raymarched/Raymarching.unity @@ -598,8 +598,8 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 683266143} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 0.26, z: -0.407} - m_LocalScale: {x: 10, y: 10, z: 10} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} m_RootOrder: 7 @@ -1678,7 +1678,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1485771011} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0.429, y: 0.221, z: -1.243} + m_LocalPosition: {x: 0.429, y: 0.221, z: -1.743} m_LocalScale: {x: 19.2, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} @@ -1761,7 +1761,7 @@ Transform: m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 1500710831} m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 1, z: -10} + m_LocalPosition: {x: 0, y: 0.009, z: -0.786} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index 169ca38..705a4f5 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -3,23 +3,39 @@ #include "UnityCG.cginc" /* -# defines with no defaults -USE_WORLD_SPACE -DISCARD_ON_MISS -USE_MATERIALS_IN_RAYMARCH // TODO: implement, this is easier to work with but more expensive -DISABLE_DEPTH +## 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 + START_RAYS_IN_BOX // TODO: implement START_RAYS_IN_SPHERE // TODO: implement */ -// scene sdf with only distance -#ifndef DISTANCE_FN -#define DISTANCE_FN default_distance_sdf -#endif // scene sdf that includes material data -#ifndef MATERIAL_FN -#define MATERIAL_FN default_material_sdf +#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 #endif // calculates a color from a Ray @@ -105,7 +121,7 @@ struct Ray { float DISTANCE_FN(float3 p); -SurfacePoint MATERIAL_FN(float3 p); +SurfacePoint SCENE_FN(float3 p); float3 LIGHT_FN(Ray ray); Ray cast_ray(float3 p, float3 d, float startDist = 0); @@ -219,7 +235,7 @@ Ray cast_ray(float3 start, float3 dir, float start_len) { ray.mat = mat(); } else { ray.normal = get_normal(ray.hit_pos, ray.dist); - ray.mat = MATERIAL_FN(ray.hit_pos).mat; + ray.mat = SCENE_FN(ray.hit_pos).mat; } return ray; } @@ -228,6 +244,10 @@ float default_distance_sdf(float3 p) { return sdSphere(p, 0.5); } +float material_fn_as_dist(float3 p) { + return SCENE_FN(p).dist; +} + SurfacePoint default_material_sdf(float3 p) { return mSphere(p, 0.5); } diff --git a/Assets/raymarched/lib/libgarbage_example.shader b/Assets/raymarched/lib/libgarbage_example.shader index a2bcde2..354fd4f 100644 --- a/Assets/raymarched/lib/libgarbage_example.shader +++ b/Assets/raymarched/lib/libgarbage_example.shader @@ -5,7 +5,7 @@ Shader "CrispyPin/LibGarbageExample" [Header(Raymarcher Properties)] _MaxSteps ("Max steps", Int) = 128 _MaxDist ("Max distance", Float) = 128 - _SurfDist ("Surface distance threshold", Range(0.00001, 0.05)) = 0.001 + _SurfDist ("Surface distance threshold", Range(0.0001, 0.05)) = 0.001 } SubShader @@ -21,38 +21,28 @@ Shader "CrispyPin/LibGarbageExample" #pragma fragment frag int _MaxSteps; - #define MAX_STEPS _MaxSteps float _MaxDist; - #define MAX_DIST _MaxDist float _SurfDist; + #define MAX_STEPS _MaxSteps + #define MAX_DIST _MaxDist #define SURF_DIST _SurfDist #define REFLECTIONS 3 - #define DISTANCE_FN main_dist - #define MATERIAL_FN main_mat + #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 USE_WORLD_SPACE #define SCENE_SCALE 0.05 - #include "libgarbage.cginc" - 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; - } - - SurfacePoint main_mat(float3 p) { + SurfacePoint main(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); @@ -71,10 +61,19 @@ Shader "CrispyPin/LibGarbageExample" return d; } + SurfacePoint separate_mat(float3 p) { + Material blue = mat(float3(0.05, 0.1, 0.2), 0); + SurfacePoint d = mSphere(p, 1, blue); + return d; + } + + float separate_dist(float3 p) { + return main(p).dist; + } + 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; From a1030ca528e96c6afd4c1d755c214023aa524a0e Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Tue, 25 Jul 2023 12:34:08 +0200 Subject: [PATCH 6/7] libgarbage: fix depth of sky, allow limiting depth to mesh, add step multiplier setting --- Assets/raymarched/lib/libgarbage.cginc | 28 ++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index 705a4f5..dff2c74 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -19,6 +19,9 @@ # don't write depth (will have the depth defined by the original mesh) DISABLE_DEPTH +# meant for frontface culled meshes, makes depth no further than the mesh edges + LIMIT_DEPTH_TO_MESH + START_RAYS_IN_BOX // TODO: implement START_RAYS_IN_SPHERE // TODO: implement */ @@ -43,6 +46,11 @@ START_RAYS_IN_SPHERE // TODO: implement #define LIGHT_FN default_lighting #endif +// set to less than one if there are artifacts in distorted sdfs +#ifndef STEP_MULTIPLIER +#define STEP_MULTIPLIER 1 +#endif + // max ray steps, from last bounce (reset on each reflection) #ifndef MAX_STEPS #define MAX_STEPS 128 @@ -156,16 +164,22 @@ FragOut frag (V2F i) { float prev_gloss = 0; float3 first_hit; - for (int ray_num = 0; ray_num < REFLECTIONS + 1; ray_num++) { + int ray_num = 0; + for (; ray_num < REFLECTIONS + 1;) { ray = cast_ray(ray_origin, ray_dir); if (ray_num == 0) { // before any bounces col = LIGHT_FN(ray); first_hit = ray.hit_pos; + if (ray.steps == 0) { + // discard; + // TODO: sky fn + } } else { float col_amount = color_used + ((1 - prev_gloss) * (1 - color_used)); col = lerp(LIGHT_FN(ray), col, col_amount); color_used = col_amount; } + ray_num++; if (ray.missed || ray.mat.gloss < 0.01) { break; } @@ -175,7 +189,7 @@ FragOut frag (V2F i) { } #ifdef DISCARD_ON_MISS - if (ray.missed && ray_num == 0) discard; + if (ray.missed && ray_num == 1) discard; #endif FragOut o; @@ -183,8 +197,14 @@ FragOut frag (V2F i) { #ifndef DISABLE_DEPTH float3 depth_point = first_hit * SCENE_SCALE; - if (ray.missed && ray_num == 0) + if (ray.missed && ray_num == 1) depth_point = i.hit_pos; + + #ifdef LIMIT_DEPTH_TO_MESH + if (length(ray.start - ray.hit_pos) > length(i.cam_pos - i.hit_pos)) + depth_point = i.hit_pos; + #endif + #ifdef USE_WORLD_SPACE float4 clip_pos = mul(UNITY_MATRIX_VP, float4(depth_point, 1)); #else @@ -225,7 +245,7 @@ Ray cast_ray(float3 start, float3 dir, float start_len) { break; } ray.min_dist = min(ray.min_dist, ray.dist); - ray_len += ray.dist; + ray_len += ray.dist * STEP_MULTIPLIER; if (ray_len > MAX_DIST) break; } From 299cf8ff32cfc255ed06873592f8247d45acb137 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Tue, 25 Jul 2023 12:35:36 +0200 Subject: [PATCH 7/7] libgarbage: support instancing --- Assets/raymarched/lib/libgarbage.cginc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Assets/raymarched/lib/libgarbage.cginc b/Assets/raymarched/lib/libgarbage.cginc index dff2c74..23b114e 100644 --- a/Assets/raymarched/lib/libgarbage.cginc +++ b/Assets/raymarched/lib/libgarbage.cginc @@ -82,12 +82,14 @@ START_RAYS_IN_SPHERE // TODO: implement struct AppData { float4 vertex : POSITION; + UNITY_VERTEX_INPUT_INSTANCE_ID }; struct V2F { float4 vertex : SV_POSITION; float3 cam_pos : TEXCOORD0; float3 hit_pos : TEXCOORD1; + UNITY_VERTEX_OUTPUT_STEREO }; struct FragOut { @@ -139,6 +141,9 @@ Ray cast_ray(float3 p, float3 d, float startDist = 0); V2F vert (AppData v) { V2F o; + UNITY_SETUP_INSTANCE_ID(v); + UNITY_INITIALIZE_OUTPUT(V2F, o); + UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); o.vertex = UnityObjectToClipPos(v.vertex); #ifdef USE_WORLD_SPACE o.cam_pos = _WorldSpaceCameraPos;