Skip to main content

easy_gltf/scene/model/material/
normal.rs

1use crate::utils::GltfData;
2use image::RgbImage;
3use std::sync::Arc;
4
5#[derive(Clone, Debug)]
6/// Defines the normal texture of a material.
7pub struct NormalMap {
8    /// A tangent space normal map.
9    /// The texture contains RGB components in linear space. Each texel
10    /// represents the XYZ components of a normal vector in tangent space.
11    ///
12    /// * Red [0 to 255] maps to X [-1 to 1].
13    /// * Green [0 to 255] maps to Y [-1 to 1].
14    /// * Blue [128 to 255] maps to Z [1/255 to 1].
15    ///
16    /// The normal vectors use OpenGL conventions where +X is right, +Y is up,
17    /// and +Z points toward the viewer.
18    pub texture: Arc<RgbImage>,
19
20    /// The `normal_factor` is the normal strength to be applied to the
21    /// texture value.
22    pub factor: f32,
23}
24
25impl NormalMap {
26    pub(crate) fn load(gltf_mat: &gltf::Material, data: &mut GltfData) -> Option<Self> {
27        gltf_mat.normal_texture().map(|texture| Self {
28            texture: data.load_rgb_image(&texture.texture()),
29            factor: texture.scale(),
30        })
31    }
32}