yum-slop/TaSTT
Free self-hosted STT for VRChat.
git clone https://git.yummers.dev/yum-slop/TaSTT
f7e1cf9
v0.21.0
1#ifndef __STT_TEXT_INC__ 2#define __STT_TEXT_INC__ 3 4#include "stt_generated.cginc" 5 6float Enable_Dithering; 7 8SamplerState linear_clamp_sampler; 9 10Texture2D _Font_0x0000_0x1FFF; 11float4 _Font_0x0000_0x1FFF_TexelSize; 12Texture2D _Font_0x2000_0x3FFF; 13float4 _Font_0x2000_0x3FFF_TexelSize; 14Texture2D _Font_0x4000_0x5FFF; 15float4 _Font_0x4000_0x5FFF_TexelSize; 16Texture2D _Font_0x6000_0x7FFF; 17float4 _Font_0x6000_0x7FFF_TexelSize; 18Texture2D _Font_0x8000_0x9FFF; 19float4 _Font_0x8000_0x9FFF_TexelSize; 20Texture2D _Font_0xA000_0xBFFF; 21float4 _Font_0xA000_0xBFFF_TexelSize; 22Texture2D _Font_0xC000_0xDFFF; 23float4 _Font_0xC000_0xDFFF_TexelSize; 24Texture2D _Img_0xE000_0xE03F; 25float4 _Img_0xE000_0xE03F_TexelSize; 26 27float2 AddMarginToUV(float2 uv, float2 margin) 28{ 29 float2 lo = float2(-margin.x / 2, -margin.y / 2); 30 float2 hi = float2(1.0 + margin.x / 2, 1.0 + margin.y / 2); 31 32 return clamp(lerp(lo, hi, uv), 0.0, 1.0); 33} 34 35// Generate a random number on [0, 1]. 36float prng(float2 p) 37{ 38 return frac(sin(dot(p, float2(561.0, 885.0))) * 776.2) / 2.0; 39} 40 41bool f3ltf3(float3 a, float3 b) 42{ 43 return (a[0] < b[0]) * 44 (a[1] < b[1]) * 45 (a[2] < b[2]); 46} 47 48// Write the nth letter in the current cell and return the value of the 49// pixel. 50// `texture_rows` and `texture_cols` indicate how many rows and columns are 51// in the texture being sampled. 52float2 GetLetterUV(float2 uv, int nth_letter, 53 float texture_cols, float texture_rows, 54 float board_cols, float board_rows, 55 float margin) 56{ 57 // UV spans from [0,1] to [0,1]. 58 // 'U' is horizontal; cols. 59 // 'V' is vertical; rows. 60 // 61 // I want to divide the mesh into an m x n grid. 62 // I want to know what grid cell I'm in. This is simply u * m, v * n. 63 64 // OK, I know what cell I'm in. Now I need to know how far across it I 65 // am. Produce a float in the range [0, 1). 66 float CHAR_FRAC_COL = uv.x * board_cols - floor(uv.x * board_cols); 67 float CHAR_FRAC_ROW = uv.y * board_rows - floor(uv.y * board_rows); 68 69 // Avoid rendering pixels right on the edge of the slot. If we were to 70 // do this, then that value would get stretched due to clamping 71 // (AddMarginToUV), resulting in long lines on the edge of the display. 72 float lo = margin / 2; 73 float hi = 1.0 - margin / 2; 74 bool skip_result = (margin != 0) * 75 !(CHAR_FRAC_ROW > lo * 76 CHAR_FRAC_COL > lo * 77 CHAR_FRAC_ROW < hi * 78 CHAR_FRAC_COL < hi); 79 80 float LETTER_COL = fmod(nth_letter, floor(texture_cols)); 81 float LETTER_ROW = floor(texture_rows) - floor(nth_letter / floor(texture_cols)); 82 83 float LETTER_UV_ROW = (LETTER_ROW + CHAR_FRAC_ROW - 1.00) / texture_rows; 84 float LETTER_UV_COL = (LETTER_COL + CHAR_FRAC_COL) / texture_cols; 85 86 float2 result; 87 result.x = LETTER_UV_COL; 88 result.y = LETTER_UV_ROW; 89 90 return lerp(result, -1, skip_result);; 91} 92 93float4 GetLetter(float2 uv) { 94 uint letter = GetLetterParameter(uv); 95 96 float texture_cols; 97 float texture_rows; 98 float2 letter_uv; 99 bool is_emote = false; 100 101 if (letter < 0xE000) { 102 letter_uv = GetLetterUV(uv, letter % 0x2000, TEXTURE_NCOLS, TEXTURE_NROWS, BOARD_NCOLS, BOARD_NROWS, /*margin=*/0); 103 } else { 104 is_emote = true; 105 texture_cols = 16.0; 106 texture_rows = 8.0; 107 // This will need to be updated if we create multiple emote textures. 108 letter_uv = GetLetterUV(uv, letter % 0x2000, texture_cols, texture_rows, BOARD_NCOLS, BOARD_NROWS, /*margin=*/0); 109 } 110 111 bool discard_text = (letter_uv.x == -1) * (letter_uv.y == -1); 112 113 const float iddx = ddx(uv.x); 114 const float iddy = ddy(uv.y); 115 116 float4 text = float4(0, 0, 0, 0); 117 int which_texture = (int) floor(letter / (uint) (64 * 128)); 118 [forcecase] switch (which_texture) 119 { 120 case 0: 121 text = _Font_0x0000_0x1FFF.SampleGrad(linear_clamp_sampler, 122 letter_uv, iddx, iddy); 123 break; 124 case 1: 125 text = _Font_0x2000_0x3FFF.SampleGrad(linear_clamp_sampler, 126 letter_uv, iddx, iddy); 127 break; 128 case 2: 129 text = _Font_0x4000_0x5FFF.SampleGrad(linear_clamp_sampler, 130 letter_uv, iddx, iddy); 131 break; 132 case 3: 133 text = _Font_0x6000_0x7FFF.SampleGrad(linear_clamp_sampler, 134 letter_uv, iddx, iddy); 135 break; 136 case 4: 137 text = _Font_0x8000_0x9FFF.SampleGrad(linear_clamp_sampler, 138 letter_uv, iddx, iddy); 139 break; 140 case 5: 141 text = _Font_0xA000_0xBFFF.SampleGrad(linear_clamp_sampler, 142 letter_uv, iddx, iddy); 143 break; 144 case 6: 145 text = _Font_0xC000_0xDFFF.SampleGrad(linear_clamp_sampler, 146 letter_uv, iddx, iddy); 147 break; 148 case 7: 149 text = _Img_0xE000_0xE03F.SampleGrad(linear_clamp_sampler, 150 letter_uv, iddx, iddy); 151 break; 152 default: 153 // Return some distinctive pattern that will look like a bug. 154 return float4(1, 0, _SinTime[0], 1); 155 } 156 157 // The edges of each letter cell can be slightly grey due to mip maps. 158 // Detect this and shade it as the background. 159 float3 grey = 0.7; 160 bool disc = !(!f3ltf3(text.rgb, grey) * !discard_text * !is_emote); 161 return lerp(float4(text.rgb, 1), 0, disc); 162} 163 164#endif // __STT_TEXT_INC__ 165