Feed
The so-called post effects, which is post-processing, is the final stage in the rendered pipeline, and the object being processed is a picture generated by the scene. The common post-processing effect has hdr,motion blur and so on, through the screen space after processing, you can easily adjust the overall style of the game. Here are the scenarios to use.
Unity's Shaderlab built-in variables
For ease of programming, some variables are built into the Shaderlab and can be referenced directly in the shader.
The sine function of the _sintime-time is used here.
More built-in variables can be referenced here Shaderlab built-in values
Basic Steps
The main idea is first 1) to pass the camera's rendertexture to shder inside, shader in the GPU to calculate, and then 2) and then transfer the processed image back.
The first step needs to be handled in C # script, and the second step is to create the shader yourself.
Gray
Create a script
TestRenderImage.cs
Using unityengine;using System.Collections; [Executeineditmode]public class testrenderimage:monobehaviour{#region variablespublic Shader curshader;public float Grayscaleamout = 1.0f;private Material curmaterial; Material material{get{if (curmaterial = = null) {curmaterial = new Material (curshader); curmaterial.hideflags = Hideflags.hideanddontsave;} return curmaterial;}} #endregion//Use of this for Initializationvoid Start () {if (! systeminfo.supportsimageeffects) {enabled = false;} if (!curshader &&!curshader.issupported) {enabled = false;}} Update is called once per framevoid update () {grayscaleamout = Mathf.clamp (Grayscaleamout, 0.0f, 1.0f);} void Onrenderimage (rendertexture source, rendertexture target) {if (Curshader! = null) {material. SetFloat ("_luminosityamount", grayscaleamout); Graphics.blit (source, target, material);D ebug. Log ("Onrenderimage:" + grayscaleamout);} Else{graphics.blit (source, target);}} void Ondisable () {if (curmaterial) {destroyimmediate (curmaterial);}}}
Because you want to use shader, you create a texture dynamically in your program.
Onrenderimage is a unity built-in callback function that is called after all rendering is complete to render the image's post-processing effect.
In the code, the main change is the _luminosityamount variable in the shader.
The Graphics.blit function is used to copy the source texture to the destination render texture.
This is primarily used to achieve image effects.
Blit sets the dest to the active render texture, sets the source as the _maintex property on the material, and draws a full-screen quad.
Drag the script onto the Maincamera.
Next, create a shader that reads as follows
Shader "Custom/grayscale" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_luminosityamount ("grayscale Amount", Rang E (0.0, 1)) = 1.0}subshader {pass{cgprogram#pragma vertex vert_img#pragma fragment Frag#pragma fragmentoption arb_ Precision_hint_fastest#include "Unitycg.cginc" uniform sampler2d _maintex;fixed _luminosityamount;fixed _NumPixelH; Fixed _numpixelv;fixed4 frag (v2f_img i): Color{//get the colors from the rendertexture and the UV ' S//from the v2f_img str Uctfixed4 Rendertex = tex2d (_maintex, I.UV);//apply the luminosity values to our render texturefloat luminosity = 0.299 * RENDERTEX.R + 0.587 * rendertex.g + 0.114 * rendertex.b;fixed4 Finalcolor = lerp (Rendertex, luminosity, _luminosityamount) ; return finalcolor;} ENDCG}} FallBack "Diffuse"}
Drag to the previous script, adjust the parameters on the panel, you can get different effects of black and white degree.
Pixel painting
This effect can be converted to the pixel wind directly into the scene.
See Shader directly
Shader "Custom/pixelize" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_numpixel ("Pixel Count", Range (10, 200)) = 50}subshader {pass{cgprogram#pragma vertex vert_img#pragma fragment Frag#pragma fragmentoption Arb_precision_hint_ Fastest#include "Unitycg.cginc" uniform sampler2d _maintex;fixed _numpixel;fixed4 Frag (v2f_img i): Color{float stepSize = 1.0/_numpixel;float2 fragment = FLOAT2 (Stepsize * Floor (i.uv.x * _numpixel), stepsize * Floor (I.UV.Y * _numpixel)); fix Ed4 Finalcolor = tex2d (_maintex, fragment); return finalcolor;} ENDCG}} FallBack "Diffuse"}
Modify the _numpixel variable in C # to get a picture of different degrees of pixel style.
Rendering depth
The first thing to do is to pass the camera's depth map to the shader. In the shader, just declare
The sampler2d _cameradepthtexture variable allows you to reference the camera's depth map.
You also need to include the update function in the script
Camera.main.depthTextureMode = depthtexturemode.depth;
Finally, post the shade code
Shader "Custom/deptheffect" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_depthpower ("Depthpower", Range (1,5)) = 0 .2}subshader {pass{cgprogram#pragma vertex vert_img#pragma fragment Frag#pragma fragmentoption Arb_precision_hint_ Fastest#include "Unitycg.cginc" uniform sampler2d _maintex;fixed _depthpower;sampler2d _cameradepthtexture;fixed4 Frag (v2f_img i): color{float d = unity_sample_depth (tex2d (_cameradepthtexture, i.uv.xy));d = Pow (linear01depth (d), _ Depthpower); return D;} ENDCG}} FallBack "Diffuse"}
Run results
Incorporating post effects into game Play
The game often uses the effect of the old film, the idea is actually the overlay layer.
+ + + =
Shader code
Shader "Hidden/oldfilmeffectshader" {Properties {_maintex ("Base (RGB)", 2D) = "White" {}_vignettetex ("Vignette Texture" , 2D) = "White" {}_scratchestex ("Scartches Texture", 2D) = ' White ' {}_dusttex ("Dust Texture", 2D) = "White" {}_sepiacolor ( "Sepia color", color) = (1,1,1,1) _effectamount ("Old Film Effect Amount", Range (0,1)) = 1.0_vignetteamount ("Vignette Opac ity ", Range (0,1)) = 1.0_scratchesyspeed (" Scratches Y speed ", float) = 10.0_scratchesxspeed (" Scratches X speed ", float) = 10.0_dustxspeed ("Dust X speed", float) = 10.0_dustyspeed ("Dust Y speed", float) = 10.0_randomvalue ("Random Value", Flo at) = 1.0_contrast ("contrast", float) = 3.0_distortion ("distortion", float) = 0.2_cubicdistortion ("Cubic distortion", F loat) = 0.6_scale ("scale (Zoom)", Float) = 0.8}subshader {pass{cgprogram#pragma vertex vert_img#pragma fragment Frag#prag Ma fragmentoption arb_precision_hint_fastest#include "unitycg.cginc" Uniform sampler2d _maintex;uniform Sampler2D _ Vignettetex;uniform sampler2d _SCRatchestex;uniform sampler2d _dusttex;fixed4 _sepiacolor;fixed _vignetteamount;fixed _ScratchesYSpeed;fixed _ scratchesxspeed;fixed _dustxspeed;fixed _dustyspeed;fixed _effectamount;fixed _randomvalue;fixed _Contrast;float _ Distortion;float _cubicdistortion;float _scale;float2 barreldistortion (float2 coord) {//Inspired by Syntheyes Lens Distortion algorithm//See http://www.ssontech.com/content/lensalg.htmfloat2 h = coord.xy-float2 (0.5, 0.5); float r2 = h. X * h.x + h.y * h.y;float F = 1.0 + r2 * (_distortion + _cubicdistortion * sqrt (R2)); return f * _scale * H + 0.5;} Fixed4 Frag (v2f_img i): Color{//get the colors from the rendertexture and the UV ' S//from the v2f_img structhalf2 distorte DUV = Barreldistortion (i.uv);d Istorteduv = Half2 (i.uv.x, I.uv.y + (_randomvalue * _sintime.z * 0.005)); Fixed4 Rendertex = Tex2d (_maintex, I.UV);//get the pixels from the Vignette Texturefixed4 Vignettetex = tex2d (_vignettetex, I.UV);//process t He scratches UV and pixelshalf2 SCRATCHESUV = Half2 (i.uv.x+ (_randomvalue * _sintime.z * _scratchesxspeed), I.UV.Y + (_time.x * _scratchesyspeed)); Fixed4 Scratchestex = Tex2D (_SCRA Tchestex, SCRATCHESUV);//process the Dust UV and Pixelshalf2 dustuv = HALF2 (i.uv.x + (_randomvalue * (_SINTIME.Z * _DUSTXS peed)), I.uv.y + (_randomvalue * (_SINTIME.Z * _dustyspeed)); Fixed4 Dusttex = tex2d (_dusttex, DUSTUV);//Get the Luminosi Ty values from the render texture using the YIQ values.fixed lum = dot (fixed3 (0.299, 0.587, 0.114), RENDERTEX.RGB);//add The constant color to the Lum valuesfixed4 Finalcolor = lum + lerp (_sepiacolor, _sepiacolor + fixed4 (0.1f,0.1f,0.1f,1.0 f), _randomvalue); finalcolor = Pow (finalcolor, _contrast);//create A constant white color we can use to adjust opacity of Effectsfixed3 constantwhite = fixed3 (1,1,1);//composite together the different layers to create finsl screen Effectfinalco Lor = Lerp (Finalcolor, Finalcolor * Vignettetex, _vignetteamount); Finalcolor.rgb *= lerp (Scratchestex, ConstantWhite, (_ Randomvalue)); FINALCOLOR.RGB *= lerp (Dusttex.rgb, Constantwhite, (_randomvalue * _sintime.z)); Finalcolor = Lerp (Rendertex, FinalColor, _ Effectamount); return finalcolor;} ENDCG}} FallBack off}
The barreldistortion is used to produce a lens distortion. The reason for the formula:
If (U,V) is the coordinates of a feature in the undistorted perfect image plane, then (U ', V ') is the coordinates of the Feature on the distorted image plate, ie the scanned or captured image from the camera. The distortion occurs radially away from the image center with a correction for the image aspect ratio (Image_aspect = Phys ical image width/height), as follows:
r2 = image_aspect*image_aspect*u*u + V*VF = 1 + r2* (k + kcube*sqrt (R2)) u ' = f*uv ' = F*v
The constant k is the distortion coefficient this appears on the lens panel and through Sizzle. It is generally a small positive or negative number under 1%. The constant kcube is the cubic distortion value found on the image preprocessor ' s lens panel:it can be used to Undistort or redistort images, but it does not affect or get computed by the solver. When no cubic distortion are needed, neither is the square root, saving time.
The explanations in the other sections refer to the articles in the reference.
Reference
Unityshaders and Effects Cookbook
Unity Shaders gameplay and picture effects-create an old movie-style picture effect
Lens Distortion White paper-https://www.ssontech.com/content/lensalg.html
Post Effects in Unity3d