summaryrefslogtreecommitdiffstats
path: root/ray_marching_maps.slang
blob: 1b8fe84de756bb1c5bc96c19e255e84108b92f0c (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
55
56
57
58
59
60
61
62
63
64
#ifndef __RAY_MARCHING_MAPS_INC
#define __RAY_MARCHING_MAPS_INC

#include "math.cginc"
#include "pema99.cginc"

// Macros for transforming normal and tangent using autodiff.
// r3r1 refers to "r3 to r1 transform", aka a mapping from a 3d real-valued
// space to a 1d space. This is intended for use with a ray marcher.
#define R3R1_DECLARE_BASIS_VECTORS(xyz) \
  DifferentialPair<float3> dp_x = diffPair(xyz, float3(1, 0, 0)); \
  DifferentialPair<float3> dp_y = diffPair(xyz, float3(0, 1, 0)); \
  DifferentialPair<float3> dp_z = diffPair(xyz, float3(0, 0, 1))

#define R3R1_AUTODIFF_BASIS_VECTORS(fun, ...) \
  DifferentialPair<float> dp_x_out = fwd_diff(fun)(dp_x, __VA_ARGS__); \
  DifferentialPair<float> dp_y_out = fwd_diff(fun)(dp_y, __VA_ARGS__); \
  DifferentialPair<float> dp_z_out = fwd_diff(fun)(dp_z, __VA_ARGS__)

#define R3R1_DEFORM_NORMAL_AND_TANGENT(normal, tangent) \
  { \
    float3 gradient = float3(dp_x_out.d, dp_y_out.d, dp_z_out.d); \
    normal = normalize(gradient); \
    float3 helper = abs(normal.z) < 0.999 ? float3(0, 0, 1) : float3(0, 1, 0); \
    tangent = normalize(cross(helper, normal)); \
  }

// Syntactic sugar - wraps the previous three macros.
#define R3R1_RAY_MARCH_NORMALS(xyz, normal, tangent, fun, ...)  \
  R3R1_DECLARE_BASIS_VECTORS(xyz);                              \
  R3R1_AUTODIFF_BASIS_VECTORS(fun, __VA_ARGS__);              \
  R3R1_DEFORM_NORMAL_AND_TANGENT(normal, tangent)

[Differentiable]
public float map_ball(float3 p, no_diff float r) {
  return length(p) - r;
}

public void map_ball_normal(float r, inout float3 xyz, inout float3 normal,
    inout float3 tangent) {
  R3R1_RAY_MARCH_NORMALS(xyz, normal, tangent, map_ball, r);
}

[Differentiable]
public float map_hexagon(float3 p, no_diff float2 h)
{
  float3 q = abs(p);

  const float3 k = float3(-0.8660254, 0.5, 0.57735);
  p = abs(p);
  p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy;
  float2 d = float2(
      length(p.xy - float2(clamp(p.x, -k.z*h.x, k.z*h.x), h.x))*sign(p.y - h.x),
      p.z-h.y );
  return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}

public void map_hexagon_normal(float2 h, inout float3 xyz, inout float3 normal,
    inout float3 tangent) {
  R3R1_RAY_MARCH_NORMALS(xyz, normal, tangent, map_hexagon, h);
}

#endif  // __RAY_MARCHING_MAPS_INC