- Home /
Question by
neerajamk07 · Apr 11, 2018 at 11:38 AM ·
unity 5shaders
I am new to shaders and I want to write a shader to get erosion in a binary image.
screen-shot-2018-04-11-at-13536-pm.png
(82.1 kB)
Comment
Best Answer
Answer by BastianUrbach · Apr 11, 2018 at 09:17 PM
It's fairly simple, just use the Image Effect Shader template and change the part in the end to this:
sampler2D _MainTex; float4 _MainTex_TexelSize; fixed4 frag (v2f i) : SV_Target { float2 o = _MainTex_TexelSize.xy; float v = 0; v += tex2D(_MainTex, i.uv + float2( 1, 1) * o); v += tex2D(_MainTex, i.uv + float2( 1, 0) * o); v += tex2D(_MainTex, i.uv + float2( 1, -1) * o); v += tex2D(_MainTex, i.uv + float2( 0, 1) * o); v += tex2D(_MainTex, i.uv + float2( 0, 0) * o); v += tex2D(_MainTex, i.uv + float2( 0, -1) * o); v += tex2D(_MainTex, i.uv + float2(-1, 1) * o); v += tex2D(_MainTex, i.uv + float2(-1, 0) * o); v += tex2D(_MainTex, i.uv + float2(-1, -1) * o); return v == 9 ? 1 : 0; }Basically _MainTex_TexelSize.xy is the size of each pixel on the main texture. We sample the texture in a 3x3-grid around the pixel we are currently shading and only return 1 if all 9 pixels are 1.
I tried this code. But ins$$anonymous$$d of edges getting eroded, they are getting enhanced. Do you know the reason why?