bevy icon indicating copy to clipboard operation
bevy copied to clipboard

feat: vertex attribute validation for Material

Open dataphract opened this issue 3 years ago • 3 comments
trafficstars

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_attributes trait method, allowing implementors to specify the vertex attributes required for a Material.
  • Added verify_material_mesh_attributes() system to MaterialPlugin. Errors are now emitted if a Material is applied to a Mesh missing required attributes.
  • Added name() and id() methods on MeshVertexAttribute to retrieve the name and unique ID.

dataphract avatar Oct 22 '22 05:10 dataphract

@dataphract Cool idea! Do you think one of the examples (maybe shader_material.rs) should implement required_vertex_attributes() to show the feature?

DGriffin91 avatar Oct 22 '22 08:10 DGriffin91

Cool idea! Do you think one of the examples (maybe shader_material.rs) should implement required_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.

dataphract avatar Oct 22 '22 17:10 dataphract

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`

dataphract avatar Nov 07 '22 07:11 dataphract