Skip to content

Commit

Permalink
All map level info getting pushed
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescarterbell committed Jan 5, 2024
1 parent df38c85 commit 261e767
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 149 deletions.
15 changes: 11 additions & 4 deletions crates/bevy_tiles_render/examples/hello_tile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ use bevy::{
app::{App, Startup},
core_pipeline::core_2d::Camera2dBundle,
ecs::system::Commands,
math::Vec3,
transform::{components::Transform, TransformBundle},
math::{Quat, Vec3},
transform::{
components::{GlobalTransform, Transform},
TransformBundle,
},
DefaultPlugins,
};
use bevy_tiles::{commands::TileCommandExt, maps::TileMapLabel, TilesPlugin};
use bevy_tiles_render::{
maps::{TileMapRenderer, TileMapRenderingBundle},
maps::{TileGridSize, TileMapRenderer, TileMapRenderingBundle, TileSize},
TilesRenderPlugin,
};

Expand All @@ -28,7 +31,11 @@ impl TileMapLabel for GameLayer {

fn spawn(mut commands: Commands) {
let mut tile_commands = commands.tiles::<GameLayer, 2>();
tile_commands.spawn_map(TileMapRenderingBundle::default());
tile_commands.spawn_map(TileMapRenderingBundle {
tile_size: TileSize(16.0),
grid_size: TileGridSize(24.0),
..Default::default()
});

for i in -10..10 {
tile_commands.spawn_tile([0, 10 * i], ());
Expand Down
121 changes: 77 additions & 44 deletions crates/bevy_tiles_render/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use bevy::{
math::{Affine3, Vec2, Vec4},
render::{
render_resource::{
BindGroup, BindGroupLayout, BindGroupLayoutDescriptor, BindGroupLayoutEntry,
BindingType, Buffer, BufferBindingType, BufferInitDescriptor, BufferUsages, ShaderSize,
ShaderStages, ShaderType, UniformBuffer,
BindGroup, BindGroupEntries, BindGroupLayout, BindGroupLayoutDescriptor,
BindGroupLayoutEntry, BindingType, Buffer, BufferBindingType, BufferInitDescriptor,
BufferUsages, ShaderSize, ShaderStages, ShaderType, UniformBuffer,
},
renderer::{RenderDevice, RenderQueue},
},
transform::components::GlobalTransform,
};

use crate::buffer_helpers::*;
use crate::{buffer_helpers::*, maps::internal::MapInfo};

#[derive(Component)]
pub struct ChunkBatchBindGroups {
Expand All @@ -23,32 +23,86 @@ pub struct ChunkBatchBindGroups {
}

#[derive(Component)]
pub struct MapChunkSizeBuffer(pub UniformBuffer<u32>);
pub struct MapBatchBuffer {
chunk_size: UniformBuffer<u32>,
tile_size: UniformBuffer<f32>,
grid_size: UniformBuffer<f32>,
transform: UniformBuffer<MapTransformUniform>,
}

impl MapChunkSizeBuffer {
pub fn new(map_chunk_size: &u32) -> Self {
let mut map_chunk_size_buffer = UniformBuffer::<u32>::default();
map_chunk_size_buffer.set(*map_chunk_size);
Self(map_chunk_size_buffer)
impl MapBatchBuffer {
pub fn new(map_info: &MapInfo) -> Self {
Self {
chunk_size: map_info.chunk_size.into(),
tile_size: map_info.tile_size.0.into(),
grid_size: map_info.grid_size.0.into(),
transform: MapTransformUniform::from(&map_info.transform).into(),
}
}

pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) {
self.0.write_buffer(device, queue);
self.chunk_size.write_buffer(device, queue);
self.transform.write_buffer(device, queue);
self.tile_size.write_buffer(device, queue);
self.grid_size.write_buffer(device, queue);
}
}

#[derive(Component)]
pub struct MapTransformUniformBuffer(pub UniformBuffer<MapTransformUniform>);

impl MapTransformUniformBuffer {
pub fn new(map_transform: &MapTransformUniform) -> Self {
let mut map_transform_buffer = UniformBuffer::<MapTransformUniform>::default();
map_transform_buffer.set(map_transform.clone());
Self(map_transform_buffer)
pub fn bindings(&self) -> BindGroupEntries<4> {
BindGroupEntries::with_indices((
(0, self.transform.binding().unwrap()),
(1, self.chunk_size.binding().unwrap()),
(2, self.tile_size.binding().unwrap()),
(3, self.grid_size.binding().unwrap()),
))
}

pub fn write_buffer(&mut self, device: &RenderDevice, queue: &RenderQueue) {
self.0.write_buffer(device, queue);
pub fn layout_entries() -> Vec<BindGroupLayoutEntry> {
vec![
// transform
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX_FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: Some(MapTransformUniform::SHADER_SIZE),
},
count: None,
},
// chunk_size
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::VERTEX_FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: Some(u32::SHADER_SIZE),
},
count: None,
},
// tile_size
BindGroupLayoutEntry {
binding: 2,
visibility: ShaderStages::VERTEX_FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: Some(f32::SHADER_SIZE),
},
count: None,
},
// grid_size
BindGroupLayoutEntry {
binding: 3,
visibility: ShaderStages::VERTEX_FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: Some(f32::SHADER_SIZE),
},
count: None,
},
]
}
}

Expand Down Expand Up @@ -89,28 +143,7 @@ impl FromWorld for ChunkBatchBindGroupLayouts {

let map_layouts = device.create_bind_group_layout(&BindGroupLayoutDescriptor {
label: Some("bevy_tiles_map_bind_group"),
entries: &[
BindGroupLayoutEntry {
binding: 0,
visibility: ShaderStages::VERTEX_FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: Some(MapTransformUniform::SHADER_SIZE),
},
count: None,
},
BindGroupLayoutEntry {
binding: 1,
visibility: ShaderStages::VERTEX_FRAGMENT,
ty: BindingType::Buffer {
ty: BufferBindingType::Uniform,
has_dynamic_offset: false,
min_binding_size: Some(u32::SHADER_SIZE),
},
count: None,
},
],
entries: &MapBatchBuffer::layout_entries(),
});

let chunk_layouts = device.create_bind_group_layout(&BindGroupLayoutDescriptor {
Expand Down
13 changes: 7 additions & 6 deletions crates/bevy_tiles_render/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ use bevy::{
sprite::SetMesh2dViewBindGroup,
};

use crate::{
bindings::ChunkBatchBindGroups, chunk::internal::BatchSize, maps::internal::ChunkSize,
};
use crate::{bindings::ChunkBatchBindGroups, chunk::internal::BatchSize, maps::internal::MapInfo};

pub type DrawChunks = (
SetItemPipeline,
Expand Down Expand Up @@ -103,17 +101,20 @@ impl RenderCommand<Transparent2d> for DrawChunkBatch {

type ViewWorldQuery = ();

type ItemWorldQuery = (Read<ChunkSize>, Read<BatchSize>);
type ItemWorldQuery = (Read<MapInfo>, Read<BatchSize>);

#[inline]
fn render<'w>(
item: &Transparent2d,
_view: ROQueryItem<'w, Self::ViewWorldQuery>,
(chunk_size, batch_size): ROQueryItem<'w, Self::ItemWorldQuery>,
(map_info, batch_size): ROQueryItem<'w, Self::ItemWorldQuery>,
_: SystemParamItem<'w, '_, Self::Param>,
pass: &mut TrackedRenderPass<'w>,
) -> RenderCommandResult {
pass.draw(0..(chunk_size.0 * chunk_size.0 * 6), 0..**batch_size);
pass.draw(
0..(map_info.chunk_size * map_info.chunk_size * 6),
0..**batch_size,
);
RenderCommandResult::Success
}
}
12 changes: 6 additions & 6 deletions crates/bevy_tiles_render/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use bevy_tiles::{

use crate::{
chunk::internal::ExtractedTileInstances,
maps::{internal::ChunkSize, TileGridSize, TileMapRenderer, TileSize},
maps::{internal::MapInfo, TileGridSize, TileMapRenderer, TileSize},
};

pub fn extract_chunks(
Expand All @@ -42,13 +42,13 @@ pub fn extract_chunks(
let grid_size = grid_size.cloned().unwrap_or_default();
extracted_maps.push((
map_id,
(
transform,
ChunkSize(map.chunk_size as u32),
renderer.clone(),
MapInfo {
chunk_size: map.chunk_size as u32,
tile_map_renderer: renderer.clone(),
tile_size,
grid_size,
),
transform,
},
));
}
commands.insert_or_spawn_batch(extracted_maps);
Expand Down
14 changes: 11 additions & 3 deletions crates/bevy_tiles_render/src/maps/internal.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use bevy::{ecs::component::Component, prelude::Deref};
use bevy::{ecs::component::Component, prelude::Deref, transform::components::GlobalTransform};

#[derive(Deref, Clone, Default, Component)]
pub struct ChunkSize(pub u32);
use super::{TileGridSize, TileMapRenderer, TileSize};

#[derive(Clone, Component)]
pub struct MapInfo {
pub chunk_size: u32,
pub tile_map_renderer: TileMapRenderer,
pub tile_size: TileSize,
pub grid_size: TileGridSize,
pub transform: GlobalTransform,
}
13 changes: 7 additions & 6 deletions crates/bevy_tiles_render/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use bevy::{
ecs::{bundle::Bundle, component::Component},
prelude::Deref,
transform::components::GlobalTransform,
transform::components::{GlobalTransform, Transform},
};

pub(crate) mod internal;
Expand All @@ -13,7 +13,8 @@ pub struct TileMapRenderingBundle {
pub tile_map_renderer: TileMapRenderer,
pub tile_size: TileSize,
pub grid_size: TileGridSize,
pub transform: GlobalTransform,
pub transform: Transform,
pub global_transform: GlobalTransform,
}

/// Marks a tilemap as renderable, without this it cannot be rendered.
Expand All @@ -30,24 +31,24 @@ impl Default for TileMapRenderer {

/// The size of a tile in pixels.
#[derive(Clone, Deref, Component)]
pub struct TileSize(pub u32);
pub struct TileSize(pub f32);

/// Defaults to 16 pixels
impl Default for TileSize {
fn default() -> Self {
Self(16)
Self(16.0)
}
}
/// The size of a tile grid in pixels.
/// # Example
/// A [`TileSize`] of 16 with a [`GridSize`] of 18 would lead to a 2 pixel gap between tiles.
/// A [`TileSize`] of 16 with a [`GridSize`] of 14 would lead to a 2 pixel overlap between tiles.
#[derive(Clone, Deref, Component)]
pub struct TileGridSize(pub u32);
pub struct TileGridSize(pub f32);

/// Defaults to 16 pixels
impl Default for TileGridSize {
fn default() -> Self {
Self(16)
Self(16.0)
}
}
Loading

0 comments on commit 261e767

Please sign in to comment.