Use hand texture as render background
This commit is contained in:
@@ -1,16 +1,51 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use anyhow::*;
|
||||
use eframe::{
|
||||
egui,
|
||||
egui_wgpu::{self, wgpu},
|
||||
wgpu::util::DeviceExt,
|
||||
};
|
||||
use eframe::wgpu;
|
||||
use image::GenericImageView;
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum TextureColorSpace {
|
||||
Srgb,
|
||||
Linear,
|
||||
}
|
||||
|
||||
impl TextureColorSpace {
|
||||
pub fn format(self) -> wgpu::TextureFormat {
|
||||
match self {
|
||||
Self::Srgb => wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
Self::Linear => wgpu::TextureFormat::Rgba8Unorm,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct TextureSamplerDescriptor {
|
||||
pub address_mode_u: wgpu::AddressMode,
|
||||
pub address_mode_v: wgpu::AddressMode,
|
||||
pub mag_filter: wgpu::FilterMode,
|
||||
pub min_filter: wgpu::FilterMode,
|
||||
pub mipmap_filter: wgpu::MipmapFilterMode,
|
||||
}
|
||||
|
||||
impl Default for TextureSamplerDescriptor {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
address_mode_u: wgpu::AddressMode::Repeat,
|
||||
address_mode_v: wgpu::AddressMode::Repeat,
|
||||
mag_filter: wgpu::FilterMode::Linear,
|
||||
min_filter: wgpu::FilterMode::Linear,
|
||||
mipmap_filter: wgpu::MipmapFilterMode::Linear,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Texture {
|
||||
pub texture: wgpu::Texture,
|
||||
pub view: wgpu::TextureView,
|
||||
pub sampler: wgpu::Sampler,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
impl Texture {
|
||||
@@ -55,6 +90,8 @@ impl Texture {
|
||||
texture,
|
||||
view,
|
||||
sampler,
|
||||
width: config.width,
|
||||
height: config.height,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,7 +126,7 @@ impl Texture {
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
format: TextureColorSpace::Srgb.format(),
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||
view_formats: &[],
|
||||
});
|
||||
@@ -125,6 +162,8 @@ impl Texture {
|
||||
texture,
|
||||
view,
|
||||
sampler,
|
||||
width: dimensions.0,
|
||||
height: dimensions.1,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -135,6 +174,28 @@ impl Texture {
|
||||
width: u32,
|
||||
height: u32,
|
||||
label: &str,
|
||||
) -> Result<Self> {
|
||||
Self::from_rgba8_with_sampler(
|
||||
device,
|
||||
queue,
|
||||
rgba,
|
||||
width,
|
||||
height,
|
||||
label,
|
||||
TextureColorSpace::Srgb,
|
||||
TextureSamplerDescriptor::default(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn from_rgba8_with_sampler(
|
||||
device: &wgpu::Device,
|
||||
queue: &wgpu::Queue,
|
||||
rgba: &[u8],
|
||||
width: u32,
|
||||
height: u32,
|
||||
label: &str,
|
||||
color_space: TextureColorSpace,
|
||||
sampler_desc: TextureSamplerDescriptor,
|
||||
) -> Result<Self> {
|
||||
let size = wgpu::Extent3d {
|
||||
width,
|
||||
@@ -147,7 +208,7 @@ impl Texture {
|
||||
mip_level_count: 1,
|
||||
sample_count: 1,
|
||||
dimension: wgpu::TextureDimension::D2,
|
||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||
format: color_space.format(),
|
||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||
view_formats: &[],
|
||||
});
|
||||
@@ -170,12 +231,12 @@ impl Texture {
|
||||
|
||||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||
let sampler = device.create_sampler(&wgpu::SamplerDescriptor {
|
||||
address_mode_u: wgpu::AddressMode::ClampToEdge,
|
||||
address_mode_v: wgpu::AddressMode::ClampToEdge,
|
||||
address_mode_u: sampler_desc.address_mode_u,
|
||||
address_mode_v: sampler_desc.address_mode_v,
|
||||
address_mode_w: wgpu::AddressMode::ClampToEdge,
|
||||
mag_filter: wgpu::FilterMode::Linear,
|
||||
min_filter: wgpu::FilterMode::Nearest,
|
||||
mipmap_filter: wgpu::MipmapFilterMode::Nearest,
|
||||
mag_filter: sampler_desc.mag_filter,
|
||||
min_filter: sampler_desc.min_filter,
|
||||
mipmap_filter: sampler_desc.mipmap_filter,
|
||||
..Default::default()
|
||||
});
|
||||
|
||||
@@ -183,6 +244,8 @@ impl Texture {
|
||||
texture,
|
||||
view,
|
||||
sampler,
|
||||
width,
|
||||
height,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user