Search Unity

Efficiently drawing tilemap on mesh

Discussion in 'Scripting' started by Sendatsu_Yoshimitsu, May 8, 2019.

  1. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    673
    I'd like to render a large 2d world with a tile-based art style, a-la rimworld, escapists, and tons of roguelikes. I don't need logic on the tiles, since collision and pathfinding are handled elsewhere, I just need a visual representation of what a given tile's terrain looks like.

    Right now I'm storing each world as a giant int[][], so I can simply check the array at the desired coordinates, get back the int ID of its terrain type, then query my rendering system for the sprite to be drawn at that ID.

    Right now I'm doing this with Get/SetPixels: I have a reference sprite that's just one big spritesheet with each tile type, and a blank white texture that I redraw at runtime in tileWidth x tileHeight squares, iterating through the int array one coordinate at a time, getting the pixels of that tile, and setting them to the texture at the desired coordinates. By applying the texture to a plane, I can draw thousands of tiles per chunk with negligible rendering cost.

    That kinda-sorta works, but it's really slow and clumsy. I've been playing with a few alternatives, like drawing a custom mesh with vertices cutting the plane into tiles and UV coordinates specifying the tile instead of a manual lookup, or even using a shader, but I have very limited graphics background and am largely throwing random ideas at the wall to see what sticks.

    So is there a standard or recommended way to approach this? It feels like explicitly drawing the texture pixel-by-pixel is way too brute force-y.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    2,211
  3. Sendatsu_Yoshimitsu

    Sendatsu_Yoshimitsu

    Joined:
    May 19, 2014
    Posts:
    673
    I found that unity's implementation isn't a good fit for larger worlds. It's fine if you're doing something like an SNES-style game where you can fake scrolling/screen wipes and otherwise use one screen's worth of tiles to render your entire thing, but I'd like the ability to zoom far out and visualize a large chunk of the world at once. That's trivial if the world is just a bunch of 2d meshes with static textures, but quickly becomes really performance-intensive if you're doing it with tilemaps.
     
unityunity