Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/examples/3d/meshlet.rs
9331 views
1
//! Meshlet rendering for dense high-poly scenes (experimental).
2
3
// Note: This example showcases the meshlet API, but is not the type of scene that would benefit from using meshlets.
4
5
use bevy::{
6
camera_controller::free_camera::{FreeCamera, FreeCameraPlugin},
7
light::{CascadeShadowConfigBuilder, DirectionalLightShadowMap},
8
pbr::experimental::meshlet::{MeshletMesh3d, MeshletPlugin},
9
prelude::*,
10
render::render_resource::AsBindGroup,
11
};
12
use std::f32::consts::PI;
13
14
const ASSET_URL: &str =
15
"https://github.com/bevyengine/bevy_asset_files/raw/6dccaef517bde74d1969734703709aead7211dbc/meshlet/bunny.meshlet_mesh";
16
17
fn main() {
18
App::new()
19
.insert_resource(DirectionalLightShadowMap { size: 4096 })
20
.add_plugins((
21
DefaultPlugins,
22
MeshletPlugin {
23
cluster_buffer_slots: 1 << 14,
24
},
25
MaterialPlugin::<MeshletDebugMaterial>::default(),
26
FreeCameraPlugin,
27
))
28
.add_systems(Startup, setup)
29
.run();
30
}
31
32
fn setup(
33
mut commands: Commands,
34
asset_server: Res<AssetServer>,
35
mut standard_materials: ResMut<Assets<StandardMaterial>>,
36
mut debug_materials: ResMut<Assets<MeshletDebugMaterial>>,
37
mut meshes: ResMut<Assets<Mesh>>,
38
) {
39
commands.spawn((
40
Camera3d::default(),
41
Transform::from_translation(Vec3::new(1.8, 0.4, -0.1)).looking_at(Vec3::ZERO, Vec3::Y),
42
Msaa::Off,
43
EnvironmentMapLight {
44
diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
45
specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
46
intensity: 150.0,
47
..default()
48
},
49
FreeCamera::default(),
50
));
51
52
commands.spawn((
53
DirectionalLight {
54
illuminance: light_consts::lux::FULL_DAYLIGHT,
55
shadow_maps_enabled: true,
56
..default()
57
},
58
CascadeShadowConfigBuilder {
59
num_cascades: 1,
60
maximum_distance: 15.0,
61
..default()
62
}
63
.build(),
64
Transform::from_rotation(Quat::from_euler(EulerRot::ZYX, 0.0, PI * -0.15, PI * -0.15)),
65
));
66
67
// A custom file format storing a [`bevy_mesh::Mesh`]
68
// that has been converted to a [`bevy_pbr::meshlet::MeshletMesh`]
69
// using [`bevy_pbr::meshlet::MeshletMesh::from_mesh`], which is
70
// a function only available when the `meshlet_processor` cargo feature is enabled.
71
let meshlet_mesh_handle = asset_server.load(ASSET_URL);
72
let debug_material = debug_materials.add(MeshletDebugMaterial::default());
73
74
for x in -2..=2 {
75
commands.spawn((
76
MeshletMesh3d(meshlet_mesh_handle.clone()),
77
MeshMaterial3d(standard_materials.add(StandardMaterial {
78
base_color: match x {
79
-2 => Srgba::hex("#dc2626").unwrap().into(),
80
-1 => Srgba::hex("#ea580c").unwrap().into(),
81
0 => Srgba::hex("#facc15").unwrap().into(),
82
1 => Srgba::hex("#16a34a").unwrap().into(),
83
2 => Srgba::hex("#0284c7").unwrap().into(),
84
_ => unreachable!(),
85
},
86
perceptual_roughness: (x + 2) as f32 / 4.0,
87
..default()
88
})),
89
Transform::default()
90
.with_scale(Vec3::splat(0.2))
91
.with_translation(Vec3::new(x as f32 / 2.0, 0.0, -0.3)),
92
));
93
}
94
for x in -2..=2 {
95
commands.spawn((
96
MeshletMesh3d(meshlet_mesh_handle.clone()),
97
MeshMaterial3d(debug_material.clone()),
98
Transform::default()
99
.with_scale(Vec3::splat(0.2))
100
.with_rotation(Quat::from_rotation_y(PI))
101
.with_translation(Vec3::new(x as f32 / 2.0, 0.0, 0.3)),
102
));
103
}
104
105
commands.spawn((
106
Mesh3d(meshes.add(Plane3d::default().mesh().size(5.0, 5.0))),
107
MeshMaterial3d(standard_materials.add(StandardMaterial {
108
base_color: Color::WHITE,
109
perceptual_roughness: 1.0,
110
..default()
111
})),
112
));
113
}
114
115
#[derive(Asset, TypePath, AsBindGroup, Clone, Default)]
116
struct MeshletDebugMaterial {
117
_dummy: (),
118
}
119
120
impl Material for MeshletDebugMaterial {}
121
122