Unity3d Tutorial Shader: The 25th lecture Blur

Source: Internet
Author: User

reprinted from the Wind Yu Chong Unity3d Tutorial CollegeBlur Fuzzy actually understand the later very simple. The core principle is
The 1-point color does not use the color of the point, but instead uses the mean of all points around that point
(1) Determine the pickup range, such as 3 pixels around or 10 pixels around
(2) Determine the weight of each point, which is also the origin of the Gaussian blur, the main color distribution is a normal distribution, that is, Gaussian distribution.


Example 1: The simplest blur
(1) New scene, plane a sticker on top
(2) plane on the shader as follows
Shader "Custom/objectblur" {Properties {_maintex ("Base (RGB)", 2D) = "white" {}} subshader { tags{"Queue" = "Transparent"} pass {Cgprogram #pragma vertex vert #pr            Agma fragment Frag #include "unitycg.cginc" sampler2d _maintex;            FLOAT4 _maintex_st;                       float Uvoffset;                struct V2F {float4 pos:sv_position;            FLOAT2 uv:texcoord0;            } ;                v2f Vert (Appdata_base v) {v2f o;                O.pos = Mul (Unity_matrix_mvp,v.vertex);                O.UV = Transform_tex (V.texcoord,_maintex);            return o; } float4 Frag (v2f i): COLOR {float4 S1 = tex2d (_maintex,i.uv + FLOAT2 (uvoffset,0.0                0));                FLOAT4 s2 = tex2d (_maintex,i.uv + float2 (-uvoffset,0.00)); FLOAT4 s3 = tex2d (_maintex,i.uv+ FLOAT2 (0.00,uvoffset));                               FLOAT4 S4 = tex2d (_maintex,i.uv + float2 (0.00,-uvoffset));                FLOAT4 Texcol = tex2d (_MAINTEX,I.UV);                               FLOAT4 OUTP;                float pct=0.2;                OUTP = texcol* (1-pct*4) + s1* pct + s2* pct+ s3* pct + s4* pct;            return OUTP; } ENDCG}}}

  


As well as the BlurManager.cs script, as follows
Using unityengine;using System.collections;public class Blurmanager:monobehaviour {    private float length =3f;    private float showTime = -100;    private float hidetime = -100;    void Update () {        if (showTime >0)        {            ShowTime-= time.deltatime;            Shader.setglobalfloat ("Uvoffset", (showtime/length) * 0.005f);        }               if (hidetime >0)        {            Hidetime-= time.deltatime;            Shader.setglobalfloat ("Uvoffset", (1-hidetime/length) * 0.005f);        }    }       void Ongui ()    {        if (GUI. button (new Rect (0,0,100,50), "Show"))        {            showTime = length;        }               if (GUI. button (new Rect (100,0,100,50), "Hide"))        {            hidetime = length;}}    }

  

After running, click the Show button, the graph will clear from the blur, click the Hide button will be blurred from the clear.
This is basically the simplest blur, take this point and its up and down around the 4 offset point. The weights of each point are 0.2. UV offset from 0 to 0.005
The effect is good.

Original


The effect after blurring



Reference articles

Unity3d Tutorial Shader: The 25th lecture Blur

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.