Minor cleanup
parent
a417abf099
commit
99f198c9d6
|
@ -1,4 +1,4 @@
|
||||||
[engine."plasma"]
|
[engine."plasma"]
|
||||||
|
|
||||||
thrust = 50
|
thrust = 50
|
||||||
flare.sprite = "flare::ion"
|
flare.sprite_texture = "flare::ion"
|
||||||
|
|
|
@ -10,7 +10,7 @@ rate = 0.2
|
||||||
rate_rng = 0.1
|
rate_rng = 0.1
|
||||||
|
|
||||||
|
|
||||||
projectile.sprite = "projectile::blaster"
|
projectile.sprite_texture = "projectile::blaster"
|
||||||
# Height of projectile in game units
|
# Height of projectile in game units
|
||||||
projectile.size = 10
|
projectile.size = 10
|
||||||
projectile.size_rng = 0.0
|
projectile.size_rng = 0.0
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
[ship."Gypsum"]
|
[ship."Gypsum"]
|
||||||
sprite = "ship::gypsum"
|
sprite_texture = "ship::gypsum"
|
||||||
size = 100
|
size = 100
|
||||||
mass = 10
|
mass = 10
|
||||||
hull = 200
|
hull = 200
|
||||||
|
|
|
@ -1,17 +1,17 @@
|
||||||
[system."12 Autumn Above"]
|
[system."12 Autumn Above"]
|
||||||
|
|
||||||
object.star.sprite = "star::star"
|
object.star.sprite_texture = "star::star"
|
||||||
object.star.position = [0.0, 0.0, 30.0]
|
object.star.position = [0.0, 0.0, 30.0]
|
||||||
object.star.size = 2000
|
object.star.size = 2000
|
||||||
|
|
||||||
object.earth.sprite = "planet::earth"
|
object.earth.sprite_texture = "planet::earth"
|
||||||
object.earth.position.center = "star"
|
object.earth.position.center = "star"
|
||||||
object.earth.position.radius = 4000
|
object.earth.position.radius = 4000
|
||||||
object.earth.position.angle = 0
|
object.earth.position.angle = 0
|
||||||
object.earth.position.z = 10.0
|
object.earth.position.z = 10.0
|
||||||
object.earth.size = 1000
|
object.earth.size = 1000
|
||||||
|
|
||||||
object.luna.sprite = "planet::luna"
|
object.luna.sprite_texture = "planet::luna"
|
||||||
object.luna.position.center = "earth"
|
object.luna.position.center = "earth"
|
||||||
object.luna.position.radius = 1600
|
object.luna.position.radius = 1600
|
||||||
object.luna.position.angle = 135
|
object.luna.position.angle = 135
|
||||||
|
|
|
@ -17,7 +17,7 @@ pub(super) mod syntax {
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Flare {
|
pub struct Flare {
|
||||||
pub sprite: String,
|
pub sprite_texture: String,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ pub struct Engine {
|
||||||
/// The flare sprite this engine creates.
|
/// The flare sprite this engine creates.
|
||||||
/// Its location and size is determined by a ship's
|
/// Its location and size is determined by a ship's
|
||||||
/// engine points.
|
/// engine points.
|
||||||
pub flare_sprite: TextureHandle,
|
pub flare_sprite_texture: TextureHandle,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl super::Build for Engine {
|
impl super::Build for Engine {
|
||||||
|
@ -41,11 +41,11 @@ impl super::Build for Engine {
|
||||||
|
|
||||||
fn build(engine: Self::InputSyntax, ct: &mut Content) -> Result<()> {
|
fn build(engine: Self::InputSyntax, ct: &mut Content) -> Result<()> {
|
||||||
for (engine_name, engine) in engine {
|
for (engine_name, engine) in engine {
|
||||||
let th = match ct.texture_index.get(&engine.flare.sprite) {
|
let th = match ct.texture_index.get(&engine.flare.sprite_texture) {
|
||||||
None => bail!(
|
None => bail!(
|
||||||
"In engine `{}`: texture `{}` doesn't exist",
|
"In engine `{}`: texture `{}` doesn't exist",
|
||||||
engine_name,
|
engine_name,
|
||||||
engine.flare.sprite
|
engine.flare.sprite_texture
|
||||||
),
|
),
|
||||||
Some(t) => *t,
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
@ -53,7 +53,7 @@ impl super::Build for Engine {
|
||||||
ct.engines.push(Self {
|
ct.engines.push(Self {
|
||||||
name: engine_name,
|
name: engine_name,
|
||||||
thrust: engine.thrust,
|
thrust: engine.thrust,
|
||||||
flare_sprite: th,
|
flare_sprite_texture: th,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub(super) mod syntax {
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Projectile {
|
pub struct Projectile {
|
||||||
pub sprite: String,
|
pub sprite_texture: String,
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
pub size_rng: f32,
|
pub size_rng: f32,
|
||||||
pub speed: f32,
|
pub speed: f32,
|
||||||
|
@ -59,7 +59,7 @@ pub struct Gun {
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Projectile {
|
pub struct Projectile {
|
||||||
/// The projectile sprite
|
/// The projectile sprite
|
||||||
pub sprite: TextureHandle,
|
pub sprite_texture: TextureHandle,
|
||||||
|
|
||||||
/// The average size of this projectile
|
/// The average size of this projectile
|
||||||
/// (height in game units)
|
/// (height in game units)
|
||||||
|
@ -87,11 +87,11 @@ impl super::Build for Gun {
|
||||||
|
|
||||||
fn build(gun: Self::InputSyntax, ct: &mut Content) -> Result<()> {
|
fn build(gun: Self::InputSyntax, ct: &mut Content) -> Result<()> {
|
||||||
for (gun_name, gun) in gun {
|
for (gun_name, gun) in gun {
|
||||||
let th = match ct.texture_index.get(&gun.projectile.sprite) {
|
let th = match ct.texture_index.get(&gun.projectile.sprite_texture) {
|
||||||
None => bail!(
|
None => bail!(
|
||||||
"In gun `{}`: texture `{}` doesn't exist",
|
"In gun `{}`: texture `{}` doesn't exist",
|
||||||
gun_name,
|
gun_name,
|
||||||
gun.projectile.sprite
|
gun.projectile.sprite_texture
|
||||||
),
|
),
|
||||||
Some(t) => *t,
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
@ -102,7 +102,7 @@ impl super::Build for Gun {
|
||||||
rate: gun.rate,
|
rate: gun.rate,
|
||||||
rate_rng: gun.rate_rng,
|
rate_rng: gun.rate_rng,
|
||||||
projectile: Projectile {
|
projectile: Projectile {
|
||||||
sprite: th,
|
sprite_texture: th,
|
||||||
size: gun.projectile.size,
|
size: gun.projectile.size,
|
||||||
size_rng: gun.projectile.size_rng,
|
size_rng: gun.projectile.size_rng,
|
||||||
speed: gun.projectile.speed,
|
speed: gun.projectile.speed,
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub(super) mod syntax {
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Ship {
|
pub struct Ship {
|
||||||
pub sprite: String,
|
pub sprite_texture: String,
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
pub engines: Vec<Engine>,
|
pub engines: Vec<Engine>,
|
||||||
pub guns: Vec<Gun>,
|
pub guns: Vec<Gun>,
|
||||||
|
@ -52,7 +52,7 @@ pub struct Ship {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
|
||||||
/// This ship's sprite
|
/// This ship's sprite
|
||||||
pub sprite: TextureHandle,
|
pub sprite_texture: TextureHandle,
|
||||||
|
|
||||||
/// The size of this ship.
|
/// The size of this ship.
|
||||||
/// Measured as unrotated height,
|
/// Measured as unrotated height,
|
||||||
|
@ -113,11 +113,11 @@ impl super::Build for Ship {
|
||||||
|
|
||||||
fn build(ship: Self::InputSyntax, ct: &mut Content) -> Result<()> {
|
fn build(ship: Self::InputSyntax, ct: &mut Content) -> Result<()> {
|
||||||
for (ship_name, ship) in ship {
|
for (ship_name, ship) in ship {
|
||||||
let th = match ct.texture_index.get(&ship.sprite) {
|
let th = match ct.texture_index.get(&ship.sprite_texture) {
|
||||||
None => bail!(
|
None => bail!(
|
||||||
"In ship `{}`: texture `{}` doesn't exist",
|
"In ship `{}`: texture `{}` doesn't exist",
|
||||||
ship_name,
|
ship_name,
|
||||||
ship.sprite
|
ship.sprite_texture
|
||||||
),
|
),
|
||||||
Some(t) => *t,
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
@ -128,7 +128,7 @@ impl super::Build for Ship {
|
||||||
ct.ships.push(Self {
|
ct.ships.push(Self {
|
||||||
aspect,
|
aspect,
|
||||||
name: ship_name,
|
name: ship_name,
|
||||||
sprite: th,
|
sprite_texture: th,
|
||||||
mass: ship.mass,
|
mass: ship.mass,
|
||||||
size,
|
size,
|
||||||
hull: ship.hull,
|
hull: ship.hull,
|
||||||
|
|
|
@ -17,7 +17,7 @@ pub(super) mod syntax {
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct Object {
|
pub struct Object {
|
||||||
pub sprite: String,
|
pub sprite_texture: String,
|
||||||
pub position: Position,
|
pub position: Position,
|
||||||
|
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
|
@ -94,7 +94,7 @@ pub struct System {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Object {
|
pub struct Object {
|
||||||
/// This object's sprite
|
/// This object's sprite
|
||||||
pub sprite: TextureHandle,
|
pub sprite_texture: TextureHandle,
|
||||||
|
|
||||||
/// This object's size.
|
/// This object's size.
|
||||||
/// Measured as height in game units.
|
/// Measured as height in game units.
|
||||||
|
@ -185,17 +185,17 @@ impl super::Build for System {
|
||||||
let mut cycle_detector = HashSet::new();
|
let mut cycle_detector = HashSet::new();
|
||||||
cycle_detector.insert(label.clone());
|
cycle_detector.insert(label.clone());
|
||||||
|
|
||||||
let th = match ct.texture_index.get(&obj.sprite) {
|
let th = match ct.texture_index.get(&obj.sprite_texture) {
|
||||||
None => bail!(
|
None => bail!(
|
||||||
"In system `{}`: texture `{}` doesn't exist",
|
"In system `{}`: texture `{}` doesn't exist",
|
||||||
system_name,
|
system_name,
|
||||||
obj.sprite
|
obj.sprite_texture
|
||||||
),
|
),
|
||||||
Some(t) => *t,
|
Some(t) => *t,
|
||||||
};
|
};
|
||||||
|
|
||||||
objects.push(Object {
|
objects.push(Object {
|
||||||
sprite: th,
|
sprite_texture: th,
|
||||||
position: resolve_position(&system.object, &obj, cycle_detector)
|
position: resolve_position(&system.object, &obj, cycle_detector)
|
||||||
.with_context(|| format!("In object {:#?}", label))?,
|
.with_context(|| format!("In object {:#?}", label))?,
|
||||||
size: obj.size,
|
size: obj.size,
|
||||||
|
|
|
@ -103,7 +103,7 @@ impl<'a> ShipOutfits {
|
||||||
// TODO: better way to pick flare texture
|
// TODO: better way to pick flare texture
|
||||||
self.engine_flare_sprites.clear();
|
self.engine_flare_sprites.clear();
|
||||||
let t = if let Some(e) = self.iter_engines().next() {
|
let t = if let Some(e) = self.iter_engines().next() {
|
||||||
e.flare_sprite
|
e.flare_sprite_texture
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
|
@ -50,7 +50,7 @@ impl System {
|
||||||
for o in &ct.objects {
|
for o in &ct.objects {
|
||||||
s.bodies.push(SystemObject {
|
s.bodies.push(SystemObject {
|
||||||
pos: o.position,
|
pos: o.position,
|
||||||
sprite: o.sprite,
|
sprite_texture: o.sprite_texture,
|
||||||
size: o.size,
|
size: o.size,
|
||||||
angle: o.angle,
|
angle: o.angle,
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,7 +4,7 @@ use galactica_content::TextureHandle;
|
||||||
use crate::render::Sprite;
|
use crate::render::Sprite;
|
||||||
|
|
||||||
pub struct SystemObject {
|
pub struct SystemObject {
|
||||||
pub sprite: TextureHandle,
|
pub sprite_texture: TextureHandle,
|
||||||
pub pos: Point3<f32>,
|
pub pos: Point3<f32>,
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
pub angle: Deg<f32>,
|
pub angle: Deg<f32>,
|
||||||
|
@ -13,7 +13,7 @@ pub struct SystemObject {
|
||||||
impl SystemObject {
|
impl SystemObject {
|
||||||
pub(super) fn get_sprite(&self) -> Sprite {
|
pub(super) fn get_sprite(&self) -> Sprite {
|
||||||
return Sprite {
|
return Sprite {
|
||||||
texture: self.sprite.clone(),
|
texture: self.sprite_texture,
|
||||||
pos: self.pos,
|
pos: self.pos,
|
||||||
angle: self.angle,
|
angle: self.angle,
|
||||||
size: self.size,
|
size: self.size,
|
||||||
|
|
|
@ -10,7 +10,7 @@ use crate::{physics::util, render::Sprite};
|
||||||
pub struct ProjectileBuilder {
|
pub struct ProjectileBuilder {
|
||||||
pub rigid_body: RigidBodyBuilder,
|
pub rigid_body: RigidBodyBuilder,
|
||||||
pub collider: ColliderBuilder,
|
pub collider: ColliderBuilder,
|
||||||
pub sprite: TextureHandle,
|
pub sprite_texture: TextureHandle,
|
||||||
pub lifetime: f32,
|
pub lifetime: f32,
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
pub damage: f32,
|
pub damage: f32,
|
||||||
|
@ -21,7 +21,7 @@ impl ProjectileBuilder {
|
||||||
Projectile {
|
Projectile {
|
||||||
rigid_body: r,
|
rigid_body: r,
|
||||||
collider: c,
|
collider: c,
|
||||||
sprite: self.sprite,
|
sprite_texture: self.sprite_texture,
|
||||||
lifetime: self.lifetime,
|
lifetime: self.lifetime,
|
||||||
size: self.size,
|
size: self.size,
|
||||||
damage: self.damage,
|
damage: self.damage,
|
||||||
|
@ -33,7 +33,7 @@ impl ProjectileBuilder {
|
||||||
pub struct Projectile {
|
pub struct Projectile {
|
||||||
pub rigid_body: RigidBodyHandle,
|
pub rigid_body: RigidBodyHandle,
|
||||||
pub collider: ColliderHandle,
|
pub collider: ColliderHandle,
|
||||||
pub sprite: TextureHandle,
|
pub sprite_texture: TextureHandle,
|
||||||
pub lifetime: f32,
|
pub lifetime: f32,
|
||||||
pub size: f32,
|
pub size: f32,
|
||||||
pub damage: f32,
|
pub damage: f32,
|
||||||
|
@ -52,7 +52,7 @@ impl Projectile {
|
||||||
let pos = util::rigidbody_position(r);
|
let pos = util::rigidbody_position(r);
|
||||||
let ang = util::rigidbody_angle(r);
|
let ang = util::rigidbody_angle(r);
|
||||||
Sprite {
|
Sprite {
|
||||||
texture: self.sprite.clone(),
|
texture: self.sprite_texture,
|
||||||
pos: Point3 {
|
pos: Point3 {
|
||||||
x: pos.x,
|
x: pos.x,
|
||||||
y: pos.y,
|
y: pos.y,
|
||||||
|
|
|
@ -42,7 +42,7 @@ pub struct Ship {
|
||||||
pub physics_handle: ShipHandle,
|
pub physics_handle: ShipHandle,
|
||||||
outfits: outfits::ShipOutfits,
|
outfits: outfits::ShipOutfits,
|
||||||
|
|
||||||
sprite: TextureHandle,
|
sprite_texture: TextureHandle,
|
||||||
size: f32,
|
size: f32,
|
||||||
pub hull: f32,
|
pub hull: f32,
|
||||||
pub controls: ShipControls,
|
pub controls: ShipControls,
|
||||||
|
@ -62,7 +62,7 @@ impl Ship {
|
||||||
Ship {
|
Ship {
|
||||||
physics_handle,
|
physics_handle,
|
||||||
outfits: o,
|
outfits: o,
|
||||||
sprite: c.sprite,
|
sprite_texture: c.sprite_texture,
|
||||||
size: c.size,
|
size: c.size,
|
||||||
hull: c.hull,
|
hull: c.hull,
|
||||||
controls: ShipControls::new(),
|
controls: ShipControls::new(),
|
||||||
|
@ -108,7 +108,7 @@ impl Ship {
|
||||||
out.push(ProjectileBuilder {
|
out.push(ProjectileBuilder {
|
||||||
rigid_body: p_r,
|
rigid_body: p_r,
|
||||||
collider: p_c,
|
collider: p_c,
|
||||||
sprite: g.kind.projectile.sprite,
|
sprite_texture: g.kind.projectile.sprite_texture,
|
||||||
lifetime: g.kind.projectile.lifetime
|
lifetime: g.kind.projectile.lifetime
|
||||||
+ rng.gen_range(
|
+ rng.gen_range(
|
||||||
-g.kind.projectile.lifetime_rng..=g.kind.projectile.lifetime_rng,
|
-g.kind.projectile.lifetime_rng..=g.kind.projectile.lifetime_rng,
|
||||||
|
@ -159,7 +159,7 @@ impl Ship {
|
||||||
|
|
||||||
Sprite {
|
Sprite {
|
||||||
pos: (ship_pos.x, ship_pos.y, 1.0).into(),
|
pos: (ship_pos.x, ship_pos.y, 1.0).into(),
|
||||||
texture: self.sprite.clone(), // TODO: sprite texture should be easy to clone
|
texture: self.sprite_texture.clone(), // TODO: sprite texture should be easy to clone
|
||||||
angle: ship_ang,
|
angle: ship_ang,
|
||||||
size: self.size,
|
size: self.size,
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,77 @@
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use galactica_content::{Content, TextureHandle};
|
use galactica_content::{Content, TextureHandle};
|
||||||
|
use image::GenericImageView;
|
||||||
use std::{collections::HashMap, fs::File, io::Read, num::NonZeroU32};
|
use std::{collections::HashMap, fs::File, io::Read, num::NonZeroU32};
|
||||||
use wgpu::BindGroupLayout;
|
use wgpu::BindGroupLayout;
|
||||||
|
|
||||||
use super::{rawtexture::RawTexture, Texture};
|
pub(super) struct RawTexture {
|
||||||
|
pub(super) view: wgpu::TextureView,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RawTexture {
|
||||||
|
pub(super) fn from_bytes(
|
||||||
|
device: &wgpu::Device,
|
||||||
|
queue: &wgpu::Queue,
|
||||||
|
bytes: &[u8],
|
||||||
|
label: &str,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let img = image::load_from_memory(bytes)?;
|
||||||
|
Self::from_image(device, queue, &img, Some(label))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn from_image(
|
||||||
|
device: &wgpu::Device,
|
||||||
|
queue: &wgpu::Queue,
|
||||||
|
img: &image::DynamicImage,
|
||||||
|
label: Option<&str>,
|
||||||
|
) -> Result<Self> {
|
||||||
|
let rgba = img.to_rgba8();
|
||||||
|
let dimensions = img.dimensions();
|
||||||
|
|
||||||
|
let size = wgpu::Extent3d {
|
||||||
|
width: dimensions.0,
|
||||||
|
height: dimensions.1,
|
||||||
|
depth_or_array_layers: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
||||||
|
label,
|
||||||
|
size,
|
||||||
|
mip_level_count: 1,
|
||||||
|
sample_count: 1,
|
||||||
|
dimension: wgpu::TextureDimension::D2,
|
||||||
|
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
||||||
|
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
||||||
|
view_formats: &[],
|
||||||
|
});
|
||||||
|
|
||||||
|
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
||||||
|
|
||||||
|
queue.write_texture(
|
||||||
|
wgpu::ImageCopyTexture {
|
||||||
|
aspect: wgpu::TextureAspect::All,
|
||||||
|
texture: &texture,
|
||||||
|
mip_level: 0,
|
||||||
|
origin: wgpu::Origin3d::ZERO,
|
||||||
|
},
|
||||||
|
&rgba,
|
||||||
|
wgpu::ImageDataLayout {
|
||||||
|
offset: 0,
|
||||||
|
bytes_per_row: Some(4 * dimensions.0),
|
||||||
|
rows_per_image: Some(dimensions.1),
|
||||||
|
},
|
||||||
|
size,
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(Self { view })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct Texture {
|
||||||
|
pub index: u32, // Index in texture array
|
||||||
|
pub aspect: f32, // width / height
|
||||||
|
}
|
||||||
|
|
||||||
pub struct TextureArray {
|
pub struct TextureArray {
|
||||||
pub bind_group: wgpu::BindGroup,
|
pub bind_group: wgpu::BindGroup,
|
|
@ -1,10 +0,0 @@
|
||||||
mod array;
|
|
||||||
mod rawtexture;
|
|
||||||
|
|
||||||
pub use array::TextureArray;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub struct Texture {
|
|
||||||
pub index: u32, // Index in texture array
|
|
||||||
pub aspect: f32, // width / height
|
|
||||||
}
|
|
|
@ -1,65 +0,0 @@
|
||||||
use anyhow::Result;
|
|
||||||
use image::GenericImageView;
|
|
||||||
|
|
||||||
pub(super) struct RawTexture {
|
|
||||||
pub(super) view: wgpu::TextureView,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl RawTexture {
|
|
||||||
pub(super) fn from_bytes(
|
|
||||||
device: &wgpu::Device,
|
|
||||||
queue: &wgpu::Queue,
|
|
||||||
bytes: &[u8],
|
|
||||||
label: &str,
|
|
||||||
) -> Result<Self> {
|
|
||||||
let img = image::load_from_memory(bytes)?;
|
|
||||||
Self::from_image(device, queue, &img, Some(label))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn from_image(
|
|
||||||
device: &wgpu::Device,
|
|
||||||
queue: &wgpu::Queue,
|
|
||||||
img: &image::DynamicImage,
|
|
||||||
label: Option<&str>,
|
|
||||||
) -> Result<Self> {
|
|
||||||
let rgba = img.to_rgba8();
|
|
||||||
let dimensions = img.dimensions();
|
|
||||||
|
|
||||||
let size = wgpu::Extent3d {
|
|
||||||
width: dimensions.0,
|
|
||||||
height: dimensions.1,
|
|
||||||
depth_or_array_layers: 1,
|
|
||||||
};
|
|
||||||
|
|
||||||
let texture = device.create_texture(&wgpu::TextureDescriptor {
|
|
||||||
label,
|
|
||||||
size,
|
|
||||||
mip_level_count: 1,
|
|
||||||
sample_count: 1,
|
|
||||||
dimension: wgpu::TextureDimension::D2,
|
|
||||||
format: wgpu::TextureFormat::Rgba8UnormSrgb,
|
|
||||||
usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST,
|
|
||||||
view_formats: &[],
|
|
||||||
});
|
|
||||||
|
|
||||||
let view = texture.create_view(&wgpu::TextureViewDescriptor::default());
|
|
||||||
|
|
||||||
queue.write_texture(
|
|
||||||
wgpu::ImageCopyTexture {
|
|
||||||
aspect: wgpu::TextureAspect::All,
|
|
||||||
texture: &texture,
|
|
||||||
mip_level: 0,
|
|
||||||
origin: wgpu::Origin3d::ZERO,
|
|
||||||
},
|
|
||||||
&rgba,
|
|
||||||
wgpu::ImageDataLayout {
|
|
||||||
offset: 0,
|
|
||||||
bytes_per_row: Some(4 * dimensions.0),
|
|
||||||
rows_per_image: Some(dimensions.1),
|
|
||||||
},
|
|
||||||
size,
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(Self { view })
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue