wgpu-tilemap

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

2 releases

0.1.1 Sep 26, 2023
0.1.0 Sep 21, 2023

#175 in Rendering

Apache-2.0

37KB
769 lines

wgpu-tilemap

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.

Example

// 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
self.tilemap_pipeline.upload_tilemaps(
	device,
	queue,
	&[TilemapDrawData {
		transform: Mat4::identity(),
		tilemap: Cow::Borrowed(&some_tilemap),
		tileset: 0,
		noise: TilemapNoise::default(),
	}],
);

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

License

wgpu-tilemap is licensed under the Apache License, Version 2.0, (LICENSE.apache2 or https://www.apache.org/licenses/LICENSE-2.0)

Dependencies

~3–38MB
~530K SLoC