Unity3d Ngui dynamic generation of fuzzy background images

Source: Internet
Author: User

First the effect.

Production principle: The fuzzy part is with Uitexture, front is a uisprite. Render a texture with the main camera, blur the texture, and assign the texture to uitexture.

Script code

Using unityengine;using System.Collections; [Requirecomponent (typeof (Uitexture))]public class Blurtexturemaker:monobehaviour {public int iterations = 3;public float Blurspread = 0.6f;public Shader blurshader = null;static Material m_material = null;public Camera camera;private UIT Exture mtexture;private rendertexture mrt;protected Material material{get{if (m_material = null) {m_material = new Materi Al (blurshader); m_material.hideflags = Hideflags.dontsave;} return m_material;}} protected void Awake () {mtexture = Getcomponent<uitexture> ();} protected void Start () {//Disable if we don ' t support image effectsif (! systeminfo.supportsimageeffects) {enabled = False;return;} Disable if the shader can ' t run on the Users Graphics Cardif (!blurshader | |!material.shader.issupported) {enabled = FA Lse;return;} int rtwidth = (int) (NGUITOOLS.SCREENSIZE.X/4); int rtheight = (int) (NGUITOOLS.SCREENSIZE.Y/4); MRT = Rendertexture.gette Mporary (rtwidth, rtheight, 0, rendertextureformat.default); mtexture. maintexture = Generate ();} protected void OnDestroy () {if (null! = MRT) {rendertexture.releasetemporary (MRT);}} Performs one blur iteration.public void Fourtapcone (rendertexture source, rendertexture dest, int iteration) {float off = 0.5f + Iteration * blurspread; Graphics.blitmultitap (source, dest, Material,new Vector2 (-off,-off), New Vector2 (-off, off), new Vector2 (off, off), new Vector2 (off,-off));} Downsamples the texture to a quarter resolution.private void downsample4x (rendertexture source, Rendertexture dest) {Flo at off = 1.0f; Graphics.blitmultitap (source, dest, Material,new Vector2 (-off,-off), New Vector2 (-off, off), new Vector2 (off, off), new Vector2 (off,-off));}  Called by the camera to apply the image effectvoid RenderImage (rendertexture source, rendertexture destination) {int RtW = Source.width/4;int RtH = SOURCE.HEIGHT/4; Rendertexture buffer = rendertexture.gettemporary (RtW, RtH, 0);//Copy source to the 4x4 smaller texture. downsample4x (source, buffer);//Blur the small Texturefor (int i = 0; i < iterations; i++) {Rendertexture buffer2 = rendertexture.gettemporary (RtW, RtH, 0); Fourtapcone (buffer, buffer2, i); Rendertexture.releasetemporary (buffer); buffer = Buffer2;} Graphics.blit (buffer, destination); Rendertexture.releasetemporary (buffer);}        Public Rendertexture Generate () {int rtwidth = (int) (NGUITOOLS.SCREENSIZE.X/4); int rtheight = (int) (NGUITOOLS.SCREENSIZE.Y/4); var tex = Rendertexture.gettemporary (rtwidth, rtheight, 0, Rendertextureformat.default); camera.targettexture = Tex;camera. Render (); camera.targettexture = null; RenderImage (Tex, MRT); Rendertexture.releasetemporary (Tex); return MRT;} [ContextMenu ("sample")]public void Sample () {var Tex = getcomponent<uitexture> (); tex.maintexture = Generate ();}}

  

Shader code

Shader "Hidden/blureffectconetap" {Properties {_maintex ("", any) = "" {}}subshader {Pass {ZTest always cull Off zwrite Off Fog {Mode off}settexture [_maintex] {constantcolor (0,0,0,0.25) combine texture * constant alpha}settexture [_maint Ex] {Constantcolor (0,0,0,0.25) combine texture * constant + previous}settexture [_maintex] {Constantcolor (0,0,0,0.25) CO Mbine Texture * constant + previous}settexture [_maintex] {constantcolor (0,0,0,0.25) combine texture * constant + Previou S}}}cginclude#include "Unitycg.cginc" struct v2f {float4 pos:sv_position;half2 uv:texcoord0;half2 taps[4]: TEXCOORD1; };sampler2d _maintex;half4 _maintex_texelsize;half4 _bluroffsets;v2f vert (appdata_img v) {v2f o; o.pos = Mul (UNITY_MATRI X_MVP, V.vertex); o.uv = V.texcoord-_bluroffsets.xy * _MAINTEX_TEXELSIZE.XY; Hack, see BlurEffect.cs for the reason for this. Let's make a new blur effect soono.taps[0] = O.uv + _maintex_texelsize * _bluroffsets.xy;o.taps[1] = o.uv-_maintex_texel Size * _bluroffsETS.XY;O.TAPS[2] = o.uv + _maintex_texelsize * _bluroffsets.xy * HALF2 (1,-1); o.taps[3] = o.uv-_maintex_texelsize * _Blur Offsets.xy * HALF2 (1,-1); return o;} Half4 Frag (v2f i): sv_target {Half4 color = tex2d (_maintex, i.taps[0]); color + = tex2d (_maintex, i.taps[1]); color + = tex2d (_maintex, i.taps[2]); color + tex2d (_maintex, i.taps[3]); return color * 0.25;} Endcgsubshader {Pass {ZTest always cull off zwrite off Fog {Mode Off} cgprogram #pragma fragmentoption arb_p Recision_hint_fastest #pragma vertex vert #pragma fragment Frag ENDCG}}fallback off}

  

Unity3d Ngui dynamic generation of fuzzy background images

Related Article

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.