Is there a way to convert the below piece of shader code to shader graph? I am not sure how to find float2(i, i) in shader graph.

fixed4 frag (v2f input) : SV_Target
                float2 res = _MainTex_TexelSize.xy;
                float i = _offset;
                fixed4 col;                
                col.rgb = tex2D( _MainTex, input.uv ).rgb;
                col.rgb += tex2D( _MainTex, input.uv + float2( i, i ) * res ).rgb;
                col.rgb += tex2D( _MainTex, input.uv + float2( i, -i ) * res ).rgb;
                col.rgb += tex2D( _MainTex, input.uv + float2( -i, i ) * res ).rgb;
                col.rgb += tex2D( _MainTex, input.uv + float2( -i, -i ) * res ).rgb;
                col.rgb /= 5.0f;
                return col;

1 Answer 1


This is a pretty straightforward translation.

Completed shader graph

First, let's compute the vector (i, i) * res, which is (offset / texture width, offset / texture height):

Forming the vector offset / (width, height)

Then we can multiply that by (1, 1) (no-op, so I skip that node), (1, -1), (-1, -1), and (-1, 1) to get the four shifts from our sample point, and add them to our UV to sample at those shifted positions.

We also have one sample done at our UV coordinate, with no shift applied:

Sampling at offset positions

Lastly, we add all five samples together, and divide by 5 to get our output:

Sum and division

  • \$\begingroup\$ Thanks a lot! It works well :) \$\endgroup\$
    – Saif
    Commented Sep 22, 2020 at 8:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .