terrain quilting scaling fix
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 240 B |
+2
-1
@@ -1,4 +1,5 @@
|
|||||||
pub const PIXEL_RATIO: f32 = 2.0;
|
pub const PIXEL_RATIO: f32 = 2.0;
|
||||||
pub const TILE_SIZE: f32 = 16.0;
|
pub const TILE_PIXELS: f32 = 16.0;
|
||||||
|
pub const TILE_SIZE: f32 = TILE_PIXELS * PIXEL_RATIO;
|
||||||
pub const ITILE_SIZE: i32 = TILE_SIZE as i32;
|
pub const ITILE_SIZE: i32 = TILE_SIZE as i32;
|
||||||
pub const UTILE_SIZE: u32 = TILE_SIZE as u32;
|
pub const UTILE_SIZE: u32 = TILE_SIZE as u32;
|
||||||
|
|||||||
+1
-1
@@ -24,7 +24,7 @@ pub fn move_cursor(
|
|||||||
for mut transform in query.iter_mut() {
|
for mut transform in query.iter_mut() {
|
||||||
let (camera, camera_transform) = *camera_query;
|
let (camera, camera_transform) = *camera_query;
|
||||||
|
|
||||||
let Ok(window) = windows.get_single() else {
|
let Ok(window) = windows.single() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -275,8 +275,8 @@ fn generate_chunks_from_algo(
|
|||||||
|
|
||||||
fn setup_initial_chunks(mut event_writer: EventWriter<LoadChunkEvent>) {
|
fn setup_initial_chunks(mut event_writer: EventWriter<LoadChunkEvent>) {
|
||||||
println!("setup_initial_chunks");
|
println!("setup_initial_chunks");
|
||||||
for x in -7..=7 {
|
for x in -2..=2 {
|
||||||
for y in -7..=7 {
|
for y in -2..=2 {
|
||||||
event_writer.write(LoadChunkEvent {
|
event_writer.write(LoadChunkEvent {
|
||||||
chunk_position: IVec2::new(x, y),
|
chunk_position: IVec2::new(x, y),
|
||||||
});
|
});
|
||||||
|
|||||||
+26
-12
@@ -168,11 +168,19 @@ pub fn build_quilted_terrain_sprites(
|
|||||||
let min_y = tiles.iter().map(|(pos, _)| pos.y).reduce(f32::min).unwrap();
|
let min_y = tiles.iter().map(|(pos, _)| pos.y).reduce(f32::min).unwrap();
|
||||||
let max_y = tiles.iter().map(|(pos, _)| pos.y).reduce(f32::max).unwrap();
|
let max_y = tiles.iter().map(|(pos, _)| pos.y).reduce(f32::max).unwrap();
|
||||||
|
|
||||||
|
// Snap min/max values to tile grid - ensure we're centered on tile centers
|
||||||
|
// Our tiles are positioned at their centers (not corners)
|
||||||
|
let min_x_aligned = (min_x / TILE_SIZE).floor() * TILE_SIZE;
|
||||||
|
let min_y_aligned = (min_y / TILE_SIZE).floor() * TILE_SIZE;
|
||||||
|
let max_x_aligned = (max_x / TILE_SIZE).ceil() * TILE_SIZE;
|
||||||
|
let max_y_aligned = (max_y / TILE_SIZE).ceil() * TILE_SIZE;
|
||||||
|
|
||||||
// Calculate texture dimensions in pixels
|
// Calculate texture dimensions in pixels
|
||||||
let width_tiles = ((max_x - min_x) / TILE_SIZE).ceil() as u32 + 1;
|
// We use TILE_PIXELS (16.0) as the base unit for the texture size
|
||||||
let height_tiles = ((max_y - min_y) / TILE_SIZE).ceil() as u32 + 1;
|
let width_tiles = ((max_x_aligned - min_x_aligned) / TILE_SIZE) as u32;
|
||||||
let width_px = width_tiles * UTILE_SIZE;
|
let height_tiles = ((max_y_aligned - min_y_aligned) / TILE_SIZE) as u32;
|
||||||
let height_px = height_tiles * UTILE_SIZE;
|
let width_px = width_tiles * TILE_PIXELS as u32;
|
||||||
|
let height_px = height_tiles * TILE_PIXELS as u32;
|
||||||
|
|
||||||
// Store the dimensions for later use (especially for adjusting the transform)
|
// Store the dimensions for later use (especially for adjusting the transform)
|
||||||
quilt_cache
|
quilt_cache
|
||||||
@@ -191,10 +199,16 @@ pub fn build_quilted_terrain_sprites(
|
|||||||
// Fetch the source texture data
|
// Fetch the source texture data
|
||||||
if let Some(source_texture) = images.get(texture) {
|
if let Some(source_texture) = images.get(texture) {
|
||||||
// Calculate position within the quilted texture
|
// Calculate position within the quilted texture
|
||||||
let tile_x = ((pos.x - min_x) / TILE_SIZE).floor() as u32;
|
// We need to be precise with the mapping from world coordinates to texture pixels
|
||||||
let tile_y = ((pos.y - min_y) / TILE_SIZE).floor() as u32;
|
|
||||||
|
|
||||||
|
// Calculate tile position relative to the quilted texture origin
|
||||||
|
// This gives us the tile index (whole number of tiles from origin)
|
||||||
|
let tile_x = ((pos.x - min_x_aligned) / TILE_SIZE).round() as u32;
|
||||||
|
let tile_y =
|
||||||
|
(height_tiles as f32 - 1.0 - ((pos.y - min_y_aligned) / TILE_SIZE).round())
|
||||||
|
as u32;
|
||||||
// Copy the tile texture into the quilted texture
|
// Copy the tile texture into the quilted texture
|
||||||
|
// Convert from tile indices to pixel coordinates in the output texture
|
||||||
if let Some(data) = &source_texture.data {
|
if let Some(data) = &source_texture.data {
|
||||||
blit_texture(
|
blit_texture(
|
||||||
data,
|
data,
|
||||||
@@ -203,8 +217,8 @@ pub fn build_quilted_terrain_sprites(
|
|||||||
source_texture.size().y as u32,
|
source_texture.size().y as u32,
|
||||||
width_px,
|
width_px,
|
||||||
height_px,
|
height_px,
|
||||||
tile_x * UTILE_SIZE,
|
(tile_x * TILE_PIXELS as u32) + (TILE_PIXELS / 2.0) as u32,
|
||||||
tile_y * UTILE_SIZE,
|
(tile_y * TILE_PIXELS as u32) + (TILE_PIXELS / 2.0) as u32,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -226,8 +240,9 @@ pub fn build_quilted_terrain_sprites(
|
|||||||
let texture_handle = images.add(quilted_texture);
|
let texture_handle = images.add(quilted_texture);
|
||||||
|
|
||||||
// Calculate the center position of the quilted sprite
|
// Calculate the center position of the quilted sprite
|
||||||
let center_x = min_x + (max_x - min_x) / 2.0;
|
// Using the aligned coordinates to ensure proper centering
|
||||||
let center_y = min_y + (max_y - min_y) / 2.0;
|
let center_x = min_x_aligned + (max_x_aligned - min_x_aligned) / 2.0;
|
||||||
|
let center_y = min_y_aligned + (max_y_aligned - min_y_aligned) / 2.0;
|
||||||
|
|
||||||
// Spawn the quilted sprite
|
// Spawn the quilted sprite
|
||||||
commands.spawn((
|
commands.spawn((
|
||||||
@@ -235,8 +250,7 @@ pub fn build_quilted_terrain_sprites(
|
|||||||
image: texture_handle,
|
image: texture_handle,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
Transform::from_xyz(center_x, center_y, *z_index as f32)
|
Transform::from_xyz(center_x, center_y, 0.0).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||||
.with_scale(Vec3::splat(PIXEL_RATIO)),
|
|
||||||
Visibility::Hidden,
|
Visibility::Hidden,
|
||||||
TerrainSprite { z_index: *z_index },
|
TerrainSprite { z_index: *z_index },
|
||||||
));
|
));
|
||||||
|
|||||||
Reference in New Issue
Block a user