Search Unity

Texture scale filtering in Unity 5 vs Unity 4

Discussion in 'General Graphics' started by scottharber, Aug 5, 2016.

  1. scottharber

    scottharber

    Joined:
    May 14, 2014
    Posts:
    42
    Hello all,

    Apologies in advance if this is an old topic. I couldn't find any info online.

    I have a texture, saved from Photoshop as a 1024x1024, but imported into Unity as a 128x128 (the discrepancy in resolution is for future-proofing, as my team ports to more powerful formats during development).

    We're in the process of upgrading to Unity 5, and have found that the texture seems to look worse than it did in Unity 4...
    03_smallcompressed.jpg
    ...disabling compression has no impact...
    02_smalluncompressed.jpg

    ...but running the texture at source res seems to bring the textures back to parity.
    04_largecompressed.jpg

    I've also tried various other settings, and can rule out aniso level, quality settings, wrap mode, filter mode, and the presence of mip maps.

    From the above, I've concluded that the difference in appearance between the two Unities is because of how the texture is scaled on import.

    Without reducing the texture resolution in Photoshop, is there a way to make the textures in Unity 5 look as good as they do in Unity 4? The Unity 4 texture import seems to do a sharpen filter as it scales. Is there an option to enable such a filter on Unity 5?
     

    Attached Files:

  2. scotthNM

    scotthNM

    Joined:
    Apr 17, 2015
    Posts:
    4
    (bump)
     
  3. Matjio

    Matjio

    Unity Technologies

    Joined:
    Dec 1, 2014
    Posts:
    108
    Hi @scottharber, could you share the texture with me please so that I can take a look?
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,791
    I believe Unity 4 was doing simple nearest neighbour, while 5 seems to be doing some sort of filtering. (see how some thin brick lines are missed in the unity 4 and how they are consistent in Unity 5)
     
  5. scottharber

    scottharber

    Joined:
    May 14, 2014
    Posts:
    42
    Hi @Matjio . Sorry for the late reply.

    As requested, here is the texture.
     

    Attached Files:

  6. scottharber

    scottharber

    Joined:
    May 14, 2014
    Posts:
    42
    Solved it. I wrote a postprocessor that sharpens the image on import. :)

    sharpener.jpg

    Code:
    Code (CSharp):
    1. public class TextureSharpener : AssetPostprocessor
    2. {
    3.     void OnPostprocessTexture(Texture2D texture)
    4.     {
    5.         for (int m = 0; m < texture.mipmapCount; m++)
    6.         {
    7.             Color[] s = texture.GetPixels(m);
    8.             Color[] d = texture.GetPixels(m);
    9.  
    10.             int texWidth = texture.width;
    11.  
    12.             for (int i = 0; i < d.Length; i++)
    13.             {
    14.                 Color topMid = Color.black;
    15.                 Color bottomMid = Color.black;
    16.                 Color bottomLeft = Color.black;
    17.                 Color bottomRight = Color.black;
    18.                 Color topLeft = Color.black;
    19.                 Color topRight = Color.black;
    20.                 Color left = Color.black;
    21.                 Color right = Color.black;
    22.  
    23.                 if (i - texWidth > 0) topMid = s[i - texWidth];
    24.                 if (i + texWidth <  d.Length)  bottomMid = s[i + texWidth];
    25.                 if (i + texWidth -1 < d.Length) bottomLeft = s[i + texWidth - 1];
    26.                 if (i + texWidth +1 <  d.Length) bottomRight = s[i + texWidth + 1];
    27.                 if (i - texWidth - 1 > 0) topLeft = s[i - texWidth - 1];
    28.                 if (i - texWidth + 1 > 0) topRight = s[i - texWidth + 1];
    29.                 if (i - 1 > 0) left = s[i - 1];
    30.                 if (i +1 >  d.Length) right = s[i + 1];
    31.              
    32.                 Color pixel = s[i];
    33.  
    34.                 d[i].r = ((topLeft.r * 0) + (topMid.r * -2) + (topRight.r * 0) + (left.r * -2) + (pixel.r * 11) + (right.r * -2) + (bottomLeft.r * 0) + (bottomMid.r * -2) + (bottomRight.r * 0) ) / 5.0f;
    35.                 d[i].g = ((topLeft.g * 0) + (topMid.g * -2) + (topRight.g * 0) + (left.g * -2) + (pixel.g * 11) + (right.g * -2) + (bottomLeft.g * 0) + (bottomMid.g * -2) + (bottomRight.g * 0) ) / 5.0f;
    36.                 d[i].b = ((topLeft.b * 0) + (topMid.b * -2) + (topRight.b * 0) + (left.b * -2) + (pixel.b * 11) + (right.b * -2) + (bottomLeft.b * 0) + (bottomMid.b * -2) + (bottomRight.b * 0) ) / 5.0f;
    37.  
    38.                 if (d[i].r < 0) d[i].r = 0;
    39.                 if (d[i].r > 1) d[i].r = 1;
    40.  
    41.                 if (d[i].g < 0) d[i].g = 0;
    42.                 if (d[i].g > 1) d[i].g = 1;
    43.  
    44.                 if (d[i].b < 0) d[i].b = 0;
    45.                 if (d[i].b > 1) d[i].b = 1;
    46.  
    47.             }
    48.             texture.SetPixels(d, m);
    49.         }
    50.         texture.Apply(true, true);
    51.     }
    52. }
     
  7. Tapgames

    Tapgames

    Joined:
    Dec 1, 2009
    Posts:
    242
    That looks really good! How did you even come-up with this script? :eek: