yum-slop/TaSTT
Free self-hosted STT for VRChat.
git clone https://git.yummers.dev/yum-slop/TaSTT
c218e2a
v0.19.1
1#ifndef PBS_LIGHTING 2#define PBS_LIGHTING 3 4#include "AutoLight.cginc" 5#include "UnityPBSLighting.cginc" 6 7struct appdata 8{ 9 float4 position : POSITION; 10 float2 uv : TEXCOORD0; 11 float3 normal : NORMAL; 12}; 13 14struct v2f 15{ 16 float4 position : SV_POSITION; 17 float4 uv : TEXCOORD0; 18 float3 normal : TEXCOORD1; 19 float3 worldPos : TEXCOORD2; 20 21 #if defined(VERTEXLIGHT_ON) 22 float3 vertexLightColor : TEXCOORD3; 23 #endif 24}; 25 26float BG_Enable; 27sampler2D BG_BaseColor; 28sampler2D BG_NormalMap; 29sampler2D BG_Metallic; 30sampler2D BG_Smoothness; 31sampler2D BG_Emission_Mask; 32float BG_Smoothness_Invert; 33float BG_NormalStrength; 34float3 BG_Emission_Color; 35float4 BG_BaseColor_ST; 36float4 BG_NormalMap_ST; 37float4 BG_Metallic_ST; 38float4 BG_Smoothness_ST; 39float4 BG_Emission_Mask_ST; 40 41float Enable_Custom_Cubemap; 42UNITY_DECLARE_TEXCUBE(Custom_Cubemap); 43 44void getVertexLightColor(inout v2f i) 45{ 46 #if defined(VERTEXLIGHT_ON) 47 float3 light_pos = float3(unity_4LightPosX0.x, unity_4LightPosY0.x, 48 unity_4LightPosZ0.x); 49 float3 light_vec = light_pos - i.worldPos; 50 float3 light_dir = normalize(light_vec); 51 float ndotl = DotClamped(i.normal, light_dir); 52 // Light fills an expanding sphere with surface area 4 * pi * r^2. 53 // By conservation of energy, this means that at distance r, light intensity 54 // is proportional to 1/(r^2). 55 float attenuation = 1 / (1 + dot(light_vec, light_vec) * unity_4LightAtten0.x); 56 i.vertexLightColor = unity_LightColor[0].rgb * ndotl * attenuation; 57 58 i.vertexLightColor = Shade4PointLights( 59 unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, 60 unity_LightColor[0].rgb, 61 unity_LightColor[1].rgb, 62 unity_LightColor[2].rgb, 63 unity_LightColor[3].rgb, 64 unity_4LightAtten0, i.worldPos, i.normal 65 ); 66 #endif 67} 68 69v2f vert(appdata v) 70{ 71 v2f o; 72 o.position = mul(UNITY_MATRIX_MVP, v.position); 73 o.worldPos = mul(unity_ObjectToWorld, v.position); 74 o.normal = UnityObjectToWorldNormal(v.normal); 75 o.uv.xy = TRANSFORM_TEX(v.uv, BG_BaseColor); 76 o.uv.zw = 1.0 - v.uv; 77 getVertexLightColor(o); 78 return o; 79} 80 81fixed sq_dist(fixed2 p0, fixed2 p1) 82{ 83 fixed2 delta = p1 - p0; 84 return max(abs(delta.x), abs(delta.y)); 85} 86 87float3 HUEtoRGB(in float H) 88{ 89 float R = abs(H * 6 - 3) - 1; 90 float G = 2 - abs(H * 6 - 2); 91 float B = 2 - abs(H * 6 - 4); 92 return saturate(float3(R, G, B)); 93} 94 95float3 HSVtoRGB(in float3 HSV) 96{ 97 float3 RGB = HUEtoRGB(HSV.x); 98 return ((RGB - 1) * HSV.y + 1) * HSV.z; 99} 100 101fixed4 effect_squares (v2f i) 102{ 103 float2 uv = i.uv.zw; 104 uv.y *= 2; // Text box has 2:1 aspect ratio 105 const fixed time = _Time.y; 106 107 #define PI 3.1415926535 108 fixed theta = PI/4 + sin(time / 4) * 0.1; 109 fixed2x2 rot = 110 fixed2x2(cos(theta), -1 * sin(theta), 111 sin(theta), cos(theta)); 112 113 #define NSQ_X 9.0 114 #define NSQ_Y 5.0 115 116 // Map uv from [0, 1] to [-.5, .5]. 117 fixed2 p = uv - 0.5; 118 p *= fixed2(NSQ_X, NSQ_Y); 119 p = mul(rot, p); 120 p -= 0.5; 121 122 // See how far we are from the nearest grid point 123 fixed2 intra_pos = frac(p); 124 fixed2 intra_center = fixed2(0.5, 0.5); 125 fixed intra_dist = sq_dist(intra_pos, intra_center); 126 127 fixed st0 = (sin(time) + 1) / 2; 128 fixed st1 = (sin(time + PI/8) + 1) / 2; 129 fixed st2 = (sin(time + PI/2) + 1) / 2; 130 fixed st3 = (sin(time + PI/2 + PI/8) + 1) / 2; 131 132 fixed2 center = fixed2(0, 0); 133 center = mul(rot, center); 134 center -= 0.5; 135 fixed2 rot_lim = fixed2(NSQ_X, NSQ_Y); 136 rot_lim = mul(rot, rot_lim); 137 rot_lim -= 0.5; 138 139 float v = 0; 140 float x = 0; 141 142 if (intra_dist > 0.5 * (0.5 + sin(time * 1.5) * 0.1)) { 143 v = intra_dist; 144 } else { 145 v = 0; 146 } 147 148 fixed extra_dist = sq_dist(p, center); 149 fixed check = max(rot_lim.x, rot_lim.y) / 2; 150 if (extra_dist > check * st0) { 151 v = 1.0 - v; 152 } 153 if (extra_dist > check * st1) { 154 v = 1.0 - v; 155 } 156 if (extra_dist > check * st2) { 157 v = 1.0 - v; 158 } 159 if (extra_dist > check * st3) { 160 v = 1.0 - v; 161 } else { 162 x = 0.50; 163 } 164 165 fixed3 hsv; 166 hsv[0] = (v * 0.2 * (1 - x * .8) + 0.55) - x; 167 hsv[1] = 0.7; 168 hsv[2] = 0.8; 169 170 fixed3 col = HSVtoRGB(hsv); 171 172 return fixed4(col, 1.0); 173} 174 175fixed4 margin_effect(v2f i) 176{ 177 return effect_squares(i); 178} 179 180UnityLight GetLight(v2f i) 181{ 182 UNITY_LIGHT_ATTENUATION(attenuation, 0, i.worldPos); 183 float3 light_color = _LightColor0.rgb * attenuation; 184 185 UnityLight light; 186 light.color = light_color; 187 #if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT) 188 light.dir = normalize(_WorldSpaceLightPos0.xyz - i.worldPos); 189 #else 190 light.dir = _WorldSpaceLightPos0.xyz; 191 #endif 192 light.ndotl = DotClamped(i.normal, light.dir); 193 194 return light; 195} 196 197UnityIndirect GetIndirect(v2f i, float3 view_dir, float smoothness) { 198 UnityIndirect indirect; 199 indirect.diffuse = 0; 200 indirect.specular = 0; 201 202 #if defined(VERTEXLIGHT_ON) 203 indirect.diffuse = i.vertexLightColor; 204 #endif 205 206 #if defined(FORWARD_BASE_PASS) 207 indirect.diffuse += max(0, ShadeSH9(float4(i.normal, 1))); 208 float3 reflect_dir = reflect(-view_dir, i.normal); 209 // There's a nonlinear relationship between mipmap level and roughness. 210 float roughness = 1 - smoothness; 211 roughness *= 1.7 - .7 * roughness; 212 float3 env_sample; 213 if (Enable_Custom_Cubemap) { 214 env_sample = UNITY_SAMPLE_TEXCUBE_LOD( 215 Custom_Cubemap, 216 reflect_dir, 217 roughness * UNITY_SPECCUBE_LOD_STEPS); 218 } else { 219 env_sample = UNITY_SAMPLE_TEXCUBE_LOD( 220 unity_SpecCube0, 221 reflect_dir, 222 roughness * UNITY_SPECCUBE_LOD_STEPS); 223 } 224 indirect.specular = env_sample; 225 #endif 226 227 return indirect; 228} 229 230void initNormal(inout v2f i) 231{ 232 if (BG_Enable) { 233 i.normal = UnpackScaleNormal( 234 tex2Dgrad(BG_NormalMap, i.uv.xy, ddx(i.uv.x), ddy(i.uv.y)), 235 BG_NormalStrength); 236 // Swap Y and Z 237 i.normal = i.normal.xzy; 238 } 239 i.normal = normalize(i.normal); 240} 241 242fixed4 light(v2f i, 243 sampler2D albedo_map, 244 sampler2D normal_map, 245 float normal_str, 246 sampler2D metallic_map, 247 sampler2D smoothness_map, 248 float invert_smoothness, 249 sampler2D emission_mask, 250 float3 emission_color) 251{ 252 initNormal(i); 253 254 float2 iddx = ddx(i.uv.x); 255 float2 iddy = ddy(i.uv.y); 256 fixed4 albedo = tex2Dgrad(albedo_map, i.uv, iddx, iddy); 257 258 fixed3 normal = UnpackScaleNormal( 259 tex2Dgrad(normal_map, i.uv.xy, iddx, iddy), 260 normal_str); 261 // Swap Y and Z 262 normal = normal.xzy; 263 264 float3 view_dir = normalize(_WorldSpaceCameraPos - i.worldPos); 265 266 float metallic = tex2Dgrad(metallic_map, i.uv.xy, iddx, iddy); 267 268 float3 specular_tint; 269 float one_minus_reflectivity; 270 albedo.rgb = DiffuseAndSpecularFromMetallic( 271 albedo, metallic, specular_tint, one_minus_reflectivity); 272 273 UnityIndirect indirect_light; 274 indirect_light.diffuse = 0; 275 indirect_light.specular = 0; 276 277 float smoothness = tex2Dgrad(smoothness_map, i.uv.xy, iddx, iddy); 278 if (invert_smoothness) { 279 smoothness = 1 - smoothness; 280 } 281 282 fixed3 emission = tex2Dgrad(emission_mask, i.uv.xy, iddx, iddy) * emission_color; 283 284 fixed3 pbr = UNITY_BRDF_PBS(albedo, specular_tint, 285 one_minus_reflectivity, smoothness, 286 i.normal, view_dir, GetLight(i), GetIndirect(i, view_dir, smoothness)).rgb; 287 pbr.rgb += emission; 288 289 return fixed4(pbr, albedo.a); 290} 291 292fixed4 frag(v2f i) : SV_Target 293{ 294 float2 uv = i.uv.zw; 295 // Fix text orientation 296 uv.y = 0.5 - uv.y; 297 uv.x = 1.0 - uv.x; 298 uv.y *= 2; // Text box has 2:1 aspect ratio 299 300 // Derived from github.com/pema99/shader-knowledge (MIT license). 301 if (unity_CameraProjection[2][0] != 0.0 || 302 unity_CameraProjection[2][1] != 0.0) { 303 uv.x = 1.0 - uv.x; 304 } 305 306 if (BG_Enable) { 307 return light(i, 308 BG_BaseColor, 309 BG_NormalMap, 310 BG_NormalStrength, 311 BG_Metallic, 312 BG_Smoothness, 313 BG_Smoothness_Invert, 314 BG_Emission_Mask, 315 BG_Emission_Color); 316 } else { 317 return fixed4(1, 1, 1, 0); 318 } 319} 320 321#endif // PBS_LIGHTING 322