lyra-engine/lyra-resource/src/texture.rs

108 lines
2.1 KiB
Rust
Raw Normal View History

use std::ops::{Deref, DerefMut};
//pub use gltf::texture::{MagFilter, MinFilter, WrappingMode};
2023-10-23 01:49:31 +00:00
use image::DynamicImage;
use lyra_reflect::Reflect;
use crate::lyra_engine;
2023-09-12 18:25:33 +00:00
use crate::ResHandle;
use crate::ResourceData;
/// The filter mode of the sampler.
///
/// This is used for minification, magnification, and mipmap filters
#[derive(Clone, Copy, PartialEq, Eq, Reflect)]
pub enum FilterMode {
Nearest,
Linear,
}
/// The wrapping mode of the Texture coordinates
#[derive(Clone, Copy, PartialEq, Eq, Reflect)]
pub enum WrappingMode {
ClampToEdge,
MirroredRepeat,
Repeat,
}
/// The descriptor of the sampler for a Texture.
#[derive(Clone, Reflect)]
pub struct TextureSampler {
pub mag_filter: Option<FilterMode>,
pub min_filter: Option<FilterMode>,
pub mipmap_filter: Option<FilterMode>,
pub wrap_u: WrappingMode,
pub wrap_v: WrappingMode,
pub wrap_w: WrappingMode,
}
#[derive(Clone, Reflect)]
pub struct Image(#[reflect(skip)] DynamicImage);
impl ResourceData for Image {
fn dependencies(&self) -> Vec<crate::UntypedResHandle> {
vec![]
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}
impl Deref for Image {
type Target = DynamicImage;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Image {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl From<DynamicImage> for Image {
fn from(value: DynamicImage) -> Self {
Self(value)
}
}
#[derive(Clone, Reflect)]
2023-09-12 18:25:33 +00:00
pub struct Texture {
pub image: ResHandle<Image>,
pub sampler: Option<TextureSampler>,
2023-09-12 18:25:33 +00:00
}
impl ResourceData for Texture {
fn dependencies(&self) -> Vec<crate::UntypedResHandle> {
vec![self.image.untyped_clone()]
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}
2023-09-12 18:25:33 +00:00
impl Texture {
/// Create a texture from an image.
pub fn from_image(image: ResHandle<Image>) -> Self {
Self {
image,
sampler: None,
}
}
2023-09-12 18:25:33 +00:00
}