Skip to content

Commit

Permalink
glTF 定義の小さな改善・修正: EXT_* のモジュール分けなど (#91)
Browse files Browse the repository at this point in the history
part of #51 

ちょっとした改善・修正です。3D Tilesまわりの拡張の、種類ごとのモジュール分けなど。
  • Loading branch information
ciscorn authored Dec 21, 2023
1 parent 841a636 commit 0a372cb
Show file tree
Hide file tree
Showing 21 changed files with 222 additions and 267 deletions.
22 changes: 9 additions & 13 deletions nusamai-gltf/examples/geometry_to_gltf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ struct Triangles {

impl Triangles {
pub fn new(indices: Vec<u32>, vertices: IndexSet<[u32; 3]>) -> Self {
Self {
indices,
vertices,
..Default::default()
}
Self { indices, vertices }
}
}

Expand Down Expand Up @@ -364,7 +360,7 @@ fn make_gltf_json(triangles: &Triangles) -> String {
buffer.byte_length = indices_byte_length + vertices_byte_length;
buffer.uri = Some("data.bin".to_string());

gltf.buffers = Some(vec![buffer]);
gltf.buffers = vec![buffer];

// glTF のバッファビューを作成
let mut buffer_view1 = BufferView::new();
Expand All @@ -379,7 +375,7 @@ fn make_gltf_json(triangles: &Triangles) -> String {
buffer_view2.byte_offset = indices_byte_length;
buffer_view2.target = Some(BufferViewTarget::ArrayBuffer);

gltf.buffer_views = Some(vec![buffer_view1, buffer_view2]);
gltf.buffer_views = vec![buffer_view1, buffer_view2];

// glTF のアクセサを作成
let mut accessor1 = Accessor::new();
Expand All @@ -389,8 +385,8 @@ fn make_gltf_json(triangles: &Triangles) -> String {
accessor1.count = indices.len() as u32;
accessor1.type_ = AccessorType::Scalar;
let max_indices = indices.iter().max().unwrap();
accessor1.max = Some(vec![*max_indices as f32]);
accessor1.min = Some(vec![0.0]);
accessor1.max = vec![*max_indices as f32].into();
accessor1.min = vec![0.0].into();

let mut accessor2 = Accessor::new();
accessor2.buffer_view = Some(1);
Expand All @@ -413,7 +409,7 @@ fn make_gltf_json(triangles: &Triangles) -> String {
accessor2.max = Some(max_vertex.to_vec());
accessor2.min = Some(min_vertex.to_vec());

gltf.accessors = Some(vec![accessor1, accessor2]);
gltf.accessors = vec![accessor1, accessor2];

// glTF のメッシュを作成
let mut mesh = Mesh::new();
Expand All @@ -428,19 +424,19 @@ fn make_gltf_json(triangles: &Triangles) -> String {

mesh.primitives = vec![primitive1];

gltf.meshes = Some(vec![mesh]);
gltf.meshes = vec![mesh];

// glTF のシーンを作成
let mut scene = Scene::new();
scene.nodes = Some(vec![0]);

gltf.scenes = Some(vec![scene]);
gltf.scenes = vec![scene];

// glTF のノードを作成
let mut node = Node::new();
node.mesh = Some(0);

gltf.nodes = Some(vec![node]);
gltf.nodes = vec![node];

// glTF のシーンを設定
gltf.scene = Some(0);
Expand Down
98 changes: 20 additions & 78 deletions nusamai-gltf/examples/make_gltf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::HashMap;
/// glTFを生成するサンプル
///
/// % cargo run --example make_gltf --release
Expand All @@ -9,46 +8,16 @@ use std::vec;
use nusamai_gltf::*;

fn main() -> io::Result<()> {
let asset = Asset {
version: "2.0".to_string(),
copyright: None,
generator: None,
min_version: None,
extensions: None,
extras: None,
};

let mut gltf = Gltf {
extensions_used: None,
extensions_required: None,
accessors: None,
animations: None,
asset,
buffers: None,
buffer_views: None,
cameras: None,
images: None,
materials: None,
meshes: None,
nodes: None,
samplers: None,
scene: None,
scenes: None,
skins: None,
textures: None,
extensions: None,
extras: None,
..Default::default()
};

let byte_length = 44;
let mut buffer = Buffer {
name: None,
byte_length,
uri: None,
extensions: None,
extras: None,
..Default::default()
};
buffer.uri = Some("data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAA=".to_string());
buffer.uri = "data:application/octet-stream;base64,AAABAAIAAAAAAAAAAAAAAAAAAAAAAIA/AAAAAAAAAAAAAAAAAACAPwAAAAA=".to_string().into();

let buffer_view1 = BufferView {
name: None,
Expand All @@ -69,81 +38,54 @@ fn main() -> io::Result<()> {
};

let accessor1 = Accessor {
name: None,
buffer_view: Some(0),
byte_offset: 0,
component_type: ComponentType::UnsignedShort,
count: 3,
type_: AccessorType::Scalar,
max: Some(vec![2.0]),
min: Some(vec![0.0]),
sparse: None,
normalized: None,
extensions: None,
extras: None,
max: vec![2.0].into(),
min: vec![0.0].into(),
..Default::default()
};

let accessor2 = Accessor {
name: None,
buffer_view: Some(1),
byte_offset: 0,
component_type: ComponentType::Float,
count: 3,
type_: AccessorType::Vec3,
max: Some(vec![1.0, 1.0, 0.0]),
min: Some(vec![0.0, 0.0, 0.0]),
sparse: None,
normalized: None,
extensions: None,
extras: None,
max: vec![1.0, 1.0, 0.0].into(),
min: vec![0.0, 0.0, 0.0].into(),
..Default::default()
};

let mut primitive = MeshPrimitive {
attributes: HashMap::new(),
indices: Some(0),
material: None,
mode: PrimitiveMode::Triangles,
targets: None,
extensions: None,
extras: None,
..Default::default()
};
primitive.attributes.insert("POSITION".to_string(), 1);

let mesh = Mesh {
primitives: vec![primitive],
weights: None,
name: None,
extensions: None,
extras: None,
..Default::default()
};

let node = Node {
camera: None,
children: None,
skin: None,
matrix: None,
mesh: Some(0),
rotation: None,
scale: None,
translation: None,
weights: None,
name: None,
extensions: None,
extras: None,
..Default::default()
};

let scene = Scene {
name: None,
nodes: Some(vec![0]),
nodes: vec![0].into(),
};

gltf.buffers = Some(vec![buffer]);
gltf.buffer_views = Some(vec![buffer_view1, buffer_view2]);
gltf.accessors = Some(vec![accessor1, accessor2]);
gltf.meshes = Some(vec![mesh]);
gltf.nodes = Some(vec![node]);
gltf.scenes = Some(vec![scene]);
gltf.scene = Some(0);
gltf.buffers = vec![buffer];
gltf.buffer_views = vec![buffer_view1, buffer_view2];
gltf.accessors = vec![accessor1, accessor2];
gltf.meshes = vec![mesh];
gltf.nodes = vec![node];
gltf.scenes = vec![scene];
gltf.scene = 0.into();

println!("gltf: {:?}", gltf);

Expand Down
2 changes: 1 addition & 1 deletion nusamai-gltf/src/models/accessor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub struct Accessor {

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

impl Accessor {
Expand Down
3 changes: 2 additions & 1 deletion nusamai-gltf/src/models/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub struct Animation {

/// An array of animation channels. An animation channel combines an animation sampler with a target property being animated.
pub channels: Vec<AnimationChannel>,

/// An array of animation samplers. An animation sampler combines timestamps with a sequence of output values and defines an interpolation algorithm.
pub samplers: Vec<AnimationSampler>,

Expand All @@ -84,7 +85,7 @@ pub struct Animation {

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

#[derive(Serialize, Deserialize, Debug, Default)]
Expand Down
7 changes: 6 additions & 1 deletion nusamai-gltf/src/models/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Asset {
pub generator: Option<String>,

/// The glTF version in the form of `<major>.<minor>` that this asset targets.
#[serde(default = "default_version")]
pub version: String,

/// The minimum glTF version in the form of `<major>.<minor>` that this asset targets. This property **MUST NOT** be greater than the asset version.
Expand All @@ -30,7 +31,11 @@ pub struct Asset {

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

fn default_version() -> String {
"2.0".to_string()
}

#[derive(Serialize, Deserialize, Debug, Default)]
Expand Down
4 changes: 2 additions & 2 deletions nusamai-gltf/src/models/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub struct Buffer {

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

#[derive(Serialize, Deserialize, Debug, Default)]
Expand Down Expand Up @@ -72,7 +72,7 @@ pub struct BufferView {

/// The stride, in bytes, between vertex attributes. When this is not defined, data is tightly packed. When two or more accessors use the same buffer view, this field MUST be defined.
#[serde(skip_serializing_if = "Option::is_none")]
pub byte_stride: Option<u32>,
pub byte_stride: Option<u8>,

/// The hint representing the intended GPU buffer type to use with this buffer view.
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down
2 changes: 1 addition & 1 deletion nusamai-gltf/src/models/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub struct Camera {

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

#[derive(Serialize, Deserialize, Debug, Default)]
Expand Down
Loading

0 comments on commit 0a372cb

Please sign in to comment.