How to display the central part of a texture on the viewport?

I’m trying to show a central part of the texture on the viewport.
For example, this texture (which size is larger than viewport) below:


I want to show the center part (about 1/4 size) on the full viewport:

So I wrote a shader to do this:

UNITY_DECLARE_TEX2D(_RealWorldTex);
...
half4 _RealWorldTex_TexelSize;
...
fixed4 frag(v2f i) : SV_Target
            {
                float2 offset = ((_RealWorldTex_TexelSize.z - _MainTex_TexelSize.z) * 0.5,
                                 (_RealWorldTex_TexelSize.w - _MainTex_TexelSize.w) * 0.5);
                fixed4 realWorldTex = _RealWorldTex.Load(int3((i.uv * _RealWorldTex_TexelSize.zw + offset), 0));
                ...
            }

But it didn’t work as I expected:

It didn’t show the central part of the texture on the full viewport.

Shader code was inspired by bgolus from What is GLSL's texelFetch in Unity's unlit shader?

Hi,
I think this should do the trick (from my notes):

// Offset UV so that it begins from center of the image
i.uv -= 0.5;
// Scale it
i.uv *= _Scale;
// Move it back to center
i.uv += 0.5;
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);

So you move that 0-1 UV space so that it’s origin is in the center of the image, then scale and move back.

EDIT: Tried it, work fine (scales from center). But you have to of course provide correct scale factor to get the desired image cropping.

Either generate the scaling values by calculating them or use some list of predefined values…

Thanks Olmi, tex2D() works fine with me.