UGUI—图片置灰

作者:追风剑情 发布于:2024-4-17 17:18 分类:Shader

  1. Shader "Custom/Luminance"
  2. {
  3. Properties
  4. {
  5. _MainTex ("Texture", 2D) = "white" {}
  6. //材质上加个复选框
  7. [Toggle(UNITY_UI_LUMINANCE)]
  8. _Luminance ("Luminance", Float) = 0
  9.  
  10. //支持 UGUI ScrollRect 遮罩
  11. _StencilComp ("Stencil Comparison", Float) = 8
  12. _Stencil ("Stencil ID", Float) = 0
  13. _StencilOp ("Stencil Operation", Float) = 0
  14. _StencilWriteMask ("Stencil Write Mask", Float) = 255
  15. _StencilReadMask ("Stencil Read Mask", Float) = 255
  16. _ColorMask ("Color Mask", Float) = 15
  17. //END
  18. }
  19. SubShader
  20. {
  21. Tags { "RenderType"="Transparent" "Queue" = "Transparent" "IgnoreProjector" = "True" "PreviewType" = "Plane" }
  22. LOD 100
  23.  
  24. //支持 UGUI ScrollRect 遮罩
  25. Stencil
  26. {
  27. Ref [_Stencil]
  28. Comp [_StencilComp]
  29. Pass [_StencilOp]
  30. ReadMask [_StencilReadMask]
  31. WriteMask [_StencilWriteMask]
  32. }
  33. ColorMask [_ColorMask]
  34. //END
  35.  
  36. //Alpha混合
  37. Blend SrcAlpha OneMinusSrcAlpha
  38.  
  39. Pass
  40. {
  41. CGPROGRAM
  42. #pragma target 3.0 //DX9 着色器模型 3.0
  43. #pragma vertex vert
  44. #pragma fragment frag
  45. //生成多版本shader
  46. #pragma multi_compile __ UNITY_UI_LUMINANCE
  47.  
  48. #include "UnityCG.cginc"
  49.  
  50. struct appdata
  51. {
  52. float4 vertex : POSITION;
  53. float2 uv : TEXCOORD0;
  54. };
  55.  
  56. struct v2f
  57. {
  58. float2 uv : TEXCOORD0;
  59. float4 vertex : SV_POSITION;
  60. };
  61.  
  62. sampler2D _MainTex;
  63. float4 _MainTex_ST;
  64. float _Luminance;
  65.  
  66. v2f vert (appdata v)
  67. {
  68. v2f o;
  69. o.vertex = UnityObjectToClipPos(v.vertex);
  70. o.uv = TRANSFORM_TEX(v.uv, _MainTex);
  71. return o;
  72. }
  73.  
  74. fixed4 frag (v2f i) : SV_Target
  75. {
  76. fixed4 col = tex2D(_MainTex, i.uv);
  77.  
  78. #ifdef UNITY_UI_LUMINANCE
  79. col.rgb = Luminance(col.rgb);
  80. #endif
  81.  
  82. return col;
  83. }
  84. ENDCG
  85. }
  86. }
  87. }

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号