Skip to content

Commit

Permalink
3D Tilesのglbファイルをgzipで圧縮した状態で表示するオプションを追加 (#672)
Browse files Browse the repository at this point in the history
<!-- Close or Related Issues -->
Close #665

### What I did(変更内容)
<!-- Please describe the motivation behind this PR and the changes it
introduces. -->
<!-- どのような変更をしますか? 目的は? -->

- gzipで圧縮するオプションを追加した
- 拡張子は変更なしで「.glb」のまま
- そのままではCesiumで読み込めない(配信サーバーでヘッダーを付与する必要がある)ことをマニュアルに追記する必要がある

### Notes(連絡事項)
<!-- If manual testing is required, please describe the procedure. -->
<!-- 手動での動作確認が必要なら手順を簡単に伝えてください。そのほか連絡事項など。 -->

None / なし
  • Loading branch information
nokonoko1203 authored Oct 23, 2024
1 parent 3da4084 commit 8377f80
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion nusamai-plateau/src/models/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ pub enum TopLevelCityObject {
#[citygml(path = b"urf:TideFacility")]
TideFacility(urf::TideFacility),
#[citygml(path = b"urf:TrafficFacility")]
TrafficFacility(urf::TrafficFacility),
TrafficFacility(Box<urf::TrafficFacility>),
#[citygml(path = b"urf:TreatmentFacility")]
TreatmentFacility(urf::TreatmentFacility),
#[citygml(path = b"urf:TreePlantingDistrict")]
Expand Down
26 changes: 21 additions & 5 deletions nusamai/src/sink/cesiumtiles/gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::io::Write;

use ahash::{HashMap, HashSet};
use byteorder::{ByteOrder, LittleEndian};
use flate2::{write::GzEncoder, Compression};
use indexmap::IndexSet;
use nusamai_gltf_json::extensions::mesh::ext_mesh_features;

Expand All @@ -16,6 +17,7 @@ pub struct PrimitiveInfo {

pub type Primitives = HashMap<material::Material, PrimitiveInfo>;

#[allow(clippy::too_many_arguments)]
pub fn write_gltf_glb<W: Write>(
feedback: &feedback::Feedback,
writer: W,
Expand All @@ -24,6 +26,7 @@ pub fn write_gltf_glb<W: Write>(
primitives: Primitives,
num_features: usize,
metadata_encoder: MetadataEncoder,
gzip_compress: bool,
) -> Result<(), PipelineError> {
use nusamai_gltf_json::*;

Expand Down Expand Up @@ -281,12 +284,25 @@ pub fn write_gltf_glb<W: Write>(
..Default::default()
};

// Write glb to the writer
nusamai_gltf::glb::Glb {
json: serde_json::to_vec(&gltf).unwrap().into(),
bin: Some(bin_content.into()),
if gzip_compress {
// Write glb to the writer with gzip compression
let mut encoder = GzEncoder::new(writer, Compression::default());

nusamai_gltf::glb::Glb {
json: serde_json::to_vec(&gltf).unwrap().into(),
bin: Some(bin_content.into()),
}
.to_writer_with_alignment(&mut encoder, 8)?;

encoder.finish()?;
} else {
// Write glb to the writer
nusamai_gltf::glb::Glb {
json: serde_json::to_vec(&gltf).unwrap().into(),
bin: Some(bin_content.into()),
}
.to_writer_with_alignment(writer, 8)?;
}
.to_writer_with_alignment(writer, 8)?;

Ok(())
}
16 changes: 16 additions & 0 deletions nusamai/src/sink/cesiumtiles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ impl DataSinkProvider for CesiumTilesSinkProvider {
},
});
params.define(limit_texture_resolution_parameter(false));
params.define(ParameterDefinition {
key: "gzip".into(),
entry: ParameterEntry {
description: "gzip compress".into(),
required: false,
parameter: ParameterType::Boolean(BooleanParameter { value: Some(false) }),
label: Some("gzipで圧縮する".into()),
},
});

params
}
Expand All @@ -112,12 +121,14 @@ impl DataSinkProvider for CesiumTilesSinkProvider {
let max_z = get_parameter_value!(params, "max_z", Integer).unwrap() as u8;
let limit_texture_resolution =
*get_parameter_value!(params, "limit_texture_resolution", Boolean);
let gzip_compress = *get_parameter_value!(params, "gzip", Boolean);
let transform_settings = self.transformer_options();

Box::<CesiumTilesSink>::new(CesiumTilesSink {
output_path: output_path.as_ref().unwrap().into(),
transform_settings,
limit_texture_resolution,
gzip_compress,
min_z,
max_z,
})
Expand All @@ -128,6 +139,7 @@ struct CesiumTilesSink {
output_path: PathBuf,
transform_settings: TransformerRegistry,
limit_texture_resolution: Option<bool>,
gzip_compress: Option<bool>,
min_z: u8,
max_z: u8,
}
Expand Down Expand Up @@ -157,6 +169,7 @@ impl DataSink for CesiumTilesSink {
let max_zoom = self.max_z;

let limit_texture_resolution = self.limit_texture_resolution;
let gzip_compress = self.gzip_compress;

// TODO: refactoring

Expand Down Expand Up @@ -205,6 +218,7 @@ impl DataSink for CesiumTilesSink {
tile_id_conv,
schema,
limit_texture_resolution,
gzip_compress,
) {
feedback.fatal_error(error);
}
Expand Down Expand Up @@ -327,6 +341,7 @@ fn tile_writing_stage(
tile_id_conv: TileIdMethod,
schema: &Schema,
limit_texture_resolution: Option<bool>,
gzip_compress: Option<bool>,
) -> Result<()> {
let ellipsoid = nusamai_projection::ellipsoid::wgs84();
let contents: Arc<Mutex<Vec<TileContent>>> = Default::default();
Expand Down Expand Up @@ -707,6 +722,7 @@ fn tile_writing_stage(
primitives,
features.len(),
metadata_encoder,
gzip_compress.unwrap_or_default(),
)?;

Ok::<(), PipelineError>(())
Expand Down
2 changes: 1 addition & 1 deletion nusamai/src/sink/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn limit_texture_resolution_parameter(default_value: bool) -> ParameterDefin
parameter: ParameterType::Boolean(BooleanParameter {
value: Some(default_value),
}),
label: Some("距離(メートル)あたりのテクスチャの解像度を制限する".into()),
label: Some("距離あたりの解像度を制限する".into()),
},
}
}

0 comments on commit 8377f80

Please sign in to comment.