summaryrefslogtreecommitdiffstats
path: root/eyes.cginc
blob: c5fbc1bbf835256aeb97565d94b5ac2d2ee54a6e (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef __EYES_INC
#define __EYES_INC

#if defined(_GIMMICK_EYES_00)

float eyes00_distance_from_sphere(float3 p, float3 c, float r)
{
    return length(p - c) - r;
}

float eyes00_map(float3 p)
{
    float t = _Time.y;
    float theta = sin(_Time[0]) / 2;
    float2x2 rot = float2x2(
      cos(theta), -sin(theta),
      sin(theta), cos(theta));

    float dist = 1000 * 1000 * 1000;
    #define Y_STEPS 5
    for (int y = 0; y < Y_STEPS; y++)
    {
      const int yy = y - Y_STEPS/2;
      #define X_STEPS 5
      for (int x = 0; x < X_STEPS; x++)
      {
        const int xx = x - X_STEPS/2;
        float2 pp = float2(xx * 2, yy * 2);
        pp = mul(rot, pp);
        float radius = cos((x + y + _Time[0]) * 3.14159) * 0.5 + 1;
        float sphere = eyes00_distance_from_sphere(p, float3(pp.x, pp.y, 0.0), radius);
        dist = min(dist, sphere);
        dist += sin(5.0 * pp.x) * sin(5.0 * pp.y) * 0.5;
      }
    }

    return dist;
}

float3 eyes00_calc_normal(in float3 p)
{
    const float3 small_step = float3(0.0001, 0.0, 0.0);

    float gradient_x = eyes00_map(p + small_step.xyy) - eyes00_map(p - small_step.xyy);
    float gradient_y = eyes00_map(p + small_step.yxy) - eyes00_map(p - small_step.yxy);
    float gradient_z = eyes00_map(p + small_step.yyx) - eyes00_map(p - small_step.yyx);

    float3 normal = float3(gradient_x, gradient_y, gradient_z);

    return normalize(normal);
}

float3 __eyes00_march(float3 ro, float3 rd, inout float3 normal)
{
    float total_distance_traveled = 0.0;
    const float MINIMUM_HIT_DISTANCE = 0.001;
    const float MAXIMUM_TRACE_DISTANCE = 1000.0;

    #define EYES00_MARCH_STEPS 10
    float distance_to_closest;
    float3 current_position;
    for (int i = 0; i < EYES00_MARCH_STEPS; i++)
    {
        current_position = ro + total_distance_traveled * rd;

        distance_to_closest = eyes00_map(current_position);

        if (distance_to_closest < MINIMUM_HIT_DISTANCE) 
        {
          break;
        }

        if (total_distance_traveled > MAXIMUM_TRACE_DISTANCE)
        {
            break;
        }
        total_distance_traveled += distance_to_closest;
    }

    if (distance_to_closest < MINIMUM_HIT_DISTANCE) {
      normal = eyes00_calc_normal(current_position);
      return float3(1.0, 1.0, 1.0);
    }

    return float3(0, 0, 0);
}

float4 eyes00_march(float2 uv, inout float3 normal)
{
    uv = uv * 2.0 - 1.0;

    float3 camera_position = float3(0.0, 0.0, -5.0);
    float3 ro = camera_position;
    float3 rd = float3(uv.x, uv.y, 1.0);

    float3 shaded_color = __eyes00_march(ro, rd, normal);

    return float4(shaded_color, 1.0);
}

#endif  // _EYES

#endif  // __EYES_INC