Unity's screen post-processing effect, implemented using Monobehaviour.onrenderimage.
The code is as follows:
Posteffectrenderer:
1 usingUnityengine;2 3[Requirecomponent (typeof(Camera))]4 Public Abstract classPosteffectrenderer:gamebehaviour5 {6 protected Abstract stringShadername {Get; }7 8 Material M_mat;9 Ten voidStart () One { A if(!systeminfo.supportsimageeffects) - Notsupport (); - } the - protected Virtual voidonrenderimage (rendertexture src, rendertexture dest) - { - graphics.blit (SRC, dest, Mat); + } - + protected voidNotsupport () A { atEnabled =false; - } - - protectedMaterial Mat - { - Get in { - if(!M_mat) to { +Shader Shader =Shader.find (shadername); - if(Shader! =NULL&&shader.issupported) theM_mat =NewMaterial (shader); * Else $ Notsupport ();Panax Notoginseng } - returnM_mat; the } + } A}
Posteffectrenderer
Screencolorsadjuster
1 usingUnityengine;2 3 Public classScreencolorsadjuster:posteffectrenderer4 {5 [Serializefield]6 floatM_brightness =1;7 [Serializefield]8 floatM_saturation =0;9 [Serializefield]Ten floatM_contrast =0; One A protected Override voidonrenderimage (rendertexture src, rendertexture dest) - { - Base. Mat.setfloat ("_brightness", m_brightness); the Base. Mat.setfloat ("_saturation", m_saturation); - Base. Mat.setfloat ("_contrast", m_contrast); - Base. Onrenderimage (SRC, dest); - } + - protected Override stringShadername + { A Get{return "custom/screencolors Adjuster"; } at } -}
Screencolorsadjuster
Shader as follows:
1Shader"custom/screencolors Adjuster"2 {3 Properties4 {5_maintex ("Main Texture", 2D) =" White" {}6_brightness ("Brightness", Float) =17_saturation ("saturation", Float) =08_contrast ("contrast", Float) =09 }Ten One Subshader A { - Pass - { the ZTest always - Zwrite Off - Cull Off - + Cgprogram - #pragmaVertex vert + #pragmaFragment Frag A at sampler2d _maintex; - float_brightness; - float_saturation; - float_contrast; - - structAppData in { - float4 vertex:position; to float2 uv:texcoord0; + }; - the structv2f * { $ float4 pos:sv_position;Panax Notoginseng float2 uv:texcoord0; - }; the + v2f Vert (AppData v) A { the v2f o; +O.pos =Mul (UNITY_MATRIX_MVP, V.vertex); -O.UV =V.uv; $ $ returno; - } - the fixed4 Frag (v2f i): Sv_target - {WuyiFixed4 Tex =tex2d (_maintex, I.UV); the -Fixed3 Finalcol = Tex.rgb *_brightness; Wu - fixedLuminance = dot (fixed3 (0.2125,0.7154,0.0721), TEX.RGB); AboutFixed3 Luminancecol =fixed3 (luminance, luminance, luminance); $Finalcol =lerp (Finalcol, Luminancecol, _saturation); - -Fixed3 Avgcol = Fixed3 (0.5,0.5,0.5); -Finalcol =lerp (Finalcol, Avgcol, _contrast); A + returnfixed4 (Finalcol, tex.a); the } - $ ENDCG the } the } the the Fallback Off -}
View Code
The Screencolorsadjuster script hangs on the camera:
To adjust the parameters, the effect is as follows:
Unity shader the screen brightness, saturation, contrast of the later effect