Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Radius("Radius",Range(0,10))=0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
float2 uv1 : TEXCOORD1;
float2 uv2 : TEXCOORD2;
float2 uv3 : TEXCOORD3;
float2 uv4 : TEXCOORD4;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _MainTex_TexelSize;
float _Radius;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
//计算UV上下左右4个点的uv坐标
o.uv1=v.texcoord.xy+_MainTex_TexelSize*float2(1,1)*_Radius;
o.uv2=v.texcoord.xy+_MainTex_TexelSize*float2(1,-1)*_Radius;
o.uv3=v.texcoord.xy+_MainTex_TexelSize*float2(-1,1)*_Radius;
o.uv4=v.texcoord.xy+_MainTex_TexelSize*float2(-1,-1)*_Radius;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 color=fixed4(0,0,0,0);
color+=tex2D(_MainTex,i.uv);
color+=tex2D(_MainTex,i.uv1);
color+=tex2D(_MainTex,i.uv2);
color+=tex2D(_MainTex,i.uv3);
color+=tex2D(_MainTex,i.uv4);
return color/5;
}
ENDCG
}
}
Unity shader 初学 模糊图像效果
于 2023-08-08 19:41:53 首次发布
文章详细描述了如何在Unity中创建一个着色器,使用纹理和自定义的UV扩展,实现对2D纹理的多点采样。它展示了如何设置纹理属性和子着色器Pass中的CG代码来实现无缝纹理融合。
摘要由CSDN通过智能技术生成