From ed96abeb45bae58f2d509b086862d8163bafe2cf Mon Sep 17 00:00:00 2001 From: khang Date: Tue, 25 Feb 2025 10:35:49 -0500 Subject: [PATCH] blbh: change uv shrink to uv clamp strategy and that works way better --- gchimp-native/src/gui/programs/blbh.rs | 36 ++++++------- .../modules/blender_lightmap_baker_helper.rs | 52 ++++++++++++++----- 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/gchimp-native/src/gui/programs/blbh.rs b/gchimp-native/src/gui/programs/blbh.rs index 341d2a4..64d77a5 100644 --- a/gchimp-native/src/gui/programs/blbh.rs +++ b/gchimp-native/src/gui/programs/blbh.rs @@ -7,7 +7,7 @@ use std::{ use eframe::egui; use gchimp::modules::blender_lightmap_baker_helper::{ - blender_lightmap_baker_helper, BLBHOptions, BLBH, BLBH_DEFAULT_UV_SHRINK_FACTOR, + blender_lightmap_baker_helper, BLBHOptions, BLBH, BLBH_DEFAULT_UV_CLAMP_FACTOR, }; use crate::{ @@ -21,8 +21,8 @@ pub struct BLBHGui { smd_path: String, texture_path: String, options: BLBHOptions, - shrink_value: String, - check_shrink_value: bool, + clamp_value: String, + check_clamp_value: bool, // origin: String, status: Arc>, } @@ -34,8 +34,8 @@ impl BLBHGui { smd_path: Default::default(), texture_path: Default::default(), options: BLBHOptions::default(), - shrink_value: BLBH_DEFAULT_UV_SHRINK_FACTOR.to_string(), - check_shrink_value: false, + clamp_value: BLBH_DEFAULT_UV_CLAMP_FACTOR.to_string(), + check_clamp_value: false, // origin: "0 0 0".to_string(), status: Arc::new(Mutex::new("Idle".to_string())), } @@ -158,33 +158,33 @@ impl TabProgram for BLBHGui { }); ui.horizontal(|ui| { - ui.label("UV Shrink"); + ui.label("UV Clamp"); // only check value if lost focus - let text_editor = egui::TextEdit::singleline(&mut self.shrink_value).desired_width(80.); + let text_editor = egui::TextEdit::singleline(&mut self.clamp_value).desired_width(80.); let text_editor_ui = ui.add(text_editor).on_hover_text( "\ -UV coordinate from centroid will scale by this value. \n\ -If your texture has weird seams, consider lowering this number. \n\ -For best results, change this value by 1/512.", +There is a problem with the edge where it is repeatedly filtered. +With this option, the polygon UV will not sample the edge by avoiding the edge. +By default, it will \"shrink\" the UV in by 1 pixel wherever applicable.", ); if text_editor_ui.has_focus() { - self.check_shrink_value = true; + self.check_clamp_value = true; } - if text_editor_ui.lost_focus() && self.check_shrink_value { + if text_editor_ui.lost_focus() && self.check_clamp_value { let shrink_value = self - .shrink_value + .clamp_value .parse::() - .unwrap_or(BLBH_DEFAULT_UV_SHRINK_FACTOR); - self.shrink_value = shrink_value.to_string(); - self.options.uv_shrink_factor = shrink_value; - self.check_shrink_value = false; + .unwrap_or(BLBH_DEFAULT_UV_CLAMP_FACTOR); + self.clamp_value = shrink_value.to_string(); + self.options.uv_clamp_factor = shrink_value; + self.check_clamp_value = false; } if ui.button("Default").clicked() { - self.shrink_value = BLBH_DEFAULT_UV_SHRINK_FACTOR.to_string() + self.clamp_value = BLBH_DEFAULT_UV_CLAMP_FACTOR.to_string() } // origin diff --git a/gchimp/src/modules/blender_lightmap_baker_helper.rs b/gchimp/src/modules/blender_lightmap_baker_helper.rs index dd09604..fd1b916 100644 --- a/gchimp/src/modules/blender_lightmap_baker_helper.rs +++ b/gchimp/src/modules/blender_lightmap_baker_helper.rs @@ -33,7 +33,7 @@ pub struct BLBHOptions { pub convert_smd: bool, pub compile_model: bool, pub flat_shade: bool, - pub uv_shrink_factor: f32, + pub uv_clamp_factor: f32, // pub origin: DVec3, pub studiomdl: String, #[cfg(target_os = "linux")] @@ -47,7 +47,7 @@ impl Default for BLBHOptions { convert_smd: true, compile_model: true, flat_shade: true, - uv_shrink_factor: BLBH_DEFAULT_UV_SHRINK_FACTOR, + uv_clamp_factor: BLBH_DEFAULT_UV_CLAMP_FACTOR, // origin: DVec3::ZERO, studiomdl: Default::default(), #[cfg(target_os = "linux")] @@ -56,8 +56,7 @@ impl Default for BLBHOptions { } } -// shrink by 2 pixels inward -pub const BLBH_DEFAULT_UV_SHRINK_FACTOR: f32 = 0.99609375; +pub const BLBH_DEFAULT_UV_CLAMP_FACTOR: f32 = 0.001953125; const MINIMUM_SIZE: u32 = 512; @@ -150,18 +149,43 @@ pub fn blender_lightmap_baker_helper(blbh: &BLBH) -> eyre::Result<()> { DVec2::new(u, v) }; - // each vertex needs to shrink inward by 2 pixels so there's no seam + // ~~each vertex needs to shrink inward by 2 pixels so there's no seam // if not, the texture would repeat and that means texture filtering // the shitty thing here would be that the color difference might just be a seam - // but at least it isn't scaled up to a pixel - let shrink_uvs = |uvs: Vec| { - let centroid = uvs.iter().fold(DVec2::ZERO, |acc, &e| acc + e) / 3.; + // but at least it isn't scaled up to a pixel~~ + // UPDATE: + // the original problem is that the edge is touching the seam. so at first i did it by shrinking UV + // of every vertices + // it seems stupid because it is. Now the better solution is to clamp the UV + // we know that the issue is only at the edge, so clamp it so that they don't touch the edges + let clamp_uvs = |uvs: Vec| { + // old dumb code + // let centroid = uvs.iter().fold(DVec2::ZERO, |acc, &e| acc + e) / 3.; + + // uvs.iter() + // .map(|&uv| { + // let vector = uv - centroid; + // let vector = vector * options.uv_shrink_factor as f64; + // vector + centroid + // }) + // .collect::>() uvs.iter() - .map(|&uv| { - let vector = uv - centroid; - let vector = vector * options.uv_shrink_factor as f64; - vector + centroid + .map(|uv| { + let u = uv.x; + let v = uv.y; + + // uv_shrink factor should be small enough + let u = u.clamp( + 0. + blbh.options.uv_clamp_factor as f64, + 1. - blbh.options.uv_clamp_factor as f64, + ); + let v = v.clamp( + 0. + blbh.options.uv_clamp_factor as f64, + 1. - blbh.options.uv_clamp_factor as f64, + ); + + [u, v].into() }) .collect::>() }; @@ -405,7 +429,7 @@ pub fn blender_lightmap_baker_helper(blbh: &BLBH) -> eyre::Result<()> { ]; // scale the triangle so there's no seam - let uvs = shrink_uvs(uvs); + let uvs = clamp_uvs(uvs); let v0 = Vertex { parent: original_sin.parent, @@ -562,7 +586,7 @@ mod test { compile_model: true, flat_shade: true, // origin: DVec3::ZERO, - uv_shrink_factor: BLBH_DEFAULT_UV_SHRINK_FACTOR, + uv_clamp_factor: BLBH_DEFAULT_UV_CLAMP_FACTOR, studiomdl: String::from("/home/khang/gchimp/dist/studiomdl.exe"), #[cfg(target_os = "linux")] wineprefix: String::from("/home/khang/.local/share/wineprefixes/wine32/"),