Unity3D NGUI动态生成模糊背景图
先上效果。

制作原理:模糊的部分是用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动态生成模糊背景图的更多相关文章
- PHP实现动态生成饼状图、柱状图和折线图(转载)
PHP在图像操作方面的表现非常出色,我们只需借助可以免费得到的GD库便可以轻松实现图.表勾画.下面将分别介绍PHP实现的饼状图.折线图和柱状图以 及他们的使用方法,这几段代码的特点就是不需要再把它们复 ...
- JAVA实现二维码生成加背景图
pom.xml依赖 <!-- 二维码生成 --> <!-- https://mvnrepository.com/artifact/com.google.zxing/c ...
- PHP实现动态生成饼状图 (转载)
<?php //变量定义,画椭圆弧时的角度大小 define("ANGLELENGTH", 10); /** * 绘制图片 * @param $title 3D图的标题 * ...
- js 动态生成背景图 GeoPattern
以前有个想法,能不能用JS动态创建CANVAS绘制图案当网页背景,在网络发现有现成的别人已经实现的:GeoPattern 代码如下: <!DOCTYPE html> <html> ...
- NGUI 动态添加控件
本文链接地址: Unity3D NGUI动态创建按钮 本例仅以熟悉NGUI组件功能为目的,想快捷简便的创建按钮或其它游戏物体请参考 “Unity3D 动态实例化Prefab” 以动态创建服务器列表为例 ...
- NGUI 动态字体边缘模糊,毛边的问题解决办法
NGUI支持生成动态字体,将ttf格式的字体文件拖入工程,用NGUIFontMaker制作即可,但是制作完之后会发现字体有毛边,边缘模糊. 这时选中你生成的字体预设,在该预设的UIFont脚本上更改P ...
- [Unity3D插件]2dtoolkit系列二 动画精灵的创建以及背景图的无限滚动
经过昨天2dtoolkit系列教程一的推出,感觉对新手还有有一定的启发作用,引导学习使用unity 2dToolKit插件的使用过程,今天继续系列二——动画精灵的创建,以及背景图的无限循环滚动,在群里 ...
- Unity3D独立游戏开发日记(一):动态生成树木
目前写的独立游戏是一个沙盒类型的游戏.游戏DEMO视频如下: 提到沙盒类型的游戏,就有人给出了这样的定义: 游戏世界离现实世界越近,自由度.随机度越高才叫沙盒游戏.所谓自由度,就是你在游戏里想干啥就干 ...
- Unity3D 创建动态的立方体图系统
Unity3D 创建动态的立方体图系统 这一篇主要是利用上一篇的Shader,通过脚本来完成一个动态的立方体图变化系统. 准备工作如下: 创建一个新的场景.一个球体.提供给场景一个平行光,准备2个立方 ...
随机推荐
- rpc框架之avro 学习 1 - hello world
avro是hadoop的一个子项目,提供的功能与thrift.Protocol Buffer类似,都支持二进制高效序列化,也自带RPC机制,但是avro使用起来更简单,无需象thrift那样生成目标语 ...
- jQuery延迟加载插件(Lazy Load)详解
最 新版本的Lazy Load并不能替代你的网页.即便你使用JavaScript移除了图片的src属性,有些现代的浏览器仍然会加载图片.现在你必须修改你的html代 码,使用占位图片作为img标签的s ...
- ionic 开发笔记
1.AngularJS 外部的控制器(DOM 事件.外部的回调函数如 jQuery UI 空间等)调用了 AngularJS 函数之后,必须调用$apply.在这种情况下,你需要命令 AngularJ ...
- React Native 接入微博、微信、QQ 登录功能
在 App 开发中我们经常需要在用户登录模块接入 SNS 登录组件,这样会大大提高用户的注册体验.特别当一个不是刚性需求 App 推广的时候,这样会很大的降低用户体验的成本,没有人愿意忍受输入邮箱.手 ...
- Java部署_IntelliJ创建一个可运行的jar包(实践)
一.本文目的:使用Intellij Idea 13生成一个简单可执行的jar,用于快速在linux验证某个功能 二.项目源码 1.结构图 2.StaticC1.java 1 2 3 4 5 6 7 ...
- 使用CocoaPods开发并打包静态库
Cocoapods作为OS X和iOS开发平台的类库管理工具,已经非常完善和强大.通常我们用pod来管理第三方开源类库,但我们也极有可能会开发一个用pod管理依赖关系的静态类库给其他人使用,而又不愿意 ...
- 【51Nod 1501】【算法马拉松 19D】石头剪刀布威力加强版
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1501 dp求出环状不连续的前缀和,剩下东西都可以算出来,比较繁琐. 时间 ...
- UI: 多窗口
1.自定义帮助类,用于简化 SecondaryView 的管理UI/MultipleViews/SecondaryViewHelper.cs /* * SecondaryViewHelper - 自定 ...
- 解决Unable to create new native thread
两种类型的Out of Memory java.lang.OutOfMemoryError: Java heap space error 当JVM尝试在堆中分配对象,堆中空间不足时抛出.一般通过设定J ...
- SQL查询为什么不推荐使用select count(*)
select count(1). count(字段名) .count(*) 的区别