Skip to content

Commit

Permalink
3dtiles: EXT_texture_webpを利用する (#604)
Browse files Browse the repository at this point in the history
<!-- Close or Related Issues -->
Close #597

### Description(変更内容)
<!-- Please describe the motivation behind this PR and the changes it introduces. -->
<!-- 何のために、どのような変更をしますか? -->

- テクスチャにwebpが含まれている場合、EXT_texture_webpの拡張機能を利用するように変更

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

- None / なし
  • Loading branch information
satoshi7190 authored Jul 18, 2024
1 parent 11e5a8b commit 944b234
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod gltf;
pub mod mesh;
pub mod texture;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
pub struct ExtTextureWebp {
pub source: u32,
}

#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]

pub struct TextureExtensions {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "EXT_texture_webp")]
pub ext_texture_webp: Option<ExtTextureWebp>,

#[serde(flatten)]
pub others: HashMap<String, Value>,
}
2 changes: 2 additions & 0 deletions nusamai-gltf/nusamai-gltf-json/src/models/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub enum MimeType {
ImageJpeg,
#[serde(rename = "image/png")]
ImagePng,
#[serde(rename = "image/webp")]
ImageWebp,
}

/// Image data used to create a texture. Image MAY be referenced by an URI (or IRI) or a buffer view index.
Expand Down
12 changes: 2 additions & 10 deletions nusamai-gltf/nusamai-gltf-json/src/models/texture.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::HashMap;

use crate::extensions;
use serde::{Deserialize, Serialize};
use serde_json::Value;

Expand All @@ -22,16 +21,9 @@ pub struct Texture {

/// JSON object with extension-specific objects.
#[serde(skip_serializing_if = "Option::is_none")]
pub extensions: Option<TextureExtensions>,
pub extensions: Option<extensions::texture::TextureExtensions>,

/// Application-specific data.
#[serde(skip_serializing_if = "Option::is_none")]
pub extras: Option<Value>,
}

#[derive(Serialize, Deserialize, Debug, Default, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TextureExtensions {
#[serde(flatten)]
others: HashMap<String, Value>,
}
27 changes: 23 additions & 4 deletions nusamai/src/sink/cesiumtiles/gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,28 @@ pub fn write_gltf_glb<W: Write>(
buffers
};

let has_webp = gltf_textures.iter().any(|texture| {
texture
.extensions
.as_ref()
.and_then(|ext| ext.ext_texture_webp.as_ref())
.map_or(false, |_| true)
});

let extensions_used = {
let mut extensions_used = vec![
"EXT_mesh_features".to_string(),
"EXT_structural_metadata".to_string(),
];

// Add "EXT_texture_webp" extension if WebP textures are present
if has_webp {
extensions_used.push("EXT_texture_webp".to_string());
}

extensions_used
};

feedback.ensure_not_canceled()?;

// Build the JSON part of glTF
Expand All @@ -255,10 +277,7 @@ pub fn write_gltf_glb<W: Write>(
..Default::default()
}
.into(),
extensions_used: vec![
"EXT_mesh_features".to_string(),
"EXT_structural_metadata".to_string(),
],
extensions_used,
..Default::default()
};

Expand Down
32 changes: 29 additions & 3 deletions nusamai/src/sink/cesiumtiles/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,31 @@ impl Texture {
let (image_index, _) = images.insert_full(Image {
uri: self.uri.clone(),
});
nusamai_gltf_json::Texture {
source: Some(image_index as u32),
..Default::default()

// Get the file extension
let extension = Path::new(self.uri.path())
.extension()
.and_then(|ext| ext.to_str())
.map(|ext| ext.to_lowercase());

if extension == Some("webp".to_string()) {
nusamai_gltf_json::Texture {
extensions: Some(nusamai_gltf_json::extensions::texture::TextureExtensions {
ext_texture_webp: Some(
nusamai_gltf_json::extensions::texture::ExtTextureWebp {
source: image_index as u32,
},
),
..Default::default()
}),
source: Some(image_index as u32),
..Default::default()
}
} else {
nusamai_gltf_json::Texture {
source: Some(image_index as u32),
..Default::default()
}
}
}
}
Expand Down Expand Up @@ -134,6 +156,10 @@ fn load_image(feedback: &Feedback, path: &Path) -> std::io::Result<(Vec<u8>, Mim
feedback.info(format!("Embedding a jpeg as is: {:?}", path));
Ok((std::fs::read(path)?, MimeType::ImageJpeg))
}
Some("webp") => {
feedback.info(format!("Embedding a webp as is: {:?}", path));
Ok((std::fs::read(path)?, MimeType::ImageWebp))
}
_ => {
let err = format!("Unsupported image format: {:?}", path);
log::error!("{}", err);
Expand Down

0 comments on commit 944b234

Please sign in to comment.