bevy/examples/shader
Marco Buono 12a2f83edd
Add consuming builder methods for more ergonomic Mesh creation (#10056)
# Objective

- This PR aims to make creating meshes a little bit more ergonomic,
specifically by removing the need for intermediate mutable variables.

## Solution

- We add methods that consume the `Mesh` and return a mesh with the
specified changes, so that meshes can be entirely constructed via
builder-style calls, without intermediate variables;
- Methods are flagged with `#[must_use]` to ensure proper use;
- Examples are updated to use the new methods where applicable. Some
examples are kept with the mutating methods so that users can still
easily discover them, and also where the new methods wouldn't really be
an improvement.

## Examples

Before:

```rust
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, vs);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vns);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, vts);
mesh.set_indices(Some(Indices::U32(tris)));
mesh
```

After:

```rust
Mesh::new(PrimitiveTopology::TriangleList)
    .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vs)
    .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, vns)
    .with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, vts)
    .with_indices(Some(Indices::U32(tris)))
```

Before:

```rust
let mut cube = Mesh::from(shape::Cube { size: 1.0 });

cube.generate_tangents().unwrap();

PbrBundle {
    mesh: meshes.add(cube),
    ..default()
}
```

After:

```rust
PbrBundle {
    mesh: meshes.add(
        Mesh::from(shape::Cube { size: 1.0 })
            .with_generated_tangents()
            .unwrap(),
    ),
    ..default()
}
```

---

## Changelog

- Added consuming builder methods for more ergonomic `Mesh` creation:
`with_inserted_attribute()`, `with_removed_attribute()`,
`with_indices()`, `with_duplicated_vertices()`,
`with_computed_flat_normals()`, `with_generated_tangents()`,
`with_morph_targets()`, `with_morph_target_names()`.
2023-10-09 19:47:41 +00:00
..
animate_shader.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
array_texture.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
compute_shader_game_of_life.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
custom_vertex_attribute.rs Add consuming builder methods for more ergonomic Mesh creation (#10056) 2023-10-09 19:47:41 +00:00
fallback_image.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
post_processing.rs Fix some warnings shown in nightly (#10012) 2023-10-05 05:41:09 +00:00
shader_defs.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
shader_instancing.rs Use EntityHashMap<Entity, T> for render world entity storage for better performance (#9903) 2023-09-27 08:28:28 +00:00
shader_material.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
shader_material_glsl.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
shader_material_screenspace_texture.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
shader_prepass.rs Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
texture_binding_array.rs Copy on Write AssetPaths (#9729) 2023-09-09 23:15:10 +00:00