Crate wgpu_tilemap

source ·
Expand description


wgpu-tilemap is wgpu middleware for GPU-accelerated tilemap rendering, primarily targeted at 2d games.

It draws each tilemap as a single quad, so the vertex count is independent of the size of the tilemap. It uses texture arrays for the tilesets, so the fragment shader is essentially 2 texture loads: one from the tilemap and one from the tileset. It discards fully transparent fragments, so drawing multiple layers can be accelerated with a depth buffer.


// Create a tilemap pipeline
let mut tilemap_pipeline = TilemapPipeline::new(device, surface_config.format, None);

// Specify a camera matrix
tilemap_pipeline.set_camera(queue, FULLSCREEN_QUAD_CAMERA);

// Create/load a tileset
use image::io::Reader as ImageReader;
let tileset_image = ImageReader::open("tileset.png").unwrap().decode().unwrap();  
let tileset = TilesetRef::from_image(&tileset_image, Vec2::new(32, 32));

// Upload a tileset to the GPU
tilemap_pipeline.upload_tilesets(device, queue, &[tileset]);

// Create/load a tilemap
let some_tilemap = TilemapRef::zeroed(Vec2::broadcast(size));

// Upload a tilemap to the GPU
	&[TilemapDrawData {
		transform: Mat4::identity(),
		tilemap: Cow::Borrowed(&some_tilemap),
		tileset: 0,
		noise: TilemapNoise::default(),

// Render the uploaded tilemaps
tilemap_pipeline.render(&device, &mut rpass);


wgpu-tilemap is licensed under the Apache License, Version 2.0, (LICENSE.apache2 or


  • An instruction to draw a tilemap.
  • Apply noise to the tilemap at a multiple of the tile size (e.g. for sand effects). TilemapNoise::default() applies no noise.
  • The entry point to this crate.
  • A reference to tilemap data to be uploaded as a texture and used as indices into the tileset.
  • A reference to tileset data to be uploaded as a texture. This is the image data drawn for each tile of the corresponding tilemap.


  • Camera matrix to scale a tilemap to the whole screen. Maps x and y from [0, 1] to [-1, 1], leaving z and w unchanged.