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

3dtiles: structural metadata のエンコーダ #393

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
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
21 changes: 13 additions & 8 deletions nusamai/src/sink/cesiumtiles/gltf.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::io::Write;

use crate::sink::cesiumtiles::metadata::make_dummy_metadata;

use super::material;
use super::{material, metadata::MetadataEncoder};
use ahash::{HashMap, HashSet};
use byteorder::{ByteOrder, LittleEndian};
use indexmap::IndexSet;
Expand All @@ -16,13 +14,13 @@ pub struct PrimitiveInfo {

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

/// とりいそぎの実装
pub fn write_gltf_glb<W: Write>(
writer: W,
translation: [f64; 3],
vertices: impl IntoIterator<Item = [u32; 9]>,
primitives: Primitives,
num_features: usize,
metadata_encoder: MetadataEncoder,
) -> std::io::Result<()> {
use nusamai_gltf_json::*;

Expand Down Expand Up @@ -62,6 +60,7 @@ pub fn write_gltf_glb<W: Write>(
let len_vertices = bin_content.len() - buffer_offset;
if len_vertices > 0 {
gltf_buffer_views.push(BufferView {
name: Some("vertices".to_string()),
byte_offset: buffer_offset as u32,
byte_length: len_vertices as u32,
byte_stride: Some(VERTEX_BYTE_STRIDE as u8),
Expand All @@ -71,6 +70,7 @@ pub fn write_gltf_glb<W: Write>(

// accessor (positions)
gltf_accessors.push(Accessor {
name: Some("positions".to_string()),
buffer_view: Some(gltf_buffer_views.len() as u32 - 1),
component_type: ComponentType::Float,
count: vertices_count,
Expand All @@ -82,6 +82,7 @@ pub fn write_gltf_glb<W: Write>(

// accessor (normal)
gltf_accessors.push(Accessor {
name: Some("normals".to_string()),
buffer_view: Some(gltf_buffer_views.len() as u32 - 1),
byte_offset: 4 * 3,
component_type: ComponentType::Float,
Expand All @@ -92,6 +93,7 @@ pub fn write_gltf_glb<W: Write>(

// accessor (texcoords)
gltf_accessors.push(Accessor {
name: Some("texcoords".to_string()),
buffer_view: Some(gltf_buffer_views.len() as u32 - 1),
byte_offset: 4 * 6,
component_type: ComponentType::Float,
Expand All @@ -102,6 +104,7 @@ pub fn write_gltf_glb<W: Write>(

// accessor (feature_id)
gltf_accessors.push(Accessor {
name: Some("_feature_ids".to_string()),
buffer_view: Some(gltf_buffer_views.len() as u32 - 1),
byte_offset: 4 * 8,
component_type: ComponentType::Float,
Expand All @@ -114,6 +117,9 @@ pub fn write_gltf_glb<W: Write>(

let mut gltf_primitives = vec![];

let structural_metadata =
metadata_encoder.into_metadata(&mut bin_content, &mut gltf_buffer_views);

// indices
{
let indices_offset = bin_content.len();
Expand All @@ -127,6 +133,7 @@ pub fn write_gltf_glb<W: Write>(
}

gltf_accessors.push(Accessor {
name: Some("indices".to_string()),
buffer_view: Some(gltf_buffer_views.len() as u32),
byte_offset,
component_type: ComponentType::UnsignedInt,
Expand Down Expand Up @@ -170,6 +177,7 @@ pub fn write_gltf_glb<W: Write>(
let indices_len = bin_content.len() - indices_offset;
if indices_len > 0 {
gltf_buffer_views.push(BufferView {
name: Some("indices".to_string()),
byte_offset: indices_offset as u32,
byte_length: indices_len as u32,
target: Some(BufferViewTarget::ElementArrayBuffer),
Expand Down Expand Up @@ -205,9 +213,6 @@ pub fn write_gltf_glb<W: Write>(
});
}

let ext_structural_metadata =
make_dummy_metadata(num_features, &mut bin_content, &mut gltf_buffer_views);

let gltf_buffers = {
let mut buffers = vec![];
if !bin_content.is_empty() {
Expand Down Expand Up @@ -238,7 +243,7 @@ pub fn write_gltf_glb<W: Write>(
buffer_views: gltf_buffer_views,
buffers: gltf_buffers,
extensions: nusamai_gltf_json::extensions::gltf::Gltf {
ext_structural_metadata,
ext_structural_metadata: structural_metadata,
..Default::default()
}
.into(),
Expand Down
1 change: 1 addition & 0 deletions nusamai/src/sink/cesiumtiles/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
let content = load_image(&path)?;

buffer_views.push(BufferView {
name: Some("image".to_string()),

Check warning on line 90 in nusamai/src/sink/cesiumtiles/material.rs

View check run for this annotation

Codecov / codecov/patch

nusamai/src/sink/cesiumtiles/material.rs#L90

Added line #L90 was not covered by tests
byte_offset: bin_content.len() as u32,
byte_length: content.len() as u32,
..Default::default()
Expand Down
Loading