summaryrefslogtreecommitdiffstats
path: root/custom31.cginc
blob: 6f29403693242cf3dea3cfcbf19bfa2e71fe9af9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef __CUSTOM31_INC
#define __CUSTOM31_INC

#include "globals.cginc"
#include "interpolators.cginc"

float map(float3 p) {
  return length(p) - 0.1;
}

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));

  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;
    }
    if (d > MAX_DIST) {
      break;
    }
  }

  if (d < MIN_DIST) {
    pbr.albedo.xyz = 1;
  } else {
    pbr.albedo.xyz = 0;
  }

  float3 p = ro + rd * d_acc;
  p = mul(float4(p, 1), tbn);
#endif
}

#endif  // __CUSTOM31_INC