meshlet/
meshlet.rs

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