0
\$\begingroup\$

Good day, I'm trying to generate a tilemap, my current setup is a base tileset, and a few overlays for the different biomes / terrain heights. In my code, I generate each biome's tileset, based off of the base tileset, multiplied by the biome's overlay. However, as you can see in he image, it's reusing the first biome's overlay for all biomes.

Preview of result TileSets and Overlays

Base Tileset Base TileSet

Overlays Overlays

Here's my code:

        for (int i = 0; i < biomes.Length; i++)
        {
            StatusText.text = "Generating Biome["+i+"] Tileset...";
            biomes[i].TileSet = new Sprite[_tileSet.Length]; // Creates the sprite array for the tileset of biome [i].
            Color[] b = biomes[i].Overlay.texture.GetPixels(); // Gets biome[i] overlay in a color array.
            for (int x = 0; x < _tileSet.Length; x++)
            {
                Debug.Log(biomes[i].Name);
                Debug.Log(biomes[i].Overlay);
                Color[] a = _tileSet[x].GetPixels(); // Get's base tile [x] in tileset pixels.
                for (int j = 0; j < a.Length; j++)
                {
                    a[j] *= b[j]; // Multiplies each pixel of the copy of base tile in the new color array with the overlay of biome[i].
                }
                Texture2D tex = new Texture2D(8, 8);
                tex.SetPixels(a);
                tex.filterMode = FilterMode.Point;
                tex.Apply();

                biomes[i].TileSet[x] = Sprite.Create(tex, new Rect(0, 0, 8, 8), new Vector2(4, 4), 8); // Creates sprite from generated texture.
            }
        }
\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Sprite.texture retrieves the base texture off of which the sprite is based, so in this case instead of getting the overlay sprite, it gets the entire overlay texture. It's easily solved retrieving the pixel data directly from the base texture.

Here is the edit

overlays = new Texture2D[biomes.Length];
    for (int i = 0; i < biomes.Length; i++)
    {
        overlays[i] = new Texture2D(tileSize.x, tileSize.y);
        overlays[i].filterMode = FilterMode.Point;
        overlays[i].wrapMode = TextureWrapMode.Clamp;
        for (int x = 0; x < tileSize.x; x++)
        {
            for (int y = 0; y < tileSize.y; y++)
            {
                int Y = tileSize.y - y;
                overlays[i].SetPixel(x, y, biomes[i].Overlay.texture.GetPixel(x, y + i * tileSize.y));
            }
        }
    }
    for (int i = 0; i < biomes.Length; i++)
    {
        StatusText.text = "Generating Biome["+i+"] Tileset...";
        biomes[i].TileSet = new Sprite[_tileSet.Length];
        for (int x = 0; x < _tileSet.Length; x++)
        {
            Color[] b = overlays[i].GetPixels();
            Color[] a = _tileSet[x].GetPixels();
            for (int j = 0; j < a.Length; j++)
            {
                a[j] *= b[j];
            }
            Texture2D tex = new Texture2D(tileSize.x, tileSize.y);
            tex.SetPixels(a);
            tex.filterMode = FilterMode.Point;
            tex.wrapMode = TextureWrapMode.Clamp;
            tex.Apply();

            biomes[i].TileSet[x] = Sprite.Create(tex,new Rect(0,0,tex.width,tex.height),new Vector2(.5f,.5f), Mathf.Min(tex.width,tex.height));
        }
    }
\$\endgroup\$
0

You must log in to answer this question.

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