Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

テクスチャのスケーリング (#586) #623

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 60 additions & 4 deletions nusamai/src/sink/cesiumtiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@
};
use utils::calculate_normal;

const MAX_PIXEL_PER_DISTANCE: f64 = 30.0;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

30くらいに設定しないと、何が記載されている看板か不明なレベルまで解像度が落ちてしまったので、暫定的に30にしました。
より良い数値は見つけていきましょう!

// WARN: This function has an equivalent in `atlas-packer/src/texture.rs`.
fn uv_to_pixel_coords(uv_coords: &[(f64, f64)], width: u32, height: u32) -> Vec<(u32, u32)> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

atlas-packer側の実装を変更してまでDRYを貫く必要性はないと感じているため、ここでは一旦重複した状態のままでコミットしています

良いと思います!

uv_coords
.iter()
.map(|(u, v)| {
(
(u.clamp(0.0, 1.0) * width as f64).min(width as f64 - 1.0) as u32,
((1.0 - v.clamp(0.0, 1.0)) * height as f64).min(height as f64 - 1.0) as u32,
)
})
.collect()
}

Check warning on line 62 in nusamai/src/sink/cesiumtiles/mod.rs

View check run for this annotation

Codecov / codecov/patch

nusamai/src/sink/cesiumtiles/mod.rs#L52-L62

Added lines #L52 - L62 were not covered by tests
Comment on lines +51 to +62
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関数 uv_to_pixel_coords の重複について

この関数はUV座標をピクセル座標に変換する機能を提供しますが、atlas-packer に同様の機能が存在しているとの警告があります。この重複が意図的である場合は問題ありませんが、将来的にメンテナンスの負担を減らすために一元化を検討することをお勧めします。

重複するコードの削減を検討してください。


pub struct CesiumTilesSinkProvider {}

impl DataSinkProvider for CesiumTilesSinkProvider {
Expand Down Expand Up @@ -502,14 +517,54 @@
.iter()
.map(|[x, y, z, u, v]| (*x, *y, *z, *u, *v))
.collect::<Vec<(f64, f64, f64, f64, f64)>>();

Check warning on line 520 in nusamai/src/sink/cesiumtiles/mod.rs

View check run for this annotation

Codecov / codecov/patch

nusamai/src/sink/cesiumtiles/mod.rs#L520

Added line #L520 was not covered by tests
let uv_coords = original_vertices
.iter()
.map(|(_, _, _, u, v)| (*u, *v))
.collect::<Vec<(f64, f64)>>();

let texture_uri = base_texture.uri.to_file_path().unwrap();
let texture_size = texture_size_cache.get_or_insert(&texture_uri);
let factor = apply_downsample_factor(tile_zoom);

let pixel_coords =
uv_to_pixel_coords(&uv_coords, texture_size.0, texture_size.1);

let pixel_per_distance = (0..original_vertices.len())
.map(|i| {
let j = (i + 1) % original_vertices.len();
let (euc0, txl0) = (
(
original_vertices[i].0,
original_vertices[i].1,
original_vertices[i].2,
),
pixel_coords[i],
);
let (euc1, txl1) = (
(
original_vertices[j].0,
original_vertices[j].1,
original_vertices[j].2,
),
pixel_coords[j],
);
let euc_dist = ((euc0.0 - euc1.0).powi(2)
+ (euc0.1 - euc1.1).powi(2)
+ (euc0.2 - euc1.2).powi(2))
.sqrt();
let txl_dist = ((txl0.0 as f64 - txl1.0 as f64).powi(2)
+ (txl0.1 as f64 - txl1.1 as f64).powi(2))
.sqrt();
txl_dist / euc_dist
})
.min_by(|a, b| a.total_cmp(b))
.unwrap_or(1.0);

let max_pixel_per_distance = MAX_PIXEL_PER_DISTANCE;
let downsample_scale =
(1.0_f64).min(max_pixel_per_distance / pixel_per_distance);
let factor = apply_downsample_factor(tile_zoom, downsample_scale as f32);

Check warning on line 567 in nusamai/src/sink/cesiumtiles/mod.rs

View check run for this annotation

Codecov / codecov/patch

nusamai/src/sink/cesiumtiles/mod.rs#L528-L567

Added lines #L528 - L567 were not covered by tests
let downsample_factor = DownsampleFactor::new(&factor);
let cropped_texture = CroppedTexture::new(
&texture_uri,
Expand Down Expand Up @@ -675,11 +730,12 @@
Ok(())
}

fn apply_downsample_factor(z: u8) -> f32 {
match z {
fn apply_downsample_factor(z: u8, downsample_scale: f32) -> f32 {
let f = match z {

Check warning on line 734 in nusamai/src/sink/cesiumtiles/mod.rs

View check run for this annotation

Codecov / codecov/patch

nusamai/src/sink/cesiumtiles/mod.rs#L733-L734

Added lines #L733 - L734 were not covered by tests
0..=14 => 0.0,
15..=16 => 0.25,
17 => 0.5,
_ => 1.0,
}
};
f * downsample_scale

Check warning on line 740 in nusamai/src/sink/cesiumtiles/mod.rs

View check run for this annotation

Codecov / codecov/patch

nusamai/src/sink/cesiumtiles/mod.rs#L740

Added line #L740 was not covered by tests
}