Texture2D Blurring/Banding

I’ve been trying to figure out how to get rid of the banding/blurring, on the 2D Texture I’m programmatically creating.

What this will be used for is old-style 2D pixel games, I thought instead of creating a load of png files in photoshop, I would programmatically create some, I’ve commented out the Dithering Method it does work fine but, if you zone close you get this blurring where the two colours meet, looks more like a gradient I suppose.

public class TextureComponent : MonoBehaviour
{
 public SpriteRenderer mRenderer;

 private void Start()
 {
  Texture2D mTexture = new Texture2D(3840, 2560, TextureFormat.ARGB32, false, true);
  FilledRectangle(mTexture, 0, mTexture.height, mTexture.width, 50, Color.red);
  FilledRectangle(mTexture, 0, mTexture.height - 51, mTexture.width, 100, Color.green);
  //Dithering(mTexture, 0, mTexture.height - 40, mTexture.width, 20, Color.red, Color.green);
  mTexture.Apply();
  mRenderer.sprite = Sprite.Create(mTexture, new Rect(0, 0, mTexture.width, mTexture.height), new Vector2(0.5f, 0.5f), 64.0F);
 }

 private void FilledRectangle(Texture2D texture, Int32 left, Int32 top, Int32 width, Int32 height, Color colour)
 {
  for (int y = top; y >= top - height; y--)
   for (int x = left; x < width; x++)
    texture.SetPixel(x, y, colour);
 }

 private void Dithering(Texture2D texture, Int32 left, Int32 top, Int32 width, Int32 height, Color d1colour, Color d2color)
 {
  for (int y = top; y >= top - height; y--)
   for (int x = left; x < width; x++)
    texture.SetPixel(x, y,  (( ((x+y) % 2) == 0) ? d1colour : d2color ));
 }
}

Try setting the filtering to point on the texture2d. I forget what the exact parameter is but I know you can do it

@spryx

Thanks for taking the time to reply, and your absolutely spot-on, All I did was this “mTexture.filterMode = FilterMode.Point;” and the blurring/band has now gone, it’s funny as you normally do this in the editor, when importing images but it never occurred to me to set it in code.

Cheers

1 Like