Skip to content

Commit

Permalink
Merge pull request #408 from StarArawn/fix-z-index
Browse files Browse the repository at this point in the history
Fixed issues with z-index by hiding the y-sort behind a flag.
  • Loading branch information
StarArawn authored Mar 28, 2023
2 parents 05ec72e + 0f5da8d commit d15f332
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/3d_iso.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn main() {
// Map size is 12x12 so we'll have render chunks that are:
// 12 tiles wide and 1 tile tall.
render_chunk_size: UVec2::new(3, 1),
y_sort: true,
})
.add_plugins(
DefaultPlugins
Expand Down
1 change: 1 addition & 0 deletions examples/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ fn main() {
)
.insert_resource(TilemapRenderSettings {
render_chunk_size: UVec2::new(256, 256),
..Default::default()
})
.add_plugin(LogDiagnosticsPlugin::default())
.add_plugin(FrameTimeDiagnosticsPlugin::default())
Expand Down
1 change: 1 addition & 0 deletions examples/chunking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ fn main() {
// `TilemapRenderSettings` be added before the `TilemapPlugin`.
.insert_resource(TilemapRenderSettings {
render_chunk_size: RENDER_CHUNK_SIZE,
..Default::default()
})
.add_plugin(TilemapPlugin)
.insert_resource(ChunkManager::default())
Expand Down
7 changes: 7 additions & 0 deletions src/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ pub struct TilemapRenderSettings {
///
/// Smaller chunk sizes will benefit tilemaps which change frequently.
pub render_chunk_size: UVec2,
/// If true, uses the chunk's `z` and `y` values when sorting during rendering.
///
/// When using this option with layered tilemaps, `z` values for layers should be separated by
/// at least `1.0` units.
///
/// `render_chunk_size`'s `z` value should be `1` when using this for 3d isometric tilemaps.
pub y_sort: bool,
}

/// A component which stores a reference to the tilemap entity.
Expand Down
14 changes: 11 additions & 3 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ impl RenderChunkSize {
}
}

/// Sorts chunks using Y sort during render.
///
/// Initialized from [`TilemapRenderSettings`](crate::map::TilemapRenderSettings) resource, if
/// provided. Otherwise, defaults to false.
#[derive(Resource, Debug, Copy, Clone, Deref)]
pub struct RenderYSort(bool);

pub struct TilemapRenderingPlugin;

#[derive(Resource, Default, Deref, DerefMut)]
Expand Down Expand Up @@ -123,10 +130,10 @@ impl Plugin for TilemapRenderingPlugin {

// Extract the chunk size from the TilemapRenderSettings used to initialize the
// ChunkCoordinate resource to insert into the render pipeline
let chunk_size = {
let (chunk_size, y_sort) = {
match app.world.get_resource::<TilemapRenderSettings>() {
Some(settings) => settings.render_chunk_size,
None => CHUNK_SIZE_2D,
Some(settings) => (settings.render_chunk_size, settings.y_sort),
None => (CHUNK_SIZE_2D, false),
}
};

Expand Down Expand Up @@ -225,6 +232,7 @@ impl Plugin for TilemapRenderingPlugin {
render_app
.insert_resource(DefaultSampler(sampler))
.insert_resource(RenderChunkSize(chunk_size))
.insert_resource(RenderYSort(y_sort))
.insert_resource(RenderChunk2dStorage::default())
.insert_resource(SecondsSinceStartup(0.0))
.add_systems((extract::extract, extract::extract_removal).in_schedule(ExtractSchedule))
Expand Down
14 changes: 10 additions & 4 deletions src/render/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use super::{
draw::DrawTilemap,
pipeline::{TilemapPipeline, TilemapPipelineKey},
prepare::{MeshUniformResource, TilemapUniformResource},
RenderYSort,
};

#[derive(Resource)]
Expand Down Expand Up @@ -95,6 +96,7 @@ pub struct ImageBindGroups {
#[allow(clippy::too_many_arguments)]
pub fn queue_meshes(
mut commands: Commands,
y_sort: Res<RenderYSort>,
chunk_storage: Res<RenderChunk2dStorage>,
transparent_2d_draw_functions: Res<DrawFunctions<Transparent2d>>,
render_device: Res<RenderDevice>,
Expand Down Expand Up @@ -196,10 +198,14 @@ pub fn queue_meshes(
};

let pipeline_id = pipelines.specialize(&pipeline_cache, &tilemap_pipeline, key);
let z = transform.translation.z
+ (1.0
- (transform.translation.y
/ (chunk.map_size.y as f32 * chunk.tile_size.y)));
let z = if **y_sort {
transform.translation.z
+ (1.0
- (transform.translation.y
/ (chunk.map_size.y as f32 * chunk.tile_size.y)))
} else {
transform.translation.z
};
transparent_phase.add(Transparent2d {
entity,
draw_function: draw_tilemap,
Expand Down

0 comments on commit d15f332

Please sign in to comment.