Skip to content

Commit 4645da3

Browse files
author
Julian Heinken
authored
Mesh overhaul with custom vertex attributes #592 (#599)
Mesh overhaul with custom vertex attributes
1 parent ad940fb commit 4645da3

25 files changed

+452
-682
lines changed

CHANGELOG.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ to view all changes since the `0.2.1` release.
1111
## Unreleased
1212

1313
### Added
14-
14+
- [Mesh overhaul with custom vertex attributes][616] for `Mesh`
15+
- Any vertex attribute can now be added over `mesh.attributes.insert()`. For example: `mesh.attributes.insert(Cow::Borrowed(Mesh::ATTRIBUTE_POSITION), points.into())`.
16+
- For missing attributes (requested by shader, but not defined by mesh), Bevy will provide a zero-filled fallback buffer.
17+
- [Touch Input][696]
18+
- [Do not depend on spirv on wasm32 target][689]
19+
- [Another fast compile flag for macOS][552]
20+
- [Mesh overhaul with custom vertex attributes][599]
1521
- [Introduce Mouse capture API][679]
1622
- [`bevy_input::touch`: implement touch input][696]
1723
- [D-pad support on MacOS][653]
@@ -56,6 +62,12 @@ to view all changes since the `0.2.1` release.
5662
`Color::rgb` and `Color::rgba` will be converted to linear sRGB under-the-hood.
5763
- This allows drop-in use of colors from most applications.
5864
- New methods `Color::rgb_linear` and `Color::rgba_linear` will accept colors already in linear sRGB (the old behavior)
65+
- Individual color-components must now be accessed through setters and getters: `.r`, `.g`, `.b`, `.a`, `.set_r`, `.set_g`, `.set_b`, `.set_a`, and the corresponding methods with the `*_linear` suffix.
66+
- Breaking Change: [Mesh overhaul with custom vertex attributes][616] for `Mesh`
67+
- Removed `VertexAttribute`, `Vertex`, `AsVertexBufferDescriptor`.
68+
69+
- Despawning an entity multiple times causes a debug-level log message to be emitted instead of a panic [649] [651]
70+
- Breaking Change: Migrated to rodio 0.12, this means:
5971
- Individual color-components must now be accessed through setters and getters:
6072
`.r`, `.g`, `.b`, `.a`, `.set_r`, `.set_g`, `.set_b`, `.set_a`, and the corresponding methods with the `*_linear` suffix.
6173
- Despawning an entity multiple times causes a debug-level log message to be emitted instead of a panic: [#649][649], [#651][651]
@@ -65,6 +77,9 @@ to view all changes since the `0.2.1` release.
6577

6678
### Fixed
6779

80+
[599]: https://github.com/bevyengine/bevy/pull/599
81+
[696]: https://github.com/bevyengine/bevy/pull/696
82+
[689]: https://github.com/bevyengine/bevy/pull/689
6883
- [Properly update bind group ids when setting dynamic bindings][560]
6984
- [Properly exit the app on AppExit event][610]
7085
- [Fix FloatOrd hash being different for different NaN values][618]

crates/bevy_derive/src/as_vertex_buffer_descriptor.rs

Lines changed: 0 additions & 124 deletions
This file was deleted.

crates/bevy_derive/src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
extern crate proc_macro;
22

33
mod app_plugin;
4-
mod as_vertex_buffer_descriptor;
54
mod bytes;
65
mod modules;
76
mod render_resource;
@@ -45,12 +44,6 @@ pub fn derive_shader_defs(input: TokenStream) -> TokenStream {
4544
shader_defs::derive_shader_defs(input)
4645
}
4746

48-
/// Derives the AsVertexBufferDescriptor trait.
49-
#[proc_macro_derive(AsVertexBufferDescriptor, attributes(vertex, as_crate))]
50-
pub fn derive_as_vertex_buffer_descriptor(input: TokenStream) -> TokenStream {
51-
as_vertex_buffer_descriptor::derive_as_vertex_buffer_descriptor(input)
52-
}
53-
5447
/// Generates a dynamic plugin entry point function for the given `Plugin` type.
5548
#[proc_macro_derive(DynamicPlugin)]
5649
pub fn derive_dynamic_plugin(input: TokenStream) -> TokenStream {

crates/bevy_gltf/src/loader.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bevy_ecs::{bevy_utils::BoxedFuture, World, WorldBuilderSource};
44
use bevy_math::Mat4;
55
use bevy_pbr::prelude::{PbrComponents, StandardMaterial};
66
use bevy_render::{
7-
mesh::{Indices, Mesh, VertexAttribute},
7+
mesh::{Indices, Mesh, VertexAttributeValues},
88
pipeline::PrimitiveTopology,
99
prelude::{Color, Texture},
1010
texture::{AddressMode, FilterMode, SamplerDescriptor, TextureFormat},
@@ -20,7 +20,7 @@ use gltf::{
2020
Primitive,
2121
};
2222
use image::{GenericImageView, ImageFormat};
23-
use std::path::Path;
23+
use std::{borrow::Cow, path::Path};
2424
use thiserror::Error;
2525

2626
/// An error that occurs when loading a GLTF file
@@ -88,23 +88,26 @@ async fn load_gltf<'a, 'b>(
8888

8989
if let Some(vertex_attribute) = reader
9090
.read_positions()
91-
.map(|v| VertexAttribute::position(v.collect()))
91+
.map(|v| VertexAttributeValues::Float3(v.collect()))
9292
{
93-
mesh.attributes.push(vertex_attribute);
93+
mesh.attributes
94+
.insert(Cow::Borrowed(Mesh::ATTRIBUTE_POSITION), vertex_attribute);
9495
}
9596

9697
if let Some(vertex_attribute) = reader
9798
.read_normals()
98-
.map(|v| VertexAttribute::normal(v.collect()))
99+
.map(|v| VertexAttributeValues::Float3(v.collect()))
99100
{
100-
mesh.attributes.push(vertex_attribute);
101+
mesh.attributes
102+
.insert(Cow::Borrowed(Mesh::ATTRIBUTE_NORMAL), vertex_attribute);
101103
}
102104

103105
if let Some(vertex_attribute) = reader
104106
.read_tex_coords(0)
105-
.map(|v| VertexAttribute::uv(v.into_f32().collect()))
107+
.map(|v| VertexAttributeValues::Float2(v.into_f32().collect()))
106108
{
107-
mesh.attributes.push(vertex_attribute);
109+
mesh.attributes
110+
.insert(Cow::Borrowed(Mesh::ATTRIBUTE_UV_0), vertex_attribute);
108111
}
109112

110113
if let Some(indices) = reader.read_indices() {

crates/bevy_render/src/draw.rs

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
pipeline::{
33
PipelineCompiler, PipelineDescriptor, PipelineLayout, PipelineSpecialization,
4-
VertexBufferDescriptors,
4+
VERTEX_FALLBACK_LAYOUT_NAME,
55
},
66
renderer::{
77
BindGroup, BindGroupId, BufferId, BufferUsage, RenderResource, RenderResourceBinding,
@@ -131,7 +131,6 @@ pub struct DrawContext<'a> {
131131
pub shaders: ResMut<'a, Assets<Shader>>,
132132
pub pipeline_compiler: ResMut<'a, PipelineCompiler>,
133133
pub render_resource_context: Res<'a, Box<dyn RenderResourceContext>>,
134-
pub vertex_buffer_descriptors: Res<'a, VertexBufferDescriptors>,
135134
pub shared_buffers: Res<'a, SharedBuffers>,
136135
pub current_pipeline: Option<Handle<PipelineDescriptor>>,
137136
}
@@ -143,7 +142,6 @@ impl<'a> UnsafeClone for DrawContext<'a> {
143142
shaders: self.shaders.unsafe_clone(),
144143
pipeline_compiler: self.pipeline_compiler.unsafe_clone(),
145144
render_resource_context: self.render_resource_context.unsafe_clone(),
146-
vertex_buffer_descriptors: self.vertex_buffer_descriptors.unsafe_clone(),
147145
shared_buffers: self.shared_buffers.unsafe_clone(),
148146
current_pipeline: self.current_pipeline.clone(),
149147
}
@@ -166,7 +164,6 @@ impl<'a> FetchResource<'a> for FetchDrawContext {
166164
resources.borrow_mut::<Assets<Shader>>();
167165
resources.borrow_mut::<PipelineCompiler>();
168166
resources.borrow::<Box<dyn RenderResourceContext>>();
169-
resources.borrow::<VertexBufferDescriptors>();
170167
resources.borrow::<SharedBuffers>();
171168
}
172169

@@ -175,7 +172,6 @@ impl<'a> FetchResource<'a> for FetchDrawContext {
175172
resources.release_mut::<Assets<Shader>>();
176173
resources.release_mut::<PipelineCompiler>();
177174
resources.release::<Box<dyn RenderResourceContext>>();
178-
resources.release::<VertexBufferDescriptors>();
179175
resources.release::<SharedBuffers>();
180176
}
181177

@@ -205,9 +201,6 @@ impl<'a> FetchResource<'a> for FetchDrawContext {
205201
render_resource_context: Res::new(
206202
resources.get_unsafe_ref::<Box<dyn RenderResourceContext>>(ResourceIndex::Global),
207203
),
208-
vertex_buffer_descriptors: Res::new(
209-
resources.get_unsafe_ref::<VertexBufferDescriptors>(ResourceIndex::Global),
210-
),
211204
shared_buffers: Res::new(
212205
resources.get_unsafe_ref::<SharedBuffers>(ResourceIndex::Global),
213206
),
@@ -221,7 +214,6 @@ impl<'a> FetchResource<'a> for FetchDrawContext {
221214
access.add_write(TypeId::of::<Assets<Shader>>());
222215
access.add_write(TypeId::of::<PipelineCompiler>());
223216
access.add_read(TypeId::of::<Box<dyn RenderResourceContext>>());
224-
access.add_read(TypeId::of::<VertexBufferDescriptors>());
225217
access.add_read(TypeId::of::<SharedBuffers>());
226218
access
227219
}
@@ -262,7 +254,6 @@ impl<'a> DrawContext<'a> {
262254
&mut self.pipelines,
263255
&mut self.shaders,
264256
pipeline_handle,
265-
&self.vertex_buffer_descriptors,
266257
specialization,
267258
)
268259
};
@@ -358,18 +349,21 @@ impl<'a> DrawContext<'a> {
358349
let layout = pipeline_descriptor
359350
.get_layout()
360351
.ok_or(DrawError::PipelineHasNoLayout)?;
361-
for (slot, vertex_buffer_descriptor) in layout.vertex_buffer_descriptors.iter().enumerate()
362-
{
363-
for bindings in render_resource_bindings.iter() {
364-
if let Some((vertex_buffer, index_buffer)) =
365-
bindings.get_vertex_buffer(&vertex_buffer_descriptor.name)
366-
{
367-
draw.set_vertex_buffer(slot as u32, vertex_buffer, 0);
368-
if let Some(index_buffer) = index_buffer {
369-
draw.set_index_buffer(index_buffer, 0);
370-
}
371-
372-
break;
352+
// figure out if the fallback buffer is needed
353+
let need_fallback_buffer = layout
354+
.vertex_buffer_descriptors
355+
.iter()
356+
.any(|x| x.name == VERTEX_FALLBACK_LAYOUT_NAME);
357+
for bindings in render_resource_bindings.iter() {
358+
if let Some(index_buffer) = bindings.index_buffer {
359+
draw.set_index_buffer(index_buffer, 0);
360+
}
361+
if let Some(main_vertex_buffer) = bindings.vertex_attribute_buffer {
362+
draw.set_vertex_buffer(0, main_vertex_buffer, 0);
363+
}
364+
if need_fallback_buffer {
365+
if let Some(fallback_vertex_buffer) = bindings.vertex_fallback_buffer {
366+
draw.set_vertex_buffer(1, fallback_vertex_buffer, 0);
373367
}
374368
}
375369
}

crates/bevy_render/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use camera::{
3838
};
3939
use pipeline::{
4040
DynamicBinding, IndexFormat, PipelineCompiler, PipelineDescriptor, PipelineSpecialization,
41-
PrimitiveTopology, ShaderSpecialization, VertexBufferDescriptors,
41+
PrimitiveTopology, ShaderSpecialization,
4242
};
4343
use render_graph::{
4444
base::{self, BaseRenderGraphBuilder, BaseRenderGraphConfig},
@@ -119,7 +119,6 @@ impl Plugin for RenderPlugin {
119119
.init_resource::<RenderGraph>()
120120
.init_resource::<PipelineCompiler>()
121121
.init_resource::<RenderResourceBindings>()
122-
.init_resource::<VertexBufferDescriptors>()
123122
.init_resource::<TextureResourceSystemState>()
124123
.init_resource::<AssetRenderResourceBindings>()
125124
.init_resource::<ActiveCameras>()

0 commit comments

Comments
 (0)