Skip to content

Commit

Permalink
Preparing atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Dec 17, 2024
1 parent 3fb05a6 commit 2d18deb
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ regex = "1"
num_cpus = "1.16.0"
half = { version = "2.4", features = ["serde"] }
# rusterix = "0.1.0"
#rusterix = { path = "../../rusterix" }
# rusterix = { path = "../../rusterix" }
rusterix = { git = "https://github.com/markusmoenig/Rusterix" }

vek = { version = "0.17", default-features = false, features = ["rgba"] }
Expand Down
77 changes: 75 additions & 2 deletions shared/src/maprender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use vek::*;

pub struct MapRender {
pub textures: FxHashMap<Uuid, TheRGBATile>,
pub tiles: FxHashMap<Uuid, rusterix::Tile>,
tiles: FxHashMap<Uuid, rusterix::Tile>,
atlas: rusterix::Texture,

atlas_size: f32,
sampler: Option<Tiled<Nearest<RgbaTexture>>>,
Expand All @@ -28,6 +29,7 @@ impl MapRender {
Self {
textures: FxHashMap::default(),
tiles: FxHashMap::default(),
atlas: rusterix::Texture::default(),

atlas_size: 1.0,
sampler: None,
Expand Down Expand Up @@ -60,6 +62,73 @@ impl MapRender {
pub fn set_region(&mut self, _region: &Region) {}

pub fn set_textures(&mut self, textures: FxHashMap<Uuid, TheRGBATile>) {
let atlas_size = 1024;

let mut packer = Packer::new(Config {
width: atlas_size,
height: atlas_size,
border_padding: 0,
rectangle_padding: 0,
});

let mut tiles: FxHashMap<Uuid, rusterix::Tile> = FxHashMap::default();
let mut elements: FxHashMap<Uuid, Vec<vek::Vec4<i32>>> = FxHashMap::default();

for (id, t) in textures.iter() {
let mut array: Vec<vek::Vec4<i32>> = vec![];
let mut texture_array: Vec<rusterix::Texture> = vec![];
for b in &t.buffer {
if let Some(rect) = packer.pack(b.dim().width, b.dim().height, false) {
array.push(vek::Vec4::new(rect.x, rect.y, rect.width, rect.height));
}

let texture = rusterix::Texture::new(
b.pixels().to_vec(),
b.dim().width as usize,
b.dim().height as usize,
);
texture_array.push(texture);
}
let tile = rusterix::Tile {
id: t.id,
name: t.name.clone(),
role: t.role,
blocking: t.blocking,
billboard: t.billboard,
uvs: array.clone(),
textures: texture_array.clone(),
};
elements.insert(*id, array);
tiles.insert(*id, tile);

// texture_sampler.insert(*id, sammpler_array);
}

// Create atlas
let mut atlas = vec![0; atlas_size as usize * atlas_size as usize * 4];

// Copy textures into atlas
for (id, tile) in textures.iter() {
if let Some(rects) = elements.get(id) {
for (buffer, rect) in tile.buffer.iter().zip(rects) {
let width = buffer.dim().width as usize;
let height = buffer.dim().height as usize;
let rect_x = rect.x as usize;
let rect_y = rect.y as usize;

for y in 0..height {
for x in 0..width {
let src_index = (y * width + x) * 4;
let dest_index =
((rect_y + y) * atlas_size as usize + (rect_x + x)) * 4;

atlas[dest_index..dest_index + 4]
.copy_from_slice(&buffer.pixels()[src_index..src_index + 4]);
}
}
}
}
}
/*
let atlas_size = 1024;
let mut packer = Packer::new(Config {
Expand Down Expand Up @@ -126,6 +195,9 @@ impl MapRender {
self.texture_sampler = texture_sampler;
*/
self.atlas = rusterix::Texture::new(atlas, atlas_size as usize, atlas_size as usize);
self.atlas_size = atlas_size as f32;
self.tiles = tiles;
self.textures = textures;
}

Expand Down Expand Up @@ -181,7 +253,7 @@ impl MapRender {
// let render_settings_params: Vec<Vec<f32>> = region.regionfx.load_parameters(&settings.time);

if let Some(server_ctx) = server_ctx {
let mut drawer = EucDraw::new(width, height);
let drawer = EucDraw::new(width, height);

if region.map.camera == MapCamera::TwoD {
/*
Expand Down Expand Up @@ -229,6 +301,7 @@ impl MapRender {
let mut scene = builder.build(
&region.map,
tiles,
self.atlas.clone(),
vek::Vec2::new(width as f32, height as f32),
);

Expand Down

0 comments on commit 2d18deb

Please sign in to comment.