Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Pixel Perfect Camera Offsets Screen Shader

Discussion in 'Shaders' started by Lekyaira, Jul 21, 2020.

  1. Lekyaira

    Lekyaira

    Joined:
    Feb 28, 2020
    Posts:
    1
    I made a simple screen shader which replaces the output color in specific regions of the screen. It works fine at some screen sizes and not others. As I resize the view, the region of the replacement color slides around rather than being fixed in location to the tile it's supposed to be affecting. I assume that I need to bring in the camera's view texture rather than the whole _MainTex screen texture, or otherwise change my UVs by some ratio of the camera's view, but I'm not sure where to go from here or how to do that.

    I'm using Pixel Perfect Camera for an orthographic view of a tile map. I have two textures which are simple blocks of color, each pixel representing the replacement color of a tile, one texture for the foreground color and one for the background color.

    Here's my shader code, it's being called by a simple
    Graphics.Blit(source, destination, material);


    Code (CSharp):
    1. Shader "Hidden/Colorizer"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _FGTex ("Texture", 2D) = "white" {}
    7.         _BGTex ("Texture", 2D) = "white" {}
    8.     }
    9.     SubShader
    10.     {
    11.         // No culling or depth
    12.         Cull Off ZWrite Off ZTest Always
    13.  
    14.         Pass
    15.         {
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.  
    20.             #include "UnityCG.cginc"
    21.  
    22.             struct appdata
    23.             {
    24.                 float4 vertex : POSITION;
    25.                 float2 uv : TEXCOORD0;
    26.             };
    27.  
    28.             struct v2f
    29.             {
    30.                 float2 uv : TEXCOORD0;
    31.                 float4 vertex : SV_POSITION;
    32.             };
    33.  
    34.             v2f vert (appdata v)
    35.             {
    36.                 v2f o;
    37.                 o.vertex = UnityObjectToClipPos(v.vertex);
    38.                 o.uv = v.uv;
    39.                 return o;
    40.             }
    41.  
    42.             sampler2D _MainTex;
    43.             sampler2D _FGTex;
    44.             sampler2D _BGTex;
    45.  
    46.             fixed4 frag (v2f i) : SV_Target
    47.             {
    48.                 fixed4 col = tex2D(_MainTex, i.uv);
    49.                 fixed4 fg = tex2D(_FGTex, i.uv);
    50.                 fixed4 bg = tex2D(_BGTex, i.uv);
    51.                 if(col.r == 1.0 && col.g == 1.0 && col.b == 1.0)
    52.                 {
    53.                     return fg;
    54.                 }
    55.                 else if(col.r == 0.0 && col.g == 0.0 && col.b == 0.0)
    56.                 {
    57.                     return bg;
    58.                 }
    59.                 else
    60.                 {
    61.                     return col;
    62.                 }
    63.             }
    64.             ENDCG
    65.         }
    66.     }
    67. }
    68.