Global Game Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
5.2 KiB

  1. struct vertex_t {
  2. UNITY_VERTEX_INPUT_INSTANCE_ID
  3. float4 position : POSITION;
  4. float3 normal : NORMAL;
  5. float4 color : COLOR;
  6. float2 texcoord0 : TEXCOORD0;
  7. float2 texcoord1 : TEXCOORD1;
  8. };
  9. struct pixel_t {
  10. UNITY_VERTEX_INPUT_INSTANCE_ID
  11. UNITY_VERTEX_OUTPUT_STEREO
  12. float4 position : SV_POSITION;
  13. float4 faceColor : COLOR;
  14. float4 outlineColor : COLOR1;
  15. float4 texcoord0 : TEXCOORD0;
  16. float4 param : TEXCOORD1; // weight, scaleRatio
  17. float2 mask : TEXCOORD2;
  18. #if (UNDERLAY_ON || UNDERLAY_INNER)
  19. float4 texcoord2 : TEXCOORD3;
  20. float4 underlayColor : COLOR2;
  21. #endif
  22. };
  23. float4 SRGBToLinear(float4 rgba) {
  24. return float4(lerp(rgba.rgb / 12.92f, pow((rgba.rgb + 0.055f) / 1.055f, 2.4f), step(0.04045f, rgba.rgb)), rgba.a);
  25. }
  26. pixel_t VertShader(vertex_t input)
  27. {
  28. pixel_t output;
  29. UNITY_INITIALIZE_OUTPUT(pixel_t, output);
  30. UNITY_SETUP_INSTANCE_ID(input);
  31. UNITY_TRANSFER_INSTANCE_ID(input, output);
  32. UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
  33. float bold = step(input.texcoord1.y, 0);
  34. float4 vert = input.position;
  35. vert.x += _VertexOffsetX;
  36. vert.y += _VertexOffsetY;
  37. float4 vPosition = UnityObjectToClipPos(vert);
  38. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  39. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  40. // Generate UV for the Masking Texture
  41. float4 clampedRect = clamp(_ClipRect, -2e10, 2e10);
  42. float2 maskUV = (vert.xy - clampedRect.xy) / (clampedRect.zw - clampedRect.xy);
  43. float4 color = input.color;
  44. #if (FORCE_LINEAR && !UNITY_COLORSPACE_GAMMA)
  45. color = SRGBToLinear(input.color);
  46. #endif
  47. float opacity = color.a;
  48. #if (UNDERLAY_ON | UNDERLAY_INNER)
  49. opacity = 1.0;
  50. #endif
  51. float4 faceColor = float4(color.rgb, opacity) * _FaceColor;
  52. faceColor.rgb *= faceColor.a;
  53. float4 outlineColor = _OutlineColor;
  54. outlineColor.a *= opacity;
  55. outlineColor.rgb *= outlineColor.a;
  56. output.position = vPosition;
  57. output.faceColor = faceColor;
  58. output.outlineColor = outlineColor;
  59. output.texcoord0 = float4(input.texcoord0.xy, maskUV.xy);
  60. output.param = float4(0.5 - weight, 1.3333 * _GradientScale * (_Sharpness + 1) / _TextureWidth, _OutlineWidth * _ScaleRatioA * 0.5, 0);
  61. float2 mask = float2(0, 0);
  62. #if UNITY_UI_CLIP_RECT
  63. mask = vert.xy * 2 - clampedRect.xy - clampedRect.zw;
  64. #endif
  65. output.mask = mask;
  66. #if (UNDERLAY_ON || UNDERLAY_INNER)
  67. float4 underlayColor = _UnderlayColor;
  68. underlayColor.rgb *= underlayColor.a;
  69. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _TextureWidth;
  70. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _TextureHeight;
  71. output.texcoord2 = float4(input.texcoord0 + float2(x, y), input.color.a, 0);
  72. output.underlayColor = underlayColor;
  73. #endif
  74. return output;
  75. }
  76. float4 PixShader(pixel_t input) : SV_Target
  77. {
  78. UNITY_SETUP_INSTANCE_ID(input);
  79. float d = tex2D(_MainTex, input.texcoord0.xy).a;
  80. float2 UV = input.texcoord0.xy;
  81. float scale = rsqrt(abs(ddx(UV.x) * ddy(UV.y) - ddy(UV.x) * ddx(UV.y))) * input.param.y;
  82. #if (UNDERLAY_ON | UNDERLAY_INNER)
  83. float layerScale = scale;
  84. layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  85. float layerBias = input.param.x * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
  86. #endif
  87. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  88. float4 faceColor = input.faceColor * saturate((d - input.param.x) * scale + 0.5);
  89. #ifdef OUTLINE_ON
  90. float4 outlineColor = lerp(input.faceColor, input.outlineColor, sqrt(min(1.0, input.param.z * scale * 2)));
  91. faceColor = lerp(outlineColor, input.faceColor, saturate((d - input.param.x - input.param.z) * scale + 0.5));
  92. faceColor *= saturate((d - input.param.x + input.param.z) * scale + 0.5);
  93. #endif
  94. #if UNDERLAY_ON
  95. d = tex2D(_MainTex, input.texcoord2.xy).a * layerScale;
  96. faceColor += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - layerBias) * (1 - faceColor.a);
  97. #endif
  98. #if UNDERLAY_INNER
  99. float bias = input.param.x * scale - 0.5;
  100. float sd = saturate(d * scale - bias - input.param.z);
  101. d = tex2D(_MainTex, input.texcoord2.xy).a * layerScale;
  102. faceColor += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - layerBias)) * sd * (1 - faceColor.a);
  103. #endif
  104. #ifdef MASKING
  105. float a = abs(_MaskInverse - tex2D(_MaskTex, input.texcoord0.zw).a);
  106. float t = a + (1 - _MaskWipeControl) * _MaskEdgeSoftness - _MaskWipeControl;
  107. a = saturate(t / _MaskEdgeSoftness);
  108. faceColor.rgb = lerp(_MaskEdgeColor.rgb * faceColor.a, faceColor.rgb, a);
  109. faceColor *= a;
  110. #endif
  111. // Alternative implementation to UnityGet2DClipping with support for softness
  112. #if UNITY_UI_CLIP_RECT
  113. float2 maskZW = 0.25 / (0.25 * half2(_MaskSoftnessX, _MaskSoftnessY) + (1 / scale));
  114. float2 m = saturate((_ClipRect.zw - _ClipRect.xy - abs(input.mask.xy)) * maskZW);
  115. faceColor *= m.x * m.y;
  116. #endif
  117. #if (UNDERLAY_ON | UNDERLAY_INNER)
  118. faceColor *= input.texcoord2.z;
  119. #endif
  120. #if UNITY_UI_ALPHACLIP
  121. clip(faceColor.a - 0.001);
  122. #endif
  123. return faceColor;
  124. }