Unity3d Shader Gaussian Blur

Source: Internet
Author: User
Tags mul


Shader

Shader"Hidden/gaussianblur"{Properties {_maintex ("Texture", 2D) =" White"{} _blursize ("Blur Size", Float) =0.1} cginclude #include"Unitycg.cginc"sampler2d _maintex;      Uniform Half4 _maintex_texelsize; Uniformfloat_blursize; Static ConstHalf weight[4] = {0.0205,0.0855,0.232,0.324}; Static ConstHalf4 Coordoffset = Half4 (1.0h,1.0h,-1.0h,-1. 0h); structV2F_BLURSGX {float4 pos:sv_position;        Half2 uv:texcoord0; Half4 uvoff[3]:texcoord1;    };        V2F_BLURSGX Vert_blurhorizontal (appdata_img v) {V2F_BLURSGX o; O.pos=Mul (Unity_matrix_mvp,v.vertex); O.uv=V.texcoord.xy; HALF2 Offs= _maintex_texelsize.xy*half2 (1,0)*_blursize; o.uvoff[0] = v.texcoord.xyxy+offs.xyxy*coordoffset*3; o.uvoff[1] = v.texcoord.xyxy+offs.xyxy*coordoffset*2; o.uvoff[2] = v.texcoord.xyxy+offs.xyxy*Coordoffset; returno;        } V2F_BLURSGX vert_blurvertical (Appdata_img v) {V2F_BLURSGX o; O.pos=Mul (Unity_matrix_mvp,v.vertex); O.uv=V.texcoord.xy; HALF2 Offs= _maintex_texelsize.xy*half2 (0,1)*_blursize; o.uvoff[0] = v.texcoord.xyxy+offs.xyxy*coordoffset*3; o.uvoff[1] = v.texcoord.xyxy+offs.xyxy*coordoffset*2; o.uvoff[2] = v.texcoord.xyxy+offs.xyxy*Coordoffset; returno; } fixed4 Frag_blur (V2F_BLURSGX i): sv_target {fixed4 C= tex2d (_MAINTEX,I.UV) *weight[3];  for(intidx=0; idx<3; idx++) {C+=tex2d (_MAINTEX,I.UVOFF[IDX].XY) *Weight[idx]; C+=tex2d (_MAINTEX,I.UVOFF[IDX].ZW) *Weight[idx]; }        returnC; } ENDCG Subshader {//No culling or depthcull off Zwrite off Pass {ZTest always cgprogram#pragmaVertex vert_blurhorizontal#pragmaFragment Frag_blurENDCG} Pass {ZTest always cgprogram#pragmaVertex vert_blurvertical#pragmaFragment Frag_blurENDCG} }}

C#

usingUnityengine;usingSystem.Collections; [Requirecomponent (typeof(Camera))] [Executeineditmode] Public classBlur:monobehaviour { PublicMaterial Ma;  Public floatBlursize =Ten;  Public intInterator =2; voidonrenderimage (rendertexture sourcetexture, rendertexture desttexture) {ma. SetFloat ("_blursize", blursize); intRtW = sourcetexture.width/8; intRtH = sourcetexture.height/8; Rendertexture Rttempa= Rendertexture.gettemporary (RtW, RtH,0, Sourcetexture.format); Rttempa.filtermode=filtermode.bilinear; Rendertexture RTTEMPB= Rendertexture.gettemporary (RtW, RtH,0, Sourcetexture.format); Rttempb.filtermode=filtermode.bilinear;  for(inti =0; i < Interator; i++) {            if(0==i) {graphics.blit (sourcetexture, Rttempa,ma,0); Graphics.blit (Rttempa, RTTEMPB, MA,1); } Else{graphics.blit (RTTEMPB, Rttempa, MA,0); Graphics.blit (Rttempa, Rttempb,ma,1);        }} graphics.blit (RTTEMPB, desttexture);        Rendertexture.releasetemporary (RTTEMPA);    Rendertexture.releasetemporary (RTTEMPB); }}

Unity3d Shader Gaussian 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.