pub struct TextureAsset {
pub data: Vec<u8>,
pub width: u32,
pub height: u32,
pub format: TextureFormat,
}Expand description
A loaded texture asset containing image data.
TextureAsset stores decoded image data in memory. It does not contain
GPU resources - those should be created separately from this data.
§Fields
data: Raw pixel data in RGBA8 format (4 bytes per pixel)width: Image width in pixelsheight: Image height in pixelsformat: The original image format (PNG, JPG, etc.)
§Example
use goud_engine::assets::{Asset, loaders::TextureAsset};
let texture = TextureAsset {
data: vec![255; 64 * 64 * 4], // 64x64 white texture
width: 64,
height: 64,
format: goud_engine::assets::loaders::TextureFormat::Png,
};
assert_eq!(texture.pixel_count(), 64 * 64);
assert_eq!(texture.bytes_per_pixel(), 4);Fields§
§data: Vec<u8>Raw pixel data in RGBA8 format (4 bytes per pixel).
width: u32Width of the texture in pixels.
height: u32Height of the texture in pixels.
format: TextureFormatThe original image format this texture was loaded from.
Implementations§
Source§impl TextureAsset
impl TextureAsset
Sourcepub fn new(
data: Vec<u8>,
width: u32,
height: u32,
format: TextureFormat,
) -> Self
pub fn new( data: Vec<u8>, width: u32, height: u32, format: TextureFormat, ) -> Self
Creates a new texture asset from raw RGBA8 data.
§Arguments
data: Raw pixel data in RGBA8 format (must be width × height × 4 bytes)width: Image width in pixelsheight: Image height in pixelsformat: The image format this data represents
§Panics
Panics if data length doesn’t match width × height × 4.
§Example
use goud_engine::assets::loaders::{TextureAsset, TextureFormat};
let data = vec![255; 4 * 4 * 4]; // 4x4 white texture
let texture = TextureAsset::new(data, 4, 4, TextureFormat::Png);
assert_eq!(texture.pixel_count(), 16);Sourcepub fn pixel_count(&self) -> u32
pub fn pixel_count(&self) -> u32
Returns the total number of pixels in the texture.
Sourcepub const fn bytes_per_pixel(&self) -> u32
pub const fn bytes_per_pixel(&self) -> u32
Returns the number of bytes per pixel (always 4 for RGBA8).
Sourcepub fn size_bytes(&self) -> usize
pub fn size_bytes(&self) -> usize
Returns the total size of the texture data in bytes.
Sourcepub fn aspect_ratio(&self) -> f32
pub fn aspect_ratio(&self) -> f32
Returns the aspect ratio (width / height) of the texture.
Sourcepub fn is_power_of_two(&self) -> bool
pub fn is_power_of_two(&self) -> bool
Returns true if the texture dimensions are powers of two.
Power-of-two textures are required by some older GPUs and can be more efficient for certain operations.
Sourcepub fn get_pixel(&self, x: u32, y: u32) -> Option<&[u8]>
pub fn get_pixel(&self, x: u32, y: u32) -> Option<&[u8]>
Returns a slice of the pixel data for a specific pixel.
Returns None if the coordinates are out of bounds.
§Example
use goud_engine::assets::loaders::{TextureAsset, TextureFormat};
let data = vec![255, 0, 0, 255, 0, 255, 0, 255]; // 2 pixels: red, green
let texture = TextureAsset::new(data, 2, 1, TextureFormat::Png);
let pixel = texture.get_pixel(0, 0).unwrap();
assert_eq!(pixel, &[255, 0, 0, 255]); // Red
let pixel = texture.get_pixel(1, 0).unwrap();
assert_eq!(pixel, &[0, 255, 0, 255]); // GreenTrait Implementations§
Source§impl Asset for TextureAsset
impl Asset for TextureAsset
Source§fn asset_type_name() -> &'static str
fn asset_type_name() -> &'static str
Source§fn asset_type() -> AssetType
fn asset_type() -> AssetType
Source§fn extensions() -> &'static [&'static str]
fn extensions() -> &'static [&'static str]
Source§impl Clone for TextureAsset
impl Clone for TextureAsset
Source§fn clone(&self) -> TextureAsset
fn clone(&self) -> TextureAsset
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for TextureAsset
impl RefUnwindSafe for TextureAsset
impl Send for TextureAsset
impl Sync for TextureAsset
impl Unpin for TextureAsset
impl UnsafeUnpin for TextureAsset
impl UnwindSafe for TextureAsset
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
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>
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>
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