From 7d7ff71d6d9e013a731fb6b609701a1f38770326 Mon Sep 17 00:00:00 2001 From: Vijfhoek Date: Sat, 19 Jun 2021 12:31:50 +0200 Subject: [PATCH] Allow for loading texture atlases, use this for water animation --- assets/water_still_plains.png | 3 - assets/water_still_plains/frame-0.png | 3 - assets/water_still_plains/frame-1.png | 3 - assets/water_still_plains/frame-10.png | 3 - assets/water_still_plains/frame-11.png | 3 - assets/water_still_plains/frame-12.png | 3 - assets/water_still_plains/frame-13.png | 3 - assets/water_still_plains/frame-14.png | 3 - assets/water_still_plains/frame-15.png | 3 - assets/water_still_plains/frame-16.png | 3 - assets/water_still_plains/frame-17.png | 3 - assets/water_still_plains/frame-18.png | 3 - assets/water_still_plains/frame-19.png | 3 - assets/water_still_plains/frame-2.png | 3 - assets/water_still_plains/frame-20.png | 3 - assets/water_still_plains/frame-21.png | 3 - assets/water_still_plains/frame-22.png | 3 - assets/water_still_plains/frame-23.png | 3 - assets/water_still_plains/frame-24.png | 3 - assets/water_still_plains/frame-25.png | 3 - assets/water_still_plains/frame-26.png | 3 - assets/water_still_plains/frame-27.png | 3 - assets/water_still_plains/frame-28.png | 3 - assets/water_still_plains/frame-29.png | 3 - assets/water_still_plains/frame-3.png | 3 - assets/water_still_plains/frame-30.png | 3 - assets/water_still_plains/frame-31.png | 3 - assets/water_still_plains/frame-4.png | 3 - assets/water_still_plains/frame-5.png | 3 - assets/water_still_plains/frame-6.png | 3 - assets/water_still_plains/frame-7.png | 3 - assets/water_still_plains/frame-8.png | 3 - assets/water_still_plains/frame-9.png | 3 - src/texture.rs | 97 +++++++++++++++++++++----- src/world/block.rs | 7 +- 35 files changed, 84 insertions(+), 119 deletions(-) delete mode 100644 assets/water_still_plains.png delete mode 100644 assets/water_still_plains/frame-0.png delete mode 100644 assets/water_still_plains/frame-1.png delete mode 100644 assets/water_still_plains/frame-10.png delete mode 100644 assets/water_still_plains/frame-11.png delete mode 100644 assets/water_still_plains/frame-12.png delete mode 100644 assets/water_still_plains/frame-13.png delete mode 100644 assets/water_still_plains/frame-14.png delete mode 100644 assets/water_still_plains/frame-15.png delete mode 100644 assets/water_still_plains/frame-16.png delete mode 100644 assets/water_still_plains/frame-17.png delete mode 100644 assets/water_still_plains/frame-18.png delete mode 100644 assets/water_still_plains/frame-19.png delete mode 100644 assets/water_still_plains/frame-2.png delete mode 100644 assets/water_still_plains/frame-20.png delete mode 100644 assets/water_still_plains/frame-21.png delete mode 100644 assets/water_still_plains/frame-22.png delete mode 100644 assets/water_still_plains/frame-23.png delete mode 100644 assets/water_still_plains/frame-24.png delete mode 100644 assets/water_still_plains/frame-25.png delete mode 100644 assets/water_still_plains/frame-26.png delete mode 100644 assets/water_still_plains/frame-27.png delete mode 100644 assets/water_still_plains/frame-28.png delete mode 100644 assets/water_still_plains/frame-29.png delete mode 100644 assets/water_still_plains/frame-3.png delete mode 100644 assets/water_still_plains/frame-30.png delete mode 100644 assets/water_still_plains/frame-31.png delete mode 100644 assets/water_still_plains/frame-4.png delete mode 100644 assets/water_still_plains/frame-5.png delete mode 100644 assets/water_still_plains/frame-6.png delete mode 100644 assets/water_still_plains/frame-7.png delete mode 100644 assets/water_still_plains/frame-8.png delete mode 100644 assets/water_still_plains/frame-9.png diff --git a/assets/water_still_plains.png b/assets/water_still_plains.png deleted file mode 100644 index dcbb276..0000000 --- a/assets/water_still_plains.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:9cea73456c45f9ff747bd3cb97ea31188ad12e2e320728cb0d96e6fc63368416 -size 9014 diff --git a/assets/water_still_plains/frame-0.png b/assets/water_still_plains/frame-0.png deleted file mode 100644 index 728e800..0000000 --- a/assets/water_still_plains/frame-0.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:a89fc6705174413e8721c161046fea4bbc6faa88193784fc8c035158a3fcd9fb -size 3321 diff --git a/assets/water_still_plains/frame-1.png b/assets/water_still_plains/frame-1.png deleted file mode 100644 index 52ec811..0000000 --- a/assets/water_still_plains/frame-1.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:d3cfc4263ad610df84a74f8d25c10ec2d71d42c87262986e9fce880f83de3806 -size 3323 diff --git a/assets/water_still_plains/frame-10.png b/assets/water_still_plains/frame-10.png deleted file mode 100644 index b0c2364..0000000 --- a/assets/water_still_plains/frame-10.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:a481253ac31e8b9905bd875b474bba002546999f55f76cd9a761c9c0be4f7410 -size 3311 diff --git a/assets/water_still_plains/frame-11.png b/assets/water_still_plains/frame-11.png deleted file mode 100644 index e570eba..0000000 --- a/assets/water_still_plains/frame-11.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ef61d24415c1f555b8066931592af9dcd1983144a05c2b284c3212d5d32ac1a9 -size 3316 diff --git a/assets/water_still_plains/frame-12.png b/assets/water_still_plains/frame-12.png deleted file mode 100644 index 169b658..0000000 --- a/assets/water_still_plains/frame-12.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:35c498c37bb7938bfeb7268188ac02413e0918e6ddc5efd29be5353fc7101413 -size 3316 diff --git a/assets/water_still_plains/frame-13.png b/assets/water_still_plains/frame-13.png deleted file mode 100644 index 1fe625c..0000000 --- a/assets/water_still_plains/frame-13.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:901302b4538135e6baaf4524bf5447108d40f6324c361e2547af6cfb98148a0f -size 3326 diff --git a/assets/water_still_plains/frame-14.png b/assets/water_still_plains/frame-14.png deleted file mode 100644 index c6a1299..0000000 --- a/assets/water_still_plains/frame-14.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:41d9953880460f4ab178277180daec577161cca33498a79190f0d27ed3a19e0f -size 3318 diff --git a/assets/water_still_plains/frame-15.png b/assets/water_still_plains/frame-15.png deleted file mode 100644 index 4bf0d00..0000000 --- a/assets/water_still_plains/frame-15.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ff7662f73afc61c941d21ef5e1100a95a586d9b51948d5214c1bd6b3907d45e0 -size 3324 diff --git a/assets/water_still_plains/frame-16.png b/assets/water_still_plains/frame-16.png deleted file mode 100644 index 179abee..0000000 --- a/assets/water_still_plains/frame-16.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:cca471b01dafa0cf74b6f0ba36f5abbf594d2dd005feedaa0d500b6bab478f6c -size 3323 diff --git a/assets/water_still_plains/frame-17.png b/assets/water_still_plains/frame-17.png deleted file mode 100644 index 3c8ab9b..0000000 --- a/assets/water_still_plains/frame-17.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:7587fe7bf46bc35c2291b83d6719cc0f7c91da770ad8d76b199355edc3afd68e -size 3325 diff --git a/assets/water_still_plains/frame-18.png b/assets/water_still_plains/frame-18.png deleted file mode 100644 index fa97911..0000000 --- a/assets/water_still_plains/frame-18.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:81f5710ca16b28892b5b899b9509f23bdfbe6a50e7e038a58199e9fea0908cec -size 3323 diff --git a/assets/water_still_plains/frame-19.png b/assets/water_still_plains/frame-19.png deleted file mode 100644 index 14677a8..0000000 --- a/assets/water_still_plains/frame-19.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:9a6e88d2e7e8d7aae4253eb7f88299821e65ea8fc0363b26a8242223c352b9a9 -size 3323 diff --git a/assets/water_still_plains/frame-2.png b/assets/water_still_plains/frame-2.png deleted file mode 100644 index a346f41..0000000 --- a/assets/water_still_plains/frame-2.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:c199a71e527510fa913a6112e2cee415ad573180b1e4ffebe626f6c38167301b -size 3325 diff --git a/assets/water_still_plains/frame-20.png b/assets/water_still_plains/frame-20.png deleted file mode 100644 index 4b6f04a..0000000 --- a/assets/water_still_plains/frame-20.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:0203dd5b775361097394d8773c3a9dec3920d98a2a83e5bd25bcbcf1ccb8a502 -size 3325 diff --git a/assets/water_still_plains/frame-21.png b/assets/water_still_plains/frame-21.png deleted file mode 100644 index f5d3469..0000000 --- a/assets/water_still_plains/frame-21.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:c4ca7d6b1c53634568ecba469c36f23bf7fdd67efb6f9eb7cf81bfb27ffdb368 -size 3320 diff --git a/assets/water_still_plains/frame-22.png b/assets/water_still_plains/frame-22.png deleted file mode 100644 index b090468..0000000 --- a/assets/water_still_plains/frame-22.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ab5ab02ce41fb6850f82f855842ca3ca38575729ad7ff6261c4a8ec335106917 -size 3319 diff --git a/assets/water_still_plains/frame-23.png b/assets/water_still_plains/frame-23.png deleted file mode 100644 index c66db64..0000000 --- a/assets/water_still_plains/frame-23.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:818c6246750be69f2dfc16d07a31a228b5c5e65bccfcaa3f0cfeca86ae9927ca -size 3314 diff --git a/assets/water_still_plains/frame-24.png b/assets/water_still_plains/frame-24.png deleted file mode 100644 index 6ef4309..0000000 --- a/assets/water_still_plains/frame-24.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:6430a8f6e7b884b386453f6d7318040f64809e9c8bcb46bb52f56302495199b8 -size 3307 diff --git a/assets/water_still_plains/frame-25.png b/assets/water_still_plains/frame-25.png deleted file mode 100644 index 58e8fa6..0000000 --- a/assets/water_still_plains/frame-25.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:ac6ded8a16a3a1506d90391a700b28ff73878b03638a4a431a51a2502d41098e -size 3315 diff --git a/assets/water_still_plains/frame-26.png b/assets/water_still_plains/frame-26.png deleted file mode 100644 index 9023367..0000000 --- a/assets/water_still_plains/frame-26.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:98c00f5d2699d95e34fc1c383374372429ad53b0ee595e8686e0b68fe5afb1fa -size 3306 diff --git a/assets/water_still_plains/frame-27.png b/assets/water_still_plains/frame-27.png deleted file mode 100644 index 8d19f23..0000000 --- a/assets/water_still_plains/frame-27.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:0da70613dd9a62c2d7fae5b8503687af43df200b4e64f5e66e2396dfddb99f6f -size 3306 diff --git a/assets/water_still_plains/frame-28.png b/assets/water_still_plains/frame-28.png deleted file mode 100644 index 2d64674..0000000 --- a/assets/water_still_plains/frame-28.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:99a24fb98239f1a5b5ce3bbfb1ad738c81357832407024cc4c451f94db800363 -size 3305 diff --git a/assets/water_still_plains/frame-29.png b/assets/water_still_plains/frame-29.png deleted file mode 100644 index 5684edc..0000000 --- a/assets/water_still_plains/frame-29.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:04b563d065bff3261ba070c23fac952cbbdce04019f346d123e3140b0a741964 -size 3311 diff --git a/assets/water_still_plains/frame-3.png b/assets/water_still_plains/frame-3.png deleted file mode 100644 index cc15e14..0000000 --- a/assets/water_still_plains/frame-3.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:23e50f416683e391860bc078f766fdc2438196e8a89fdd5f012315827cf00e50 -size 3322 diff --git a/assets/water_still_plains/frame-30.png b/assets/water_still_plains/frame-30.png deleted file mode 100644 index 9dd834d..0000000 --- a/assets/water_still_plains/frame-30.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:5ac0c66cf9ee5adcca1f7b373b6477c1e83051115af278d876c9df9f4aa4813c -size 3310 diff --git a/assets/water_still_plains/frame-31.png b/assets/water_still_plains/frame-31.png deleted file mode 100644 index 09f48e7..0000000 --- a/assets/water_still_plains/frame-31.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:3bec1cd494f4b65e5366c0bd6e74b86e8dd3a113f4b97fac951bb8e9107adc5a -size 3317 diff --git a/assets/water_still_plains/frame-4.png b/assets/water_still_plains/frame-4.png deleted file mode 100644 index ecd4443..0000000 --- a/assets/water_still_plains/frame-4.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:51954e5330b8410d1daefb68217777527c55c41bae730ddfcae5e1c2292a3869 -size 3317 diff --git a/assets/water_still_plains/frame-5.png b/assets/water_still_plains/frame-5.png deleted file mode 100644 index 6b57454..0000000 --- a/assets/water_still_plains/frame-5.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:21c08711757999fe3109add710c352d3f4613850ebb081114de0bdbc876e7f42 -size 3321 diff --git a/assets/water_still_plains/frame-6.png b/assets/water_still_plains/frame-6.png deleted file mode 100644 index 1d6236d..0000000 --- a/assets/water_still_plains/frame-6.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:5acb4dc48becb0e65cdb1edcf08ca01f14a7ef77f5a04d98991df8a0dc600337 -size 3324 diff --git a/assets/water_still_plains/frame-7.png b/assets/water_still_plains/frame-7.png deleted file mode 100644 index 6603c3d..0000000 --- a/assets/water_still_plains/frame-7.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:7e205f4e01db099f2ee3ebf5c0d4accd74883c40d5d2f025033f18ee568b1f76 -size 3323 diff --git a/assets/water_still_plains/frame-8.png b/assets/water_still_plains/frame-8.png deleted file mode 100644 index 4864b86..0000000 --- a/assets/water_still_plains/frame-8.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:454a2c2aaed4254e40e05739f71c5b9dbaadcc3b3a1d0d16c1da2e18790236db -size 3314 diff --git a/assets/water_still_plains/frame-9.png b/assets/water_still_plains/frame-9.png deleted file mode 100644 index 01f603e..0000000 --- a/assets/water_still_plains/frame-9.png +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:5470b67531c9675fa2e9aaa1e0b55e39a3988492935b514b642695a19b721f7d -size 3321 diff --git a/src/texture.rs b/src/texture.rs index 0f4ff99..931a856 100644 --- a/src/texture.rs +++ b/src/texture.rs @@ -1,6 +1,7 @@ -use std::num::NonZeroU32; +use std::{num::NonZeroU32, ops::Range}; -use image::EncodableLayout; +use cgmath::{Vector2, Zero}; +use image::{EncodableLayout, ImageBuffer, Rgba}; use wgpu::Origin3d; use crate::render_context::RenderContext; @@ -56,18 +57,16 @@ impl Texture { } } - pub fn from_bytes( + fn from_rgba8( render_context: &RenderContext, - bytes: &[u8], + rgba: &ImageBuffer, Vec>, + origin: Vector2, + size: Vector2, label: &str, ) -> anyhow::Result { - let image = image::load_from_memory(bytes)?; - let rgba = image.into_rgba8(); - let (width, height) = rgba.dimensions(); - let texture_size = wgpu::Extent3d { - width, - height, + width: size.x, + height: size.y, depth_or_array_layers: 1, }; @@ -85,17 +84,19 @@ impl Texture { | wgpu::TextureUsage::COPY_SRC, }); + let stride = 4 * rgba.width(); + let offset = (origin.y * stride + origin.x * 4) as usize; render_context.queue.write_texture( wgpu::ImageCopyTexture { texture: &texture, mip_level: 0, origin: wgpu::Origin3d::ZERO, }, - rgba.as_bytes(), + &rgba.as_bytes()[offset..offset + (size.y * stride) as usize], wgpu::ImageDataLayout { offset: 0, - bytes_per_row: NonZeroU32::new(4 * width), - rows_per_image: NonZeroU32::new(height), + bytes_per_row: NonZeroU32::new(stride), + rows_per_image: NonZeroU32::new(size.y), }, texture_size, ); @@ -112,6 +113,52 @@ impl Texture { view, }) } + + pub fn from_bytes( + render_context: &RenderContext, + bytes: &[u8], + label: &str, + ) -> anyhow::Result { + let image = image::load_from_memory(bytes)?; + let rgba = image.into_rgba8(); + let (width, height) = rgba.dimensions(); + Self::from_rgba8( + render_context, + &rgba, + Vector2::zero(), + Vector2::new(width, height), + label, + ) + } + + pub fn from_bytes_atlas( + render_context: &RenderContext, + bytes: &[u8], + tile_size: Vector2, + label: &str, + ) -> anyhow::Result> { + let image = image::load_from_memory(bytes)?; + let rgba = image.into_rgba8(); + + let (width, height) = rgba.dimensions(); + assert_eq!(width % tile_size.x, 0); + assert_eq!(height % tile_size.y, 0); + + let mut tiles = Vec::new(); + for y in (0..height).step_by(tile_size.y as usize) { + for x in (0..width).step_by(tile_size.x as usize) { + tiles.push(Self::from_rgba8( + render_context, + &rgba, + Vector2::new(x, y), + tile_size, + &format!("{}({},{})", label, x, y), + )?); + } + } + + Ok(tiles) + } } pub const TEXTURE_COUNT: usize = 44; @@ -174,6 +221,8 @@ impl TextureManager { } pub fn load_all(&mut self, render_context: &RenderContext) -> anyhow::Result<()> { + let tile_size = Vector2::new(16, 16); + self.load(render_context, "assets/block/cobblestone.png")?; // 0 self.load(render_context, "assets/block/dirt.png")?; // 1 self.load(render_context, "assets/block/stone.png")?; // 2 @@ -182,10 +231,7 @@ impl TextureManager { self.load(render_context, "assets/block/bedrock.png")?; // 5 self.load(render_context, "assets/block/sand.png")?; // 6 self.load(render_context, "assets/block/gravel.png")?; // 7 - for i in 0..32 { - let path = format!("assets/water_still_plains/frame-{}.png", i); - self.load(render_context, &path)?; // 8 - 39 - } + self.load_atlas(render_context, "assets/block/water_still.png", tile_size)?; // 8 - 39 self.load(render_context, "assets/block/oak_log.png")?; // 40 self.load(render_context, "assets/block/oak_log_top.png")?; // 41 self.load(render_context, "assets/block/oak_planks.png")?; // 42 @@ -280,4 +326,21 @@ impl TextureManager { println!("loaded {} to {}", path, id); Ok(id) } + + pub fn load_atlas( + &mut self, + render_context: &RenderContext, + path: &str, + tile_size: Vector2, + ) -> anyhow::Result> { + let bytes = std::fs::read(path)?; + let mut textures = Texture::from_bytes_atlas(render_context, &bytes, tile_size, path)?; + + let start = self.textures.len(); + self.textures.append(&mut textures); + let end = self.textures.len(); + + println!("loaded atlas {} to {}..{}", path, start, end); + Ok(start..end) + } } diff --git a/src/world/block.rs b/src/world/block.rs index a62831e..74cc305 100644 --- a/src/world/block.rs +++ b/src/world/block.rs @@ -37,11 +37,12 @@ impl BlockType { } } + #[rustfmt::skip] pub const fn color(self) -> Vector4 { match self { - Self::Water => Vector4::new(0.1540, 0.2885, 0.5575, 1.0), - Self::OakLeaves => Vector4::new(0.4784, 0.7294, 0.1255, 1.0), - _ => Vector4::new(1.0, 1.0, 1.0, 1.0), + Self::Water => Vector4::new(0.247, 0.463, 0.894, 1.0), + Self::OakLeaves => Vector4::new(0.478, 0.729, 0.126, 1.0), + _ => Vector4::new(1.0, 1.0, 1.0, 1.0), } }