gltf_json/
scene.rs

1use crate::validation::Validate;
2use crate::{camera, extensions, mesh, scene, skin, Extras, Index};
3use gltf_derive::Validate;
4use serde_derive::{Deserialize, Serialize};
5
6/// A node in the node hierarchy.  When the node contains `skin`, all
7/// `mesh.primitives` must contain `JOINTS_0` and `WEIGHTS_0` attributes.
8/// A node can have either a `matrix` or any combination of
9/// `translation`/`rotation`/`scale` (TRS) properties. TRS properties are converted
10/// to matrices and postmultiplied in the `T * R * S` order to compose the
11/// transformation matrix; first the scale is applied to the vertices, then the
12/// rotation, and then the translation. If none are provided, the transform is the
13/// identity. When a node is targeted for animation (referenced by an
14/// animation.channel.target), only TRS properties may be present; `matrix` will not
15/// be present.
16#[derive(Clone, Debug, Default, Deserialize, Serialize, Validate)]
17pub struct Node {
18    /// The index of the camera referenced by this node.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub camera: Option<Index<camera::Camera>>,
21
22    /// The indices of this node's children.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub children: Option<Vec<Index<scene::Node>>>,
25
26    /// Extension specific data.
27    #[serde(default, skip_serializing_if = "Option::is_none")]
28    pub extensions: Option<extensions::scene::Node>,
29
30    /// Optional application specific data.
31    #[serde(default)]
32    #[cfg_attr(feature = "extras", serde(skip_serializing_if = "Option::is_none"))]
33    #[cfg_attr(not(feature = "extras"), serde(skip_serializing))]
34    pub extras: Extras,
35
36    /// 4x4 column-major transformation matrix.
37    ///
38    /// glTF 2.0 specification:
39    ///     When a node is targeted for animation (referenced by an
40    ///     animation.channel.target), only TRS properties may be present;
41    ///     matrix will not be present.
42    ///
43    /// TODO: Ensure that .matrix is set to None or otherwise skipped during
44    ///       serialization, if the node is targeted for animation.
45    ///
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub matrix: Option<[f32; 16]>,
48
49    /// The index of the mesh in this node.
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub mesh: Option<Index<mesh::Mesh>>,
52
53    /// Optional user-defined name for this object.
54    #[cfg(feature = "names")]
55    #[cfg_attr(feature = "names", serde(skip_serializing_if = "Option::is_none"))]
56    pub name: Option<String>,
57
58    /// The node's unit quaternion rotation in the order (x, y, z, w), where w is
59    /// the scalar.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    pub rotation: Option<UnitQuaternion>,
62
63    /// The node's non-uniform scale.
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub scale: Option<[f32; 3]>,
66
67    /// The node's translation.
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub translation: Option<[f32; 3]>,
70
71    /// The index of the skin referenced by this node.
72    #[serde(skip_serializing_if = "Option::is_none")]
73    pub skin: Option<Index<skin::Skin>>,
74
75    /// The weights of the instantiated Morph Target. Number of elements must match
76    /// the number of Morph Targets of used mesh.
77    #[serde(skip_serializing_if = "Option::is_none")]
78    pub weights: Option<Vec<f32>>,
79}
80
81/// The root `Node`s of a scene.
82#[derive(Clone, Debug, Deserialize, Serialize, Validate)]
83pub struct Scene {
84    /// Extension specific data.
85    #[serde(default, skip_serializing_if = "Option::is_none")]
86    pub extensions: Option<extensions::scene::Scene>,
87
88    /// Optional application specific data.
89    #[serde(default)]
90    #[cfg_attr(feature = "extras", serde(skip_serializing_if = "Option::is_none"))]
91    #[cfg_attr(not(feature = "extras"), serde(skip_serializing))]
92    pub extras: Extras,
93
94    /// Optional user-defined name for this object.
95    #[cfg(feature = "names")]
96    #[cfg_attr(feature = "names", serde(skip_serializing_if = "Option::is_none"))]
97    pub name: Option<String>,
98
99    /// The indices of each root node.
100    #[serde(skip_serializing_if = "Vec::is_empty")]
101    pub nodes: Vec<Index<Node>>,
102}
103
104/// Unit quaternion rotation in the order (x, y, z, w), where w is the scalar.
105#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
106pub struct UnitQuaternion(pub [f32; 4]);
107
108impl Default for UnitQuaternion {
109    fn default() -> Self {
110        UnitQuaternion([0.0, 0.0, 0.0, 1.0])
111    }
112}
113
114impl Validate for UnitQuaternion {}