Texture

Struct Texture 

Source
pub struct Texture {
    pub bind_group: BindGroup,
    pub texture: Texture,
    pub view: TextureView,
}
Expand description

A texture and associated structs.

Fields§

§bind_group: BindGroup

A bind group of the GPU texture and its view.

§texture: Texture

The texture as represented by wgpu.

§view: TextureView

A view into the texture.

Implementations§

Source§

impl Texture

Source

pub fn new( size: UVec2, sampler: SamplerType, format: TextureFormat, usage: TextureUsages, ) -> Self

Creates a new empty texture.

Source

pub fn from_image(image: &RgbaImage, sampler: SamplerType) -> Result<Self>

Creates a texture from specified image data.

Examples found in repository?
examples/perlinimage.rs (line 47)
23    fn init() -> Self {
24        let noise = match std::env::args().nth(1) {
25            Some(seed) => Noise::from_seed(seed.parse().unwrap()),
26            None => Noise::new(),
27        };
28        game_data().set_window_size((IMAGE_WIDTH, IMAGE_HEIGHT).into());
29
30        let mut image = RgbaImage::new(IMAGE_WIDTH, IMAGE_HEIGHT);
31
32        for x in 0..IMAGE_WIDTH {
33            for y in 0..IMAGE_HEIGHT {
34                let perlin = [
35                    perlin_2d(&noise, Vec2::new(x as f32, y as f32) / 16.0, smootherstep) * 0.0625,
36                    perlin_2d(&noise, Vec2::new(x as f32, y as f32) / 32.0, smootherstep) * 0.125,
37                    perlin_2d(&noise, Vec2::new(x as f32, y as f32) / 64.0, smootherstep) * 0.25,
38                    perlin_2d(&noise, Vec2::new(x as f32, y as f32) / 128.0, smootherstep) * 0.5,
39                    perlin_2d(&noise, Vec2::new(x as f32, y as f32) / 256.0, smootherstep),
40                ];
41                let value = ((perlin.into_iter().sum::<f32>() + 1.0) * 127.0) as u8;
42                image.put_pixel(x, y, Rgba([value, value, value, 255]));
43            }
44        }
45
46        let mut renderer = SimpleRendererBuilder::default().build();
47        let texture = Arc::new(Texture::from_image(&image, SamplerType::NEAREST).unwrap());
48
49        renderer.models.push(Model::quad_texture(
50            texture,
51            vec![Transform::scale(Vec3::new(2.0, 2.0, 1.0))
52                .with_translation(Vec3::new(-1.0, -1.0, 0.0))],
53        ));
54
55        Self(renderer)
56    }
Source

pub fn from_color<T: Into<Rgba<u8>> + Clone>( color: &T, resolution: UVec2, sampler: SamplerType, ) -> Result<Self>

Creates a texture by filling it with a solid color.

Source

pub fn from_texture(texture: Texture, sampler: SamplerType) -> Self

Creates a texture from a wgpu::Texture.

Source

pub fn from_path<P: AsRef<Path> + Display>( path: P, sampler: SamplerType, ) -> Result<Self>

Loads an image from path and makes a Texture from it.

Examples found in repository?
examples/postprocess.rs (line 22)
19    fn init() -> Self {
20        let mut renderer = SimpleRendererBuilder::default().build();
21        renderer.models.push(Model::quad_texture(
22            Arc::new(Texture::from_path("examples/test.png", SamplerType::LINEAR).unwrap()),
23            vec![Transform::scale((0.5, 0.5, 1.0))],
24        ));
25
26        Self {
27            renderer: PostProcess::new(renderer, Effect::resolution((64, 48).into())),
28        }
29    }
More examples
Hide additional examples
examples/image.rs (lines 28-34)
18    fn init() -> Self {
19        let mut renderer = SimpleRendererBuilder {
20            projection_builder: Box::new(|size| {
21                SimpleProjection::center_height(size.as_vec2(), 2.0)
22            }),
23            ..SimpleRendererBuilder::default()
24        }
25        .build();
26        renderer.models.push(Model::quad_texture(
27            Arc::new(
28                Texture::from_path(
29                    "examples/test.png",
30                    SamplerType {
31                        address_mode: wgpu::AddressMode::ClampToEdge,
32                        filter_mode: wgpu::FilterMode::Linear,
33                    },
34                )
35                .unwrap(),
36            ),
37            vec![Transform::scale((0.5, 0.5, 1.0))],
38        ));
39
40        Self { renderer }
41    }
Source

pub fn write_image(&self, image: &RgbaImage)

Overwrite the image data in the texture.

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>