Fix world position of tiles

This commit is contained in:
Maximilian Ammann 2022-01-03 21:27:44 +01:00
parent 5e47c7c68e
commit 982f6599ac
3 changed files with 22 additions and 12 deletions

View File

@ -76,16 +76,19 @@ pub mod tile {
array_stride: std::mem::size_of::<GpuVertexUniform>() as u64, array_stride: std::mem::size_of::<GpuVertexUniform>() as u64,
step_mode: wgpu::VertexStepMode::Vertex, step_mode: wgpu::VertexStepMode::Vertex,
attributes: &[ attributes: &[
// position
wgpu::VertexAttribute { wgpu::VertexAttribute {
offset: 0, offset: 0,
format: wgpu::VertexFormat::Float32x2, format: wgpu::VertexFormat::Float32x2,
shader_location: 0, shader_location: 0,
}, },
// normal
wgpu::VertexAttribute { wgpu::VertexAttribute {
offset: wgpu::VertexFormat::Float32x2.size(), offset: wgpu::VertexFormat::Float32x2.size(),
format: wgpu::VertexFormat::Float32x2, format: wgpu::VertexFormat::Float32x2,
shader_location: 1, shader_location: 1,
}, },
// tile_id
wgpu::VertexAttribute { wgpu::VertexAttribute {
offset: 2 * wgpu::VertexFormat::Float32x2.size(), offset: 2 * wgpu::VertexFormat::Float32x2.size(),
format: wgpu::VertexFormat::Uint32, format: wgpu::VertexFormat::Uint32,

View File

@ -33,7 +33,8 @@ fn main(
) -> VertexOutput { ) -> VertexOutput {
let z = 0.0; let z = 0.0;
let world_pos = position + translate + normal; // position the anchor of a tile at the top left, instead of bottom right
let world_pos = vec2<f32>(1.0, -1.0) * (position + normal * 3.0) + translate;
let position = globals.camera.view_proj * vec4<f32>(world_pos, z, 1.0); let position = globals.camera.view_proj * vec4<f32>(world_pos, z, 1.0);

View File

@ -162,17 +162,16 @@ impl State {
let instances = [ let instances = [
// Step 1 // Step 1
MaskInstanceUniform::new([0.0, 0.0], 4.0, 4.0, [1.0, 0.0, 0.0, 1.0]), // horizontal MaskInstanceUniform::new([0.0, 0.0], 4.0, -4.0, [1.0, 0.0, 0.0, 1.0]), // horizontal
//MaskInstanceUniform::new([0.0, 2.0 * 4096.0], 4.0, 1.0, [1.0, 0.0, 0.0, 1.0]), // vertical
// Step 2 // Step 2
MaskInstanceUniform::new([1.0 * 4096.0, 0.0], 1.0, 4.0, [0.0, 1.0, 0.0, 1.0]), // vertical MaskInstanceUniform::new([1.0 * 4096.0, 0.0], 1.0, -4.0, [0.0, 1.0, 0.0, 1.0]), // vertical
MaskInstanceUniform::new([3.0 * 4096.0, 0.0], 1.0, 4.0, [0.0, 1.0, 0.0, 1.0]), // vertical MaskInstanceUniform::new([3.0 * 4096.0, 0.0], 1.0, -4.0, [0.0, 1.0, 0.0, 1.0]), // vertical
// Step 3 // Step 3
MaskInstanceUniform::new([0.0, 1.0 * 4096.0], 4.0, 1.0, [0.0, 0.0, 1.0, 1.0]), // horizontal MaskInstanceUniform::new([0.0, -1.0 * 4096.0], 4.0, 1.0, [0.0, 0.0, 1.0, 1.0]), // horizontal
MaskInstanceUniform::new([0.0, 3.0 * 4096.0], 4.0, 1.0, [0.0, 0.0, 1.0, 1.0]), // horizontal MaskInstanceUniform::new([0.0, -3.0 * 4096.0], 4.0, 1.0, [0.0, 0.0, 1.0, 1.0]), // horizontal
// Step 4 // Step 4
MaskInstanceUniform::new([1.0 * 4096.0, 0.0], 1.0, 4.0, [0.5, 0.25, 0.5, 1.0]), // vertical MaskInstanceUniform::new([1.0 * 4096.0, 0.0], 1.0, -4.0, [0.5, 0.25, 0.5, 1.0]), // vertical
MaskInstanceUniform::new([3.0 * 4096.0, 0.0], 1.0, 4.0, [0.5, 0.25, 0.5, 1.0]), // vertical MaskInstanceUniform::new([3.0 * 4096.0, 0.0], 1.0, -4.0, [0.5, 0.25, 0.5, 1.0]), // vertical
]; ];
let tile_mask_instances = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let tile_mask_instances = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
@ -382,7 +381,10 @@ impl State {
let uniform = TileUniform::new( let uniform = TileUniform::new(
[0.0, 0.0, 0.0, 1.0], [0.0, 0.0, 0.0, 1.0],
[new_coords.x as f32 * 4096.0, new_coords.y as f32 * 4096.0], [
new_coords.x as f32 * 4096.0,
-1.0 * new_coords.y as f32 * 4096.0, // FIXME: Improve conversion to world tile coordinates
],
); );
self.queue.write_buffer( self.queue.write_buffer(
&self.tiles_uniform_buffer, &self.tiles_uniform_buffer,
@ -458,15 +460,19 @@ impl State {
// Draw masks // Draw masks
pass.set_pipeline(&self.mask_pipeline); pass.set_pipeline(&self.mask_pipeline);
pass.set_vertex_buffer(0, self.tile_mask_instances.slice(..)); pass.set_vertex_buffer(0, self.tile_mask_instances.slice(..));
// Draw 11 squares each out of 6 vertices // Draw 7 squares each out of 6 vertices
pass.draw(0..6, 0..7); pass.draw(0..6, 0..7);
} }
{ {
for entry in self.buffer_pool.available_vertices() { for entry in self.buffer_pool.available_vertices() {
let TileCoords { x, y, .. } = entry.coords; let TileCoords { x, y, .. } = entry.coords;
// FIXME: Improve conversion
let world_x = x as i32 - MUNICH_OFFSET_X as i32;
let world_y = y as i32 - MUNICH_OFFSET_Y as i32 * -1;
pass.set_pipeline(&self.render_pipeline); pass.set_pipeline(&self.render_pipeline);
let reference = match (x, y) { let reference = match (world_x, world_y) {
(x, y) if x % 2 == 0 && y % 2 == 0 => 1, (x, y) if x % 2 == 0 && y % 2 == 0 => 1,
(x, y) if x % 2 == 0 && y % 2 != 0 => 2, (x, y) if x % 2 == 0 && y % 2 != 0 => 2,
(x, y) if x % 2 != 0 && y % 2 == 0 => 3, (x, y) if x % 2 != 0 && y % 2 == 0 => 3,