Search Unity

2D shader with strange pixel 'edges'?

Discussion in 'Shaders' started by toastertub, Sep 7, 2017.

  1. toastertub

    toastertub

    Joined:
    Oct 28, 2013
    Posts:
    25
    OddPixelEdges.png

    I'm getting these weird edges on my sprites after trying to make a color swap shader. I'm not great with writing shaders, so that could be part of the issue. But, as you can see in the image, the edges are less than a pixel, if that makes any sense. I don't see how they're popping up.

    Here is the shader I'm using.

    Code (csharp):
    1.  
    2. Shader "Hidden/GuyColorShader"
    3. {
    4.     Properties
    5.     {
    6.         _MainTex ("Texture", 2D) = "white" {}
    7.         _RampTex("RampTex", 2D) = "white"{}
    8.  
    9.  
    10.     }
    11.     SubShader
    12.     {
    13.         Tags {"Queue" = "Transparent+1" "RenderType" = "Opaque"}
    14.         // No culling or depth
    15.         ZWrite Off
    16.         Blend SrcAlpha OneMinusSrcAlpha
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.          
    24.             #include "UnityCG.cginc"
    25.  
    26.             struct appdata
    27.             {
    28.                 float4 vertex : POSITION;
    29.                 float2 uv : TEXCOORD0;
    30.             };
    31.  
    32.             struct v2f
    33.             {
    34.                 float2 uv : TEXCOORD0;
    35.                 float4 vertex : SV_POSITION;
    36.             };
    37.  
    38.             v2f vert (appdata v)
    39.             {
    40.                 v2f o;
    41.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    42.                 o.uv = v.uv;
    43.                 return o;
    44.             }
    45.  
    46.             sampler2D _RampTex;
    47.  
    48.             sampler2D _MainTex;
    49.  
    50.             fixed4 frag (v2f i) : SV_Target
    51.             {
    52.                 fixed4 hold = tex2D(_MainTex, i.uv);
    53.                 fixed4 col = tex2D(_RampTex,float2(hold.r,.5));
    54.  
    55.  
    56.  
    57.                 col.a = hold.a;
    58.  
    59.                 return col;
    60.             }
    61.             ENDCG
    62.         }
    63.     }
    64. }
    65.  
    66.  
    I've tried changing the queue and render type, but no matter what, I'm getting those strange edges. The edges are definitely getting their colors from the gradient image I'm passing in, it's almost like the pictures are being blurred a little, but I have the import settings set to no compression.

    Thank you for your time.

    edit:
    Also, the edge's ratio of edge size to pixel size gets worse the further the camera is zoomed out. But, if the camera's zoom/orthographic size is set to .54, and the player's position is at a whole number, then the edges are gone.
     
    Last edited: Sep 7, 2017
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,795
    Do you have the texture's filtering set to point? If not, try that.
     
  3. toastertub

    toastertub

    Joined:
    Oct 28, 2013
    Posts:
    25
    I knew I forgot to mention something, sorry. Yes, the filtering is set to point on all of the images.

    I don't know if this helps any, but here is another image, before the shader is on the right, with the shader applied is on the left.

    Oddedges2.png
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,795
    Untick "generate mipmaps" from the ramp texture.
     
  5. toastertub

    toastertub

    Joined:
    Oct 28, 2013
    Posts:
    25
    Settings.png

    Yup, got that too. These are the settings I'm using on everything (Except for read/write, that's only on the ramp), in case I'm forgetting something else with them.

    I would upload my project if it would help, but there's no need to download 62 megs for this.

    EDIT:

    Also, in the quality settings, anti aliasing and anisotropic textures are off on all quality levels.
     
    Last edited: Sep 7, 2017