BrightnessSaturationContrastShader.shader 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. Shader "MyShader/BrightnessSaturationContrast" { // 调整亮度、饱和度、对比度
  2. Properties{
  3. _MainTex("Base (RGB)", 2D) = "white" {} // 主纹理
  4. _Brightness("Brightness", Float) = 1 // 亮度
  5. _Saturation("Saturation", Float) = 1 // 饱和度
  6. _Contrast("Contrast", Float) = 1 // 对比度
  7. }
  8. SubShader{
  9. Pass {
  10. // 深度测试始终通过, 关闭深度写入
  11. ZTest Always ZWrite Off
  12. CGPROGRAM
  13. #pragma vertex vert_img // 使用内置的vert_img顶点着色器
  14. #pragma fragment frag
  15. #include "UnityCG.cginc"
  16. sampler2D _MainTex; // 主纹理
  17. half _Brightness; // 亮度
  18. half _Saturation; // 饱和度
  19. half _Contrast; // 对比度
  20. fixed4 frag(v2f_img i) : SV_Target { // v2f_img为内置结构体, 里面只包含pos和uv
  21. fixed4 tex = tex2D(_MainTex, i.uv); // 纹理采样
  22. fixed3 finalColor = tex.rgb * _Brightness; // 应用亮度_Brightness
  23. fixed luminance = 0.2125 * tex.r + 0.7154 * tex.g + 0.0721 * tex.b; // 计算亮度
  24. fixed3 luminanceColor = fixed3(luminance, luminance, luminance); // 饱和度为0、亮度为luminance的颜色
  25. finalColor = lerp(luminanceColor, finalColor, _Saturation); // 应用饱和度_Saturation
  26. fixed3 avgColor = fixed3(0.5, 0.5, 0.5); // 饱和度为0、亮度为0.5的颜色
  27. finalColor = lerp(avgColor, finalColor, _Contrast); // 应用对比度_Contrast
  28. return fixed4(finalColor, tex.a);
  29. }
  30. ENDCG
  31. }
  32. }
  33. Fallback Off
  34. }