Hello Guest

Author Topic: How to apply darkness to tilemaps via code?  (Read 5024 times)

Quells122

  • Newbie
  • *
  • Posts: 18
    • View Profile
How to apply darkness to tilemaps via code?
« on: September 29, 2014, 03:35:59 am »
I'm using the tilemap class and constructing levels during run time since some levels are randomly generated. I've had success constructing the tilemaps using tilemap.SetTile(), and then using tilemap.Build().

However, now I'd like to have darkness in the scene and I'd like to have the tilemaps react to lights. It looks like tilemap has the functionality (according to this tutorial: http://2dtoolkit.com/docs/latest/tilemap/tutorial.html) but I am confused as to how this can be done via code. Help would be greatly appreciated!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to apply darkness to tilemaps via code?
« Reply #1 on: September 29, 2014, 11:12:03 am »
That is vertex painting. You'll have to roll your own "lights" by accumulating them to the vertex colors.
Code: [Select]
tilemap.ColorChannel.SetColor(tileX, tileY, Color)
You'll still need to calculate the falloffs, add the colors yourself, etc. but its doable.

Quells122

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to apply darkness to tilemaps via code?
« Reply #2 on: September 29, 2014, 07:05:40 pm »
Sorry, can you explain in more detail how this can be done? I've tried using tilemap.ColorChannel.SetColor(tileX, tileY, Color), but I keep getting DivideByZeroExceptions. The culprit is "divX" inside SetColor function of tk2dTileMapChunks script. I've tried setting the color channel before and after my build call, but to no avail.

This is how I draw to the tilemap currently.

Code: [Select]
    private void _DrawTileLayer(string zlib_64_data, int layer_index) {
        byte[] raw_data = Convert.FromBase64String(zlib_64_data);
        Stream data_s = new MemoryStream(raw_data, false);
        Stream data = new Ionic.Zlib.ZlibStream(data_s, Ionic.Zlib.CompressionMode.Decompress, false);
        using (var binary_reader = new BinaryReader(data)) {
            int tile_map_height_index;
            for (int h = 0; h < curr_level_height; h++) {
                tile_map_height_index = tile_map.height - 1 - h; // Some math. TMX counts tiles with top == 0. 2DTK counts with bottom == 0
                for (int w = 0; w < curr_level_width; w++) {
                    uint tile_gid = binary_reader.ReadUInt32();
                    if (tile_gid == 0) continue;
                    tile_map.SetTile(w, tile_map_height_index, layer_index, (int)tile_gid - 1);
                    //tile_map.ColorChannel.SetColor(w, tile_map_height_index, Color.blue); Here, this line causes a DivideByZero exception
                    tile_map.SetTileFlags(w, tile_map_height_index, layer_index, TILE_FLAGS_MAP[tile_gid >> 29]);
                }
            }
        }
    }
After which I call tilemap.Build(). Can you also explain what it means to calculate falloffs?

Also is "vertex painting" the only method to make the tilemaps react to lights? Or is there some other preferred way? Thanks!

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How to apply darkness to tilemaps via code?
« Reply #3 on: September 30, 2014, 10:56:42 am »
You should call tilemap.CreateColorChannel() before doing all of that. I don't think it'll have the color channels in place otherwise.

By calculating falloffs I mean the falloff from the light. (http://www.angelosimonecrociani.com/media/2012/05/inversesquare.jpg)

Quote
Also is "vertex painting" the only method to make the tilemaps react to lights? Or is there some other preferred way? Thanks!
Vertex painting lights gives you unlimited static lights for nearly free. If all you want is a few normal lights or you want dynamic lights, that isn't the best way to go about it - you could use normal Unity lights instead. Be sure to have normal generation ticked in the sprite collection editor if you wanna do that.

Quells122

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: How to apply darkness to tilemaps via code?
« Reply #4 on: October 01, 2014, 04:50:23 am »
Thanks for the help! I think I was confused, but I understand now. I was actually thinking of dynamic lights, however I can see some useful functionality coming out of vertex painting so I'm glad I asked :)

For posterity sake, if someone is after dynamic lights, I used these two topics:
http://2dtoolkit.com/forum/index.php?topic=1422.0
http://2dtoolkit.com/forum/index.php?topic=1080.0