标签:code play abs bst ras alt set protect 对比
Unity的屏幕后期处理效果,使用MonoBehaviour.OnRenderImage来实现。
如代码如下:
PostEffectRenderer:
1 using UnityEngine; 2 3 [RequireComponent(typeof(Camera))] 4 public abstract class PostEffectRenderer : GameBehaviour 5 { 6 protected abstract string ShaderName { get; } 7 8 Material m_mat; 9 10 void Start() 11 { 12 if (!SystemInfo.supportsImageEffects) 13 NotSupport(); 14 } 15 16 protected virtual void OnRenderImage(RenderTexture src, RenderTexture dest) 17 { 18 Graphics.Blit(src, dest, Mat); 19 } 20 21 protected void NotSupport() 22 { 23 enabled = false; 24 } 25 26 protected Material Mat 27 { 28 get 29 { 30 if (!m_mat) 31 { 32 Shader shader = Shader.Find(ShaderName); 33 if (shader != null && shader.isSupported) 34 m_mat = new Material(shader); 35 else 36 NotSupport(); 37 } 38 return m_mat; 39 } 40 } 41 }
ScreencolorsAdjuster
1 using UnityEngine; 2 3 public class ScreencolorsAdjuster : PostEffectRenderer 4 { 5 [SerializeField] 6 float m_brightness = 1; 7 [SerializeField] 8 float m_saturation = 0; 9 [SerializeField] 10 float m_contrast = 0; 11 12 protected override void OnRenderImage(RenderTexture src, RenderTexture dest) 13 { 14 base.Mat.SetFloat("_Brightness", m_brightness); 15 base.Mat.SetFloat("_Saturation", m_saturation); 16 base.Mat.SetFloat("_Contrast", m_contrast); 17 base.OnRenderImage(src, dest); 18 } 19 20 protected override string ShaderName 21 { 22 get { return "Custom/Screencolors Adjuster"; } 23 } 24 }
shader如下:
1 Shader "Custom/Screencolors Adjuster" 2 { 3 Properties 4 { 5 _MainTex("Main Texture", 2D) = "white" {} 6 _Brightness("Brightness", Float) = 1 7 _Saturation("Saturation", Float) = 0 8 _Contrast("Contrast", Float) = 0 9 } 10 11 SubShader 12 { 13 Pass 14 { 15 ZTest Always 16 ZWrite Off 17 Cull Off 18 19 CGPROGRAM 20 #pragma vertex vert 21 #pragma fragment frag 22 23 sampler2D _MainTex; 24 float _Brightness; 25 float _Saturation; 26 float _Contrast; 27 28 struct appdata 29 { 30 float4 vertex : POSITION; 31 float2 uv : TEXCOORD0; 32 }; 33 34 struct v2f 35 { 36 float4 pos : SV_POSITION; 37 float2 uv : TEXCOORD0; 38 }; 39 40 v2f vert(appdata v) 41 { 42 v2f o; 43 o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 44 o.uv = v.uv; 45 46 return o; 47 } 48 49 fixed4 frag(v2f i) : SV_TARGET 50 { 51 fixed4 tex = tex2D(_MainTex, i.uv); 52 53 fixed3 finalCol = tex.rgb * _Brightness; 54 55 fixed luminance = dot(fixed3(0.2125, 0.7154, 0.0721), tex.rgb); 56 fixed3 luminanceCol = fixed3(luminance, luminance, luminance); 57 finalCol = lerp(finalCol, luminanceCol, _Saturation); 58 59 fixed3 avgCol = fixed3(0.5, 0.5, 0.5); 60 finalCol = lerp(finalCol, avgCol, _Contrast); 61 62 return fixed4(finalCol, tex.a); 63 } 64 65 ENDCG 66 } 67 } 68 69 Fallback Off 70 }
ScreencolorsAdjuster脚本挂在摄像机上:
调整参数,效果如下:
Unity shader学习之屏幕后期效果之调整屏幕亮度,饱和度,对比度
标签:code play abs bst ras alt set protect 对比
原文地址:http://www.cnblogs.com/jietian331/p/7228063.html