首页 技术 正文
技术 2022年11月16日
0 收藏 348 点赞 2,974 浏览 2359 个字

Unity3D Shader 高斯模糊

//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;
uniform float _BlurSize; static const half weight[] = {0.0205, 0.0855, 0.232, 0.324};
static const half4 coordOffset = half4(.0h,.0h,-.0h,-.0h); struct v2f_blurSGX
{
float4 pos:SV_POSITION;
half2 uv:TEXCOORD0;
half4 uvoff[]: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(,)*_BlurSize;
o.uvoff[] = v.texcoord.xyxy+offs.xyxy*coordOffset*;
o.uvoff[] = v.texcoord.xyxy+offs.xyxy*coordOffset*;
o.uvoff[] = v.texcoord.xyxy+offs.xyxy*coordOffset; return o;
} 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(,)*_BlurSize;
o.uvoff[] = v.texcoord.xyxy+offs.xyxy*coordOffset*;
o.uvoff[] = v.texcoord.xyxy+offs.xyxy*coordOffset*;
o.uvoff[] = v.texcoord.xyxy+offs.xyxy*coordOffset; return o;
} fixed4 frag_Blur(v2f_blurSGX i):SV_Target
{ fixed4 c = tex2D(_MainTex,i.uv)*weight[];
for(int idx=; idx<; idx++)
{
c+=tex2D(_MainTex,i.uvoff[idx].xy)*weight[idx];
c+=tex2D(_MainTex,i.uvoff[idx].zw)*weight[idx];
} return c;
} ENDCG SubShader
{
// No culling or depth
Cull Off ZWrite Off Pass
{
ZTest Always
CGPROGRAM
#pragma vertex vert_BlurHorizontal
#pragma fragment frag_Blur ENDCG
} Pass
{
ZTest Always
CGPROGRAM
#pragma vertex vert_BlurVertical
#pragma fragment frag_Blur ENDCG
}
}
}

//c#

using UnityEngine;
using System.Collections;[RequireComponent(typeof(Camera))]
[ExecuteInEditMode]
public class Blur : MonoBehaviour { public Material ma; public float BlurSize =;
public int interator = ; void OnRenderImage (RenderTexture sourceTexture, RenderTexture destTexture)
{ ma.SetFloat ("_BlurSize", BlurSize);
int rtW = sourceTexture.width/;
int rtH = sourceTexture.height/; RenderTexture rtTempA = RenderTexture.GetTemporary (rtW, rtH, , sourceTexture.format);
rtTempA.filterMode = FilterMode.Bilinear; RenderTexture rtTempB = RenderTexture.GetTemporary (rtW, rtH, , sourceTexture.format);
rtTempB.filterMode = FilterMode.Bilinear; for (int i = ; i < interator; i++) {
if ( == i) {
Graphics.Blit (sourceTexture, rtTempA,ma,);
Graphics.Blit (rtTempA, rtTempB, ma, );
} else { Graphics.Blit (rtTempB, rtTempA, ma, );
Graphics.Blit (rtTempA, rtTempB,ma,);
} } Graphics.Blit(rtTempB, destTexture); RenderTexture.ReleaseTemporary(rtTempA);
RenderTexture.ReleaseTemporary(rtTempB);
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,964
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781