几个后处理Shader

有点空闲,做了几个图形学上的基础效果,以后可能会更深入研究吧

边缘提取

Shader "Hidden/Edge"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		// No culling or depth
		Cull Off ZWrite Off ZTest Always

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			sampler2D _MainTex;  
			float4 _MainTex_TexelSize;

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float2 uvLeftTop : TEXCOORD1;
				float2 uvRightBottom : TEXCOORD2;
				float4 vertex : SV_POSITION;
			};

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				float xscale = _MainTex_TexelSize.x;
				float yscale = _MainTex_TexelSize.y;
				o.uvLeftTop = v.uv + float2(-xscale,-yscale);
				o.uvRightBottom = v.uv+ float2(xscale,yscale);
				return o;
			}

			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 cent = tex2D(_MainTex, i.uv);
				// 0 1 0
				// 1-4 1
				// 0 1 0
				fixed4 up,left,right,bottom;
				up = tex2D(_MainTex, float2(i.uv.x,i.uvLeftTop.y));
				left = tex2D(_MainTex, float2(i.uvLeftTop.x,i.uv.y));
				right = tex2D(_MainTex, float2(i.uvRightBottom.x,i.uv.y));
				bottom = tex2D(_MainTex, float2(i.uv.x,i.uvRightBottom.y));

				fixed4 col = cent*-4+up+left+right+bottom;
				fixed4 transparent = float4(0,0,0,0);
				float edge = step(length(float3(0.07,0.07,0.07)),length(col.xyz));
				return edge*float4(1,1,1,1);
			}
			ENDCG
		}
	}
}

膨胀

// 膨胀
Shader "Hidden/Dilating"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		// No culling or depth
		Cull Off ZWrite Off ZTest Always

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			sampler2D _MainTex;  
			float4 _MainTex_TexelSize;

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float2 uvLeftTop : TEXCOORD1;
				float2 uvRightBottom : TEXCOORD2;
				float4 vertex : SV_POSITION;
			};

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				float xscale = _MainTex_TexelSize.x;
				float yscale = _MainTex_TexelSize.y;
				o.uvLeftTop = v.uv + float2(-xscale,-yscale);
				o.uvRightBottom = v.uv+ float2(xscale,yscale);
				return o;
			}

			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
				fixed4 four[5];
				four[0] = tex2D(_MainTex, float2(i.uv.x,i.uvLeftTop.y));
				four[1] = tex2D(_MainTex, float2(i.uvLeftTop.x,i.uv.y));
				four[2] = tex2D(_MainTex, float2(i.uvRightBottom.x,i.uv.y));
				four[3] = tex2D(_MainTex, float2(i.uv.x,i.uvRightBottom.y));
				four[4] = col;
				fixed4 v = fixed4(0,0,0,0);
				for(int i=0;i<5;i++)
				{
					v = max(v,four[i]);
				}
				return v;
			}
			ENDCG
		}
	}
}

腐蚀

// 腐蚀
Shader "Hidden/Eroding"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		// No culling or depth
		Cull Off ZWrite Off ZTest Always

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			sampler2D _MainTex;  
			float4 _MainTex_TexelSize;

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float2 uvLeftTop : TEXCOORD1;
				float2 uvRightBottom : TEXCOORD2;
				float4 vertex : SV_POSITION;
			};

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				float xscale = _MainTex_TexelSize.x;
				float yscale = _MainTex_TexelSize.y;
				o.uvLeftTop = v.uv + float2(-xscale,-yscale);
				o.uvRightBottom = v.uv+ float2(xscale,yscale);
				return o;
			}

			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
				fixed4 four[5];
				four[0] = tex2D(_MainTex, float2(i.uv.x,i.uvLeftTop.y));
				four[1] = tex2D(_MainTex, float2(i.uvLeftTop.x,i.uv.y));
				four[2] = tex2D(_MainTex, float2(i.uvRightBottom.x,i.uv.y));
				four[3] = tex2D(_MainTex, float2(i.uv.x,i.uvRightBottom.y));
				four[4] = col;
				fixed4 v = fixed4(1,1,1,1);
				for(int i=0;i<5;i++)
				{
					v = min(v,four[i]);
				}
				return v;
			}
			ENDCG
		}
	}
}

然后,边缘提取+膨胀+腐蚀+腐蚀的效果 如下:

还有个边缘叠加的:

Shader "Hidden/EdgeAdd"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		// No culling or depth
		Cull Off ZWrite Off ZTest Always

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			sampler2D _MainTex;  
			float4 _MainTex_TexelSize;

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				float2 uvLeftTop : TEXCOORD1;
				float2 uvRightBottom : TEXCOORD2;
				float4 vertex : SV_POSITION;
			};

			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				float xscale = _MainTex_TexelSize.x;
				float yscale = _MainTex_TexelSize.y;
				o.uvLeftTop = v.uv + float2(-xscale,-yscale);
				o.uvRightBottom = v.uv+ float2(xscale,yscale);
				return o;
			}

			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 cent = tex2D(_MainTex, i.uv);
				// 0 1 0
				// 1-4 1
				// 0 1 0
				fixed4 up,left,right,bottom;
				up = tex2D(_MainTex, float2(i.uv.x,i.uvLeftTop.y));
				left = tex2D(_MainTex, float2(i.uvLeftTop.x,i.uv.y));
				right = tex2D(_MainTex, float2(i.uvRightBottom.x,i.uv.y));
				bottom = tex2D(_MainTex, float2(i.uv.x,i.uvRightBottom.y));

				fixed4 col = cent*-4+up+left+right+bottom;
				fixed4 transparent = float4(0,0,0,0);
				float edge = length(col.xyz)/length(float3(1,1,1));
				return edge*float4(0,1,0,1)+cent;
			}
			ENDCG
		}
	}
}

效果如下:

大概就是这样了~

原理是在网上找的,我也就不多说了。虽然以前学过图形学,都快还给老师了,惆怅。。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值