Accept raw vertex buffer data when creating a Mesh
- Dominant language
- Rust
- Stars
- 48.2k
- Forks
- 4.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 171
Description
## What problem does this solve or what need does it fill?
Currently, to build a custom mesh, we have to do provide each vertex buffer independently like so:
```rs
Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
)
.with_inserted_indices(Indices::U32(indices))
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
```
If I have a buffer where the data is already interleaved like so:
```
[position, normal, uv, position, normal, uv, position, normal, uv, ...]
```
Splitting it in different buffers to provide each of them to `with_inserted_attribute` is a waste of memory and CPU time because in the end, the different buffers are merged into one inside [`write_packed_vertex_buffer_data`](https://github.com/bevyengine/bevy/blob/24c3bd5f005867aa483237071e3add4f13a43904/crates/bevy_mesh/src/mesh.rs#L469) before being sent to the GPU.
## What solution would you like?
So I would like to be able to provide my buffer that is already ready to be sent to the GPU and avoid all that decomposition (done by me) then recomposition (done in [`write_packed_vertex_buffer_data`](https://github.com/bevyengine/bevy/blob/24c3bd5f005867aa483237071e3add4f13a43904/crates/bevy_mesh/src/mesh.rs#L469)):
```rs
Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
)
.with_inserted_indices(Indices::U32(indices))
.with_raw_vertex_buffer(buffer, vec![Mesh::ATTRIBUTE_POSITION, Mesh::ATTRIBUTE_NORMAL, Mesh::ATTRIBUTE_UV_0]);
```
## What alternative(s) have you considered?
Currently, I split my existing buffer into multiple buffers (one for each vertex attribute), and I call with_inserted_attribute multiple times.
## Additional context
The workaround I'm using seems to be used in the [glTF file loader](https://github.com/bevyengine/bevy/blob/24c3bd5f005867aa483237071e3add4f13a43904/crates/bevy_gltf/src/loader.rs#L659-L667). So maybe this feature will greatly speed up the loading of large glTF files too?
Contributor guide
Research direction
Start in crates/bevy_mesh/src/mesh.rs at write_packed_vertex_buffer_data to understand how inserted attributes are combined before reaching the GPU. Compare that path with the glTF loader in crates/bevy_gltf/src/loader.rs around lines 659-667; done means Mesh can accept the already interleaved buffer and the attribute layout without requiring the caller to split and recombine it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- computer-graphics, game-dev
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100