In my effort to bring tighter integration to the PixiJS ecosystem, I’m upgrading external PixiJS packages and working towards lifting them to the standard of the main project. @pixi/tilemap 3 is the first package in this process. Yes, I’ve republished pixi-tilemap as @pixi/tilemap.
Here, I want to cover the new, leaner API that @pixi/tilemap 3 brings to the table. This package by Ivan Popleyshev gives you an optimized rectangular tilemap implementation you can use to render a background for your game or app composed of tile textures. The documentation is available at https://api.pixijs.io/@pixi/tilemap.html.
Tilesets
A tileset is the set of tile textures used to build the scene. Generally, you’d want the tileset to be in one big base-texture to reduce the number of network requests and improve rendering batch efficiency.
To use @pixi/tilemap, you’ll first need to export a tileset atlas as a sprite sheet. PixiJS’ spritesheet loader populates your tile textures from the sheet’s manifest. If you don’t have one at hand, you can create a sample tileset as follows:
- Download this freebie tileset from CraftPix.net here: https://craftpix.net/download/24818/. You’ll need to signup, however.
- Download and install TexturePacker: https://www.codeandweb.com/texturepacker
- Drag the “PNG” folder of the downloaded tileset into TexturePacker. It will automatically pack all the tiles into one big atlas image.
- Then click on “Publish sprite sheet” and save the manifest.
Tilemap
The Tilemap
class renders a tilemap from a predefined set of base-textures containing the tile textures. Each rendered tile references its base-texture by an index into the tileset
array. This tileset array is first passed when the tilemap is created; however, you can still append base-textures without changing previously added tiles after it is instantiated.
The following example paints a static tilemap from a CraftPix tileset.
The texture passed to tile() must be one of the atlases in the tilemap’s tileset. Otherwise, the tilemap will silently drop the tile. As we’ll discuss later on, CompositeTilemap
can be used to get around this limitation.
Animated Tiles
The options passed to tile
let you animate the rendered tile between different tile textures stored in the same base-texture. The different frames must be located uniformly in a table (or a single row/column).
The texture you pass to tile
will be the first frame. Then the following parameters specify how Tilemap will find other frames:
animX
: The x-offset between frame textures.animY
: The y-offset between frames.animCountX
: The number of frame textures per row of the table. This is 1 by default.animCountY
: The number of frames per column of the table. This is 1 by default.
If your frames are all in a row, you don’t need to specify animY
and animCountY
.
The animation frame vector (tileAnim
) specifies which frame to use for all tiles in the tilemap. tileAnim[0]
specifies the column modulo, and tileAnim[1]
specifies the row modulo. Since it wraps around when the column/row is fully animated, you don’t have to do it yourself.
The above example takes advantage of the fact that some regular doors and wide doors are placed in a row in the sample atlas. animX
, animCountX
are used to animate between them every 500ms.
Tileset limitations
Tilemap
renders all of the tilemap in one draw call. It doesn’t intermediate batches of geometry like PixiJS’ Graphics
. All the tileset base-textures are bound to the GPU together.
This means that there’s a limit to how many tile sprite sheets you can use in each tilemap. WebGL 1 guarantees that at least 8 base-textures can be used together; however, most devices support 16. You can check this limit by evaluating
renderer.gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS)
If your tileset contains more base-textures than this limit, Tilemap
will silently fail to render its scene.
If you’re using only one sprite sheet like the examples above, you don’t need to worry about hitting this limit. Otherwise, CompositeTilemap
is here to help.
CompositeTilemap
A “tilemap composite” will layer tilesets into multiple tilemaps. You don’t need to predefine the base-textures you’re going to use. Instead, it will try to find a tilemap with the same base-texture in its tileset when you add a tile; if none exists, the base-texture is added into a layered tilemap’s tileset. New tilemaps are automatically created when existing ones fill up.
In most cases, you can trivially swap usage of Tilemap
with CompositeTilemap
. However, you have to be careful about z-ordering. The tiles using textures in later tilemaps will always render above. This may become a problem with overlapping tiles in some cases.
The following example uses a CompositeTilemap
to render one of the previous examples. Instead of using a separate Sprite
for the background, it adds the background itself as a tile too.
Tilemap rendering
Geometry
Tilemap internally stores tiles in a geometry buffer, which contains interleaved data for each vertex.
- Position (in local space)
- Texture coordinates
- Texture frame of the tile
- Animation parameters (specially encoded into a 32-bit 2-vector)
- Texture index (into the tileset)
This buffer is mostly static and is lazily updated whenever the tiles are modified between rendering ticks. If the tilemap is left unchanged, the geometry is used directly from graphics memory.
Shader
The TileRenderer
plugin holds the shared tilemap shader.
The vertex program decodes the animation parameters, calculates and passes the texture frame, texture coordinates, and texture index to the fragment program. The animation frame vector is passed as a uniform.
Then, the fragment program samples the texel from the appropriate texture and outputs the pixel.
Textiles
@pixi/tilemap’s settings (which is discussed further on) contains a property called TEXTILE_UNITS
. This is the number of tile base-texture that are “sewn” together when uploaded to the GPU. You can use this to increase the tileset limit per texture.
The “combined” texture is called a textile. The textile is divided into a 2-column table of square slots. Each slot is a square of size TEXTILE_DIMEN
. Your tileset base-textures must be smaller than this dimension for the textile to work.
The following demonstration shows what a textile looks like when uploaded. The textile-tile dimension was set to 256 so that images aren’t spread out too far (it is 1024 by default).
Settings
@pixi/tilemap exports a “settings” object that you should configure before a tilemap is created.
TEXTURES_PER_TILEMAP
: This is the limit of tile base-textures kept in each layer tilemap of a composite. Once the last tilemap is filled to this limit, the next texture will go into a new tilemap.TEXTILE_DIMEN
,TEXTILE_UNITS
: Used to configure textiles. IfTEXTILE_UNITS
is set to 1 (the default), textiles are not used.TEXTILE_SCALE_MODE
: Used to set the scaling mode of the resulting textile textures.use32bitIndex
: This option enables tilemaps’ rendering with more than 16K tiles (64K vertices).DO_CLEAR
: This configures whether textile slots are cleared before the tile textures are uploaded. You can disable this if tile textures “fully” coverTEXTILE_DIMEN
and leave no space for a garbage background to develop.
Canvas support
@pixi/tilemap has a canvas fallback, although it is significantly slower. In the future, I might spin out a @pixi/canvas-tilemap to make this fallback optional.