1
\$\begingroup\$

I have a texture that I have created that looks something like this:

enter image description here.

This is a very low texture map, but it helps me in a shader that I'm making that distinguishes between land and ocean, the ocean tiles do different things than the land. To try and figure out what was going on, I used the shader to turn the ocean, as defined by the mask, black. Note that this image doesn't match the ocean texture example from above, but it's the best I had...

The ocean is a binary color, either it is ocean or isn't, and is set appropriately. But what I'm actually seeing is that if the 2x2 tile area has mixed land/ ocean, the value is averaged.

enter image description here

What I've noticed is that the pixels were combining together, a 2x2 pixel area was averaged. After tracing through this for some time, I discovered that the issue was a quality setting that reduced the resolution of certain images.

There are some images that I don't mind lowering the resolution of, but images such as this one I absolutely don't want to lower the resolution. Is there a setting that I can add to a runtime generated texture to tell it not to reduce it's resolution if requested to by the quality settings?

EDIT: Code fragments to produce the results above. It's kind of hard to pull just the fragments that produce this code and not produce an overwhelming bit, but hopefully this will do.

Code to generate textures:

Color32[] windMask=new Color32[world.width*world.height];
for (int x = 0; x < world.width; x++)
{
    for (int y = 0; y < world.height; y++)
    {
        windMask[x + world.width * y] = world.Tiles(x, y).baseType == Tile.BaseType.Ocean?new Color32(255,255,255,255):new Color32(0,0,0,255);
    }
}

sharedMaterial.SetTexture("_WindMask", TextureUtilities.TextureFromColors(world.width, world.height, windMask, FilterMode.Point));

public static Texture2D TextureFromColors(int width, int height, Color[] colors, FilterMode filterMode = FilterMode.Trilinear)
{
    Texture2D texture = new Texture2D(width, height);
    texture.SetPixels(colors);
    texture.filterMode = filterMode;
    texture.Apply();
    return texture;
}

The Shader code:

o.Albedo *= 1 - tex2D(_WindMask, IN.uv_MainTex).r;

The "Fastest" quality settings that show the issue:

enter image description here

The "Fantastic" quality, which correctly isolates the ocean.

enter image description here

Image of the correctly isolated ocean:

enter image description here

The issue is the "Texture Quality", which you will note for the fastest setting is half resolution. I would really like to keep the ability to use lower resolution in the game, but have a few textures that do not use the lower resolution if the quality settings change.

\$\endgroup\$
2
  • \$\begingroup\$ Can you show us the code you use to 1) Create and 2) Sample the texture? Without that it's hard to tell what settings you've used and whether you might be inadvertently sampling a lower mip level, for instance. \$\endgroup\$
    – DMGregory
    Aug 30, 2017 at 11:12
  • \$\begingroup\$ @DMGregory Just added a few bits of code. I'm quite sure the issue is the quality, it works just fine if I use a higher quality. Will also add an image of the quality settings, which should help show the issue. \$\endgroup\$ Aug 30, 2017 at 12:28

2 Answers 2

2
\$\begingroup\$

Can you try replacing your creation line with this?

var texture = Texture2D(width, height, TextureFormat.R8, false);

This forces the texture to a non-compressed format with no mipmaps. (You can also try TextureFormat.RGBA8, but I noticed you're only using the red channel so we can save some bytes)

You'll also want to ensure that width and height are powers of 2, otherwise on some graphics hardware Unity will be forced to scale your image, resulting in pixel values other than the ones you want.

\$\endgroup\$
1
  • \$\begingroup\$ Worth a shot, but didn't have an effect, unfortunately. I was trying to and failing to disable mipmaps, as I suspected that might be the issue, but this just confirms it... \$\endgroup\$ Aug 30, 2017 at 13:45
1
\$\begingroup\$

On the import setting of the image, you could try changing the setting, that by default says "Bilinear" (hope it is spelt correct), to "Point (no filter)". Also, there should be a setting that, by default, says "Compressed", to something else.

\$\endgroup\$
2
  • \$\begingroup\$ I'm creating it runtime. It already is point, and not compressed. \$\endgroup\$ Aug 30, 2017 at 1:50
  • \$\begingroup\$ Sorry. Didn't know. \$\endgroup\$ Aug 30, 2017 at 11:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .