yum/3ner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum/3ner

yumbugfix: c31 would break build when disabled89debfe

master
2.2 KiB75 linesraw
1#ifndef __CUSTOM31_INC
2#define __CUSTOM31_INC
3
4#include "globals.cginc"
5#include "interpolators.cginc"
6#include "math.cginc"
7#include "quilez.cginc"
8
9#if defined(_CUSTOM31_WORLD)
10float c31_map(float3 p, float3 p_quant) {
11  float2 hex_dim = float2(_Custom31_World_Hexagons_Tile_Radius, _Custom31_World_Hexagons_Tile_Thickness);
12
13  float3 axis_normal = normalize(hash33_fast(p_quant) * 2 - 1);
14  float theta = hash31_ff(p_quant) * TAU + _Time[1];
15  float4 quat = get_quaternion(axis_normal, theta);
16  p = rotate_vector(p, quat);
17
18  return distance_from_hex_prism(p, hex_dim);
19}
20
21float3 c31_normal(float3 p, float3 p_quant) {
22  const float epsilon = 1e-3;
23  const float3 xstep = float3(epsilon, 0, 0);
24  float d0 = c31_map(p, p_quant);
25  return normalize(float3(
26      c31_map(p + xstep.xyy, p_quant) - d0,
27      c31_map(p + xstep.yxy, p_quant) - d0,
28      c31_map(p + xstep.yyx, p_quant) - d0));
29}
30#endif  // _CUSTOM31_WORLD
31
32void apply_custom31_world(v2f i, inout Pbr pbr, inout float3 normal_tangent) {
33#if defined(_CUSTOM31_WORLD)
34  // We ray march in tangent (TBN) space.
35  float3 ro = float3(i.uv01.xy, 0);
36  float3 rd_world = normalize(i.worldPos - _WorldSpaceCameraPos);
37  float3 rd = normalize(mul(pbr.tbn, rd_world));
38
39#if defined(_CUSTOM31_WORLD_HEXAGONS)
40  // Apply hexagonal domain repetition.
41  float domain_rep_period = _Custom31_World_Hexagons_Grid_Scale;
42  // yx swizzle rotates hexagons 90 degrees
43  float3 h = cart_to_hex(ro.yx);
44  float3 h_rounded = round_hex(h / domain_rep_period) * domain_rep_period;
45  ro.xy = hex_to_cart(h - h_rounded).yx;
46  ro.z += _Custom31_World_Hexagons_Tile_Thickness;
47  ro.z += _Custom31_World_Ray_March_Min_Dist;
48#endif
49
50  float d_acc = 0;
51  float d;
52  [loop]
53    for (uint ii = 0; ii < _Custom31_World_Ray_March_Steps; ++ii) {
54      float3 p = ro + rd * d_acc;
55      d = c31_map(p, h_rounded);
56      d_acc += d;
57      if (d < _Custom31_World_Ray_March_Min_Dist) {
58        break;
59      }
60      if (d > _Custom31_World_Ray_March_Max_Dist) {
61        break;
62      }
63    }
64
65  if (d < _Custom31_World_Ray_March_Min_Dist) {
66    pbr.albedo.xyz = 1;
67
68    normal_tangent = c31_normal(ro + rd * d_acc, h_rounded);
69  } else {
70    pbr.albedo.xyz = 0;
71  }
72#endif
73}
74
75#endif  // __CUSTOM31_INC