Search Unity

Using screen space: how to have the texture be rendered in its native resolution and tile?

Discussion in 'Shaders' started by Sibylline-Siren, Aug 26, 2018.

  1. Sibylline-Siren

    Sibylline-Siren

    Joined:
    Jul 8, 2012
    Posts:
    22
    Hi,

    In a screen space render one might have something like this:
    Code (CSharp):
    1. float2 screenUV = (IN.screenPos.xy) / IN.screenPos.w;
    2. fixed4 c = tex2D (_MainTex, screenUV);
    If the texture is, say, 512x512 and the screen 1080p, the texture will stretch to the resolution of the screen.
    Is there a way to have the texture be rendered in 512x512, and tile as many times as needed in order to fill the screen? Hopefully the code would work regardless of the screen's resolution.

    Thanks
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Using screenPos will get you a normalized screen space position. That is 0,0 in the bottom left, 1,1 in the top right. To get your texture to appear at the correct resolution, you need to know the screen resolution and the texture resolution. Luckily both are available to shaders. _ScreenParams holds the screen resolution and is always defined, and _MainTex_TexelSize holds the texture resolution if you define it as a uniform (a variable outside a function).

    float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
    screenUV *= _ScreenParams.xy * _MainTex_TexelSize.xy;
     
  3. Sibylline-Siren

    Sibylline-Siren

    Joined:
    Jul 8, 2012
    Posts:
    22
    Thanks a lot, that worked perfectly