Skip to main content

bevy_gltf/
material.rs

1use bevy_asset::{Asset, Handle};
2use bevy_color::{Color, LinearRgba};
3use bevy_image::Image;
4use bevy_material::AlphaMode;
5use bevy_math::Affine2;
6use bevy_mesh::UvChannel;
7use bevy_reflect::TypePath;
8use wgpu_types::Face;
9
10/// Data to build a Gltf Material
11///
12/// See [`StandardMaterial`](https://docs.rs/bevy/latest/bevy/pbr/struct.StandardMaterial.html) for details
13#[derive(impl bevy_asset::VisitAssetDependencies for GltfMaterial {
    fn visit_dependencies(&self,
        visit: &mut impl ::core::ops::FnMut(bevy_asset::UntypedAssetId)) {}
}Asset, #[automatically_derived]
impl ::core::fmt::Debug for GltfMaterial {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        let names: &'static _ =
            &["base_color", "base_color_channel", "base_color_texture",
                        "emissive", "emissive_channel", "emissive_texture",
                        "perceptual_roughness", "metallic",
                        "metallic_roughness_channel", "metallic_roughness_texture",
                        "reflectance", "specular_channel", "specular_texture",
                        "specular_tint", "specular_tint_channel",
                        "specular_tint_texture", "specular_transmission",
                        "specular_transmission_channel",
                        "specular_transmission_texture", "thickness",
                        "thickness_channel", "thickness_texture", "ior",
                        "attenuation_distance", "attenuation_color",
                        "normal_map_channel", "normal_map_texture",
                        "occlusion_channel", "occlusion_texture", "clearcoat",
                        "clearcoat_perceptual_roughness", "clearcoat_channel",
                        "clearcoat_texture", "clearcoat_roughness_channel",
                        "clearcoat_roughness_texture", "clearcoat_normal_channel",
                        "clearcoat_normal_texture", "anisotropy_strength",
                        "anisotropy_rotation", "anisotropy_channel",
                        "anisotropy_texture", "double_sided", "cull_mode", "unlit",
                        "alpha_mode", "uv_transform"];
        let values: &[&dyn ::core::fmt::Debug] =
            &[&self.base_color, &self.base_color_channel,
                        &self.base_color_texture, &self.emissive,
                        &self.emissive_channel, &self.emissive_texture,
                        &self.perceptual_roughness, &self.metallic,
                        &self.metallic_roughness_channel,
                        &self.metallic_roughness_texture, &self.reflectance,
                        &self.specular_channel, &self.specular_texture,
                        &self.specular_tint, &self.specular_tint_channel,
                        &self.specular_tint_texture, &self.specular_transmission,
                        &self.specular_transmission_channel,
                        &self.specular_transmission_texture, &self.thickness,
                        &self.thickness_channel, &self.thickness_texture, &self.ior,
                        &self.attenuation_distance, &self.attenuation_color,
                        &self.normal_map_channel, &self.normal_map_texture,
                        &self.occlusion_channel, &self.occlusion_texture,
                        &self.clearcoat, &self.clearcoat_perceptual_roughness,
                        &self.clearcoat_channel, &self.clearcoat_texture,
                        &self.clearcoat_roughness_channel,
                        &self.clearcoat_roughness_texture,
                        &self.clearcoat_normal_channel,
                        &self.clearcoat_normal_texture, &self.anisotropy_strength,
                        &self.anisotropy_rotation, &self.anisotropy_channel,
                        &self.anisotropy_texture, &self.double_sided,
                        &self.cull_mode, &self.unlit, &self.alpha_mode,
                        &&self.uv_transform];
        ::core::fmt::Formatter::debug_struct_fields_finish(f, "GltfMaterial",
            names, values)
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for GltfMaterial {
    #[inline]
    fn clone(&self) -> GltfMaterial {
        GltfMaterial {
            base_color: ::core::clone::Clone::clone(&self.base_color),
            base_color_channel: ::core::clone::Clone::clone(&self.base_color_channel),
            base_color_texture: ::core::clone::Clone::clone(&self.base_color_texture),
            emissive: ::core::clone::Clone::clone(&self.emissive),
            emissive_channel: ::core::clone::Clone::clone(&self.emissive_channel),
            emissive_texture: ::core::clone::Clone::clone(&self.emissive_texture),
            perceptual_roughness: ::core::clone::Clone::clone(&self.perceptual_roughness),
            metallic: ::core::clone::Clone::clone(&self.metallic),
            metallic_roughness_channel: ::core::clone::Clone::clone(&self.metallic_roughness_channel),
            metallic_roughness_texture: ::core::clone::Clone::clone(&self.metallic_roughness_texture),
            reflectance: ::core::clone::Clone::clone(&self.reflectance),
            specular_channel: ::core::clone::Clone::clone(&self.specular_channel),
            specular_texture: ::core::clone::Clone::clone(&self.specular_texture),
            specular_tint: ::core::clone::Clone::clone(&self.specular_tint),
            specular_tint_channel: ::core::clone::Clone::clone(&self.specular_tint_channel),
            specular_tint_texture: ::core::clone::Clone::clone(&self.specular_tint_texture),
            specular_transmission: ::core::clone::Clone::clone(&self.specular_transmission),
            specular_transmission_channel: ::core::clone::Clone::clone(&self.specular_transmission_channel),
            specular_transmission_texture: ::core::clone::Clone::clone(&self.specular_transmission_texture),
            thickness: ::core::clone::Clone::clone(&self.thickness),
            thickness_channel: ::core::clone::Clone::clone(&self.thickness_channel),
            thickness_texture: ::core::clone::Clone::clone(&self.thickness_texture),
            ior: ::core::clone::Clone::clone(&self.ior),
            attenuation_distance: ::core::clone::Clone::clone(&self.attenuation_distance),
            attenuation_color: ::core::clone::Clone::clone(&self.attenuation_color),
            normal_map_channel: ::core::clone::Clone::clone(&self.normal_map_channel),
            normal_map_texture: ::core::clone::Clone::clone(&self.normal_map_texture),
            occlusion_channel: ::core::clone::Clone::clone(&self.occlusion_channel),
            occlusion_texture: ::core::clone::Clone::clone(&self.occlusion_texture),
            clearcoat: ::core::clone::Clone::clone(&self.clearcoat),
            clearcoat_perceptual_roughness: ::core::clone::Clone::clone(&self.clearcoat_perceptual_roughness),
            clearcoat_channel: ::core::clone::Clone::clone(&self.clearcoat_channel),
            clearcoat_texture: ::core::clone::Clone::clone(&self.clearcoat_texture),
            clearcoat_roughness_channel: ::core::clone::Clone::clone(&self.clearcoat_roughness_channel),
            clearcoat_roughness_texture: ::core::clone::Clone::clone(&self.clearcoat_roughness_texture),
            clearcoat_normal_channel: ::core::clone::Clone::clone(&self.clearcoat_normal_channel),
            clearcoat_normal_texture: ::core::clone::Clone::clone(&self.clearcoat_normal_texture),
            anisotropy_strength: ::core::clone::Clone::clone(&self.anisotropy_strength),
            anisotropy_rotation: ::core::clone::Clone::clone(&self.anisotropy_rotation),
            anisotropy_channel: ::core::clone::Clone::clone(&self.anisotropy_channel),
            anisotropy_texture: ::core::clone::Clone::clone(&self.anisotropy_texture),
            double_sided: ::core::clone::Clone::clone(&self.double_sided),
            cull_mode: ::core::clone::Clone::clone(&self.cull_mode),
            unlit: ::core::clone::Clone::clone(&self.unlit),
            alpha_mode: ::core::clone::Clone::clone(&self.alpha_mode),
            uv_transform: ::core::clone::Clone::clone(&self.uv_transform),
        }
    }
}Clone, const _: () =
    {
        #[allow(deprecated, reason =
        "derives on a deprecated type shouldn't be considered a usage")]
        impl bevy_reflect::TypePath for GltfMaterial where  {
            fn type_path() -> &'static str {
                "bevy_gltf::material::GltfMaterial"
            }
            fn short_type_path() -> &'static str { "GltfMaterial" }
            fn type_ident() -> ::core::option::Option<&'static str> {
                ::core::option::Option::Some("GltfMaterial")
            }
            fn crate_name() -> ::core::option::Option<&'static str> {
                ::core::option::Option::Some("bevy_gltf::material".split(':').next().unwrap())
            }
            fn module_path() -> ::core::option::Option<&'static str> {
                ::core::option::Option::Some("bevy_gltf::material")
            }
        }
    };TypePath)]
