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.

115 lines
3.9 KiB

  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. //float opacity = v.color.a;
  23. data.param.x = (lerp(_WeightNormal, _WeightBold, bold) / 4.0 + _FaceDilate) * _ScaleRatioA * 0.5; //
  24. v.texcoord1.xy = UnpackUV(v.texcoord1.x);
  25. data.viewDirEnv = mul((float3x3)_EnvMatrix, WorldSpaceViewDir(v.vertex));
  26. }
  27. void PixShader(Input input, inout SurfaceOutput o)
  28. {
  29. #if USE_DERIVATIVE | BEVEL_ON
  30. float3 delta = float3(1.0 / _TextureWidth, 1.0 / _TextureHeight, 0.0);
  31. float4 smp4x = { tex2D(_MainTex, input.uv_MainTex - delta.xz).a,
  32. tex2D(_MainTex, input.uv_MainTex + delta.xz).a,
  33. tex2D(_MainTex, input.uv_MainTex - delta.zy).a,
  34. tex2D(_MainTex, input.uv_MainTex + delta.zy).a };
  35. #endif
  36. #if USE_DERIVATIVE
  37. // Screen space scaling reciprocal with anisotropic correction
  38. float2 edgeNormal = Normalize(float2(smp4x.x - smp4x.y, smp4x.z - smp4x.w));
  39. float2 res = float2(_TextureWidth * input.param.y, _TextureHeight);
  40. float2 tdx = ddx(input.uv_MainTex)*res;
  41. float2 tdy = ddy(input.uv_MainTex)*res;
  42. float lx = length(tdx);
  43. float ly = length(tdy);
  44. float s = sqrt(min(lx, ly) / max(lx, ly));
  45. s = lerp(1, s, abs(dot(normalize(tdx + tdy), edgeNormal)));
  46. float scale = rsqrt(abs(tdx.x * tdy.y - tdx.y * tdy.x)) * (_GradientScale * 2) * s;
  47. #else
  48. float scale = input.param.y;
  49. #endif
  50. // Signed distance
  51. float c = tex2D(_MainTex, input.uv_MainTex).a;
  52. float sd = (.5 - c - input.param.x) * scale + .5;
  53. float outline = _OutlineWidth*_ScaleRatioA * scale;
  54. float softness = _OutlineSoftness*_ScaleRatioA * scale;
  55. // Color & Alpha
  56. float4 faceColor = _FaceColor;
  57. float4 outlineColor = _OutlineColor;
  58. faceColor *= input.color;
  59. outlineColor.a *= input.color.a;
  60. faceColor *= tex2D(_FaceTex, float2(input.uv2_FaceTex.x + _FaceUVSpeedX * _Time.y, input.uv2_FaceTex.y + _FaceUVSpeedY * _Time.y));
  61. outlineColor *= tex2D(_OutlineTex, float2(input.uv2_OutlineTex.x + _OutlineUVSpeedX * _Time.y, input.uv2_OutlineTex.y + _OutlineUVSpeedY * _Time.y));
  62. faceColor = GetColor(sd, faceColor, outlineColor, outline, softness);
  63. faceColor.rgb /= max(faceColor.a, 0.0001);
  64. #if BEVEL_ON
  65. // Face Normal
  66. float3 n = GetSurfaceNormal(smp4x, input.param.x);
  67. // Bumpmap
  68. float3 bump = UnpackNormal(tex2D(_BumpMap, input.uv2_FaceTex.xy)).xyz;
  69. bump *= lerp(_BumpFace, _BumpOutline, saturate(sd + outline * 0.5));
  70. bump = lerp(float3(0, 0, 1), bump, faceColor.a);
  71. n = normalize(n - bump);
  72. // Cubemap reflection
  73. fixed4 reflcol = texCUBE(_Cube, reflect(input.viewDirEnv, mul((float3x3)unity_ObjectToWorld, n)));
  74. float3 emission = reflcol.rgb * lerp(_ReflectFaceColor.rgb, _ReflectOutlineColor.rgb, saturate(sd + outline * 0.5)) * faceColor.a;
  75. #else
  76. float3 n = float3(0, 0, -1);
  77. float3 emission = float3(0, 0, 0);
  78. #endif
  79. #if GLOW_ON
  80. float4 glowColor = GetGlowColor(sd, scale);
  81. glowColor.a *= input.color.a;
  82. emission += glowColor.rgb*glowColor.a;
  83. faceColor = BlendARGB(glowColor, faceColor);
  84. faceColor.rgb /= max(faceColor.a, 0.0001);
  85. #endif
  86. // Set Standard output structure
  87. o.Albedo = faceColor.rgb;
  88. o.Normal = -n;
  89. o.Emission = emission;
  90. o.Specular = lerp(_FaceShininess, _OutlineShininess, saturate(sd + outline * 0.5));
  91. o.Gloss = 1;
  92. o.Alpha = faceColor.a;
  93. }