Unity shader实现高斯模糊效果
作者:贪玩的孩纸时代 时间:2023-11-17 20:23:45
本文实例为大家分享了Unity shader实现高斯模糊效果的具体代码,供大家参考,具体内容如下
正常图:
高斯模糊效果图:
shader代码:
Shader "Custom/GaoSiMoHu"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_BlurSize("Blur size",Float)=1.0
}
SubShader
{
ZTest Always
cull off
ZWrite off
CGINCLUDE//这个可以使其他pass块都可以使用,而不用在两个pass里都写,减少了写的次数
sampler2D _MainTex;
half4 _MainTex_TexelSize;
float _BlurSize;
struct v2f{
float4 pos:SV_POSITION;
half2 uv[5]:TEXCOORD0;
};
fixed4 fragBlur(v2f i):SV_Target{
float weight[3]={
0.4026,
0.2442,
0.0545
};
fixed3 sum = tex2D(_MainTex,i.uv[0]).rgb * weight[0];
for(int it = 1; it < 3; it++){
sum += tex2D(_MainTex,i.uv[it]).rgb * weight[it];
sum += tex2D(_MainTex,i.uv[2 * it]).rgb * weight[it];
}
return fixed4(sum,1.0);
}
ENDCG
Pass{
NAME "GAUSSIAN_BLUR_VERTICAL"//这个pass的唯一名字,可以在其他地方调用,usepass + 名字
CGPROGRAM
#pragma vertex vertlurVertical
#pragma fragment fragBlur
#include "UnityCG.cginc"
v2f vertlurVertical(appdata_img v){
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.texcoord;
o.uv[0]=uv;
o.uv[1]=uv + float2(0.0,_MainTex_TexelSize.y * 1.0) * _BlurSize;
o.uv[2]=uv - float2(0.0,_MainTex_TexelSize.y * 1.0) * _BlurSize;
o.uv[3]=uv + float2(0.0,_MainTex_TexelSize.y * 2.0) * _BlurSize;
o.uv[4]=uv - float2(0.0,_MainTex_TexelSize.y * 2.0) * _BlurSize;
return o;
}
ENDCG
}
Pass{
NAME "GAUSSIAN_BLUR_HORIZONTAL"
CGPROGRAM
#pragma vertex vertlurHorizontal
#pragma fragment fragBlur
#include "UnityCG.cginc"
v2f vertlurHorizontal(appdata_img v){
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
half2 uv = v.texcoord;
o.uv[0]=uv;
o.uv[1]=uv + float2(0.0,_MainTex_TexelSize.x * 1.0) * _BlurSize;
o.uv[2]=uv - float2(0.0,_MainTex_TexelSize.x * 1.0) * _BlurSize;
o.uv[3]=uv + float2(0.0,_MainTex_TexelSize.x * 2.0) * _BlurSize;
o.uv[4]=uv - float2(0.0,_MainTex_TexelSize.x * 2.0) * _BlurSize;
return o;
}
ENDCG
}
}
}
调节_BlurSize即可看到效果
来源:https://blog.csdn.net/yiwei151/article/details/78720896
标签:Unity,shader,高斯模糊
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis-Plus将字段设置为null解决方法
2023-04-11 08:18:55
springboot集成springsecurity 使用OAUTH2做权限管理的教程
2023-12-22 22:57:21
![](https://img.aspxhome.com/file/2023/9/101939_0s.jpg)
图解JVM垃圾内存回收算法
2023-10-13 17:24:35
![](https://img.aspxhome.com/file/2023/4/64804_0s.jpg)
JAVA基础之注解与反射的使用方法和场景
2022-02-23 19:50:00
![](https://img.aspxhome.com/file/2023/8/65778_0s.jpg)
spring如何动态指定具体实现类
2022-04-13 07:52:21
![](https://img.aspxhome.com/file/2023/9/61859_0s.jpg)
Java压缩解压zip技术_动力节点Java学院整理
2022-08-29 22:50:42
![](https://img.aspxhome.com/file/2023/8/84208_0s.png)
Java语言中Swing组件编程详解
2023-04-19 04:08:22
详解Java中的final关键字
2023-02-20 17:29:50
java中fastjson生成和解析json数据(序列化和反序列化数据)
2023-12-08 17:45:24
举例讲解JDK注解的使用和自定义注解的方法
2022-06-29 17:34:52
Android实现圆圈倒计时
2022-08-03 15:03:29
![](https://img.aspxhome.com/file/2023/5/139435_0s.jpg)
RecyclerView上拉加载封装代码
2023-05-08 21:02:05
![](https://img.aspxhome.com/file/2023/5/117145_0s.gif)
SpringBoot整合RabbitMQ实现六种工作模式的示例
2021-10-17 06:21:08
![](https://img.aspxhome.com/file/2023/7/79827_0s.png)
C#使用Protocol Buffer(ProtoBuf)进行Unity中的Socket通信
2021-10-21 09:08:21
浅析java修饰符访问权限(动力节点Java学院整理)
2023-07-13 18:07:34
Java中list.contains()的用法及拓展
2022-11-06 21:04:59
![](https://img.aspxhome.com/file/2023/9/113199_0s.jpg)
Java详细讲解包的作用以及修饰符的介绍
2021-09-14 05:27:26
Android中Glide加载圆形图片和圆角图片实例代码
2022-08-06 08:26:59
java base64编码、解码的三种方式总结
2022-12-29 22:20:51
android阅读器长按选择文字功能实现代码
2023-09-16 08:48:42
![](https://img.aspxhome.com/file/2023/8/122128_0s.jpg)