NewUnlitShader.shader 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. Shader "My Shader/AlphaShaderNormal"
  2. {
  3. Properties
  4. {
  5. _Color ("Color", Color) = (1,1,1,1)
  6. _MainTex ("Texture", 2D) = "white" {}
  7. _AlphaScale ("Alpha Scale", Range(0, 1)) = 1
  8. _LuminosityAmount("GrayScale Amount",Range(0.0,1)) = 1.0
  9. }
  10. SubShader
  11. {
  12. // 透明度混合队列为Transparent,所以Queue=Transparent
  13. // RenderType标签让Unity把这个Shader归入提前定义的组中,以指明该Shader是一个使用了透明度混合的Shader
  14. // IgonreProjector为True表明此Shader不受投影器(Projectors)影响
  15. Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
  16. Pass
  17. {
  18. // 开启深度写入
  19. ZWrite On
  20. // 设置颜色通道的写掩码,0为不写入任何颜色
  21. ColorMask 0
  22. }
  23. Pass
  24. {
  25. //Tags { "LightMode"="ForwardBase" }
  26. // 关闭深度写入
  27. ZWrite On
  28. // 开启混合模式,并设置混合因子为SrcAlpha和OneMinusSrcAlpha
  29. Blend SrcAlpha OneMinusSrcAlpha
  30. CGPROGRAM
  31. #pragma vertex vert
  32. #pragma fragment frag
  33. #include "UnityCG.cginc"
  34. #include "Lighting.cginc"
  35. struct a2v
  36. {
  37. float4 vertex : POSITION;
  38. float3 normal : NORMAL;
  39. float4 texcoord : TEXCOORD0;
  40. };
  41. struct v2f
  42. {
  43. float4 pos : SV_POSITION;
  44. float2 uv : TEXCOORD0;
  45. float3 worldNormal : TEXCOORD1;
  46. float3 worldPos : TEXCOORD2;
  47. };
  48. sampler2D _MainTex;
  49. float4 _MainTex_ST;
  50. fixed4 _Color;
  51. // 用于决定调用clip函数时进行的透明度测试使用的判断条件
  52. fixed _AlphaScale;
  53. fixed _LuminosityAmount;
  54. v2f vert (a2v v)
  55. {
  56. v2f o;
  57. o.pos = UnityObjectToClipPos(v.vertex);
  58. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  59. o.worldNormal = UnityObjectToWorldNormal(v.normal);
  60. o.worldPos = mul(unity_ObjectToWorld, v.vertex);
  61. return o;
  62. }
  63. fixed4 frag (v2f i) : SV_Target
  64. {
  65. fixed3 worldNormal = normalize(i.worldNormal);
  66. fixed3 worldPos = normalize(i.worldPos);
  67. fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(worldPos));
  68. // 纹素值
  69. fixed4 texColor = tex2D(_MainTex, i.uv);
  70. float luminosity = 0.299 * texColor.r + 0.587 * texColor.g + 0.114 * texColor.b;
  71. fixed4 finalColor = lerp(texColor,luminosity,_LuminosityAmount);
  72. // 反射率
  73. // 返回颜色,透明度部分乘以我们设定的值
  74. return fixed4(finalColor.rgb, texColor.a * _AlphaScale);
  75. }
  76. ENDCG
  77. }
  78. }
  79. }