Refers to the effect of the rear screen, the current rendering the entire scene has been completed in FIG output to the screen, and then the screen image output operation performed.
In Unity, the general process is usually:
1. Create material shader for processing and temporary effect, shader script to pass parameters and variables to be controlled
2. Use OnRenderImage function grab the current screen rendering textures
OnRenderImage (Render source Texture, Texture Render destination) {}
The first parameter is the pre-processed texture, the second texture of the final display
3. OnRenderImage function call method Graphics.Blit texture crawling specific post-operation
Graphics.Blit(source, destination, material,-1);
material for the material to be treated, the default value of the parameter pass -1, showing all calls to sequentially pass the shader herein may be omitted
To do this, first of all can establish in advance a base class ScreenEffectBase, mainly used to check Shader and create a temporary material:
Script is as follows:
. 1 the using UnityEngine; 2 . 3 [ExecuteInEditMode] . 4 // rear screen processing effect is mainly for operating the camera, the camera needs to be bound . 5 [RequireComponent ( typeof (Camera))] . 6 public class ScreenEffectBase: MonoBehaviour . 7 { . 8 public Shader Shader; . 9 Private Material's Material's; 10 protected Material's Material's . 11 { 12 is GET 13 is { 14 Material's = CheckShaderAndCreatMat (Shader, Material's); 15 returnMaterial's; 16 } . 17 } 18 is . 19 // used to check and create temporary material 20 is Private Material's CheckShaderAndCreatMat (Shader Shader, Material's Material's) 21 is { 22 is Material's nullMat = null ; 23 is IF (! Shader = null ) 24 { 25 IF (Shader .isSupported) 26 is { 27 IF (Material's && material.shader == Shader)} { 28 the else 29 { 30 material = new Material(shader){ hideFlags = HideFlags.DontSave }; 31 } 32 return material; 33 } 34 } 35 return nullMat; 36 } 37 }
After treatment effect screen control script after create can inherit from the base class, for example, we create control scripts on basic color correction:
1 using UnityEngine; 2 3 public class ColorCorrectionCtrl : ScreenEffectBase 4 { 5 private const string _Brightness = "_Brightness"; 6 private const string _Saturation = "_Saturation"; 7 private const string _Contrast = "_Contrast"; 8 9 [Range(0, 3)] 10 public float brightness = 1.0f; 11 [Range(0, 3)] 12 public float saturation = 1.0f; 13 [Range(0, 3)] 14 public float contrast = 1.0f; 15 16 private void OnRenderImage(RenderTexture source, RenderTexture destination) 17 { 18 if (Material!=null) 19 { 20 Material.SetFloat(_Brightness, brightness); 21 Material.SetFloat(_Saturation, saturation); 22 Material.SetFloat(_Contrast, contrast); 23 24 Graphics.Blit(source, destination, Material); 25 } 26 else 27 Graphics.Blit(source, destination); 28 } 29 }
Wherein, brightness, saturation, contrast adjustment parameters are - brightness, saturation and contrast, _Brightness, _Saturation, corresponding to the shader after _Contrast properties requires a corresponding parameter definitions.
Here the use of building materials Material shader attribute assignment and call Graphics.Blit processed after screen effect.
Color correction shader embodied as follows:
1 Shader " MyUnlit / ColorCorrection " 2 { 3 the Properties 4 { 5 // parameter here is used mainly to show adjusted in the panel material, but because this is a temporary creation of the material parameters have been placed in a C # script adjustments , thus corresponding parameters can be omitted . 6 _MainTex ( " the Texture " , 2D) = " White " {} . 7 } . 8 SubShader . 9 { 10 Tags { " RenderType " = " of Opaque " } . 11 12 is Pass 13 is { 14 // call OnRenderImage transparent object may be rendered after order not to affect occurs before rendering transparent objects, it requires: rendering the double-sided opening depth test + + deep write off 15 ZTEST Always 16 Cull OFF . 17 ZWrite OFF 18 is . 19 CGPROGRAM 20 is #pragma Vertex Vert 21 is #pragma the fragment the frag 22 is #pragma multi_compile_fog 23 is 24 #include " UnityCG.cginc " 25 26 is struct AppData 27 { 28 float4 Vertex: the POSITION; 29 float2 uv : TEXCOORD0; 30 }; 31 32 struct v2f 33 { 34 float2 uv : TEXCOORD0; 35 UNITY_FOG_COORDS(1) 36 float4 vertex : SV_POSITION; 37 }; 38 39 sampler2D _MainTex; 40 half _Brightness; 41 half _Saturation; 42 half _Contrast; 43 44 v2f vert (appdata v) 45 { 46 O V2f; 47 o.vertex = UnityObjectToClipPos (v.vertex); 48 // because the material is temporarily created, there is no need for any transformation operations texture (material no operation panel), can be directly transmitted, _MainTex_ST also No definition 49 o.uv = v.uv; 50 UNITY_TRANSFER_FOG (O, o.vertex); 51 is return O; 52 is } 53 is 54 is fixed4 the frag (V2f I): SV_Target 55 { 56 is fixed4 COL = tex2D (_MainTex, I. UV); 57 is 58 // luminance calculation is directly added 59 fixed3 * Color = col.rgb_Brightness; 60 61 is // saturation, and gray is related to the image is calculated at the lowest gamma, subsequent to the original image interpolation operation 62 is fixed3 fixed3 Gray = ( 0.2125 , .7154 , 0.0721 ); 63 is // dot product obtained the minimum gradation value, constituting the lowest gray-scale image 64 Fixed minGray = DOT (gray, col.rgb); 65 fixed3 grayColor = fixed3 (minGray, minGray, minGray); 66 // gray image and the original image interpolation operation to give the final display image coefficients 67 Color = Lerp (grayColor, Color, _Saturation); 68 69 // similar contrast effects, to calculate the minimum contrast image, i.e. (0.5,0.5,0.5), followed by an interpolation operation 70 minContrast = fixed3 fixed3 ( 0.5 , 0.5 , 0.5 ); 71 is Color = Lerp (minContrast, Color, _Contrast); 72 73 is // get color image after all processing is complete, the alpha unchanged 74 fixed4 finColor = fixed4 (Color, col.a); 75 76 UNITY_APPLY_FOG (i.fogCoord, finColor); 77 return finColor; 78 } 79 ENDCG 80 } 81 } 82 // Close callback 83 fallback oFF 84 }
Results are as follows (casual tone do not care ~):