From 500d81a45d87729a69d01647f190045b908afd9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Le=20Berre?= Date: Fri, 28 Aug 2020 12:15:03 +0200 Subject: [PATCH 1/2] add support for non zero map origin --- src/map.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/src/map.rs b/src/map.rs index c6a191b..1021835 100644 --- a/src/map.rs +++ b/src/map.rs @@ -47,25 +47,25 @@ pub struct Map { } impl Map { - pub fn center(&self) -> Translation { + pub fn center(&self, origin : &Translation) -> Translation { let tile_size = Vec2::new(self.map.tile_width as f32, self.map.tile_height as f32); let width = self.map.width as f32; let height = self.map.height as f32; match self.map.orientation { tiled::Orientation::Orthogonal => Translation::new( - -tile_size.x() * width * 2.0, - tile_size.y() * height * 2.0, - 0.0, + origin.x() - tile_size.x() * width * 2.0, + origin.y() + tile_size.y() * height * 2.0, + origin.z(), ), tiled::Orientation::Isometric => Translation::new( - ((tile_size.x() * (width * 2.0) / 2.0) + (height * tile_size.x() / 2.0) + origin.x() + ((tile_size.x() * (width * 2.0) / 2.0) + (height * tile_size.x() / 2.0) - (height / 4.0 * tile_size.x() / 2.0)) * -2.0, - (((height - (height / 4.0) - 1.0) * tile_size.y() / 2.0) + origin.y() + (((height - (height / 4.0) - 1.0) * tile_size.y() / 2.0) + (width * tile_size.y() / 2.0) - (width / 4.0 * tile_size.y() / 2.0)) * -4.0, - 0.0, + origin.z(), ), _ => panic!("Unsupported orientation {:?}", self.map.orientation), @@ -79,6 +79,7 @@ pub struct TiledMapComponents { pub map_asset: Handle, pub materials: HashMap>, pub center: bool, + pub origin : Translation } impl Default for TiledMapComponents { @@ -87,6 +88,7 @@ impl Default for TiledMapComponents { map_asset: Handle::default(), materials: HashMap::default(), center: false, + origin : Translation::new(0., 0., 0.) } } } @@ -160,6 +162,7 @@ pub fn process_loaded_tile_maps( &bool, &Handle, &mut HashMap>, + &Translation )>, ) { let mut changed_maps = HashSet::>::new(); @@ -183,7 +186,7 @@ pub fn process_loaded_tile_maps( for changed_map in changed_maps.iter() { let map = maps.get_mut(changed_map).unwrap(); - for (_, _, _, mut materials_map) in &mut query.iter() { + for (_, _, _, mut materials_map, _) in &mut query.iter() { for tileset in &map.map.tilesets { if !materials_map.contains_key(&tileset.first_gid) { let texture_path = @@ -208,14 +211,14 @@ pub fn process_loaded_tile_maps( } } - for (_, center, map_handle, materials_map) in &mut query.iter() { + for (_, center, map_handle, materials_map, origin) in &mut query.iter() { if new_meshes.contains_key(map_handle) { let map = maps.get(map_handle).unwrap(); let translation = if *center { - map.center() + map.center(origin) } else { - Translation::default() + *origin }; let mesh_list = new_meshes.get_mut(map_handle).unwrap(); From 58eafc845b7f52b7ac8a2f644735e7aba00b48dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Le=20Berre?= Date: Fri, 28 Aug 2020 12:17:42 +0200 Subject: [PATCH 2/2] Fixed scaling : the map was centered like it was scaled 4 times, but it was actually scaled 5 times. I put the base scaling to 4, but it could also be fixed by changing map.center() --- src/tile_map.vert | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tile_map.vert b/src/tile_map.vert index da4d64b..06879d5 100644 --- a/src/tile_map.vert +++ b/src/tile_map.vert @@ -20,7 +20,7 @@ layout(set = 2, binding = 1) uniform TileMapChunk { void main() { v_Uv = Vertex_Uv; - vec3 position = Vertex_Position * vec3(5.0, 5.0, 1.0); + vec3 position = Vertex_Position * vec3(4.0, 4.0, 1.0); position.z = layer_id; gl_Position = ViewProj * Model * vec4(position, 1.0); } \ No newline at end of file