Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spacing fixes #309

Merged
merged 4 commits into from
Oct 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/tiles-spaced.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 80 additions & 0 deletions examples/spacing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use bevy::{prelude::*, render::texture::ImageSettings};
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simple example based off of the layer example(just to show multiple tiles). The spacing is 8 pixels between each tile on both the X and Y axes.

use bevy_ecs_tilemap::prelude::*;
mod helpers;

fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(Camera2dBundle::default());

let texture_handle: Handle<Image> = asset_server.load("tiles-spaced.png");

let tilemap_size = TilemapSize { x: 32, y: 32 };

// Layer 1
let mut tile_storage = TileStorage::empty(tilemap_size);
let tilemap_entity = commands.spawn().id();

fill_tilemap(
TileTexture(0),
tilemap_size,
TilemapId(tilemap_entity),
&mut commands,
&mut tile_storage,
);

let tile_size = TilemapTileSize { x: 16.0, y: 16.0 };
let grid_size = tile_size.into();

commands
.entity(tilemap_entity)
.insert_bundle(TilemapBundle {
grid_size,
size: tilemap_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle.clone()),
tile_size,
spacing: TilemapSpacing { x: 8.0, y: 8.0 },
transform: get_tilemap_center_transform(&tilemap_size, &grid_size, 0.0),
..Default::default()
});

// Layer 2
let mut tile_storage = TileStorage::empty(tilemap_size);
let tilemap_entity = commands.spawn().id();

fill_tilemap(
TileTexture(2),
tilemap_size,
TilemapId(tilemap_entity),
&mut commands,
&mut tile_storage,
);

commands
.entity(tilemap_entity)
.insert_bundle(TilemapBundle {
grid_size: grid_size,
size: tilemap_size,
storage: tile_storage,
texture: TilemapTexture::Single(texture_handle),
tile_size: TilemapTileSize { x: 16.0, y: 16.0 },
transform: get_tilemap_center_transform(&tilemap_size, &grid_size, 1.0)
* Transform::from_xyz(32.0, 32.0, 0.0),
..Default::default()
});
}

fn main() {
App::new()
.insert_resource(WindowDescriptor {
width: 1270.0,
height: 720.0,
title: String::from("Spacing Example"),
..Default::default()
})
.insert_resource(ImageSettings::default_nearest())
.add_plugins(DefaultPlugins)
.add_plugin(TilemapPlugin)
.add_startup_system(startup)
.add_system(helpers::camera::movement)
.run();
}
3 changes: 2 additions & 1 deletion src/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ impl From<&Vec2> for TilemapGridSize {
}
}

/// Spacing between tiles inside of the texture atlas.
/// Spacing between tiles in pixels inside of the texture atlas.
/// Defaults to 0.0
#[derive(Component, Default, Clone, Copy, Debug)]
pub struct TilemapSpacing {
pub x: f32,
Expand Down
6 changes: 2 additions & 4 deletions src/render/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,8 @@ impl ExtractedTilemapTexture {
it is being extracted as a texture!",
);
let texture_size: TilemapTextureSize = image.size().into();
let tile_count_x =
(texture_size.x + tile_spacing.x) / (tile_size.x + tile_spacing.x);
let tile_count_y =
(texture_size.y + tile_spacing.y) / (tile_size.y + tile_spacing.y);
let tile_count_x = ((texture_size.x) / (tile_size.x + tile_spacing.x)).floor();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to floor here because otherwise you can get tile counts that are larger than what's available in the atlas.

let tile_count_y = ((texture_size.y) / (tile_size.y + tile_spacing.y)).floor();
((tile_count_x * tile_count_y) as u32, texture_size)
}
#[cfg(not(feature = "atlas"))]
Expand Down
21 changes: 11 additions & 10 deletions src/render/texture_array_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,8 @@ impl TextureArrayCache {
it is being extracted as a texture!",
);
let texture_size: TilemapTextureSize = image.size().into();
let tile_count_x =
(texture_size.x + tile_spacing.x) / (tile_size.x + tile_spacing.x);
let tile_count_y =
(texture_size.y + tile_spacing.y) / (tile_size.y + tile_spacing.y);
let tile_count_x = ((texture_size.x) / (tile_size.x + tile_spacing.x)).floor();
let tile_count_y = ((texture_size.y) / (tile_size.y + tile_spacing.y)).floor();
((tile_count_x * tile_count_y) as u32, texture_size)
}
TilemapTexture::Vector(handles) => {
Expand Down Expand Up @@ -206,12 +204,15 @@ impl TextureArrayCache {
});

for i in 0..count {
let columns = (texture_size.x as f32 + spacing.x as f32)
/ (tile_size.x as f32 + spacing.x as f32);
let sprite_sheet_x: f32 =
(i as f32 % columns).floor() * (tile_size.x + spacing.x) as f32;
let sprite_sheet_y: f32 =
(i as f32 / columns).floor() * (tile_size.y + spacing.y) as f32;
let columns = ((texture_size.x as f32)
/ (tile_size.x as f32 + spacing.x as f32))
.floor();
let sprite_sheet_x: f32 = (i as f32 % columns).floor()
* (tile_size.x + spacing.x) as f32
+ spacing.x;
let sprite_sheet_y: f32 = (i as f32 / columns).floor()
* (tile_size.y + spacing.y) as f32
+ spacing.y;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't divide by 2 here. This is because spacing is treated as the "space" between tiles. This is what tiled map editor does and I think it's something we should stick with.


command_encoder.copy_texture_to_texture(
ImageCopyTexture {
Expand Down