Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/3d/clearcoat.rs
9366 views
1
//! Demonstrates the clearcoat PBR feature.
2
//!
3
//! Clearcoat is a separate material layer that represents a thin translucent
4
//! layer over a material. Examples include (from the Filament spec [1]) car paint,
5
//! soda cans, and lacquered wood.
6
//!
7
//! In glTF, clearcoat is supported via the `KHR_materials_clearcoat` [2]
8
//! extension. This extension is well supported by tools; in particular,
9
//! Blender's glTF exporter maps the clearcoat feature of its Principled BSDF
10
//! node to this extension, allowing it to appear in Bevy.
11
//!
12
//! This Bevy example is inspired by the corresponding three.js example [3].
13
//!
14
//! [1]: https://google.github.io/filament/Filament.html#materialsystem/clearcoatmodel
15
//!
16
//! [2]: https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_materials_clearcoat/README.md
17
//!
18
//! [3]: https://threejs.org/examples/webgl_materials_physical_clearcoat.html
19
20
use std::f32::consts::PI;
21
22
use bevy::{
23
camera::Hdr,
24
color::palettes::css::{BLUE, GOLD, WHITE},
25
core_pipeline::tonemapping::Tonemapping::AcesFitted,
26
image::ImageLoaderSettings,
27
light::Skybox,
28
math::vec3,
29
prelude::*,
30
};
31
32
/// The size of each sphere.
33
const SPHERE_SCALE: f32 = 0.9;
34
35
/// The speed at which the spheres rotate, in radians per second.
36
const SPHERE_ROTATION_SPEED: f32 = 0.8;
37
38
/// Which type of light we're using: a point light or a directional light.
39
#[derive(Clone, Copy, PartialEq, Resource, Default)]
40
enum LightMode {
41
#[default]
42
Point,
43
Directional,
44
}
45
46
/// Tags the example spheres.
47
#[derive(Component)]
48
struct ExampleSphere;
49
50
/// Entry point.
51
pub fn main() {
52
App::new()
53
.init_resource::<LightMode>()
54
.add_plugins(DefaultPlugins)
55
.add_systems(Startup, setup)
56
.add_systems(Update, animate_light)
57
.add_systems(Update, animate_spheres)
58
.add_systems(Update, (handle_input, update_help_text).chain())
59
.run();
60
}
61
62
/// Initializes the scene.
63
fn setup(
64
mut commands: Commands,
65
mut meshes: ResMut<Assets<Mesh>>,
66
mut materials: ResMut<Assets<StandardMaterial>>,
67
asset_server: Res<AssetServer>,
68
light_mode: Res<LightMode>,
69
) {
70
let sphere = create_sphere_mesh(&mut meshes);
71
spawn_car_paint_sphere(&mut commands, &mut materials, &asset_server, &sphere);
72
spawn_coated_glass_bubble_sphere(&mut commands, &mut materials, &sphere);
73
spawn_golf_ball(&mut commands, &asset_server);
74
spawn_scratched_gold_ball(&mut commands, &mut materials, &asset_server, &sphere);
75
76
spawn_light(&mut commands);
77
spawn_camera(&mut commands, &asset_server);
78
spawn_text(&mut commands, &light_mode);
79
}
80
81
/// Generates a sphere.
82
fn create_sphere_mesh(meshes: &mut Assets<Mesh>) -> Handle<Mesh> {
83
// We're going to use normal maps, so make sure we've generated tangents, or
84
// else the normal maps won't show up.
85
86
let mut sphere_mesh = Sphere::new(1.0).mesh().build();
87
sphere_mesh
88
.generate_tangents()
89
.expect("Failed to generate tangents");
90
meshes.add(sphere_mesh)
91
}
92
93
/// Spawn a regular object with a clearcoat layer. This looks like car paint.
94
fn spawn_car_paint_sphere(
95
commands: &mut Commands,
96
materials: &mut Assets<StandardMaterial>,
97
asset_server: &AssetServer,
98
sphere: &Handle<Mesh>,
99
) {
100
commands
101
.spawn((
102
Mesh3d(sphere.clone()),
103
MeshMaterial3d(materials.add(StandardMaterial {
104
clearcoat: 1.0,
105
clearcoat_perceptual_roughness: 0.1,
106
normal_map_texture: Some(asset_server.load_with_settings(
107
"textures/BlueNoise-Normal.png",
108
|settings: &mut ImageLoaderSettings| settings.is_srgb = false,
109
)),
110
metallic: 0.9,
111
perceptual_roughness: 0.5,
112
base_color: BLUE.into(),
113
..default()
114
})),
115
Transform::from_xyz(-1.0, 1.0, 0.0).with_scale(Vec3::splat(SPHERE_SCALE)),
116
))
117
.insert(ExampleSphere);
118
}
119
120
/// Spawn a semitransparent object with a clearcoat layer.
121
fn spawn_coated_glass_bubble_sphere(
122
commands: &mut Commands,
123
materials: &mut Assets<StandardMaterial>,
124
sphere: &Handle<Mesh>,
125
) {
126
commands
127
.spawn((
128
Mesh3d(sphere.clone()),
129
MeshMaterial3d(materials.add(StandardMaterial {
130
clearcoat: 1.0,
131
clearcoat_perceptual_roughness: 0.1,
132
metallic: 0.5,
133
perceptual_roughness: 0.1,
134
base_color: Color::srgba(0.9, 0.9, 0.9, 0.3),
135
alpha_mode: AlphaMode::Blend,
136
..default()
137
})),
138
Transform::from_xyz(-1.0, -1.0, 0.0).with_scale(Vec3::splat(SPHERE_SCALE)),
139
))
140
.insert(ExampleSphere);
141
}
142
143
/// Spawns an object with both a clearcoat normal map (a scratched varnish) and
144
/// a main layer normal map (the golf ball pattern).
145
///
146
/// This object is in glTF format, using the `KHR_materials_clearcoat`
147
/// extension.
148
fn spawn_golf_ball(commands: &mut Commands, asset_server: &AssetServer) {
149
commands.spawn((
150
SceneRoot(
151
asset_server.load(GltfAssetLabel::Scene(0).from_asset("models/GolfBall/GolfBall.glb")),
152
),
153
Transform::from_xyz(1.0, 1.0, 0.0).with_scale(Vec3::splat(SPHERE_SCALE)),
154
ExampleSphere,
155
));
156
}
157
158
/// Spawns an object with only a clearcoat normal map (a scratch pattern) and no
159
/// main layer normal map.
160
fn spawn_scratched_gold_ball(
161
commands: &mut Commands,
162
materials: &mut Assets<StandardMaterial>,
163
asset_server: &AssetServer,
164
sphere: &Handle<Mesh>,
165
) {
166
commands
167
.spawn((
168
Mesh3d(sphere.clone()),
169
MeshMaterial3d(materials.add(StandardMaterial {
170
clearcoat: 1.0,
171
clearcoat_perceptual_roughness: 0.3,
172
clearcoat_normal_texture: Some(asset_server.load_with_settings(
173
"textures/ScratchedGold-Normal.png",
174
|settings: &mut ImageLoaderSettings| settings.is_srgb = false,
175
)),
176
metallic: 0.9,
177
perceptual_roughness: 0.1,
178
base_color: GOLD.into(),
179
..default()
180
})),
181
Transform::from_xyz(1.0, -1.0, 0.0).with_scale(Vec3::splat(SPHERE_SCALE)),
182
))
183
.insert(ExampleSphere);
184
}
185
186
/// Spawns a light.
187
fn spawn_light(commands: &mut Commands) {
188
commands.spawn(create_point_light());
189
}
190
191
/// Spawns a camera with associated skybox and environment map.
192
fn spawn_camera(commands: &mut Commands, asset_server: &AssetServer) {
193
commands
194
.spawn((
195
Camera3d::default(),
196
Hdr,
197
Projection::Perspective(PerspectiveProjection {
198
fov: 27.0 / 180.0 * PI,
199
..default()
200
}),
201
Transform::from_xyz(0.0, 0.0, 10.0),
202
AcesFitted,
203
))
204
.insert(Skybox {
205
brightness: 5000.0,
206
image: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
207
..default()
208
})
209
.insert(EnvironmentMapLight {
210
diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
211
specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
212
intensity: 2000.0,
213
..default()
214
});
215
}
216
217
/// Spawns the help text.
218
fn spawn_text(commands: &mut Commands, light_mode: &LightMode) {
219
commands.spawn((
220
light_mode.create_help_text(),
221
Node {
222
position_type: PositionType::Absolute,
223
bottom: px(12),
224
left: px(12),
225
..default()
226
},
227
));
228
}
229
230
/// Moves the light around.
231
fn animate_light(
232
mut lights: Query<&mut Transform, Or<(With<PointLight>, With<DirectionalLight>)>>,
233
time: Res<Time>,
234
) {
235
let now = time.elapsed_secs();
236
for mut transform in lights.iter_mut() {
237
transform.translation = vec3(
238
ops::sin(now * 1.4),
239
ops::cos(now * 1.0),
240
ops::cos(now * 0.6),
241
) * vec3(3.0, 4.0, 3.0);
242
transform.look_at(Vec3::ZERO, Vec3::Y);
243
}
244
}
245
246
/// Rotates the spheres.
247
fn animate_spheres(mut spheres: Query<&mut Transform, With<ExampleSphere>>, time: Res<Time>) {
248
let now = time.elapsed_secs();
249
for mut transform in spheres.iter_mut() {
250
transform.rotation = Quat::from_rotation_y(SPHERE_ROTATION_SPEED * now);
251
}
252
}
253
254
/// Handles the user pressing Space to change the type of light from point to
255
/// directional and vice versa.
256
fn handle_input(
257
mut commands: Commands,
258
mut light_query: Query<Entity, Or<(With<PointLight>, With<DirectionalLight>)>>,
259
keyboard: Res<ButtonInput<KeyCode>>,
260
mut light_mode: ResMut<LightMode>,
261
) {
262
if !keyboard.just_pressed(KeyCode::Space) {
263
return;
264
}
265
266
for light in light_query.iter_mut() {
267
match *light_mode {
268
LightMode::Point => {
269
*light_mode = LightMode::Directional;
270
commands
271
.entity(light)
272
.remove::<PointLight>()
273
.insert(create_directional_light());
274
}
275
LightMode::Directional => {
276
*light_mode = LightMode::Point;
277
commands
278
.entity(light)
279
.remove::<DirectionalLight>()
280
.insert(create_point_light());
281
}
282
}
283
}
284
}
285
286
/// Updates the help text at the bottom of the screen.
287
fn update_help_text(mut text_query: Query<&mut Text>, light_mode: Res<LightMode>) {
288
for mut text in text_query.iter_mut() {
289
*text = light_mode.create_help_text();
290
}
291
}
292
293
/// Creates or recreates the moving point light.
294
fn create_point_light() -> PointLight {
295
PointLight {
296
color: WHITE.into(),
297
intensity: 100000.0,
298
..default()
299
}
300
}
301
302
/// Creates or recreates the moving directional light.
303
fn create_directional_light() -> DirectionalLight {
304
DirectionalLight {
305
color: WHITE.into(),
306
illuminance: 1000.0,
307
..default()
308
}
309
}
310
311
impl LightMode {
312
/// Creates the help text at the bottom of the screen.
313
fn create_help_text(&self) -> Text {
314
let help_text = match *self {
315
LightMode::Point => "Press Space to switch to a directional light",
316
LightMode::Directional => "Press Space to switch to a point light",
317
};
318
319
Text::new(help_text)
320
}
321
}
322
323