use crate::math::Vector2;
use std::sync::Arc;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextureFormat {
Rgba8UnormSrgb,
Rgba8Unorm,
R8Unorm,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextureFilter {
Nearest,
Linear,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TextureWrap {
ClampToEdge,
Repeat,
MirroredRepeat,
}
#[derive(Debug, Clone)]
pub struct Texture {
pub width: u32,
pub height: u32,
pub format: TextureFormat,
pub mag_filter: TextureFilter,
pub min_filter: TextureFilter,
pub wrap_s: TextureWrap,
pub wrap_t: TextureWrap,
pub data: Arc<Vec<u8>>,
pub flip_y: bool,
pub offset: Vector2,
pub repeat: Vector2,
pub rotation: f32,
pub external_rt_id: Option<u32>,
}
impl Texture {
pub fn new(width: u32, height: u32, format: TextureFormat, data: Vec<u8>) -> Self {
Self {
width,
height,
format,
mag_filter: TextureFilter::Linear,
min_filter: TextureFilter::Linear,
wrap_s: TextureWrap::ClampToEdge,
wrap_t: TextureWrap::ClampToEdge,
data: Arc::new(data),
flip_y: true,
offset: Vector2::ZERO,
repeat: Vector2::ONE,
rotation: 0.0,
external_rt_id: None,
}
}
pub fn from_render_target(rt_id: u32, width: u32, height: u32, format: TextureFormat) -> Self {
Self {
width,
height,
format,
mag_filter: TextureFilter::Linear,
min_filter: TextureFilter::Linear,
wrap_s: TextureWrap::ClampToEdge,
wrap_t: TextureWrap::ClampToEdge,
data: Arc::new(Vec::new()),
flip_y: false,
offset: Vector2::ZERO,
repeat: Vector2::ONE,
rotation: 0.0,
external_rt_id: Some(rt_id),
}
}
pub fn bytes_per_pixel(&self) -> usize {
match self.format {
TextureFormat::Rgba8UnormSrgb | TextureFormat::Rgba8Unorm => 4,
TextureFormat::R8Unorm => 1,
}
}
pub fn solid(rgba: [u8; 4], format: TextureFormat) -> Self {
Self::new(1, 1, format, rgba.to_vec())
}
}