Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_gltf/src/material.rs
9333 views
1
use bevy_asset::{Asset, Handle};
2
use bevy_color::{Color, LinearRgba};
3
use bevy_image::Image;
4
use bevy_material::AlphaMode;
5
use bevy_math::Affine2;
6
use bevy_mesh::UvChannel;
7
use bevy_reflect::TypePath;
8
use 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(Asset, Debug, Clone, TypePath)]
14
pub 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
192
impl 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
}
267
268