先上效果。

制作原理:模糊的部分是用UITexture,前面是一个UISprite。用主摄像机渲染出一张纹理,把这张纹理模糊处理,把这张纹理赋值给UITexture。

脚本代码

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 UITexture mTexture; private RenderTexture mRT;
protected Material material
{
get
{
if (m_Material == null)
{
m_Material = new Material(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 effects
if (!SystemInfo.supportsImageEffects)
{
enabled = false;
return;
}
// Disable if the shader can't run on the users graphics card
if (!blurShader || !material.shader.isSupported)
{
enabled = false;
return;
} int rtWidth = (int)(NGUITools.screenSize.x / 4);
int rtHeight = (int)(NGUITools.screenSize.y / 4); mRT = RenderTexture.GetTemporary(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)
{
float 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 effect
void 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 texture
for (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代码

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 [_MainTex] {constantColor (0,0,0,0.25) combine texture * constant + previous}
SetTexture [_MainTex] {constantColor (0,0,0,0.25) combine texture * constant + previous}
SetTexture [_MainTex] {constantColor (0,0,0,0.25) combine texture * constant + previous}
}
}
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_MATRIX_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 soon
o.taps[0] = o.uv + _MainTex_TexelSize * _BlurOffsets.xy;
o.taps[1] = o.uv - _MainTex_TexelSize * _BlurOffsets.xy;
o.taps[2] = o.uv + _MainTex_TexelSize * _BlurOffsets.xy * half2(1,-1);
o.taps[3] = o.uv - _MainTex_TexelSize * _BlurOffsets.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;
}
ENDCG
SubShader {
Pass {
ZTest Always Cull Off ZWrite Off
Fog { Mode off } CGPROGRAM
#pragma fragmentoption ARB_precision_hint_fastest
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback off
}

  

Unity3D NGUI动态生成模糊背景图的更多相关文章

  1. PHP实现动态生成饼状图、柱状图和折线图(转载)

    PHP在图像操作方面的表现非常出色,我们只需借助可以免费得到的GD库便可以轻松实现图.表勾画.下面将分别介绍PHP实现的饼状图.折线图和柱状图以 及他们的使用方法,这几段代码的特点就是不需要再把它们复 ...

  2. JAVA实现二维码生成加背景图

    pom.xml依赖 <!-- 二维码生成 -->         <!-- https://mvnrepository.com/artifact/com.google.zxing/c ...

  3. PHP实现动态生成饼状图 (转载)

    <?php //变量定义,画椭圆弧时的角度大小 define("ANGLELENGTH", 10); /** * 绘制图片 * @param $title 3D图的标题 * ...

  4. js 动态生成背景图 GeoPattern

    以前有个想法,能不能用JS动态创建CANVAS绘制图案当网页背景,在网络发现有现成的别人已经实现的:GeoPattern 代码如下: <!DOCTYPE html> <html> ...

  5. NGUI 动态添加控件

    本文链接地址: Unity3D NGUI动态创建按钮 本例仅以熟悉NGUI组件功能为目的,想快捷简便的创建按钮或其它游戏物体请参考 “Unity3D 动态实例化Prefab” 以动态创建服务器列表为例 ...

  6. NGUI 动态字体边缘模糊,毛边的问题解决办法

    NGUI支持生成动态字体,将ttf格式的字体文件拖入工程,用NGUIFontMaker制作即可,但是制作完之后会发现字体有毛边,边缘模糊. 这时选中你生成的字体预设,在该预设的UIFont脚本上更改P ...

  7. [Unity3D插件]2dtoolkit系列二 动画精灵的创建以及背景图的无限滚动

    经过昨天2dtoolkit系列教程一的推出,感觉对新手还有有一定的启发作用,引导学习使用unity 2dToolKit插件的使用过程,今天继续系列二——动画精灵的创建,以及背景图的无限循环滚动,在群里 ...

  8. Unity3D独立游戏开发日记(一):动态生成树木

    目前写的独立游戏是一个沙盒类型的游戏.游戏DEMO视频如下: 提到沙盒类型的游戏,就有人给出了这样的定义: 游戏世界离现实世界越近,自由度.随机度越高才叫沙盒游戏.所谓自由度,就是你在游戏里想干啥就干 ...

  9. Unity3D 创建动态的立方体图系统

    Unity3D 创建动态的立方体图系统 这一篇主要是利用上一篇的Shader,通过脚本来完成一个动态的立方体图变化系统. 准备工作如下: 创建一个新的场景.一个球体.提供给场景一个平行光,准备2个立方 ...

随机推荐

  1. IPv4组播通信原理

    2011-05-08 21:21:14 标签:组播 vin_do,vin_do学习笔记,笔记 休闲 职场 摘自网络,感谢原作者 摘要: 本文试图成为学习TCP/IP网络组播技术的入门材料.文中介绍了组 ...

  2. jquery submit() 提交失败

    今天写一个表单提交 居然走到$('#wechat_form').submit() 这,但怎么都没有提交这个表单 google 了一下 Additional Notes:Forms and their ...

  3. [转]Eclipse启动Tomcat时45秒超时解决方法

    原文地址:http://it.oyksoft.com/post/6577/ Eclipse启动Tomcat时,默认配置的启动超时时长为45秒.假若项目启动超过45秒将会报错.两种解决方法:1.改XML ...

  4. 线段树 poj 2991

    我们只要把这些向量求和,最终所指的位置就是终点,因此我们只要维护好向量的区间和就可以了.对于第二个问题,我们可以用一个数组degree[i]表示第i个向量和第i-1一个向量当前的夹角,这样就有了当前的 ...

  5. 自定义View其实很简单系列1-12

    作者: AigeStudio  http://blog.csdn.net/aigestudio 说明:文中的1/12表示12篇中的第1篇, 1/6=2/12表示12篇中的第2篇,其它类似. 自定义控件 ...

  6. getline数据来源你的三种方式

    (1)getline从交互式的用户输入中\c中获取内容: # awk 'BEGIN {system("echo \"Input your name:\"");g ...

  7. Android Studio各种快捷功能及好用的插件

    转载:http://www.jianshu.com/p/c439605a4364 概述 现如今开发越来越追求效率和节奏,节省出时间做更多的事情,除了开发技术上的封装等,开发工具的使用技巧也是很重要的, ...

  8. icon图标

     http://images2015.cnblogs.com/blog/575577/201609/575577-20160901194344636-1596119396.png  http://im ...

  9. U盘因为装linux系统变小了

    U盘在Windows下被UltraISO等软件制作成Linux启动盘后会自动被格式化成FAT格式,导致容量变小. 用DiskGenius去修复 http://www.jb51.net/softs/75 ...

  10. codevs 1036 商务旅行(Targin求LCA)

    传送门 Description 某首都城市的商人要经常到各城镇去做生意,他们按自己的路线去做,目的是为了更好的节约时间. 假设有N个城镇,首都编号为1,商人从首都出发,其他各城镇之间都有道路连接,任意 ...