Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checked methods to add vertex attributes to mesh only if values is not empty #17690

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hukasu
Copy link
Contributor

@hukasu hukasu commented Feb 5, 2025

Objective

When creating a custom mesh, there might be an attribute that is optional, that way you would need to store the Mesh in a variable, insert the required attributes, then check if the optional attribute exists or not, and insert if it does. With this checked methods you can use with the builder patterns

Solution

Create methods insert_attribute_if_not_empty and with_inserted_attribute_if_not_empty (names are tentative).

Testing

Used in a modification to SphereMeshBuilder (PR #17691)

Showcase

Before

        let mut mesh = Mesh::new(
            PrimitiveTopology::TriangleList,
            RenderAssetUsages::default(),
        )
        .with_inserted_indices(indices)
        .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, points)
        .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
        .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs);

         if optional_attribute {
                 let optional_attribute = generate_optional_attribute();
                 mesh.insert_attribute(OPTIONAL_ATTRIBUTE, optional_attribute);
        }

       mesh

After

let optional_attribute = if optional_attribute {
          generate_optional_attribute()
} else {
    vec![]
};

Mesh::new(
    PrimitiveTopology::TriangleList,
    RenderAssetUsages::default(),
)
.with_inserted_indices(indices)
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, points)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
.with_inserted_attribute_if_not_empty(OPTIONAL_ATTRIBUTE, optional_attribue)

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

2 participants