1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
use crate::{image, Document};
pub use json::texture::{MagFilter, MinFilter, WrappingMode};
#[cfg(feature = "extensions")]
use serde_json::{Map, Value};
lazy_static! {
static ref DEFAULT_SAMPLER: json::texture::Sampler = Default::default();
}
/// A reference to a `Texture`.
#[derive(Clone, Debug)]
pub struct Info<'a> {
/// The parent `Texture` struct.
texture: Texture<'a>,
/// The corresponding JSON struct.
json: &'a json::texture::Info,
}
/// Texture sampler properties for filtering and wrapping modes.
#[derive(Clone, Debug)]
pub struct Sampler<'a> {
/// The parent `Document` struct.
#[allow(dead_code)]
document: &'a Document,
/// The corresponding JSON index - `None` when the default sampler.
index: Option<usize>,
/// The corresponding JSON struct.
json: &'a json::texture::Sampler,
}
/// A texture and its sampler.
#[derive(Clone, Debug)]
pub struct Texture<'a> {
/// The parent `Document` struct.
document: &'a Document,
/// The corresponding JSON index.
index: usize,
/// The corresponding JSON struct.
json: &'a json::texture::Texture,
}
impl<'a> Sampler<'a> {
/// Constructs a `Sampler`.
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::texture::Sampler,
) -> Self {
Self {
document,
index: Some(index),
json,
}
}
/// Constructs the default `Sampler`.
pub(crate) fn default(document: &'a Document) -> Self {
Self {
document,
index: None,
json: &DEFAULT_SAMPLER,
}
}
/// Returns the internal JSON index if this `Sampler` was explicity defined.
///
/// This function returns `None` if the `Sampler` is the default sampler.
pub fn index(&self) -> Option<usize> {
self.index
}
/// Magnification filter.
pub fn mag_filter(&self) -> Option<MagFilter> {
self.json.mag_filter.map(|filter| filter.unwrap())
}
/// Minification filter.
pub fn min_filter(&self) -> Option<MinFilter> {
self.json.min_filter.map(|filter| filter.unwrap())
}
/// Optional user-defined name for this object.
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_deref()
}
/// `s` wrapping mode.
pub fn wrap_s(&self) -> WrappingMode {
self.json.wrap_s.unwrap()
}
/// `t` wrapping mode.
pub fn wrap_t(&self) -> WrappingMode {
self.json.wrap_t.unwrap()
}
/// Returns extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
/// Queries extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
/// Optional application specific data.
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> Texture<'a> {
/// Constructs a `Texture`.
pub(crate) fn new(
document: &'a Document,
index: usize,
json: &'a json::texture::Texture,
) -> Self {
Self {
document,
index,
json,
}
}
/// Returns the internal JSON index.
pub fn index(&self) -> usize {
self.index
}
/// Optional user-defined name for this object.
#[cfg(feature = "names")]
pub fn name(&self) -> Option<&str> {
self.json.name.as_deref()
}
/// Returns the sampler used by this texture.
pub fn sampler(&self) -> Sampler<'a> {
self.json
.sampler
.as_ref()
.map(|index| self.document.samplers().nth(index.value()).unwrap())
.unwrap_or_else(|| Sampler::default(self.document))
}
/// Returns the image used by this texture.
pub fn source(&self) -> image::Image<'a> {
self.document
.images()
.nth(self.json.source.value())
.unwrap()
}
/// Returns extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
/// Queries extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
/// Optional application specific data.
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> Info<'a> {
/// Constructs a reference to a `Texture`.
pub(crate) fn new(texture: Texture<'a>, json: &'a json::texture::Info) -> Self {
Self { texture, json }
}
/// The set index of the texture's `TEXCOORD` attribute.
pub fn tex_coord(&self) -> u32 {
self.json.tex_coord
}
/// Returns the referenced `Texture`.
pub fn texture(&self) -> Texture<'a> {
self.texture.clone()
}
/// Returns texture transform information
#[cfg(feature = "KHR_texture_transform")]
#[cfg_attr(docsrs, doc(cfg(feature = "KHR_texture_transform")))]
pub fn texture_transform(&self) -> Option<TextureTransform<'a>> {
self.json
.extensions
.as_ref()?
.texture_transform
.as_ref()
.map(TextureTransform::new)
}
/// Returns extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extensions(&self) -> Option<&Map<String, Value>> {
let ext = self.json.extensions.as_ref()?;
Some(&ext.others)
}
/// Queries extension data unknown to this crate version.
#[cfg(feature = "extensions")]
#[cfg_attr(docsrs, doc(cfg(feature = "extensions")))]
pub fn extension_value(&self, ext_name: &str) -> Option<&Value> {
let ext = self.json.extensions.as_ref()?;
ext.others.get(ext_name)
}
/// Optional application specific data.
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}
impl<'a> AsRef<Texture<'a>> for Info<'a> {
fn as_ref(&self) -> &Texture<'a> {
&self.texture
}
}
/// Many techniques can be used to optimize resource usage for a 3d scene.
/// Chief among them is the ability to minimize the number of textures the GPU must load.
/// To achieve this, many engines encourage packing many objects' low-resolution textures into a single large texture atlas.
/// The region of the resulting atlas that corresponds with each object is then defined by vertical and horizontal offsets,
/// and the width and height of the region.
///
/// To support this use case, this extension adds `offset`, `rotation`, and `scale` properties to textureInfo structures.
/// These properties would typically be implemented as an affine transform on the UV coordinates.
#[cfg(feature = "KHR_texture_transform")]
pub struct TextureTransform<'a> {
/// The corresponding JSON struct.
json: &'a json::extensions::texture::TextureTransform,
}
#[cfg(feature = "KHR_texture_transform")]
impl<'a> TextureTransform<'a> {
/// Constructs `TextureTransform`
pub(crate) fn new(json: &'a json::extensions::texture::TextureTransform) -> Self {
Self { json }
}
/// The offset of the UV coordinate origin as a factor of the texture dimensions.
pub fn offset(&self) -> [f32; 2] {
self.json.offset.0
}
/// Rotate the UVs by this many radians counter-clockwise around the origin.
/// This is equivalent to a similar rotation of the image clockwise.
pub fn rotation(&self) -> f32 {
self.json.rotation.0
}
/// The scale factor applied to the components of the UV coordinates.
pub fn scale(&self) -> [f32; 2] {
self.json.scale.0
}
/// Overrides the textureInfo texCoord value if supplied, and if this extension is supported.
pub fn tex_coord(&self) -> Option<u32> {
self.json.tex_coord
}
/// Optional application specific data.
pub fn extras(&self) -> &json::Extras {
&self.json.extras
}
}