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: