bevy
bevy copied to clipboard
feat: vertex attribute validation for Material
Objective
Fixes #6128. (Original suggestion from https://github.com/bevyengine/bevy/pull/6127#pullrequestreview-1125960865).
Solution
This PR adds a new trait method, Material::required_vertex_attributes, which allows implementors to specify a list of vertex attributes that are required for the Material to function.
A new system has been added to MaterialPlugin which emits an error message when an entity is found to have a Mesh that lacks required vertex attributes for its Material.
Changelog
Added
- Added
Material::required_vertex_attributestrait method, allowing implementors to specify the vertex attributes required for aMaterial. - Added
verify_material_mesh_attributes()system toMaterialPlugin. Errors are now emitted if aMaterialis applied to aMeshmissing required attributes. - Added
name()andid()methods onMeshVertexAttributeto retrieve the name and unique ID.
@dataphract Cool idea! Do you think one of the examples (maybe shader_material.rs) should implement required_vertex_attributes() to show the feature?
Cool idea! Do you think one of the examples (maybe
shader_material.rs) should implementrequired_vertex_attributes()to show the feature?
Definitely a good idea, just want to get consensus on how the interface should look before updating examples. Those could be in one or more follow-up PRs.
This ended up being a surprisingly difficult system to write due to the distinction @superdump pointed out -- it needs to detect changes to the mesh assets themselves and not just the handles. This requires a lot of bookkeeping (did this entity's mesh change? did it change its mesh? has this mesh already been validated?) but ultimately it still runs linearly in the number of entities and doesn't require new allocations on every invocation.
The new error message looks like this:
ERROR bevy_pbr::material: Entity 1v0: mesh with ID `Id(8ecbac0f-f545-4473-ad43-e1f4243af51e, 8621032393721330913)` is missing vertex attribute `Vertex_Normal`, which is required by `bevy_pbr::pbr_material::StandardMaterial`