AlphaBlendZWrite.shader 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. Shader "Shaders Study/Alpha Blend ZWrite"
  2. {
  3. Properties
  4. {
  5. _Color ("Main Tint", Color) = (1, 1, 1, 1)
  6. _MainTex ("Main Tex", 2D) = "white" {}
  7. _AlphaScale ("Alpha Scale", Range(0, 1)) = 1
  8. _FresnelScale ("Fresnel Scale", Range(0, 5)) = 0.5
  9. _FresnelPow ("Fresnel Pow", Range(0, 10)) = 5
  10. _GradationThreshold ("Gradation Threshold", Range(0, 1)) = 0.5
  11. _GradationSmooth ("Gradation Smooth", Range(0, 0.5)) = 0.5
  12. }
  13. Subshader
  14. {
  15. Tags
  16. {
  17. "Queue" = "Geometry-1" "IgnoreProjector" = "True" "RenderType" = "Transparent" "LightMode" = "ForwardBase"
  18. }
  19. ZWrite On
  20. Blend SrcAlpha OneMinusSrcAlpha
  21. Pass
  22. {
  23. Cull Back
  24. Blend Zero One
  25. }
  26. pass
  27. {
  28. Tags { "LightMode" = "ForwardBase"}
  29. CGPROGRAM
  30. #pragma vertex vert
  31. #pragma fragment frag
  32. #include "Lighting.cginc"
  33. fixed4 _Color;
  34. sampler2D _MainTex;
  35. float4 _MainTex_ST;
  36. fixed _AlphaScale;
  37. fixed _GradationThreshold;
  38. fixed _GradationSmooth;
  39. fixed _FresnelScale;
  40. fixed _FresnelPow;
  41. struct a2v
  42. {
  43. float4 vertex : POSITION;
  44. float3 normal : NORMAL;
  45. float4 texcoord : TEXCOORD0;
  46. };
  47. struct v2f
  48. {
  49. float4 pos : SV_POSITION;
  50. float3 worldNormal : TEXCOORD0;
  51. float3 worldPos : TEXCOORD1;
  52. float2 uv : TEXCOORD2;
  53. fixed3 worldViewDir : TEXCOORD3;
  54. };
  55. v2f vert(a2v v)
  56. {
  57. v2f o;
  58. o.pos = UnityObjectToClipPos(v.vertex);
  59. o.worldNormal = UnityObjectToWorldNormal(v.normal);
  60. o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
  61. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  62. o.worldViewDir = UnityWorldSpaceViewDir(o.worldPos);
  63. return o;
  64. }
  65. half LinearStep(half minValue, half maxValue, half In)
  66. {
  67. return saturate((In-minValue) / (maxValue - minValue));
  68. }
  69. fixed4 frag (v2f i) : SV_Target
  70. {
  71. fixed3 worldNormal = normalize(i.worldNormal);
  72. fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));
  73. fixed4 texColor = tex2D(_MainTex, i.uv);
  74. fixed3 albedo = texColor.rgb * _Color.rgb;
  75. fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;
  76. fixed3 worldViewDir = normalize(i.worldViewDir);
  77. fixed fresnel = _FresnelScale * saturate(pow(1 - dot(worldViewDir, worldNormal), _FresnelPow));
  78. fixed3 diffuse = _LightColor0.rgb * albedo * (max(0, dot(worldNormal, worldLightDir))*0.5+0.5);
  79. fixed jianb=LinearStep(_GradationThreshold-_GradationSmooth,_GradationThreshold+_GradationSmooth,i.uv.y);
  80. fixed a=texColor.a * _AlphaScale*jianb+fresnel*jianb;
  81. fixed3 col=saturate((ambient + diffuse+fresnel*_Color));
  82. return fixed4(col, a);
  83. }
  84. ENDCG
  85. }
  86. }
  87. }