summaryrefslogtreecommitdiff
path: root/src/graphics/command.rs
diff options
context:
space:
mode:
authorTengu712 <genreihoutengu@gmail.com>2026-10-07 00:57:01 +0900
committerTengu712 <genreihoutengu@gmail.com>2026-10-07 00:57:01 +0900
commit3bbe5ebcdb2a5aa6810dd05576c6ec031efdc1c7 (patch)
tree844e4ba5efc755dabf091a51178937a93e1d47ed /src/graphics/command.rs
parent503486e2ce889858aac619985516374bddeb7a8f (diff)
render imageHEADmain
Diffstat (limited to 'src/graphics/command.rs')
-rw-r--r--src/graphics/command.rs53
1 files changed, 48 insertions, 5 deletions
diff --git a/src/graphics/command.rs b/src/graphics/command.rs
index f0330cb..6c0b036 100644
--- a/src/graphics/command.rs
+++ b/src/graphics/command.rs
@@ -1,8 +1,10 @@
use super::{
InstanceData,
font::{self, CharAtlas},
+ image::ImageId,
+ texture::TextureWrapper,
};
-use glam::Vec2;
+use glam::{Mat2, Vec2};
use wgpu::Queue;
pub enum HorizontalAlign {
@@ -25,8 +27,18 @@ pub struct Text {
pub v_align: VerticalAlign,
}
+pub struct Image {
+ pub id: ImageId,
+ pub origin: Vec2,
+ pub rotation: f32,
+ pub scale: Vec2,
+ pub uv_offset: Vec2,
+ pub uv_size: Vec2,
+}
+
pub enum RenderCommand {
Text(Text),
+ Image(Image),
}
impl RenderCommand {
@@ -37,6 +49,7 @@ impl RenderCommand {
/// * scale_factor - 画面拡大率
/// * queue - queue
/// * char_atlas - 文字テクスチャアトラス
+ /// * images - 画像テクスチャ
/// * buf - インスタンスバッファ
pub fn build(
&self,
@@ -45,6 +58,7 @@ impl RenderCommand {
scale_factor: f32,
queue: &Queue,
char_atlas: &mut CharAtlas,
+ images: &[TextureWrapper],
buf: &mut Vec<InstanceData>,
) {
match self {
@@ -57,6 +71,9 @@ impl RenderCommand {
char_atlas,
buf,
),
+ RenderCommand::Image(image) => {
+ build_image(image, screen_width, screen_height, images, buf)
+ }
}
}
}
@@ -108,12 +125,18 @@ fn build_text(
let center_x = x + ci.x_offset + ci.width / 2.0;
let center_y = y + ci.y_offset + ci.height / 2.0;
buf.push(InstanceData {
- position: Vec2::new(
+ transform: Mat2::from_diagonal(Vec2::new(
+ ci.width / screen_width * 2.0,
+ -ci.height / screen_height * 2.0,
+ )),
+ translation: Vec2::new(
center_x / screen_width * 2.0 - 1.0,
1.0 - center_y / screen_height * 2.0,
),
- scale: Vec2::new(ci.width / screen_width, ci.height / screen_height),
- uv: ci.uv,
+ uv_offset: ci.uv_offset,
+ uv_size: ci.uv_size,
+ tex_id: u32::MAX,
+ _pad: 0,
});
ox += font::h_advance(scale, ci.glyph_id);
}
@@ -128,9 +151,29 @@ fn build_text(
let left = (left * scale_factor).round() / scale_factor;
for instance in &mut buf[start..] {
- instance.position.x += left / screen_width * 2.0;
+ instance.translation.x += left / screen_width * 2.0;
}
baseline += line_height;
}
}
+
+fn build_image(
+ image: &Image,
+ screen_width: f32,
+ screen_height: f32,
+ images: &[TextureWrapper],
+ buf: &mut Vec<InstanceData>,
+) {
+ let texture = &images[image.id as usize];
+ let size = image.uv_size * Vec2::new(texture.width as f32, texture.height as f32) * image.scale;
+ let ndc = Vec2::new(2.0 / screen_width, -2.0 / screen_height);
+ buf.push(InstanceData {
+ transform: Mat2::from_diagonal(ndc) * Mat2::from_scale_angle(size, image.rotation),
+ translation: image.origin * ndc + Vec2::new(-1.0, 1.0),
+ uv_offset: image.uv_offset,
+ uv_size: image.uv_size,
+ tex_id: image.id as u32,
+ _pad: 0,
+ });
+}