From a2b53d46e73b98f7f5d074ed2bb8ddfedfcc5d4d Mon Sep 17 00:00:00 2001 From: Joona Aalto Date: Wed, 9 Oct 2024 18:39:10 +0300 Subject: [PATCH] Fix meshlet materials (#15755) # Objective After #15524, there are these bunny-shaped holes in rendering in the meshlet example! ![broken](https://github.com/user-attachments/assets/9e9f20ec-b820-44df-b961-68a1dee44002) This is because (1) they're using a raw asset handle instead of `MeshMaterial3d`, and (2) the system that extracts mesh materials into the render world has an unnecessary `With` filter, which makes it not account for meshlets. ## Solution Remove the redundant filter and use `MeshMaterial3d`. The bunnies got some paint! ![fixed](https://github.com/user-attachments/assets/adb42556-fd4b-4000-8ca8-1356250dd532) --- crates/bevy_pbr/src/material.rs | 2 +- crates/bevy_pbr/src/meshlet/mod.rs | 4 ++-- examples/3d/meshlet.rs | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/bevy_pbr/src/material.rs b/crates/bevy_pbr/src/material.rs index 05292f7fce846..ca9b63e95d7f5 100644 --- a/crates/bevy_pbr/src/material.rs +++ b/crates/bevy_pbr/src/material.rs @@ -550,7 +550,7 @@ pub(super) fn clear_material_instances( fn extract_mesh_materials( mut material_instances: ResMut>, - query: Extract), With>>, + query: Extract)>>, ) { for (entity, view_visibility, material) in &query { if view_visibility.get() { diff --git a/crates/bevy_pbr/src/meshlet/mod.rs b/crates/bevy_pbr/src/meshlet/mod.rs index 84c89ff4403cb..17793af5d637a 100644 --- a/crates/bevy_pbr/src/meshlet/mod.rs +++ b/crates/bevy_pbr/src/meshlet/mod.rs @@ -55,7 +55,7 @@ use self::{ }, visibility_buffer_raster_node::MeshletVisibilityBufferRasterPassNode, }; -use crate::{graph::NodePbr, Material}; +use crate::{graph::NodePbr, Material, MeshMaterial3d}; use bevy_app::{App, Plugin, PostUpdate}; use bevy_asset::{load_internal_asset, AssetApp, Handle}; use bevy_core_pipeline::{ @@ -288,7 +288,7 @@ impl Plugin for MeshletPlugin { #[derive(Bundle, Clone)] pub struct MaterialMeshletMeshBundle { pub meshlet_mesh: Handle, - pub material: Handle, + pub material: MeshMaterial3d, pub transform: Transform, pub global_transform: GlobalTransform, /// User indication of whether an entity is visible diff --git a/examples/3d/meshlet.rs b/examples/3d/meshlet.rs index d8a09a2356f7a..5a9e04019428a 100644 --- a/examples/3d/meshlet.rs +++ b/examples/3d/meshlet.rs @@ -86,7 +86,7 @@ fn setup( for x in -2..=2 { commands.spawn(MaterialMeshletMeshBundle { meshlet_mesh: meshlet_mesh_handle.clone(), - material: standard_materials.add(StandardMaterial { + material: MeshMaterial3d(standard_materials.add(StandardMaterial { base_color: match x { -2 => Srgba::hex("#dc2626").unwrap().into(), -1 => Srgba::hex("#ea580c").unwrap().into(), @@ -97,7 +97,7 @@ fn setup( }, perceptual_roughness: (x + 2) as f32 / 4.0, ..default() - }), + })), transform: Transform::default() .with_scale(Vec3::splat(0.2)) .with_translation(Vec3::new(x as f32 / 2.0, 0.0, -0.3)), @@ -107,7 +107,7 @@ fn setup( for x in -2..=2 { commands.spawn(MaterialMeshletMeshBundle { meshlet_mesh: meshlet_mesh_handle.clone(), - material: debug_material.clone(), + material: debug_material.clone().into(), transform: Transform::default() .with_scale(Vec3::splat(0.2)) .with_rotation(Quat::from_rotation_y(PI))