Skip to content

Commit

Permalink
add: tiles atlas generation with cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Sufhal committed Sep 21, 2024
1 parent b6e0e93 commit 3fa3c49
Show file tree
Hide file tree
Showing 24 changed files with 30 additions and 3 deletions.
Binary file added assets/pack/map/c1/000000/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/000001/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/000002/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/000003/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/000004/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/001000/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/001001/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/001002/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/001003/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/001004/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/002000/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/002001/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/002002/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/002003/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/002004/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/003000/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/003001/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/003002/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/003003/tiles_atlas.raw
Binary file not shown.
Binary file added assets/pack/map/c1/003004/tiles_atlas.raw
Binary file not shown.
2 changes: 1 addition & 1 deletion assets/pack/property/properties.json

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/modules/conversion/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fs;
use std::path::Path;
use image::GrayImage;
use crate::modules::conversion::common::write;
use crate::modules::conversion::utils::generate_tiles_atlas;
use crate::modules::terrain::areadata::AreaData;
use crate::modules::terrain::texture_set::{ChunkTextureSet, TextureSet};

Expand Down Expand Up @@ -191,6 +192,7 @@ pub fn convert_maps() {
colors.remove(colors.iter().position(|(v, _)| *v == color_to_merge).unwrap());
}

let mut tiles = Vec::new();
for i in 0..colors.len() {
let (index, _) = colors.get(i).unwrap();
let alpha_map = tile_indices
Expand All @@ -210,8 +212,13 @@ pub fn convert_maps() {
&format!("{}/tile_{i}.raw", element.path().to_str().unwrap()),
blurred_alpha_map.to_vec()
);

tiles.push(blurred_alpha_map);
}
let tiles_atlas = generate_tiles_atlas(tiles);
write(
&format!("{}/tiles_atlas.raw", element.path().to_str().unwrap()),
tiles_atlas.to_vec()
);

write(
&format!("{}/textureset.json", element.path().to_str().unwrap()),
Expand Down
3 changes: 2 additions & 1 deletion src/modules/conversion/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod maps;
pub mod common;
pub mod property;
pub mod environment;
pub mod environment;
pub mod utils;
19 changes: 19 additions & 0 deletions src/modules/conversion/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use image::{ImageBuffer, Luma};

pub fn generate_tiles_atlas(tiles: Vec<ImageBuffer<Luma<u8>, Vec<u8>>>) -> ImageBuffer<Luma<u8>, Vec<u8>> {
const TILE_SIZE: u32 = 256;
const ATLAS_SIZE: u32 = TILE_SIZE * 3;
let mut atlas = ImageBuffer::new(ATLAS_SIZE, ATLAS_SIZE);
let tile_size = 256;
let tiles_per_row = ATLAS_SIZE / tile_size;
for (index, tile) in tiles.iter().enumerate() {
let x = (index as u32 % tiles_per_row) * tile_size;
let y = (index as u32 / tiles_per_row) * tile_size;
for (tile_x, tile_y, pixel) in tile.enumerate_pixels() {
if x + tile_x < ATLAS_SIZE && y + tile_y < ATLAS_SIZE {
atlas.put_pixel(x + tile_x, y + tile_y, *pixel);
}
}
}
atlas
}

0 comments on commit 3fa3c49

Please sign in to comment.