14pub struct GltfMaterial {
15    /// The color of the surface of the material before lighting.
16    pub base_color: Color,
17
18    /// The UV channel to use for the [`GltfMaterial::base_color_texture`].
19    pub base_color_channel: UvChannel,
20
21    /// The texture component of the material's color before lighting.
22    pub base_color_texture: Option<Handle<Image>>,
23
24    /// Color the material "emits" to the camera.
25    pub emissive: LinearRgba,
26
27    /// The UV channel to use for the [`GltfMaterial::emissive_texture`].
28    pub emissive_channel: UvChannel,
29
30    /// The emissive map, multiplies pixels with [`GltfMaterial::emissive`]
31    /// to get the final "emitting" color of a surface.
32    pub emissive_texture: Option<Handle<Image>>,
33
34    /// Linear perceptual roughness.
35    pub perceptual_roughness: f32,
36
37    /// How "metallic" the material appears, within `[0.0, 1.0]`.
38    pub metallic: f32,
39
40    /// The UV channel to use for the [`GltfMaterial::metallic_roughness_texture`].
41    pub metallic_roughness_channel: UvChannel,
42
43    /// Metallic and roughness maps, stored as a single texture.
44    pub metallic_roughness_texture: Option<Handle<Image>>,
45
46    /// Specular intensity for non-metals on a linear scale of `[0.0, 1.0]`.
47    pub reflectance: f32,
48
49    /// The UV channel to use for the [`GltfMaterial::specular_texture`].
50    #[cfg(feature = "pbr_specular_textures")]
51    pub specular_channel: UvChannel,
52
53    /// A map that specifies reflectance for non-metallic materials.
54    #[cfg(feature = "pbr_specular_textures")]
55    pub specular_texture: Option<Handle<Image>>,
56
57    /// A color with which to modulate the [`GltfMaterial::reflectance`] for
58    /// non-metals.
59    pub specular_tint: Color,
60
61    /// The UV channel to use for the
62    /// [`GltfMaterial::specular_tint_texture`].
63    #[cfg(feature = "pbr_specular_textures")]
64    pub specular_tint_channel: UvChannel,
65
66    /// A map that specifies color adjustment to be applied to the specular
67    /// reflection for non-metallic materials.
68    #[cfg(feature = "pbr_specular_textures")]
69    pub specular_tint_texture: Option<Handle<Image>>,
70
71    /// The amount of light transmitted _specularly_ through the material (i.e. via refraction).
72    pub specular_transmission: f32,
73
74    /// The UV channel to use for the [`GltfMaterial::specular_transmission_texture`].
75    #[cfg(feature = "pbr_transmission_textures")]
76    pub specular_transmission_channel: UvChannel,
77
78    /// A map that modulates specular transmission via its red channel. Multiplied by [`GltfMaterial::specular_transmission`]
79    /// to obtain the final result.
80    #[cfg(feature = "pbr_transmission_textures")]
81    pub specular_transmission_texture: Option<Handle<Image>>,
82
83    /// Thickness of the volume beneath the material surface.
84    pub thickness: f32,
85    #[cfg(feature = "pbr_transmission_textures")]
86
87    /// The UV channel to use for the [`GltfMaterial::thickness_texture`].
88    pub thickness_channel: UvChannel,
89
90    /// A map that modulates thickness via its green channel. Multiplied by [`GltfMaterial::thickness`]
91    /// to obtain the final result.
92    #[cfg(feature = "pbr_transmission_textures")]
93    pub thickness_texture: Option<Handle<Image>>,
94
95    /// The [index of refraction](https://en.wikipedia.org/wiki/Refractive_index) of the material.
96    pub ior: f32,
97
98    /// How far, on average, light travels through the volume beneath the material's
99    /// surface before being absorbed.
100    pub attenuation_distance: f32,
101
102    /// The resulting (non-absorbed) color after white light travels through the attenuation distance.
103    pub attenuation_color: Color,
104
105    /// The UV channel to use for the [`GltfMaterial::normal_map_texture`].
106    pub normal_map_channel: UvChannel,
107
108    /// Used to fake the lighting of bumps and dents on a material.
109    pub normal_map_texture: Option<Handle<Image>>,
110
111    /// The UV channel to use for the [`GltfMaterial::occlusion_texture`].
112    pub occlusion_channel: UvChannel,
113
114    /// Specifies the level of exposure to ambient light.
115    pub occlusion_texture: Option<Handle<Image>>,
116
117    /// An extra thin translucent layer on top of the main PBR layer. This is
118    /// typically used for painted surfaces.
119    pub clearcoat: f32,
120
121    /// The roughness of the clearcoat material. This is specified in exactly
122    /// the same way as the [`GltfMaterial::perceptual_roughness`].
123    pub clearcoat_perceptual_roughness: f32,
124
125    /// The UV channel to use for the [`GltfMaterial::clearcoat_texture`].
126    #[cfg(feature = "pbr_multi_layer_material_textures")]
127    pub clearcoat_channel: UvChannel,
128
129    /// An image texture that specifies the strength of the clearcoat layer in
130    /// the red channel. Values sampled from this texture are multiplied by the
131    /// main [`GltfMaterial::clearcoat`] factor.
132    #[cfg(feature = "pbr_multi_layer_material_textures")]
133    pub clearcoat_texture: Option<Handle<Image>>,
134
135    /// The UV channel to use for the [`GltfMaterial::clearcoat_roughness_texture`].
136    #[cfg(feature = "pbr_multi_layer_material_textures")]
137    pub clearcoat_roughness_channel: UvChannel,
138
139    /// An image texture that specifies the roughness of the clearcoat level in
140    /// the green channel. Values from this texture are multiplied by the main
141    /// [`GltfMaterial::clearcoat_perceptual_roughness`] factor.
142    #[cfg(feature = "pbr_multi_layer_material_textures")]
143    pub clearcoat_roughness_texture: Option<Handle<Image>>,
144
145    /// The UV channel to use for the [`GltfMaterial::clearcoat_normal_texture`].
146    #[cfg(feature = "pbr_multi_layer_material_textures")]
147    pub clearcoat_normal_channel: UvChannel,
148
149    /// An image texture that specifies a normal map that is to be applied to
150    /// the clearcoat layer. This can be used to simulate, for example,
151    /// scratches on an outer layer of varnish. Normal maps are in the same
152    /// format as [`GltfMaterial::normal_map_texture`].
153    #[cfg(feature = "pbr_multi_layer_material_textures")]
154    pub clearcoat_normal_texture: Option<Handle<Image>>,
155
156    /// Increases the roughness along a specific direction, so that the specular
157    /// highlight will be stretched instead of being a circular lobe.
158    pub anisotropy_strength: f32,
159
160    /// The direction of increased roughness, in radians relative to the mesh
161    /// tangent.
162    pub anisotropy_rotation: f32,
163
164    /// The UV channel to use for the [`GltfMaterial::anisotropy_texture`].
165    #[cfg(feature = "pbr_anisotropy_texture")]
166    pub anisotropy_channel: UvChannel,
167
168    /// An image texture that allows the
169    /// [`GltfMaterial::anisotropy_strength`] and
170    /// [`GltfMaterial::anisotropy_rotation`] to vary across the mesh.
171    #[cfg(feature = "pbr_anisotropy_texture")]
172    pub anisotropy_texture: Option<Handle<Image>>,
173
174    /// Support two-sided lighting by automatically flipping the normals for "back" faces
175    /// within the PBR lighting shader.
176    pub double_sided: bool,
177
178    /// Support two-sided lighting by automatically flipping the normals for "back" faces
179    /// within the PBR lighting shader.
180    pub cull_mode: Option<Face>,
181
182    /// Whether to apply only the base color to this material.
183    pub unlit: bool,
184
185    /// How to apply the alpha channel of the `base_color_texture`.
186    pub alpha_mode: AlphaMode,
187
188    /// The transform applied to the UVs corresponding to `ATTRIBUTE_UV_0` on the mesh before sampling. Default is identity.
189    pub uv_transform: Affine2,
190}
191
192impl Default for GltfMaterial {
193    fn default() -> Self {
194        GltfMaterial {
195            // White because it gets multiplied with texture values if someone uses
196            // a texture.
197            base_color: Color::WHITE,
198            base_color_channel: UvChannel::Uv0,
199            base_color_texture: None,
200            emissive: LinearRgba::BLACK,
201            emissive_channel: UvChannel::Uv0,
202            emissive_texture: None,
203            // Matches Blender's default roughness.
204            perceptual_roughness: 0.5,
205            // Metallic should generally be set to 0.0 or 1.0.
206            metallic: 0.0,
207            metallic_roughness_channel: UvChannel::Uv0,
208            metallic_roughness_texture: None,
209            // Minimum real-world reflectance is 2%, most materials between 2-5%
210            // Expressed in a linear scale and equivalent to 4% reflectance see
211            // <https://google.github.io/filament/Material%20Properties.pdf>
212            reflectance: 0.5,
213            specular_transmission: 0.0,
214            #[cfg(feature = "pbr_transmission_textures")]
215            specular_transmission_channel: UvChannel::Uv0,
216            #[cfg(feature = "pbr_transmission_textures")]
217            specular_transmission_texture: None,
218            thickness: 0.0,
219            #[cfg(feature = "pbr_transmission_textures")]
220            thickness_channel: UvChannel::Uv0,
221            #[cfg(feature = "pbr_transmission_textures")]
222            thickness_texture: None,
223            ior: 1.5,
224            attenuation_color: Color::WHITE,
225            attenuation_distance: f32::INFINITY,
226            occlusion_channel: UvChannel::Uv0,
227            occlusion_texture: None,
228            normal_map_channel: UvChannel::Uv0,
229            normal_map_texture: None,
230            #[cfg(feature = "pbr_specular_textures")]
231            specular_channel: UvChannel::Uv0,
232            #[cfg(feature = "pbr_specular_textures")]
233            specular_texture: None,
234            specular_tint: Color::WHITE,
235            #[cfg(feature = "pbr_specular_textures")]
236            specular_tint_channel: UvChannel::Uv0,
237            #[cfg(feature = "pbr_specular_textures")]
238            specular_tint_texture: None,
239            clearcoat: 0.0,
240            clearcoat_perceptual_roughness: 0.5,
241            #[cfg(feature = "pbr_multi_layer_material_textures")]
242            clearcoat_channel: UvChannel::Uv0,
243            #[cfg(feature = "pbr_multi_layer_material_textures")]
244            clearcoat_texture: None,
245            #[cfg(feature = "pbr_multi_layer_material_textures")]
246            clearcoat_roughness_channel: UvChannel::Uv0,
247            #[cfg(feature = "pbr_multi_layer_material_textures")]
248            clearcoat_roughness_texture: None,
249            #[cfg(feature = "pbr_multi_layer_material_textures")]
250            clearcoat_normal_channel: UvChannel::Uv0,
251            #[cfg(feature = "pbr_multi_layer_material_textures")]
252            clearcoat_normal_texture: None,
253            anisotropy_strength: 0.0,
254            anisotropy_rotation: 0.0,
255            #[cfg(feature = "pbr_anisotropy_texture")]
256            anisotropy_channel: UvChannel::Uv0,
257            #[cfg(feature = "pbr_anisotropy_texture")]
258            anisotropy_texture: None,
259            double_sided: false,
260            cull_mode: Some(Face::Back),
261            unlit: false,
262            alpha_mode: AlphaMode::Opaque,
263            uv_transform: Affine2::IDENTITY,
264        }
265    }
266}