Allow for loading texture atlases, use this for water animation
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Sijmen 2021-06-19 12:31:50 +02:00
parent a86eaa33bd
commit 7d7ff71d6d
Signed by: vijfhoek
GPG key ID: 82D05C89B28B0DAE
35 changed files with 84 additions and 119 deletions

BIN
assets/water_still_plains.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-0.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-1.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-10.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-11.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-12.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-13.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-14.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-15.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-16.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-17.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-18.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-19.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-2.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-20.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-21.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-22.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-23.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-24.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-25.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-26.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-27.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-28.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-29.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-3.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-30.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-31.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-4.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-5.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-6.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-7.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-8.png (Stored with Git LFS)

Binary file not shown.

BIN
assets/water_still_plains/frame-9.png (Stored with Git LFS)

Binary file not shown.

View file

@ -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<Rgba<u8>, Vec<u8>>,
origin: Vector2<u32>,
size: Vector2<u32>,
label: &str,
) -> anyhow::Result<Self> {
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<Self> {
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<u32>,
label: &str,
) -> anyhow::Result<Vec<Self>> {
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<u32>,
) -> anyhow::Result<Range<usize>> {
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)
}
}

View file

@ -37,10 +37,11 @@ impl BlockType {
}
}
#[rustfmt::skip]
pub const fn color(self) -> Vector4<f32> {
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),
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),
}
}