If you look carefully at the screenshot above, you can see that some pixels on the left border are brown, bit should be green. And it seems that it’s the case only for the pixels that are brown on the right border.
Same with some pixels on the top border.
The way I generate this texture is by loading 32x32 pixels png files, then copying them into a texture using Texture2D.SetPixels() function :
public void GenerateChunk(ChunkModel chunkModel)
{
byte[] grassFileData = File.ReadAllBytes("Assets/Tilemap/Tiles/grass32_2.png");
Texture2D grassTexture = new Texture2D(/* ... */);
grassTexture.LoadImage(grassFileData);
byte[] mudFileData = /* ... */
Texture2D chunkTexture = new Texture2D(ChunkSidePixelSize, ChunkSidePixelSize);
for (int i = 0; i < ChunkModel.ChunkSideSize; ++i)
{
for (int j = 0; j < ChunkModel.ChunkSideSize; ++j)
{
switch (chunkModel.GetTile(i, j))
{
case TileType.GRASS:
chunkTexture.SetPixels(i * TileSizePixel, j * TileSizePixel, TileSizePixel, TileSizePixel, grassTexture.GetPixels());
break;
case TileType.MUD:
chunkTexture.SetPixels(i * TileSizePixel, j * TileSizePixel, TileSizePixel, TileSizePixel, mudTexture.GetPixels());
break;
}
}
}
chunkTexture.Apply();
/* ... */
Here is the settings of my png files (tiles) :
Is anybody have an idea of what I’m mlaking wrong ?
Thanks in advance,
rOd.
Turn off mipmapping. I think it’s under ‘advanced’.
You can also use Unity’s official tilemapper: Unity - Manual: Creating Tilemaps This should handle all the difficult stuff for you.
It’s what I first wanted to do, but I never managed to generate the tilemap using an algorithm. All the examples and tutorials I found was about to draw manually the tilemap.
Also, I’d like to generate huge maps, so I wonder about performances.
I have this exact same issue. Making a tile map with chunks and this issue is happening to me as well in 2020.3.33f. The debugger prints the correct pixel color, but the pixel color being rendered is wrong and its driving me crazy. I know this is an old post, but I’d love to hear if you solved this. I don’t know the etiquette for necroing, but I’m a bit desperate haha
… by default only to one decimal place… so red == 0.1f looks the same as red == 0.11f and those two reds will not be equivalent.
Another approach not mentioned above is to quantize the colors before comparing them, or provide your own CompareColor() method that will return true as long as colors are “close enough” in r, g, and b channels.
Thanks for the reply. I’ve been looking at this for a while so when I used the debugger I was using examples like black vs white. Moreover the pixels I’m copying from a sprite and if I put the sprite in the scene, the sprite and the image look different on the edges (the pixel on each edge of the 64x64 tile) Furthermore if I set just the first two rows of pixels to be white and the rest black, the top row will also have a whitish outline. I can’t make sense of it - the documentation and code seem very straightforward. It’s almost as if the tile is starting to repeat itself at the edge if that make sense. I tried the other solutions in this thread to no avail.
I will post the code tomorrow as well as some images of the renders. Thanks again for the quick response - I would be happy if you checked back tomorrow
My code for generating my tile map is a little messy and needlessly complicated for this issue, because I can recreate the problem with much simpler code. Below I have a tile being drawn by assigning each pixel a color in a color array and then using set pixels to apply it to the texture. The result should be two white rows and two white columns for the first two rows or columns, but what I am getting is something messier than that as you can see in the images attached.
The Tile 1x is what the tile looks like without zoom - just showing the visible effect it has.
The Tile 5x gives a better illustration of what’s happening.
The Tile Markup shows the Tile 5x with letters. Letter A is pointing to the grayish pixels in the first column - which my guess is blending the color from the right edge and left edge. Letter B is pointing to another set of gray, but darker gray pixels, almost as its interpolating the pixel values between row/column 2 and row column 3. And C shows the similar effect as A, however the bark is now becoming white. The corner is even more white, not sure why.
I have been working with unity as a hobbyist for about 6/7 months. But I really like to understand the innerworkings of why certain mechanisms act the way they do. So if anyone could shed some light on why this happening, that’d be great. Thanks!!
public class DrawTile : MonoBehaviour
{
private int pixelsPerTileSide = 64;
private Color[] newTexturePixelArray;
SpriteRenderer spriteRenderer;
Texture2D newTexture2D;
void Start()
{
spriteRenderer = GetComponent<SpriteRenderer>();
CreatePixelArray();
ApplyArrayToTexture();
}
private void CreatePixelArray()
{
newTexturePixelArray = new Color[pixelsPerTileSide * pixelsPerTileSide];
int i = 0;
for (int pY = 0; pY < pixelsPerTileSide; pY++)
{
for (int pX = 0; pX < pixelsPerTileSide; pX++)
{
if (pY < 2 || pX < 2)
{
newTexturePixelArray[i] = Color.white;
}
else
{
newTexturePixelArray[i] = Color.black;
}
i++;
}
}
}
private void ApplyArrayToTexture()
{
newTexture2D = new Texture2D(pixelsPerTileSide, pixelsPerTileSide);
newTexture2D.SetPixels(newTexturePixelArray);
newTexture2D.Apply();
Sprite worldSprite = Sprite.Create(newTexture2D, new Rect(0, 0, pixelsPerTileSide, pixelsPerTileSide), Vector2.zero);
spriteRenderer.sprite = worldSprite;
}
}
EDIT: This is solved by setting the texture’s filter mode to FilterMode.Point