yum-slop/TaSTT

Free self-hosted STT for VRChat.

git clone https://git.yummers.dev/yum-slop/TaSTT

yumVarious shader cleanup323eeba

v0.19.2
10.7 KiB361 linesraw
1#ifndef __RAY_MARCH_INC__
2#define __RAY_MARCH_INC__
3
4#include "eyes_data.cginc"
5#include "iq_sdf.cginc"
6#include "math.cginc"
7#include "Motion.cginc"
8#include "pbr.cginc"
9#include "pema99.cginc"
10#include "poi.cginc"
11#include "stt_text.cginc"
12
13float _Emerge;
14float _Ellipsis;
15
16float4 _Text_Color;
17float _Text_Metallic;
18float _Text_Smoothness;
19float _Text_Emissive;
20
21float4 _BG_Color;
22float _BG_Metallic;
23float _BG_Smoothness;
24float _BG_Emissive;
25
26float4 _Frame_Color;
27float _Frame_Metallic;
28float _Frame_Smoothness;
29float _Frame_Emissive;
30
31#define MY_COORD_SCALE 100
32#define MY_COORD_SCALE_INV 1.0 / MY_COORD_SCALE
33#define OBJ_SPACE_TO_MINE \
34  float4x4( \
35      MY_COORD_SCALE, 0, 0, 0, \
36      0, MY_COORD_SCALE, 0, 0, \
37      0, 0, MY_COORD_SCALE, 0, \
38      0, 0, 0, MY_COORD_SCALE \
39      )
40#define WORLD_SPACE_TO_MINE \
41  mul(unity_WorldToObject, OBJ_SPACE_TO_MINE)
42#define MY_SPACE_TO_OBJ \
43  float4x4( \
44      MY_COORD_SCALE_INV, 0, 0, 0, \
45      0, MY_COORD_SCALE_INV, 0, 0, \
46      0, 0, MY_COORD_SCALE_INV, 0, \
47      0, 0, 0, MY_COORD_SCALE_INV \
48      )
49#define MY_SPACE_TO_WORLD \
50  mul(MY_SPACE_TO_OBJ, unity_ObjectToWorld)
51
52#define MINIMUM_HIT_DISTANCE .00002 * MY_COORD_SCALE
53#define MAXIMUM_TRACE_DISTANCE 2 * MY_COORD_SCALE
54
55// Allows us to divide [0,1] into `n_phases` equal-sized slices and remap `r`
56// onto the `nth_phase`.
57//
58// A few examples:
59//  get_phase_fraction(0.9, 0, 2) = 1.0
60//  get_phase_fraction(0.9, 1, 2) = 0.8
61//  get_phase_fraction(0.5, 0, 3) = 1.0
62//  get_phase_fraction(0.5, 1, 3) = 0.5
63//  get_phase_fraction(0.5, 2, 3) = 0.0
64//
65// So if `r` is past the slice we're looking at, it returns 1; if it's before
66// the slice we're looking at, it returns 0; if it's on the slice we're looking
67// at, it gets remapped onto [0,1].
68float get_phase_fraction(float r, float nth_phase, float n_phases) {
69  float stride = 1.0 / n_phases;
70
71  // Prevent boundary condition where saturated values get set to 0 by the
72  // glsl_mod below.
73  r = min(.9999 * (nth_phase + 1) * stride, r);
74
75  float r0 = clamp(r, nth_phase * stride, (nth_phase + 1) * stride);
76  r0 = glsl_mod(r0, stride);
77
78  return r0 / stride;
79}
80
81// d = base edge length
82// h = height
83// e = frame thickness
84// Pyramid base is on xy plane.
85float distance_from_rect_pyramid_frame(float3 p, float dx, float dy, float h, float e, float skew)
86{
87  float3 p0 = float3(dx/2, dy/2, 0);
88  float3 p1 = float3(dx/2, -dy/2, 0);
89  float3 p3 = float3(-dx/2, dy/2, 0);
90  float3 p4 = float3(skew, 0, h);
91
92  float3 pp = p;
93  // Symmetry
94  pp.x = abs(pp.x);
95  float d01 = distance_from_line_segment(pp, p0, p1, e);
96
97  pp = p;
98  pp.y = abs(pp.y);
99  float d03 = distance_from_line_segment(pp, p0, p3, e);
100  float d04 = distance_from_line_segment(pp, p0, p4, e);
101  float d14 = distance_from_line_segment(pp, p3, p4, e);
102
103  float dist = 1000;
104  dist = min(dist, d01);
105  dist = min(dist, d04);
106  dist = min(dist, d03);
107  dist = min(dist, d14);
108
109  return dist;
110}
111
112#define OBJ_ID_NONE 0
113#define OBJ_ID_FRAME 1
114#define OBJ_ID_BG 2
115
116float stt_map(float3 p, out int obj_id, out float2 text_uv)
117{
118  obj_id = OBJ_ID_NONE;
119
120  float p0r = get_phase_fraction(_Emerge, 0, 4);
121  float p1r = get_phase_fraction(_Emerge, 1, 4);
122  float p2r = get_phase_fraction(_Emerge, 2, 4);
123  float p3r = get_phase_fraction(_Emerge, 3, 4);
124
125  float dist = 1000 * 1000 * 1000;
126  float3 box_scale_g = float3(1, 1, .85) * MY_COORD_SCALE;
127  float3 box_center_g = float3(.020, 0, .0122) * MY_COORD_SCALE;
128  {
129    float3 pp = p;
130    pp -= box_center_g;
131
132    float box_thck = .0002 * MY_COORD_SCALE;
133
134    float3 box_sz = float3(6, .5, 3) * .003 * box_scale_g;
135
136    // Use this to make the box grow out of the bottom left corner instead of the
137    // middle.
138    float3 emerge_offset = 0;
139    emerge_offset.x = lerp(box_sz.x, box_thck, p1r);
140    emerge_offset.z = lerp(box_sz.z, box_thck, p2r);
141    pp += emerge_offset;
142
143    box_sz.y = lerp(box_thck, box_sz.y, p0r) * p0r;
144    box_sz.x = lerp(box_thck, box_sz.x, p1r) * p0r;
145    box_sz.z = lerp(box_thck, box_sz.z, p2r) * p0r;
146
147    float d = distance_from_box_frame(pp, box_sz, box_thck);
148
149    obj_id = lerp(obj_id, OBJ_ID_FRAME, d < dist);
150    dist = min(dist, d);
151  }
152  {
153    float3 pp = p;
154    pp -= box_center_g;
155    //pp -= float3(-0.00018, .0013, -.0002) * MY_COORD_SCALE;
156    pp -= float3(-0.00018, 0, -.0002) * MY_COORD_SCALE;
157
158    float3 box_scale = float3(10, 0.1, 4.9) * .00175 * box_scale_g;
159    float3 box_pad = float3(.001, 0, .001) * MY_COORD_SCALE;
160    box_scale -= box_pad;
161
162    // Use this to make the board grow out of the left edge instead of from the
163    // center.
164    float3 emerge_offset = 0;
165    emerge_offset.x = lerp(box_scale.x, 0, p3r);
166    pp += emerge_offset;
167
168    box_scale.x = lerp(0, box_scale.x, p3r);
169    box_scale.y *= ceil(p3r);
170    box_scale.z *= ceil(p3r);
171
172    // Only use calculation when in phase 3.
173    float d = lerp(1000, distance_from_box(pp, box_scale), ceil(p3r));
174
175    text_uv = (clamp(pp.xz, -1 * box_scale.xz, box_scale.xz) / box_scale.xz);
176    text_uv = (text_uv + 1) / 2;
177
178    bool in_mirror = !((unity_CameraProjection[2][0] == 0.0) * (unity_CameraProjection[2][1] == 0.0));
179    text_uv = lerp(text_uv, float2(1.0 - text_uv.x, text_uv.y), in_mirror);
180
181    obj_id = lerp(obj_id, OBJ_ID_BG, d < dist);
182    dist = min(dist, d);
183  }
184  {
185    float3 pp = p;
186
187    pp -= box_center_g - float3(6, 0, 3) * .003 * box_scale_g;
188
189    float scale = .0025 + .0002;
190    scale *= MY_COORD_SCALE;
191    pp.x -= scale/2;
192
193    float edgex = 1 * scale;
194    float edgey = 1 * scale;
195    float height = -1.3 * scale;
196    float r = .06 * scale;
197    float skew = -.75 * scale;
198
199    pp.z += lerp(0, .0008 * MY_COORD_SCALE, p0r) * p0r;
200    r = lerp(0, r, p0r) * p0r;
201    edgey = lerp(0, edgey, p0r) * p0r;
202
203    pp.x += lerp(edgex/2, 0, p1r);
204    edgex = lerp(0, edgex, p1r);
205
206    height = lerp(0, height, p2r);
207    skew = lerp(0, skew, p3r);
208
209    float d = distance_from_rect_pyramid_frame(pp, edgex, edgey, height, r, skew);
210    obj_id = lerp(obj_id, OBJ_ID_FRAME, d < dist);
211    dist = min(dist, d);
212  }
213  if (_Ellipsis > 0.1 && _Emerge > .99) {
214    float3 pp = p;
215
216    float3 xoff = float3(.003, 0, 0) * MY_COORD_SCALE;
217
218    float r_small = .0005 * MY_COORD_SCALE;
219    float r_big = .001 * MY_COORD_SCALE;
220    float r_phase = glsl_mod(_Time[1], 1.0);
221
222    float r0_p0r = get_phase_fraction(r_phase, 0, 8);
223    float r0_p2r = get_phase_fraction(r_phase, 3, 8);
224    float r1_p0r = get_phase_fraction(glsl_mod(r_phase + .25, 1.0), 0, 8);
225    float r1_p2r = get_phase_fraction(glsl_mod(r_phase + .25, 1.0), 3, 8);
226    float r2_p0r = get_phase_fraction(glsl_mod(r_phase + .50, 1.0), 0, 8);
227    float r2_p2r = get_phase_fraction(glsl_mod(r_phase + .50, 1.0), 3, 8);
228
229    float r0 = lerp(r_small, r_big, r0_p0r * (1 - r0_p2r));
230    float r1 = lerp(r_small, r_big, r1_p0r * (1 - r1_p2r));
231    float r2 = lerp(r_small, r_big, r2_p0r * (1 - r2_p2r));
232
233    pp -= box_center_g;
234
235    float d = distance_from_sphere(pp - xoff, 0, r0);
236    d = min(d, distance_from_sphere(pp, 0, r1));
237    d = min(d, distance_from_sphere(pp + xoff, 0, r2));
238
239    obj_id = lerp(obj_id, OBJ_ID_FRAME, d < dist);
240    dist = min(dist, d);
241  }
242
243  return dist;
244}
245
246// Calculate normals for ray-marched STT structure.
247float3 stt_calculate_normal(in float3 p)
248{
249    const float3 small_step = float3(0.0001, 0.0, 0.0);
250
251    // Calculate the 3D gradient. By definition, the gradient is orthogonal
252    // (normal) to the surface.
253    int obj_id;
254    float2 text_uv;
255    float gradient_x = stt_map(p + small_step.xyy, obj_id, text_uv) - stt_map(p - small_step.xyy, obj_id, text_uv);
256    float gradient_y = stt_map(p + small_step.yxy, obj_id, text_uv) - stt_map(p - small_step.yxy, obj_id, text_uv);
257    float gradient_z = stt_map(p + small_step.yyx, obj_id, text_uv) - stt_map(p - small_step.yyx, obj_id, text_uv);
258
259    float3 normal = float3(gradient_x, gradient_y, gradient_z);
260
261    return normalize(normal);
262}
263
264float get_letter_mask(float2 text_uv, bool mirror)
265{
266  float epsilon = .005;
267  if (text_uv.x > epsilon && text_uv.x < 1 - epsilon &&
268      text_uv.y > epsilon && text_uv.y < 1 - epsilon) {
269    text_uv.y = 1.0 - text_uv.y;
270    // Make backside render left-to-right.
271    text_uv.x = lerp(text_uv.x, 1.0 - text_uv.x, mirror);
272    text_uv = AddMarginToUV(1.0 - text_uv, .01);
273    return GetLetter(text_uv);
274  }
275  return 0;
276}
277
278float3 stt_march(float3 ro, float3 rd, out int obj_id, out float2 text_uv, int steps)
279{
280    float total_distance_traveled = 0.0;
281    float3 current_position = 0;
282    float distance_to_closest = 1;
283
284    #define STT_RAY_MARCH_STEPS 48
285    for (int i = 0; (i < steps) *
286        (distance_to_closest > MINIMUM_HIT_DISTANCE/4) *
287        (total_distance_traveled < MAXIMUM_TRACE_DISTANCE); i++)
288    {
289        current_position = ro + total_distance_traveled * rd;
290        distance_to_closest = stt_map(current_position, obj_id, text_uv);
291        total_distance_traveled += distance_to_closest;
292    }
293    obj_id = lerp(OBJ_ID_NONE, obj_id, distance_to_closest < MINIMUM_HIT_DISTANCE);
294
295    return current_position;
296}
297
298float4 stt_ray_march(float3 ro, float3 rd, inout v2f v2f_i, inout float depth)
299{
300
301    // TODO(yum) remove
302    float3 old_world_pos = v2f_i.worldPos;
303    depth = getWorldSpaceDepth(old_world_pos);
304
305    int obj_id;
306    float2 text_uv;
307    float3 current_position = stt_march(ro, rd, obj_id, text_uv, /*steps=*/48);
308
309    float3 normal = stt_calculate_normal(current_position);
310    v2f_i.normal = normalize(mul(MY_SPACE_TO_WORLD, normal));
311    v2f_i.worldPos = mul(MY_SPACE_TO_WORLD, float4(current_position, 1.0)).xyz;
312
313    float letter_mask = get_letter_mask(text_uv, ((normal.y + 1) / 2) > .01);
314
315    float4 text = light(v2f_i, _Text_Color, _Text_Metallic, _Text_Smoothness);
316    text += _Text_Color * _Text_Emissive;
317    text = clamp(text, 0, 1);
318
319    float4 bg = light(v2f_i, _BG_Color, _BG_Metallic, _BG_Smoothness);
320    bg += _BG_Color * _BG_Emissive;
321    bg = clamp(bg, 0, 1);
322
323    float4 frame = light(v2f_i, _Frame_Color, _Frame_Metallic, _Frame_Smoothness);
324    frame += _Frame_Color * _Frame_Emissive;
325    frame = clamp(frame, 0, 1);
326
327    frame += _Frame_Color * _Frame_Emissive;
328    frame = clamp(frame, 0, 1);
329
330    // TODO(yum) restore
331    //depth = getWorldSpaceDepth(v2f_i.worldPos);
332
333    [forcecase]
334    switch (obj_id) {
335      case OBJ_ID_NONE:
336        depth = -1000;
337        return 0;
338      case OBJ_ID_FRAME:
339        return frame;
340      case OBJ_ID_BG:
341        return lerp(bg, text, letter_mask);
342    }
343}
344
345float4 stt_ray_march(inout v2f v2f_i, inout float depth)
346{
347  float4 ray_march_color;
348  {
349    float3 camera_position = mul(WORLD_SPACE_TO_MINE, float4(_WorldSpaceCameraPos, 1.0)).xyz;
350    float3 ro = camera_position;
351    float3 rd = normalize(mul(WORLD_SPACE_TO_MINE, float4(v2f_i.worldPos, 1.0)).xyz - ro);
352    float3 old_normal = v2f_i.normal;
353    ray_march_color = stt_ray_march(ro, rd, v2f_i, depth);
354    //v2f_i.normal = old_normal;
355  }
356
357  return ray_march_color;
358}
359
360#endif  // __RAY_MARCH_INC__
361