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