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

Added support for custom texture formats. #335

Merged
merged 1 commit into from
Nov 4, 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/hex-tile-0.ktx2
Binary file not shown.
Binary file added assets/hex-tile-1.ktx2
Binary file not shown.
Binary file added assets/hex-tile-2.ktx2
Binary file not shown.
9 changes: 7 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use bevy::{prelude::*, render::texture::ImageSettings};
use bevy::{
prelude::*,
render::texture::{BevyDefault, ImageSettings},
};
use bevy_ecs_tilemap::prelude::*;

mod helpers;
Expand Down Expand Up @@ -66,7 +69,9 @@ fn startup(
array_texture_loader.add(TilemapArrayTexture {
texture: TilemapTexture::Single(asset_server.load("tiles.png")),
tile_size,
..Default::default()
format: BevyDefault::bevy_default(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm, these all seem to be using default? Is this not covered by ..Default::default()?

tile_spacing: Default::default(),
filter: Default::default(),
});
}
}
Expand Down
12 changes: 10 additions & 2 deletions examples/texture_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const MAP_CENTER: TilePos = TilePos {
x: MAP_RADIUS + 1,
y: MAP_RADIUS + 1,
};
const TILE_SIZE: TilemapTileSize = TilemapTileSize { x: 48.0, y: 54.0 };
// const TILE_SIZE: TilemapTileSize = TilemapTileSize { x: 48.0, y: 54.0 };
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we just remove this? Why did the TilemapTileSize change?

const TILE_SIZE: TilemapTileSize = TilemapTileSize { x: 48.0, y: 56.0 };
const COORD_SYS: HexCoordSystem = HexCoordSystem::Row;

fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
Expand All @@ -20,7 +21,14 @@ fn startup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Most of the work is happening bevy side. In this case, using the `ktx2` feature. If this
// feature is not turned on, that the image won't properly be interpreted as a texture
// container. The other alternative is `dds`.
let texture_vec = TilemapTexture::TextureContainer(asset_server.load("hex-tiles.ktx2"));
// let texture_vec = TilemapTexture::TextureContainer(asset_server.load("hex-tiles.ktx2"));

// Optionally you can also load in KTX2's as regular single textures:
let texture_vec = TilemapTexture::Vector(vec![
asset_server.load("hex-tile-0.ktx2"),
asset_server.load("hex-tile-1.ktx2"),
asset_server.load("hex-tile-2.ktx2"),
]);

