有点空闲,做了几个图形学上的基础效果,以后可能会更深入研究吧
边缘提取
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
}
}
}
效果如下:
大概就是这样了~
原理是在网上找的,我也就不多说了。虽然以前学过图形学,都快还给老师了,惆怅。。