pub struct Texture {
pub bind_group: BindGroup,
pub texture: Texture,
pub view: TextureView,
}Expand description
A texture and associated structs.
Fields§
§bind_group: BindGroupA bind group of the GPU texture and its view.
texture: TextureThe texture as represented by wgpu.
view: TextureViewA view into the texture.
Implementations§
Source§impl Texture
impl Texture
Sourcepub fn new(
size: UVec2,
sampler: SamplerType,
format: TextureFormat,
usage: TextureUsages,
) -> Self
pub fn new( size: UVec2, sampler: SamplerType, format: TextureFormat, usage: TextureUsages, ) -> Self
Creates a new empty texture.
Sourcepub fn from_image(image: &RgbaImage, sampler: SamplerType) -> Result<Self>
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 }Sourcepub fn from_color<T: Into<Rgba<u8>> + Clone>(
color: &T,
resolution: UVec2,
sampler: SamplerType,
) -> Result<Self>
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.
Sourcepub fn from_texture(texture: Texture, sampler: SamplerType) -> Self
pub fn from_texture(texture: Texture, sampler: SamplerType) -> Self
Creates a texture from a wgpu::Texture.
Sourcepub fn from_path<P: AsRef<Path> + Display>(
path: P,
sampler: SamplerType,
) -> Result<Self>
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?
More 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 }Sourcepub fn write_image(&self, image: &RgbaImage)
pub fn write_image(&self, image: &RgbaImage)
Overwrite the image data in the texture.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Texture
impl !RefUnwindSafe for Texture
impl Send for Texture
impl Sync for Texture
impl Unpin for Texture
impl !UnwindSafe for Texture
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
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>
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)
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)
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
impl<T> DowncastSync for T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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