let map_size = TilemapSize {
x: MAP_DIAMETER,
Expand Down
6 changes: 4 additions & 2 deletions src/array_texture_preload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ use crate::{
prelude::{TilemapSpacing, TilemapTileSize},
TilemapTexture,
};
use bevy::render::render_resource::FilterMode;
use bevy::render::render_resource::{FilterMode, TextureFormat};
#[cfg(feature = "render")]
use bevy::{
prelude::{Assets, Image, Res, ResMut},
render::{texture::ImageSettings, Extract},
};
use std::sync::{Arc, RwLock};

#[derive(Default, Debug, Clone)]
#[derive(Debug, Clone)]
pub struct TilemapArrayTexture {
pub texture: TilemapTexture,
pub tile_size: TilemapTileSize,
pub tile_spacing: TilemapSpacing,
/// Defaults to ImageSettings.
pub filter: Option<FilterMode>,
pub format: TextureFormat,
}

/// A bevy world resource that allows you to add atlas textures for
Expand Down Expand Up @@ -63,6 +64,7 @@ pub(crate) fn extract(
array_texture.tile_size,
array_texture.tile_spacing,
default_image_settings.default_sampler.min_filter,
array_texture.format,
&images,
);
} else {
Expand Down
27 changes: 24 additions & 3 deletions src/render/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bevy::prelude::Res;
use bevy::prelude::Time;
use bevy::render::primitives::{Aabb, Frustum};
use bevy::render::render_resource::FilterMode;
use bevy::render::render_resource::TextureFormat;
use bevy::render::texture::ImageSettings;
use bevy::{math::Vec4, prelude::*, render::Extract, utils::HashMap};

Expand Down Expand Up @@ -78,6 +79,7 @@ pub(crate) struct ExtractedTilemapTexture {
pub tile_count: u32,
pub texture: TilemapTexture,
pub filtering: FilterMode,
pub format: TextureFormat,
}

impl ExtractedTilemapTexture {
Expand All @@ -89,7 +91,7 @@ impl ExtractedTilemapTexture {
filtering: FilterMode,
image_assets: &Res<Assets<Image>>,
) -> ExtractedTilemapTexture {
let (tile_count, texture_size) = match &texture {
let (tile_count, texture_size, format) = match &texture {
TilemapTexture::Single(handle) => {
let image = image_assets.get(handle).expect(
"Expected image to have finished loading if \
Expand All @@ -98,7 +100,11 @@ impl ExtractedTilemapTexture {
let texture_size: TilemapTextureSize = image.size().into();
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)
(
(tile_count_x * tile_count_y) as u32,
texture_size,
image.texture_descriptor.format,
)
}
#[cfg(not(feature = "atlas"))]
TilemapTexture::Vector(handles) => {
Expand All @@ -116,7 +122,20 @@ impl ExtractedTilemapTexture {
);
}
}
(handles.len() as u32, tile_size.into())
let first_format = image_assets
.get(handles.first().unwrap())
.unwrap()
.texture_descriptor
.format;

for handle in handles {
let image = image_assets.get(handle).unwrap();
if image.texture_descriptor.format != first_format {
panic!("Expected all provided image assets to have a format of: {:?} but found image with format: {:?}", first_format, image.texture_descriptor.format);
}
}

(handles.len() as u32, tile_size.into(), first_format)
}
#[cfg(not(feature = "atlas"))]
TilemapTexture::TextureContainer(image_handle) => {
Expand All @@ -128,6 +147,7 @@ impl ExtractedTilemapTexture {
(
image.texture_descriptor.array_layer_count(),
tile_size.into(),
image.texture_descriptor.format,
)
}
};
Expand All @@ -140,6 +160,7 @@ impl ExtractedTilemapTexture {
filtering,
tile_count,
texture_size,
format,
}
}
}
Expand Down
25 changes: 18 additions & 7 deletions src/render/texture_array_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use bevy::{
TextureFormat, TextureUsages, TextureViewDescriptor, TextureViewDimension,
},
renderer::{RenderDevice, RenderQueue},
texture::{BevyDefault, GpuImage},
texture::GpuImage,
},
utils::{HashMap, HashSet},
};
Expand All @@ -28,6 +28,7 @@ pub struct TextureArrayCache {
TilemapTextureSize,
TilemapSpacing,
FilterMode,
TextureFormat,
),
>,
prepare_queue: HashSet<TilemapTexture>,
Expand All @@ -50,6 +51,7 @@ impl TextureArrayCache {
extracted_texture.texture_size,
extracted_texture.tile_spacing,
extracted_texture.filtering,
extracted_texture.format,
),
);
self.prepare_queue
Expand All @@ -64,6 +66,7 @@ impl TextureArrayCache {
tile_size: TilemapTileSize,
tile_spacing: TilemapSpacing,
filtering: FilterMode,
format: TextureFormat,
image_assets: &Res<Assets<Image>>,
) {
let (tile_count, texture_size) = match &texture {
Expand Down Expand Up @@ -110,7 +113,14 @@ impl TextureArrayCache {
if !self.meta_data.contains_key(&texture) {
self.meta_data.insert(
texture.clone_weak(),
(tile_count, tile_size, texture_size, tile_spacing, filtering),
(
tile_count,
tile_size,
texture_size,
tile_spacing,
filtering,
format,
),
);
self.prepare_queue.insert(texture.clone_weak());
}
Expand All @@ -134,7 +144,8 @@ impl TextureArrayCache {
for texture in prepare_queue.iter() {
match texture {
TilemapTexture::Single(_) | TilemapTexture::Vector(_) => {
let (count, tile_size, _, _, filter) = self.meta_data.get(texture).unwrap();
let (count, tile_size, _, _, filter, format) =
self.meta_data.get(texture).unwrap();

// Fixes weird cubemap bug.
let count = if *count == 6 { count + 1 } else { *count };
Expand All @@ -149,7 +160,7 @@ impl TextureArrayCache {
mip_level_count: 1,
sample_count: 1,
dimension: TextureDimension::D2,
format: TextureFormat::Rgba8UnormSrgb,
format: *format,
usage: TextureUsages::COPY_DST | TextureUsages::TEXTURE_BINDING,
});

Expand Down Expand Up @@ -180,7 +191,7 @@ impl TextureArrayCache {
});

let gpu_image = GpuImage {
texture_format: TextureFormat::bevy_default(),
texture_format: *format,
texture: gpu_texture,
sampler,
texture_view,
Expand Down Expand Up @@ -220,7 +231,7 @@ impl TextureArrayCache {
continue;
};

let (count, tile_size, texture_size, spacing, _) =
let (count, tile_size, texture_size, spacing, _, _) =
self.meta_data.get(texture).unwrap();
let array_gpu_image = self.textures.get(texture).unwrap();
let count = *count;
Expand Down Expand Up @@ -284,7 +295,7 @@ impl TextureArrayCache {
}
}

let (count, tile_size, _, _, _) = self.meta_data.get(texture).unwrap();
let (count, tile_size, _, _, _, _) = self.meta_data.get(texture).unwrap();
let array_gpu_image = self.textures.get(texture).unwrap();
let count = *count;

Expand Down