Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_gltf/src/loader/extensions/khr_materials_anisotropy.rs
6598 views
1
use bevy_asset::LoadContext;
2
3
use gltf::{Document, Material};
4
5
use serde_json::Value;
6
7
#[cfg(feature = "pbr_anisotropy_texture")]
8
use {
9
crate::loader::gltf_ext::{material::uv_channel, texture::texture_handle_from_info},
10
bevy_asset::Handle,
11
bevy_image::Image,
12
bevy_pbr::UvChannel,
13
gltf::json::texture::Info,
14
serde_json::value,
15
};
16
17
/// Parsed data from the `KHR_materials_anisotropy` extension.
18
///
19
/// See the specification:
20
/// <https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_anisotropy/README.md>
21
#[derive(Default)]
22
pub(crate) struct AnisotropyExtension {
23
pub(crate) anisotropy_strength: Option<f64>,
24
pub(crate) anisotropy_rotation: Option<f64>,
25
#[cfg(feature = "pbr_anisotropy_texture")]
26
pub(crate) anisotropy_channel: UvChannel,
27
#[cfg(feature = "pbr_anisotropy_texture")]
28
pub(crate) anisotropy_texture: Option<Handle<Image>>,
29
}
30
31
impl AnisotropyExtension {
32
#[expect(
33
clippy::allow_attributes,
34
reason = "`unused_variables` is not always linted"
35
)]
36
#[allow(
37
unused_variables,
38
reason = "Depending on what features are used to compile this crate, certain parameters may end up unused."
39
)]
40
pub(crate) fn parse(
41
load_context: &mut LoadContext,
42
document: &Document,
43
material: &Material,
44
) -> Option<AnisotropyExtension> {
45
let extension = material
46
.extensions()?
47
.get("KHR_materials_anisotropy")?
48
.as_object()?;
49
50
#[cfg(feature = "pbr_anisotropy_texture")]
51
let (anisotropy_channel, anisotropy_texture) = extension
52
.get("anisotropyTexture")
53
.and_then(|value| value::from_value::<Info>(value.clone()).ok())
54
.map(|json_info| {
55
(
56
uv_channel(material, "anisotropy", json_info.tex_coord),
57
texture_handle_from_info(&json_info, document, load_context),
58
)
59
})
60
.unzip();
61
62
Some(AnisotropyExtension {
63
anisotropy_strength: extension.get("anisotropyStrength").and_then(Value::as_f64),
64
anisotropy_rotation: extension.get("anisotropyRotation").and_then(Value::as_f64),
65
#[cfg(feature = "pbr_anisotropy_texture")]
66
anisotropy_channel: anisotropy_channel.unwrap_or_default(),
67
#[cfg(feature = "pbr_anisotropy_texture")]
68
anisotropy_texture,
69
})
70
}
71
}
72
73