Skip to content

Commit

Permalink
处理【隐形墨水特效】在不同像素密度下的边界情况
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Nov 16, 2024
1 parent f980462 commit 2f446b9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
20 changes: 15 additions & 5 deletions code/integration-and-debugging/wgpu_in_web/src/particle_ink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,24 @@ impl ParticleInk {
pub fn new(app: &AppSurface, gen: &ParticleGen) -> Self {
let particle_count = gen.count as usize;
let scale_factor = app.scale_factor;
let tex_size = gen.text_tex.size;

let surface_size = vec2(app.config.width as f32, app.config.height as f32);
let mut tex_size = vec2(
gen.text_tex.size.width as f32,
gen.text_tex.size.height as f32,
);
// 如果 tex_size 的 x 或 y 大于 surface_size,则等比缩放 tex_size
if tex_size.x > surface_size.x || tex_size.y > surface_size.y {
let scale_factor = (surface_size.x / tex_size.x).min(surface_size.y / tex_size.y);
tex_size *= scale_factor;
}

// 视口位置与大小
let viewport = Vec4::new(
(app.config.width as f32 - tex_size.width as f32) / 2.0,
(app.config.height as f32 - tex_size.height as f32) / 2.0,
tex_size.width as f32,
tex_size.height as f32,
(app.config.width as f32 - tex_size.x as f32) / 2.0,
(app.config.height as f32 - tex_size.y as f32) / 2.0,
tex_size.x,
tex_size.y,
);

// 粒子像素尺寸
Expand Down
2 changes: 1 addition & 1 deletion code/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) fn application_root_dir() -> String {
"/"
}
} else if host.contains("jinleili.github.io") {
href
String::from("https://jinleili.github.io/learn-wgpu-zh/")
} else {
String::from("https://cannot.access/")
}
Expand Down
1 change: 0 additions & 1 deletion code/utils/src/load_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ pub struct AnyTexture {

#[cfg(target_arch = "wasm32")]
pub async fn get_web_img(img_name: &str) -> Result<Vec<u8>, reqwest::Error> {
log::info!("super::application_root_dir(): {}", super::application_root_dir());
let url =
reqwest::Url::parse(&format!("{}{}", super::application_root_dir(), img_name,)).unwrap();
let data = reqwest::get(url).await?.bytes().await?.to_vec();
Expand Down

0 comments on commit 2f446b9

Please sign in to comment.