terrain quilting scaling fix
This commit is contained in:
+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 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
|
||||
let width_tiles = ((max_x - min_x) / TILE_SIZE).ceil() as u32 + 1;
|
||||
let height_tiles = ((max_y - min_y) / TILE_SIZE).ceil() as u32 + 1;
|
||||
let width_px = width_tiles * UTILE_SIZE;
|
||||
let height_px = height_tiles * UTILE_SIZE;
|
||||
// We use TILE_PIXELS (16.0) as the base unit for the texture size
|
||||
let width_tiles = ((max_x_aligned - min_x_aligned) / TILE_SIZE) as u32;
|
||||
let height_tiles = ((max_y_aligned - min_y_aligned) / TILE_SIZE) as u32;
|
||||
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)
|
||||
quilt_cache
|
||||
@@ -191,10 +199,16 @@ pub fn build_quilted_terrain_sprites(
|
||||
// Fetch the source texture data
|
||||
if let Some(source_texture) = images.get(texture) {
|
||||
// Calculate position within the quilted texture
|
||||
let tile_x = ((pos.x - min_x) / TILE_SIZE).floor() as u32;
|
||||
let tile_y = ((pos.y - min_y) / TILE_SIZE).floor() as u32;
|
||||
// We need to be precise with the mapping from world coordinates to texture pixels
|
||||
|
||||
// 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
|
||||
// Convert from tile indices to pixel coordinates in the output texture
|
||||
if let Some(data) = &source_texture.data {
|
||||
blit_texture(
|
||||
data,
|
||||
@@ -203,8 +217,8 @@ pub fn build_quilted_terrain_sprites(
|
||||
source_texture.size().y as u32,
|
||||
width_px,
|
||||
height_px,
|
||||
tile_x * UTILE_SIZE,
|
||||
tile_y * UTILE_SIZE,
|
||||
(tile_x * TILE_PIXELS as u32) + (TILE_PIXELS / 2.0) as u32,
|
||||
(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);
|
||||
|
||||
// Calculate the center position of the quilted sprite
|
||||
let center_x = min_x + (max_x - min_x) / 2.0;
|
||||
let center_y = min_y + (max_y - min_y) / 2.0;
|
||||
// Using the aligned coordinates to ensure proper centering
|
||||
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
|
||||
commands.spawn((
|
||||
@@ -235,8 +250,7 @@ pub fn build_quilted_terrain_sprites(
|
||||
image: texture_handle,
|
||||
..Default::default()
|
||||
},
|
||||
Transform::from_xyz(center_x, center_y, *z_index as f32)
|
||||
.with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
Transform::from_xyz(center_x, center_y, 0.0).with_scale(Vec3::splat(PIXEL_RATIO)),
|
||||
Visibility::Hidden,
|
||||
TerrainSprite { z_index: *z_index },
|
||||
));
|
||||
|
||||
Reference in New Issue
Block a user