bevy/crates
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
..
bevy_a11y Various accessibility API updates. (#9989) 2023-10-02 21:22:52 +00:00
bevy_animation refactor: Change Option<With<T>> query params to Has<T> (#9959) 2023-10-02 01:21:41 +00:00
bevy_app reflect: TypePath part 2 (#8768) 2023-10-09 19:33:03 +00:00
bevy_asset Removed anyhow (#10003) 2023-10-06 07:20:13 +00:00
bevy_audio More ergonomic spatial audio (#9800) 2023-10-09 19:43:56 +00:00
bevy_core Bevy Asset V2 (#8624) 2023-09-07 02:07:27 +00:00
bevy_core_pipeline Fix comment grammar (#9990) 2023-10-01 20:18:34 +00:00
bevy_derive bevy_derive: Fix #[deref] breaking other attributes (#9551) 2023-08-28 17:36:18 +00:00
bevy_diagnostic Improve doc formatting. (#9840) 2023-09-18 19:43:56 +00:00
bevy_dylib Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_dynamic_plugin Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_ecs Make builder types take and return Self (#10001) 2023-10-09 19:46:17 +00:00
bevy_ecs_compile_fail_tests Updates for rust 1.73 (#10035) 2023-10-06 00:31:10 +00:00
bevy_encase_derive Bump Version after Release (#9106) 2023-07-10 21:19:27 +00:00
bevy_gilrs Finish documenting bevy_gilrs (#10010) 2023-10-04 21:10:20 +00:00
bevy_gizmos Updates for rust 1.73 (#10035) 2023-10-06 00:31:10 +00:00
bevy_gltf Removed anyhow (#10003) 2023-10-06 07:20:13 +00:00
bevy_hierarchy Fix some warnings shown in nightly (#10012) 2023-10-05 05:41:09 +00:00
bevy_input Updates for rust 1.73 (#10035) 2023-10-06 00:31:10 +00:00
bevy_internal Fix 2d_shapes and general 2D mesh instancing (#10051) 2023-10-08 20:17:01 +00:00
bevy_log Update tracy-client requirement from 0.15 to 0.16 (#9436) 2023-08-15 07:45:21 +00:00
bevy_macro_utils Update toml_edit requirement from 0.19 to 0.20 (#10058) 2023-10-09 11:55:16 +00:00
bevy_macros_compile_fail_tests bevy_derive: Fix #[deref] breaking other attributes (#9551) 2023-08-28 17:36:18 +00:00
bevy_math Automatic batching/instancing of draw commands (#9685) 2023-09-21 22:12:34 +00:00
bevy_mikktspace Fix erronenous glam version (#9653) 2023-08-31 12:55:17 +00:00
bevy_pbr Add consuming builder methods for more ergonomic Mesh creation (#10056) 2023-10-09 19:47:41 +00:00
bevy_ptr Put #[repr(transparent)] attr to bevy_ptr types (#9068) 2023-07-14 18:55:15 +00:00
bevy_reflect reflect: TypePath part 2 (#8768) 2023-10-09 19:33:03 +00:00
bevy_reflect_compile_fail_tests Improve TypeUuid's derive macro error messages (#9315) 2023-10-02 12:42:01 +00:00
bevy_render Add consuming builder methods for more ergonomic Mesh creation (#10056) 2023-10-09 19:47:41 +00:00
bevy_scene Make builder types take and return Self (#10001) 2023-10-09 19:46:17 +00:00
bevy_sprite Fix 2d_shapes and general 2D mesh instancing (#10051) 2023-10-08 20:17:01 +00:00
bevy_tasks add test for nested scopes (#10026) 2023-10-05 06:05:43 +00:00
bevy_text Removed anyhow (#10003) 2023-10-06 07:20:13 +00:00
bevy_time ignore time channel error (#9981) 2023-10-01 07:55:17 +00:00
bevy_transform Updates for rust 1.73 (#10035) 2023-10-06 00:31:10 +00:00
bevy_ui UI node outlines (#9931) 2023-10-05 12:10:32 +00:00
bevy_utils Use EntityHashMap<Entity, T> for render world entity storage for better performance (#9903) 2023-09-27 08:28:28 +00:00
bevy_window Various accessibility API updates. (#9989) 2023-10-02 21:22:52 +00:00
bevy_winit Allow Bevy to start from non-main threads on supported platforms (#10020) 2023-10-06 13:26:06 +00:00