2
0

TMPro_Surface.cginc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. void VertShader(inout appdata_full v, out Input data)
  2. {
  3. v.vertex.x += _VertexOffsetX;
  4. v.vertex.y += _VertexOffsetY;
  5. UNITY_INITIALIZE_OUTPUT(Input, data);
  6. float bold = step(v.texcoord1.y, 0);
  7. // Generate normal for backface
  8. float3 view = ObjSpaceViewDir(v.vertex);
  9. v.normal *= sign(dot(v.normal, view));
  10. #if USE_DERIVATIVE
  11. data.param.y = 1;
  12. #else
  13. float4 vert = v.vertex;
  14. float4 vPosition = UnityObjectToClipPos(vert);
  15. float2 pixelSize = vPosition.w;
  16. pixelSize /= float2(_ScaleX, _ScaleY) * mul((float2x2)UNITY_MATRIX_P, _ScreenParams.xy);
  17. float scale = rsqrt(dot(pixelSize, pixelSize));
  18. scale *= abs(v.texcoord1.y) * _GradientScale * (_Sharpness + 1);
  19. scale = lerp(scale * (1 - _PerspectiveFilter), scale, abs(dot(UnityObjectToWorldNormal(v.normal.xyz), normalize(WorldSpaceViewDir(vert)))));
  20. data.param.y = scale;
  21. #endif
  22. data.param.x = (lerp(_WeightNormal, _WeightBold, bold) / 4.0 + _FaceDilate) * _ScaleRatioA * 0.5; //
  23. v.texcoord1.xy = UnpackUV(v.texcoord1.x);
  24. data.viewDirEnv = mul((float3x3)_EnvMatrix, WorldSpaceViewDir(v.vertex));
  25. }
  26. void PixShader(Input input, inout SurfaceOutput o)
  27. {
  28. #if USE_DERIVATIVE
  29. float2 pixelSize = float2(ddx(input.uv_MainTex.y), ddy(input.uv_MainTex.y));
  30. pixelSize *= _TextureWidth * .75;
  31. float scale = rsqrt(dot(pixelSize, pixelSize)) * _GradientScale * (_Sharpness + 1);
  32. #else
  33. float scale = input.param.y;
  34. #endif
  35. // Signed distance
  36. float c = tex2D(_MainTex, input.uv_MainTex).a;
  37. float sd = (.5 - c - input.param.x) * scale + .5;
  38. float outline = _OutlineWidth*_ScaleRatioA * scale;
  39. float softness = _OutlineSoftness*_ScaleRatioA * scale;
  40. // Color & Alpha
  41. float4 faceColor = _FaceColor;
  42. float4 outlineColor = _OutlineColor;
  43. faceColor *= input.color;
  44. outlineColor.a *= input.color.a;
  45. faceColor *= tex2D(_FaceTex, float2(input.uv2_FaceTex.x + _FaceUVSpeedX * _Time.y, input.uv2_FaceTex.y + _FaceUVSpeedY * _Time.y));
  46. outlineColor *= tex2D(_OutlineTex, float2(input.uv2_OutlineTex.x + _OutlineUVSpeedX * _Time.y, input.uv2_OutlineTex.y + _OutlineUVSpeedY * _Time.y));
  47. faceColor = GetColor(sd, faceColor, outlineColor, outline, softness);
  48. faceColor.rgb /= max(faceColor.a, 0.0001);
  49. #if BEVEL_ON
  50. float3 delta = float3(1.0 / _TextureWidth, 1.0 / _TextureHeight, 0.0);
  51. float4 smp4x = {tex2D(_MainTex, input.uv_MainTex - delta.xz).a,
  52. tex2D(_MainTex, input.uv_MainTex + delta.xz).a,
  53. tex2D(_MainTex, input.uv_MainTex - delta.zy).a,
  54. tex2D(_MainTex, input.uv_MainTex + delta.zy).a };
  55. // Face Normal
  56. float3 n = GetSurfaceNormal(smp4x, input.param.x);
  57. // Bumpmap
  58. float3 bump = UnpackNormal(tex2D(_BumpMap, input.uv2_FaceTex.xy)).xyz;
  59. bump *= lerp(_BumpFace, _BumpOutline, saturate(sd + outline * 0.5));
  60. bump = lerp(float3(0, 0, 1), bump, faceColor.a);
  61. n = normalize(n - bump);
  62. // Cubemap reflection
  63. fixed4 reflcol = texCUBE(_Cube, reflect(input.viewDirEnv, mul((float3x3)unity_ObjectToWorld, n)));
  64. float3 emission = reflcol.rgb * lerp(_ReflectFaceColor.rgb, _ReflectOutlineColor.rgb, saturate(sd + outline * 0.5)) * faceColor.a;
  65. #else
  66. float3 n = float3(0, 0, -1);
  67. float3 emission = float3(0, 0, 0);
  68. #endif
  69. #if GLOW_ON
  70. float4 glowColor = GetGlowColor(sd, scale);
  71. glowColor.a *= input.color.a;
  72. emission += glowColor.rgb*glowColor.a;
  73. faceColor = BlendARGB(glowColor, faceColor);
  74. faceColor.rgb /= max(faceColor.a, 0.0001);
  75. #endif
  76. // Set Standard output structure
  77. o.Albedo = faceColor.rgb;
  78. o.Normal = -n;
  79. o.Emission = emission;
  80. o.Specular = lerp(_FaceShininess, _OutlineShininess, saturate(sd + outline * 0.5));
  81. o.Gloss = 1;
  82. o.Alpha = faceColor.a;
  83. }