gltf_json/extensions/
texture.rs

1#[cfg(feature = "KHR_texture_transform")]
2use crate::{extras::Extras, validation::Validate};
3use gltf_derive::Validate;
4use serde_derive::{Deserialize, Serialize};
5#[cfg(feature = "extensions")]
6use serde_json::{Map, Value};
7
8/// Texture sampler properties for filtering and wrapping modes.
9#[derive(Clone, Debug, Default, Deserialize, Serialize, Validate)]
10pub struct Sampler {
11    #[cfg(feature = "extensions")]
12    #[serde(default, flatten)]
13    pub others: Map<String, Value>,
14}
15
16/// A texture and its sampler.
17#[derive(Clone, Debug, Default, Deserialize, Serialize, Validate)]
18pub struct Texture {
19    #[cfg(feature = "extensions")]
20    #[serde(default, flatten)]
21    pub others: Map<String, Value>,
22}
23
24#[derive(Clone, Debug, Default, Deserialize, Serialize, Validate)]
25/// Reference to a `Texture`.
26pub struct Info {
27    #[cfg(feature = "KHR_texture_transform")]
28    #[serde(
29        default,
30        rename = "KHR_texture_transform",
31        skip_serializing_if = "Option::is_none"
32    )]
33    pub texture_transform: Option<TextureTransform>,
34    #[cfg(feature = "extensions")]
35    #[serde(default, flatten)]
36    pub others: Map<String, Value>,
37}
38
39/// Many techniques can be used to optimize resource usage for a 3d scene.
40/// Chief among them is the ability to minimize the number of textures the GPU must load.
41/// To achieve this, many engines encourage packing many objects' low-resolution textures into a single large texture atlas.
42/// The region of the resulting atlas that corresponds with each object is then defined by vertical and horizontal offsets,
43/// and the width and height of the region.
44///
45/// To support this use case, this extension adds `offset`, `rotation`, and `scale` properties to textureInfo structures.
46/// These properties would typically be implemented as an affine transform on the UV coordinates.
47#[cfg(feature = "KHR_texture_transform")]
48#[derive(Clone, Debug, Default, Deserialize, Serialize, Validate)]
49#[serde(default, rename_all = "camelCase")]
50pub struct TextureTransform {
51    // The offset of the UV coordinate origin as a factor of the texture dimensions.
52    pub offset: TextureTransformOffset,
53
54    /// Rotate the UVs by this many radians counter-clockwise around the origin.
55    /// This is equivalent to a similar rotation of the image clockwise.
56    pub rotation: TextureTransformRotation,
57
58    /// The scale factor applied to the components of the UV coordinates.
59    pub scale: TextureTransformScale,
60
61    /// Overrides the textureInfo texCoord value if supplied, and if this extension is supported.
62    pub tex_coord: Option<u32>,
63
64    /// Optional application specific data.
65    #[cfg_attr(feature = "extras", serde(skip_serializing_if = "Option::is_none"))]
66    #[cfg_attr(not(feature = "extras"), serde(skip_serializing))]
67    pub extras: Extras,
68}
69
70/// The offset of the UV coordinate origin as a factor of the texture dimensions.
71#[cfg(feature = "KHR_texture_transform")]
72#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
73pub struct TextureTransformOffset(pub [f32; 2]);
74
75#[cfg(feature = "KHR_texture_transform")]
76impl Default for TextureTransformOffset {
77    fn default() -> Self {
78        Self([0.0, 0.0])
79    }
80}
81
82#[cfg(feature = "KHR_texture_transform")]
83impl Validate for TextureTransformOffset {}
84
85/// Rotate the UVs by this many radians counter-clockwise around the origin.
86/// This is equivalent to a similar rotation of the image clockwise.
87#[cfg(feature = "KHR_texture_transform")]
88#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
89pub struct TextureTransformRotation(pub f32);
90
91#[cfg(feature = "KHR_texture_transform")]
92impl Default for TextureTransformRotation {
93    fn default() -> Self {
94        Self(0.0)
95    }
96}
97
98#[cfg(feature = "KHR_texture_transform")]
99impl Validate for TextureTransformRotation {}
100
101/// The scale factor applied to the components of the UV coordinates.
102#[cfg(feature = "KHR_texture_transform")]
103#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
104pub struct TextureTransformScale(pub [f32; 2]);
105
106#[cfg(feature = "KHR_texture_transform")]
107impl Default for TextureTransformScale {
108    fn default() -> Self {
109        Self([1.0, 1.0])
110    }
111}
112
113#[cfg(feature = "KHR_texture_transform")]
114impl Validate for TextureTransformScale {}