yum-archive/Tooner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/Tooner
6eed98f
v0.0.0
1#ifndef LTCGI_FUNCTIONS_INCLUDED 2#define LTCGI_FUNCTIONS_INCLUDED 3 4/* 5 LTC HELPERS 6*/ 7 8float3 LTCGI_IntegrateEdge(float3 v1, float3 v2) 9{ 10 float x = dot(v1, v2); 11 float y = abs(x); 12 13 float a = 0.8543985 + (0.4965155 + 0.0145206*y)*y; 14 float b = 3.4175940 + (4.1616724 + y)*y; 15 float v = a / b; 16 float theta_sintheta = (x > 0.0) ? v : 0.5*rsqrt(max(1.0 - x*x, 1e-7)) - v; 17 18 return cross(v1, v2) * theta_sintheta; 19} 20 21void LTCGI_ClipQuadToHorizon(inout float3 L[5], out int n) 22{ 23 // detect clipping config 24 uint config = 0; 25 if (L[0].z > 0.0) config += 1; 26 if (L[1].z > 0.0) config += 2; 27 if (L[2].z > 0.0) config += 4; 28 if (L[3].z > 0.0) config += 8; 29 30 n = 0; 31 32 // This [forcecase] only works when the cases are ordered in a specific manner. 33 // It gives like 10%-20% performance boost, so *make sure to leave it on*! 34 // If it breaks however, see if [branch] fixes it, and if it does, start 35 // reordering cases at random until it works again. 36 // It seems the compiler somehow optimizes away anything but setting 'n' in 37 // some orderings, including the ascending and descending ones. 38 // I wish I was joking. 39 [forcecase] 40 switch (config) { 41 case 13: // V1 V3 V4 clip V2 <- tl;dr: this fecker has to be first or shader go boom 42 n = 5; 43 L[4] = L[3]; 44 L[3] = L[2]; 45 L[2] = -L[1].z * L[2] + L[2].z * L[1]; 46 L[1] = -L[1].z * L[0] + L[0].z * L[1]; 47 break; 48 case 15: // V1 V2 V3 V4 - most common 49 n = 4; 50 break; 51 case 9: // V1 V4 clip V2 V3 52 n = 4; 53 L[1] = -L[1].z * L[0] + L[0].z * L[1]; 54 L[2] = -L[2].z * L[3] + L[3].z * L[2]; 55 break; 56 case 0: // clip all 57 break; 58 case 1: // V1 clip V2 V3 V4 59 n = 3; 60 L[1] = -L[1].z * L[0] + L[0].z * L[1]; 61 L[2] = -L[3].z * L[0] + L[0].z * L[3]; 62 L[3] = L[0]; 63 break; 64 case 2: // V2 clip V1 V3 V4 65 n = 3; 66 L[0] = -L[0].z * L[1] + L[1].z * L[0]; 67 L[2] = -L[2].z * L[1] + L[1].z * L[2]; 68 L[3] = L[0]; 69 break; 70 case 3: // V1 V2 clip V3 V4 71 n = 4; 72 L[2] = -L[2].z * L[1] + L[1].z * L[2]; 73 L[3] = -L[3].z * L[0] + L[0].z * L[3]; 74 break; 75 case 4: // V3 clip V1 V2 V4 76 n = 3; 77 L[0] = -L[3].z * L[2] + L[2].z * L[3]; 78 L[1] = -L[1].z * L[2] + L[2].z * L[1]; 79 L[3] = L[0]; 80 break; 81 case 5: // V1 V3 clip V2 V4) impossible 82 break; 83 case 6: // V2 V3 clip V1 V4 84 n = 4; 85 L[0] = -L[0].z * L[1] + L[1].z * L[0]; 86 L[3] = -L[3].z * L[2] + L[2].z * L[3]; 87 break; 88 case 7: // V1 V2 V3 clip V4 89 n = 5; 90 L[4] = -L[3].z * L[0] + L[0].z * L[3]; 91 L[3] = -L[3].z * L[2] + L[2].z * L[3]; 92 break; 93 case 8: // V4 clip V1 V2 V3 94 n = 3; 95 L[0] = -L[0].z * L[3] + L[3].z * L[0]; 96 L[1] = -L[2].z * L[3] + L[3].z * L[2]; 97 L[2] = L[3]; 98 break; 99 case 10: // V2 V4 clip V1 V3) impossible 100 break; 101 case 11: // V1 V2 V4 clip V3 102 n = 5; 103 L[4] = L[3]; 104 L[3] = -L[2].z * L[3] + L[3].z * L[2]; 105 L[2] = -L[2].z * L[1] + L[1].z * L[2]; 106 break; 107 case 12: // V3 V4 clip V1 V2 108 n = 4; 109 L[1] = -L[1].z * L[2] + L[2].z * L[1]; 110 L[0] = -L[0].z * L[3] + L[3].z * L[0]; 111 break; 112 case 14: // V2 V3 V4 clip V1 113 n = 5; 114 L[4] = -L[0].z * L[3] + L[3].z * L[0]; 115 L[0] = -L[0].z * L[1] + L[1].z * L[0]; 116 break; 117 } 118 119 // inlining these branches *unconditionally* breaks the [forcecase] above 120 // ...yeah I know 121 if (n == 3) 122 L[3] = L[0]; 123 if (n == 4) 124 L[4] = L[0]; 125} 126 127/* 128 TEXTURE SAMPLING 129*/ 130 131float2 LTCGI_inset_uv(float2 uv) 132{ 133 return uv * 0.75 + float2(0.125, 0.125); 134} 135 136half3 premul_alpha(half4 i) 137{ 138 return i.rgb * i.a; 139} 140 141void LTCGI_sample(float2 uv, uint lod, uint idx, float blend, out float3 result) 142{ 143#ifdef LTCGI_FAST_SAMPLING 144 #ifndef SHADER_TARGET_SURFACE_ANALYSIS 145 blend *= 2.5f; 146 [branch] 147 if (idx == 0) 148 { 149 result = premul_alpha(_Udon_LTCGI_Texture_LOD0.SampleLevel(LTCGI_SAMPLER, uv, blend)); 150 } 151 else 152 { 153 result = UNITY_SAMPLE_TEX2DARRAY_SAMPLER_LOD( 154 _Udon_LTCGI_Texture_LOD0_arr, 155 LTCGI_SAMPLER_RAW, 156 float3(uv, idx - 1), 157 blend 158 ).rgb; 159 } 160 #else 161 result = 0; 162 #endif 163#else 164 result = 0; 165 #ifndef LTCGI_STATIC_TEXTURES 166 idx = 0; // optimize away the branches below 167 #endif 168 169 [branch] 170 if (lod == 0) 171 { 172 // if we're outside of the 0-1 UV space we must sample a prefiltered texture 173 [branch] 174 if(any(saturate(abs(uv - 0.5) - 0.5))) 175 { 176 lod = 1; 177 } 178 else 179 { 180 // LOD0 is the original texture itself, so not prefiltered, but we can 181 // approximate it a bit with trilinear lod 182 float lod = (1 - blend) * 1.5; 183 [branch] 184 if (idx == 0) 185 { 186 #ifndef SHADER_TARGET_SURFACE_ANALYSIS 187 result = premul_alpha(_Udon_LTCGI_Texture_LOD0.SampleLevel(LTCGI_SAMPLER, uv, lod)); 188 return; 189 #else 190 result = 0; 191 return; 192 #endif 193 } 194 else 195 { 196 result = premul_alpha(UNITY_SAMPLE_TEX2DARRAY_SAMPLER_LOD( 197 _Udon_LTCGI_Texture_LOD0_arr, 198 LTCGI_SAMPLER_RAW, 199 float3(uv, idx - 1), 200 lod 201 )); 202 return; 203 } 204 } 205 } 206 207 float2 ruv = LTCGI_inset_uv(uv); 208 209 [branch] 210 if (idx == 0) 211 { 212 #ifndef SHADER_TARGET_SURFACE_ANALYSIS 213 switch (lod) 214 { 215 case 1: 216 result = _Udon_LTCGI_Texture_LOD1.SampleLevel(LTCGI_SAMPLER, ruv, 0).rgb; 217 return; 218 case 2: 219 result = _Udon_LTCGI_Texture_LOD2.SampleLevel(LTCGI_SAMPLER, ruv, 0).rgb; 220 return; 221 default: 222 result = _Udon_LTCGI_Texture_LOD3.SampleLevel(LTCGI_SAMPLER, ruv, blend*0.72).rgb; 223 return; 224 } 225 #else 226 result = 0; 227 return; 228 #endif 229 } 230 else 231 { 232 [forcecase] 233 switch (lod) 234 { 235 case 1: 236 result = UNITY_SAMPLE_TEX2DARRAY_SAMPLER_LOD( 237 _Udon_LTCGI_Texture_LOD1_arr, 238 LTCGI_SAMPLER_RAW, 239 float3(ruv, idx - 1), 240 0 241 ).rgb; 242 return; 243 case 2: 244 result = UNITY_SAMPLE_TEX2DARRAY_SAMPLER_LOD( 245 _Udon_LTCGI_Texture_LOD2_arr, 246 LTCGI_SAMPLER_RAW, 247 float3(ruv, idx - 1), 248 0 249 ).rgb; 250 return; 251 default: 252 result = UNITY_SAMPLE_TEX2DARRAY_SAMPLER_LOD( 253 _Udon_LTCGI_Texture_LOD3_arr, 254 LTCGI_SAMPLER_RAW, 255 float3(ruv, idx - 1), 256 blend 257 ).rgb; 258 return; 259 } 260 } 261#endif 262} 263 264void LTCGI_trilinear(float2 uv, float d, uint idx, out float3 result) 265{ 266#ifdef LTCGI_FAST_SAMPLING 267 LTCGI_sample(uv, 0, idx, d, result); 268#else 269 uint low = (uint)d; 270 uint high = low + 1; 271 272 // DEBUG: colorize d/lod 273 //return float3(low == 0, low == 1, low == 2); 274 275 if (low >= 3) 276 { 277 LTCGI_sample(uv, 3, idx, d - 3, result); 278 } 279 else 280 { 281 float amount = saturate(high - d); 282 float3 low_sample; 283 LTCGI_sample(uv, low, idx, amount, low_sample); 284 float3 high_sample; 285 LTCGI_sample(uv, high, idx, 0, high_sample); 286 287 result = lerp(high_sample, low_sample, amount); 288 } 289#endif 290} 291 292/* 293 GENERIC HELPERS 294*/ 295 296// from: https://seblagarde.wordpress.com/2014/12/01/inverse-trigonometric-functions-gpu-optimization-for-amd-gcn-architecture/ 297// max absolute error 9.0x10^-3 298// Eberly's polynomial degree 1 - respect bounds 299// 4 VGPR, 12 FR (8 FR, 1 QR), 1 scalar 300// input [-1, 1] and output [0, PI] 301float LTCGI_acos_fast(float inX) 302{ 303 float x = abs(inX); 304 float res = -0.156583f * x + UNITY_HALF_PI; 305 res *= sqrt(1.0f - x); 306 return (inX >= 0) ? res : UNITY_PI - res; 307} 308 309bool LTCGI_tri_ray(float3 orig, float3 dir, float3 v0, float3 v1, float3 v2, out float2 bary) { 310 float3 v0v1 = v1 - v0; 311 float3 v0v2 = v2 - v0; 312 float3 pvec = cross(dir, v0v2); 313 float det = dot(v0v1, pvec); 314 float invDet = 1 / det; 315 316 float3 tvec = orig - v0; 317 bary.x = dot(tvec, pvec) * invDet; 318 319 float3 qvec = cross(tvec, v0v1); 320 bary.y = dot(dir, qvec) * invDet; 321 322 // return false when other triangle of quad should be sampled, 323 // i.e. we went over the diagonal line 324 return bary.x >= 0; 325} 326 327float2 LTCGI_rotateVector(float2 x, float angle) 328{ 329 float c = cos(angle); 330 float s = sin(angle); 331 return mul(float2x2(c,s,-s,c), x); 332} 333 334/*float LTCGI_remap(float3 from, float3 to, float2 targetFrom, float2 targetTo, float3 value) 335{ 336 float rel = (value - from) / (to - from); 337 return lerp(targetFrom, targetTo, rel); 338}*/ 339 340float2 LTCGI_calculateUV(uint i, ltcgi_flags flags, float3 L[5], bool isTri, float4 uvStart, float4 uvEnd, out float3 ray) 341{ 342 // calculate perpendicular vector to plane defined by area light 343 float3 E1 = L[1] - L[0]; 344 float3 E2 = L[3] - L[0]; 345 ray = cross(E1, E2); 346 347 // raycast it against the two triangles formed by the quad 348 float2 bary; 349 bool hit0 = LTCGI_tri_ray(0, ray, L[0], L[2], L[3], bary) || isTri; 350 if (!hit0) { 351 LTCGI_tri_ray(0, ray, L[0], L[1], L[2], bary); 352 } 353 354 float3 bary3 = float3(bary, 1 - bary.x - bary.y); 355 float2 uv; 356 if (hit0) 357 uv = uvEnd.zw * bary3.x + uvEnd.xy * bary3.y; 358 else 359 uv = uvStart.zw * bary3.x + uvEnd.zw * bary3.y; 360 return uv + uvStart.xy * bary3.z; 361} 362 363/* 364 EXPERIMENTAL: CYLINDER HELPER 365*/ 366 367void LTCGI_GetLw(uint i, ltcgi_flags flags, float3 worldPos, out float3 Lw[4], out float4 uvStart, out float4 uvEnd, out bool isTri) { 368 bool cylinder = false; 369 #ifdef LTCGI_CYLINDER 370 // statically optimize out branch below in case disabled 371 cylinder = flags.cylinder; 372 #endif 373 374 float4 v0 = _Udon_LTCGI_Vertices_0_get(i); 375 float4 v1 = _Udon_LTCGI_Vertices_1_get(i); 376 float4 v2 = _Udon_LTCGI_Vertices_2_get(i); 377 float4 v3 = _Udon_LTCGI_Vertices_3_get(i); 378 379 [branch] 380 if (cylinder) { 381 // construct data according to worldPos to create aligned 382 // rectangle tangent to the cylinder 383 384 float3 in_base = v0.xyz; 385 float in_height = v0.w; 386 float in_radius = v1.w; 387 float in_size = v2.w; 388 float in_angle = v3.w; 389 390 // get angle between 2D unit plane and vector pointing from cylinder to shade point 391 float2 towardsCylinder = LTCGI_rotateVector((in_base - worldPos).xz, -in_angle); 392 float angle = atan2(towardsCylinder.x, towardsCylinder.y); 393 // clamp angle to size parameter, i.e. "width" of lit surface area 394 float angleClamped = clamp(angle, -in_size, in_size) + in_angle; 395 // construct vector that *most* faces shade point 396 float2 facing = float2(sin(angleClamped), cos(angleClamped)); 397 // tangent of rectangular screen on cylinder surface used for calculating lighting for shade point 398 float2 tangent = float2(facing.y, -facing.x); 399 float2 onCylinderFacing = facing * in_radius; 400 401 // clip ends, approximately 402 float rclip = saturate(lerp(1, 0, (angleClamped - in_angle) - (in_size - UNITY_HALF_PI*0.5f))); 403 float lclip = saturate(lerp(1, 0, -(angleClamped - in_angle) - (in_size - UNITY_HALF_PI*0.5f))); 404 405 float2 p1 = in_base.xz - onCylinderFacing + tangent * in_radius * lclip; 406 float2 p2 = in_base.xz - onCylinderFacing - tangent * in_radius * rclip; 407 408 Lw[0] = float3(p1.x, in_base.y, p1.y) - worldPos; 409 Lw[1] = float3(p1.x, in_base.y + in_height, p1.y) - worldPos; 410 Lw[2] = float3(p2.x, in_base.y, p2.y) - worldPos; 411 Lw[3] = float3(p2.x, in_base.y + in_height, p2.y) - worldPos; 412 413 isTri = false; 414 415 // UV depends on "viewing" angle of the shade point towards the cylinder 416 float2 viewDir = normalize((in_base - worldPos).xz); 417 // forwardAngle == atan2(cos(in_angle), sin(in_angle)); but only negative 418 float forwardAngle = -in_angle + UNITY_HALF_PI; 419 // offset from center of screen forward to the side ends, positive goes left/ccw fpv top, 420 // sine to account for the fact we're rotating around a cylinder which has depth 421 float viewAngle = forwardAngle - atan2(viewDir.y, viewDir.x); 422 // prevent rollover, since we need to clamp we must stay withing [-pi, pi] 423 if (viewAngle < -UNITY_PI) 424 viewAngle += UNITY_TWO_PI; 425 if (viewAngle > UNITY_PI) 426 viewAngle -= UNITY_TWO_PI; 427 viewAngle = clamp(viewAngle * 0.5f, -in_size, in_size); 428 viewAngle = sin(viewAngle); 429 // full view UVs, but shifted left/right depending on view angle 430 float2 uvStart2 = float2(1 - saturate(viewAngle), 0); 431 float2 uvEnd2 = float2(1 - saturate(viewAngle + 1), 1); 432 uvStart = float4(uvStart2.x, uvStart2.y, uvStart2.x, uvEnd2.y); 433 uvEnd = float4(uvEnd2.x, uvStart2.y, uvEnd2.x, uvEnd2.y); 434 435 } else { 436 // use passed in data, offset around worldPos 437 Lw[0] = v0.xyz - worldPos; 438 Lw[1] = v1.xyz - worldPos; 439 Lw[2] = v2.xyz - worldPos; 440 Lw[3] = v3.xyz - worldPos; 441 #ifndef SHADER_TARGET_SURFACE_ANALYSIS 442 uvStart = _Udon_LTCGI_static_uniforms[uint2(4, i)]; 443 uvEnd = _Udon_LTCGI_static_uniforms[uint2(5, i)]; 444 #else 445 uvStart = float4(0, 0, 1, 0); 446 uvEnd = float4(1, 1, 0, 1); 447 #endif 448 449 // we only detect triangles for "blender" import configuration, as those are the only 450 // ones that can actually be triangles (I think?) 451 isTri = /*distance(Lw[2], Lw[3]) < 0.001 || */distance(Lw[1], Lw[3]) < 0.001; 452 } 453} 454 455#endif 456 457/* 458 459Parts of the code in this file are adapted from the example code found here: 460 461 https://github.com/selfshadow/ltc_code 462 463Modifications by _pi_ (@pimaker on GitHub), licensed under the terms of the 464MIT license as far as applicable. 465 466Original copyright notice: 467 468Copyright (c) 2017, Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt. 469All rights reserved. 470 471Redistribution and use in source and binary forms, with or without 472modification, are permitted provided that the following conditions are met: 473 474* If you use (or adapt) the source code in your own work, please include a 475 reference to the paper: 476 477 Real-Time Polygonal-Light Shading with Linearly Transformed Cosines. 478 Eric Heitz, Jonathan Dupuy, Stephen Hill and David Neubelt. 479 ACM Transactions on Graphics (Proceedings of ACM SIGGRAPH 2016) 35(4), 2016. 480 Project page: https://eheitzresearch.wordpress.com/415-2/ 481 482* Redistributions of source code must retain the above copyright notice, this 483 list of conditions and the following disclaimer. 484 485* Redistributions in binary form must reproduce the above copyright notice, 486 this list of conditions and the following disclaimer in the documentation 487 and/or other materials provided with the distribution. 488 489THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 490AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 491IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 492DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 493FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 494DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 495SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 496CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 497OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 498OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 499 500*/