Skip to content

Commit

Permalink
blbh: change uv shrink to uv clamp strategy and that works way better
Browse files Browse the repository at this point in the history
  • Loading branch information
khanghugo committed Feb 25, 2025
1 parent 90658a9 commit ed96abe
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
36 changes: 18 additions & 18 deletions gchimp-native/src/gui/programs/blbh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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<Mutex<String>>,
}
Expand All @@ -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())),
}
Expand Down Expand Up @@ -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::<f32>()
.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
Expand Down
52 changes: 38 additions & 14 deletions gchimp/src/modules/blender_lightmap_baker_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand All @@ -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")]
Expand All @@ -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;

Expand Down Expand Up @@ -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<DVec2>| {
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<DVec2>| {
// 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::<Vec<DVec2>>()

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::<Vec<DVec2>>()
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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/"),
Expand Down

0 comments on commit ed96abe

Please sign in to comment.