diff options
| author | yum <yum.food.vr@gmail.com> | 2026-04-06 00:31:41 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2026-04-06 00:31:41 -0700 |
| commit | f8b7967fce24ac1678985f8e49288a1a3600ccae (patch) | |
| tree | 88bef89ce735eabd50ed470f6d1587beba4968fc /custom31.cginc | |
| parent | 736de84c38db6c6f150daacc3c8d1a0f9c92ecbb (diff) | |
c31: continue work on hex ray marcher
Diffstat (limited to 'custom31.cginc')
| -rw-r--r-- | custom31.cginc | 74 |
1 files changed, 44 insertions, 30 deletions
diff --git a/custom31.cginc b/custom31.cginc index 6f29403..f44d185 100644 --- a/custom31.cginc +++ b/custom31.cginc @@ -3,51 +3,65 @@ #include "globals.cginc" #include "interpolators.cginc" +#include "math.cginc" +#include "quilez.cginc" -float map(float3 p) { - return length(p) - 0.1; +float c31_map(float3 p) { + float2 hex_dim = float2(_Custom31_World_Hexagons_Tile_Radius, _Custom31_World_Hexagons_Tile_Thickness); + + return distance_from_hex_prism(p, hex_dim); +} + +float3 c31_normal(float3 p) { + const float epsilon = 1e-3; + const float3 xstep = float3(epsilon, 0, 0); + float d0 = c31_map(p); + return normalize(float3( + c31_map(p + xstep.xyy) - d0, + c31_map(p + xstep.yxy) - d0, + c31_map(p + xstep.yyx) - d0)); } void apply_custom31_world(v2f i, inout Pbr pbr, inout float3 normal_tangent) { #if defined(_CUSTOM31_WORLD) - // We will raymarch in (tangent, binormal, normal) coordinates. - // Make an expanded homogeneous tbn matrix to support moving back and forth. - // This first incarnation moves from world to tangent. - float4x4 tbn = float4x4( - pbr.tbn[0], 0, - pbr.tbn[1], 0, - pbr.tbn[2], 0, - 0, 0, 0, 1); - tbn = mul(tbn, unity_WorldToObject); - - float3 ro = mul(tbn, float4(i.worldPos, 1)); - float3 rd = normalize(mul((float3x3) tbn, i.worldPos - _WorldSpaceCameraPos)); + // We ray march in tangent (TBN) space. + float3 ro = float3(i.uv01.xy, 0); + float3 rd_world = normalize(i.worldPos - _WorldSpaceCameraPos); + float3 rd = normalize(mul(pbr.tbn, rd_world)); + +#if defined(_CUSTOM31_WORLD_HEXAGONS) + // Apply hexagonal domain repetition. + float domain_rep_period = _Custom31_World_Hexagons_Grid_Scale; + // yx swizzle rotates hexagons 90 degrees + float3 h = cart_to_hex(ro.yx); + float3 h_rounded = round_hex(h / domain_rep_period) * domain_rep_period; + ro.xy = hex_to_cart(h - h_rounded).yx; + ro.z += _Custom31_World_Hexagons_Tile_Thickness; + ro.z += _Custom31_World_Ray_March_Min_Dist; +#endif float d_acc = 0; float d; - const float MIN_DIST = 1e-3; - const float MAX_DIST = 1; [loop] - for (uint ii = 0; ii < 10; ++ii) { - float3 p = ro + rd * d_acc; - d = map(p); - d_acc += d; - if (d < MIN_DIST) { - break; + for (uint ii = 0; ii < _Custom31_World_Ray_March_Steps; ++ii) { + float3 p = ro + rd * d_acc; + d = c31_map(p); + d_acc += d; + if (d < _Custom31_World_Ray_March_Min_Dist) { + break; + } + if (d > _Custom31_World_Ray_March_Max_Dist) { + break; + } } - if (d > MAX_DIST) { - break; - } - } - if (d < MIN_DIST) { + if (d < _Custom31_World_Ray_March_Min_Dist) { pbr.albedo.xyz = 1; + + normal_tangent = c31_normal(ro + rd * d_acc); } else { pbr.albedo.xyz = 0; } - - float3 p = ro + rd * d_acc; - p = mul(float4(p, 1), tbn); #endif } |
