TMP_SDF_SSD.cginc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. struct vertex_t
  2. {
  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. {
  11. float4 position : SV_POSITION;
  12. float4 faceColor : COLOR;
  13. float4 outlineColor : COLOR1;
  14. float2 texcoord0 : TEXCOORD0;
  15. float4 param : TEXCOORD1; // weight, scaleRatio
  16. float2 clipUV : TEXCOORD2;
  17. #if (UNDERLAY_ON || UNDERLAY_INNER)
  18. float4 texcoord2 : TEXCOORD3;
  19. float4 underlayColor : COLOR2;
  20. #endif
  21. };
  22. sampler2D _GUIClipTexture;
  23. uniform float4x4 unity_GUIClipTextureMatrix;
  24. float4 _MainTex_TexelSize;
  25. float4 SRGBToLinear(float4 rgba)
  26. {
  27. return float4(lerp(rgba.rgb / 12.92f, pow((rgba.rgb + 0.055f) / 1.055f, 2.4f), step(0.04045f, rgba.rgb)), rgba.a);
  28. }
  29. pixel_t VertShader(vertex_t input)
  30. {
  31. pixel_t output;
  32. float bold = step(input.texcoord1.y, 0);
  33. float4 vert = input.position;
  34. vert.x += _VertexOffsetX;
  35. vert.y += _VertexOffsetY;
  36. float4 vPosition = UnityObjectToClipPos(vert);
  37. float weight = lerp(_WeightNormal, _WeightBold, bold) / 4.0;
  38. weight = (weight + _FaceDilate) * _ScaleRatioA * 0.5;
  39. // Generate UV for the Clip Texture
  40. float3 eyePos = UnityObjectToViewPos(input.position);
  41. float2 clipUV = mul(unity_GUIClipTextureMatrix, float4(eyePos.xy, 0, 1.0));
  42. float4 color = input.color;
  43. #if (FORCE_LINEAR && !UNITY_COLORSPACE_GAMMA)
  44. color = SRGBToLinear(input.color);
  45. #endif
  46. float opacity = color.a;
  47. #if (UNDERLAY_ON | UNDERLAY_INNER)
  48. opacity = 1.0;
  49. #endif
  50. float4 faceColor = float4(color.rgb, opacity) * _FaceColor;
  51. faceColor.rgb *= faceColor.a;
  52. float4 outlineColor = _OutlineColor;
  53. outlineColor.a *= opacity;
  54. outlineColor.rgb *= outlineColor.a;
  55. output.position = vPosition;
  56. output.faceColor = faceColor;
  57. output.outlineColor = outlineColor;
  58. output.texcoord0 = float2(input.texcoord0.xy);
  59. output.param = float4(0.5 - weight, 1.3333 * _GradientScale * (_Sharpness + 1) / _MainTex_TexelSize.z , _OutlineWidth * _ScaleRatioA * 0.5, 0);
  60. output.clipUV = clipUV;
  61. #if (UNDERLAY_ON || UNDERLAY_INNER)
  62. float4 underlayColor = _UnderlayColor;
  63. underlayColor.rgb *= underlayColor.a;
  64. float x = -(_UnderlayOffsetX * _ScaleRatioC) * _GradientScale / _MainTex_TexelSize.z;
  65. float y = -(_UnderlayOffsetY * _ScaleRatioC) * _GradientScale / _MainTex_TexelSize.w;
  66. output.texcoord2 = float4(input.texcoord0 + float2(x, y), input.color.a, 0);
  67. output.underlayColor = underlayColor;
  68. #endif
  69. return output;
  70. }
  71. float4 PixShader(pixel_t input) : SV_Target
  72. {
  73. float d = tex2D(_MainTex, input.texcoord0.xy).a;
  74. float2 UV = input.texcoord0.xy;
  75. float scale = rsqrt(abs(ddx(UV.x) * ddy(UV.y) - ddy(UV.x) * ddx(UV.y))) * input.param.y;
  76. #if (UNDERLAY_ON | UNDERLAY_INNER)
  77. float layerScale = scale;
  78. layerScale /= 1 + ((_UnderlaySoftness * _ScaleRatioC) * layerScale);
  79. float layerBias = input.param.x * layerScale - .5 - ((_UnderlayDilate * _ScaleRatioC) * .5 * layerScale);
  80. #endif
  81. scale /= 1 + (_OutlineSoftness * _ScaleRatioA * scale);
  82. float4 faceColor = input.faceColor * saturate((d - input.param.x) * scale + 0.5);
  83. #ifdef OUTLINE_ON
  84. float4 outlineColor = lerp(input.faceColor, input.outlineColor, sqrt(min(1.0, input.param.z * scale * 2)));
  85. faceColor = lerp(outlineColor, input.faceColor, saturate((d - input.param.x - input.param.z) * scale + 0.5));
  86. faceColor *= saturate((d - input.param.x + input.param.z) * scale + 0.5);
  87. #endif
  88. #if UNDERLAY_ON
  89. d = tex2D(_MainTex, input.texcoord2.xy).a * layerScale;
  90. faceColor += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * saturate(d - layerBias) * (1 - faceColor.a);
  91. #endif
  92. #if UNDERLAY_INNER
  93. float bias = input.param.x * scale - 0.5;
  94. float sd = saturate(d * scale - bias - input.param.z);
  95. d = tex2D(_MainTex, input.texcoord2.xy).a * layerScale;
  96. faceColor += float4(_UnderlayColor.rgb * _UnderlayColor.a, _UnderlayColor.a) * (1 - saturate(d - layerBias)) * sd * (1 - faceColor.a);
  97. #endif
  98. #if (UNDERLAY_ON | UNDERLAY_INNER)
  99. faceColor *= input.texcoord2.z;
  100. #endif
  101. faceColor *= tex2D(_GUIClipTexture, input.clipUV).a;
  102. return faceColor;
  103. }