Skip to content

Commit 16ff0ac

Browse files
committed
Fmt.
1 parent e5145c5 commit 16ff0ac

File tree

7 files changed

+125
-65
lines changed

7 files changed

+125
-65
lines changed

crates/processing_ffi/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,6 @@ pub extern "C" fn processing_geometry_index(geo_id: u64, i: u32) {
836836
error::check(|| geometry_index(entity, i));
837837
}
838838

839-
840839
#[unsafe(no_mangle)]
841840
pub extern "C" fn processing_geometry_vertex_count(geo_id: u64) -> u32 {
842841
error::clear_error();
@@ -1009,10 +1008,7 @@ pub extern "C" fn processing_model(window_id: u64, geo_id: u64) {
10091008
error::clear_error();
10101009
let window_entity = Entity::from_bits(window_id);
10111010
let geo_entity = Entity::from_bits(geo_id);
1012-
error::check(|| graphics_record_command(
1013-
window_entity,
1014-
DrawCommand::Geometry(geo_entity),
1015-
));
1011+
error::check(|| graphics_record_command(window_entity, DrawCommand::Geometry(geo_entity)));
10161012
}
10171013

10181014
#[unsafe(no_mangle)]

crates/processing_render/src/geometry/attributes.rs renamed to crates/processing_render/src/geometry/attribute.rs

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use bevy::{
88

99
use crate::error::{ProcessingError, Result};
1010

11-
use super::{hash_attr_name, Geometry};
11+
use super::{Geometry, hash_attr_name};
1212

1313
fn clamp_range(range: Range<usize>, len: usize) -> Range<usize> {
1414
range.start.min(len)..range.end.min(len)
@@ -168,7 +168,11 @@ impl Attribute {
168168
let name: &'static str = Box::leak(name.into().into_boxed_str());
169169
let id = hash_attr_name(name);
170170
let inner = MeshVertexAttribute::new(name, id, format.to_vertex_format());
171-
Self { name, format, inner }
171+
Self {
172+
name,
173+
format,
174+
inner,
175+
}
172176
}
173177

174178
pub fn from_builtin(inner: MeshVertexAttribute, format: AttributeFormat) -> Self {
@@ -195,31 +199,50 @@ pub struct BuiltinAttributes {
195199
impl FromWorld for BuiltinAttributes {
196200
fn from_world(world: &mut World) -> Self {
197201
let position = world
198-
.spawn(Attribute::from_builtin(Mesh::ATTRIBUTE_POSITION, AttributeFormat::Float3))
202+
.spawn(Attribute::from_builtin(
203+
Mesh::ATTRIBUTE_POSITION,
204+
AttributeFormat::Float3,
205+
))
199206
.id();
200207
let normal = world
201-
.spawn(Attribute::from_builtin(Mesh::ATTRIBUTE_NORMAL, AttributeFormat::Float3))
208+
.spawn(Attribute::from_builtin(
209+
Mesh::ATTRIBUTE_NORMAL,
210+
AttributeFormat::Float3,
211+
))
202212
.id();
203213
let color = world
204-
.spawn(Attribute::from_builtin(Mesh::ATTRIBUTE_COLOR, AttributeFormat::Float4))
214+
.spawn(Attribute::from_builtin(
215+
Mesh::ATTRIBUTE_COLOR,
216+
AttributeFormat::Float4,
217+
))
205218
.id();
206219
let uv = world
207-
.spawn(Attribute::from_builtin(Mesh::ATTRIBUTE_UV_0, AttributeFormat::Float2))
220+
.spawn(Attribute::from_builtin(
221+
Mesh::ATTRIBUTE_UV_0,
222+
AttributeFormat::Float2,
223+
))
208224
.id();
209225

210-
Self { position, normal, color, uv }
226+
Self {
227+
position,
228+
normal,
229+
color,
230+
uv,
231+
}
211232
}
212233
}
213234

214-
pub fn create_attribute(
235+
pub fn create(
215236
In((name, format)): In<(String, AttributeFormat)>,
216237
mut commands: Commands,
217-
) -> Entity {
218-
commands.spawn(Attribute::new(name, format)).id()
238+
) -> Result<Entity> {
239+
// TODO: validation?
240+
Ok(commands.spawn(Attribute::new(name, format)).id())
219241
}
220242

221-
pub fn destroy_attribute(In(entity): In<Entity>, mut commands: Commands) {
243+
pub fn destroy(In(entity): In<Entity>, mut commands: Commands) -> Result<()> {
222244
commands.entity(entity).despawn();
245+
Ok(())
223246
}
224247

225248
pub fn get_attribute(
@@ -277,18 +300,22 @@ pub fn get_attributes(
277300
})?;
278301

279302
match attr {
280-
VertexAttributeValues::Float32(v) => {
281-
Ok(v[clamp_range(range, v.len())].iter().map(|&x| AttributeValue::Float(x)).collect())
282-
}
283-
VertexAttributeValues::Float32x2(v) => {
284-
Ok(v[clamp_range(range, v.len())].iter().map(|&x| AttributeValue::Float2(x)).collect())
285-
}
286-
VertexAttributeValues::Float32x3(v) => {
287-
Ok(v[clamp_range(range, v.len())].iter().map(|&x| AttributeValue::Float3(x)).collect())
288-
}
289-
VertexAttributeValues::Float32x4(v) => {
290-
Ok(v[clamp_range(range, v.len())].iter().map(|&x| AttributeValue::Float4(x)).collect())
291-
}
303+
VertexAttributeValues::Float32(v) => Ok(v[clamp_range(range, v.len())]
304+
.iter()
305+
.map(|&x| AttributeValue::Float(x))
306+
.collect()),
307+
VertexAttributeValues::Float32x2(v) => Ok(v[clamp_range(range, v.len())]
308+
.iter()
309+
.map(|&x| AttributeValue::Float2(x))
310+
.collect()),
311+
VertexAttributeValues::Float32x3(v) => Ok(v[clamp_range(range, v.len())]
312+
.iter()
313+
.map(|&x| AttributeValue::Float3(x))
314+
.collect()),
315+
VertexAttributeValues::Float32x4(v) => Ok(v[clamp_range(range, v.len())]
316+
.iter()
317+
.map(|&x| AttributeValue::Float4(x))
318+
.collect()),
292319
_ => Err(ProcessingError::InvalidArgument(
293320
"Unsupported attribute format".into(),
294321
)),
@@ -346,9 +373,7 @@ pub fn set_attribute(
346373
}
347374
}
348375

349-
pub fn to_attribute_values(
350-
values: &[AttributeValue],
351-
) -> Option<VertexAttributeValues> {
376+
pub fn to_attribute_values(values: &[AttributeValue]) -> Option<VertexAttributeValues> {
352377
macro_rules! convert {
353378
($variant:ident, $bevy:ident, $default:expr) => {
354379
Some(VertexAttributeValues::$bevy(

crates/processing_render/src/geometry/layout.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ impl VertexLayout {
5252

5353
for attr in attrs {
5454
let size = attr.format.size();
55-
layout_attrs.push(VertexAttribute { attribute: attr, offset });
55+
layout_attrs.push(VertexAttribute {
56+
attribute: attr,
57+
offset,
58+
});
5659
offset += size;
5760
}
5861

@@ -105,7 +108,10 @@ impl VertexAttributes {
105108

106109
for attr in attrs {
107110
let size = attr.format.size();
108-
layout_attrs.push(VertexAttribute { attribute: attr, offset });
111+
layout_attrs.push(VertexAttribute {
112+
attribute: attr,
113+
offset,
114+
});
109115
offset += size;
110116
}
111117

@@ -195,6 +201,9 @@ pub fn build(
195201
attributes: layout_attrs,
196202
};
197203

198-
commands.entity(entity).remove::<VertexLayoutBuilder>().insert(layout);
204+
commands
205+
.entity(entity)
206+
.remove::<VertexLayoutBuilder>()
207+
.insert(layout);
199208
true
200209
}

crates/processing_render/src/geometry/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
//! Geometry is a retained-mode representation of 3D mesh data that can be used for efficient
22
//! rendering. Typically, Processing's "sketch" API creates new mesh data every frame, which can be
33
//! inefficient for complex geometries. Geometry is backed by a Bevy [`Mesh`](Mesh) asset.
4-
mod attributes;
4+
pub(crate) mod attribute;
55
pub mod layout;
66

7-
pub use attributes::*;
8-
pub use layout::{hash_attr_name, VertexAttributes, VertexLayout, VertexAttribute, VertexLayoutBuilder};
7+
pub use attribute::*;
8+
pub use layout::{
9+
VertexAttribute, VertexAttributes, VertexLayout, VertexLayoutBuilder, hash_attr_name,
10+
};
911

1012
use std::collections::HashMap;
1113

1214
use bevy::{
1315
asset::RenderAssetUsages,
14-
mesh::{Indices, Meshable, MeshVertexAttributeId, VertexAttributeValues},
16+
mesh::{Indices, MeshVertexAttributeId, Meshable, VertexAttributeValues},
1517
prelude::*,
1618
render::render_resource::PrimitiveTopology,
1719
};
@@ -88,7 +90,10 @@ impl Geometry {
8890
}
8991

9092
fn create_empty_mesh(layout: &VertexLayout, topology: Topology) -> Mesh {
91-
let mut mesh = Mesh::new(topology.to_primitive_topology(), RenderAssetUsages::default());
93+
let mut mesh = Mesh::new(
94+
topology.to_primitive_topology(),
95+
RenderAssetUsages::default(),
96+
);
9297

9398
for attr in layout.attributes() {
9499
let empty_values = match attr.attribute.format {
@@ -148,7 +153,9 @@ pub fn create_box(
148153
let mesh = cuboid.mesh().build();
149154
let handle = meshes.add(mesh);
150155

151-
commands.spawn(Geometry::new(handle, VertexLayout::default())).id()
156+
commands
157+
.spawn(Geometry::new(handle, VertexLayout::default()))
158+
.id()
152159
}
153160

154161
pub fn normal(world: &mut World, entity: Entity, nx: f32, ny: f32, nz: f32) -> Result<()> {

crates/processing_render/src/lib.rs

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,12 @@ fn create_app() -> App {
230230
});
231231

232232
app.add_plugins(plugins);
233-
app.add_plugins((ImagePlugin, GraphicsPlugin, SurfacePlugin, geometry::GeometryPlugin));
233+
app.add_plugins((
234+
ImagePlugin,
235+
GraphicsPlugin,
236+
SurfacePlugin,
237+
geometry::GeometryPlugin,
238+
));
234239
app.add_systems(First, (clear_transient_meshes, activate_cameras))
235240
.add_systems(Update, flush_draw_commands.before(AssetEventSystems));
236241

@@ -722,7 +727,10 @@ pub fn geometry_layout_add_uv(entity: Entity) -> error::Result<()> {
722727
})
723728
}
724729

725-
pub fn geometry_layout_add_attribute(layout_entity: Entity, attr_entity: Entity) -> error::Result<()> {
730+
pub fn geometry_layout_add_attribute(
731+
layout_entity: Entity,
732+
attr_entity: Entity,
733+
) -> error::Result<()> {
726734
app_mut(|app| {
727735
geometry::layout::add_attribute(app.world_mut(), layout_entity, attr_entity);
728736
Ok(())
@@ -738,17 +746,25 @@ pub fn geometry_layout_destroy(entity: Entity) -> error::Result<()> {
738746
})
739747
}
740748

741-
pub fn geometry_attribute_create(name: impl Into<String>, format: AttributeFormat) -> error::Result<Entity> {
749+
pub fn geometry_attribute_create(
750+
name: impl Into<String>,
751+
format: AttributeFormat,
752+
) -> error::Result<Entity> {
742753
app_mut(|app| {
743-
Ok(app
744-
.world_mut()
745-
.run_system_cached_with(geometry::create_attribute, (name.into(), format))
746-
.unwrap())
754+
app.world_mut()
755+
.run_system_cached_with(geometry::attribute::create, (name.into(), format))
756+
.unwrap()
747757
})
748758
}
749759

750760
pub fn geometry_attribute_position() -> Entity {
751-
app_mut(|app| Ok(app.world().resource::<geometry::BuiltinAttributes>().position)).unwrap()
761+
app_mut(|app| {
762+
Ok(app
763+
.world()
764+
.resource::<geometry::BuiltinAttributes>()
765+
.position)
766+
})
767+
.unwrap()
752768
}
753769

754770
pub fn geometry_attribute_normal() -> Entity {
@@ -766,8 +782,8 @@ pub fn geometry_attribute_uv() -> Entity {
766782
pub fn geometry_attribute_destroy(entity: Entity) -> error::Result<()> {
767783
app_mut(|app| {
768784
app.world_mut()
769-
.run_system_cached_with(geometry::destroy_attribute, entity)
770-
.unwrap();
785+
.run_system_cached_with(geometry::attribute::destroy, entity)
786+
.unwrap()?;
771787
Ok(())
772788
})
773789
}
@@ -795,7 +811,10 @@ pub fn geometry_layout_build(entity: Entity) -> error::Result<()> {
795811
})
796812
}
797813

798-
pub fn geometry_create_with_layout(layout_entity: Entity, topology: geometry::Topology) -> error::Result<Entity> {
814+
pub fn geometry_create_with_layout(
815+
layout_entity: Entity,
816+
topology: geometry::Topology,
817+
) -> error::Result<Entity> {
799818
app_mut(|app| {
800819
let layout = app
801820
.world()
@@ -809,7 +828,10 @@ pub fn geometry_create_with_layout(layout_entity: Entity, topology: geometry::To
809828
})
810829
}
811830

812-
pub fn geometry_create_with_attributes(attrs: geometry::VertexAttributes, topology: geometry::Topology) -> error::Result<Entity> {
831+
pub fn geometry_create_with_attributes(
832+
attrs: geometry::VertexAttributes,
833+
topology: geometry::Topology,
834+
) -> error::Result<Entity> {
813835
app_mut(|app| {
814836
Ok(app
815837
.world_mut()
@@ -873,7 +895,11 @@ pub fn geometry_attribute_float4(
873895
z: f32,
874896
w: f32,
875897
) -> error::Result<()> {
876-
geometry_attribute(geo_entity, attr_entity, AttributeValue::Float4([x, y, z, w]))
898+
geometry_attribute(
899+
geo_entity,
900+
attr_entity,
901+
AttributeValue::Float4([x, y, z, w]),
902+
)
877903
}
878904

879905
pub fn geometry_vertex(entity: Entity, x: f32, y: f32, z: f32) -> error::Result<()> {
@@ -944,23 +970,15 @@ pub fn geometry_get_colors(
944970
})
945971
}
946972

947-
pub fn geometry_get_uvs(
948-
entity: Entity,
949-
start: usize,
950-
end: usize,
951-
) -> error::Result<Vec<[f32; 2]>> {
973+
pub fn geometry_get_uvs(entity: Entity, start: usize, end: usize) -> error::Result<Vec<[f32; 2]>> {
952974
app_mut(|app| {
953975
app.world_mut()
954976
.run_system_cached_with(geometry::get_uvs, (entity, start..end))
955977
.unwrap()
956978
})
957979
}
958980

959-
pub fn geometry_get_indices(
960-
entity: Entity,
961-
start: usize,
962-
end: usize,
963-
) -> error::Result<Vec<u32>> {
981+
pub fn geometry_get_indices(entity: Entity, start: usize, end: usize) -> error::Result<Vec<u32>> {
964982
app_mut(|app| {
965983
app.world_mut()
966984
.run_system_cached_with(geometry::get_indices, (entity, start..end))

examples/animated_mesh.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ fn sketch() -> error::Result<()> {
3939
for x in 0..grid_size {
4040
let px = x as f32 * spacing - offset;
4141
let pz = z as f32 * spacing - offset;
42-
geometry_color(mesh, x as f32 / grid_size as f32, 0.5, z as f32 / grid_size as f32, 1.0)?;
42+
geometry_color(
43+
mesh,
44+
x as f32 / grid_size as f32,
45+
0.5,
46+
z as f32 / grid_size as f32,
47+
1.0,
48+
)?;
4349
geometry_normal(mesh, 0.0, 1.0, 0.0)?;
4450
geometry_vertex(mesh, px, 0.0, pz)?;
4551
}

examples/custom_attribute.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ fn sketch() -> error::Result<()> {
6666
graphics_begin_draw(graphics)?;
6767
graphics_record_command(graphics, DrawCommand::Geometry(mesh))?;
6868
graphics_end_draw(graphics)?;
69-
7069
}
7170
Ok(())
7271
}

0 commit comments

Comments
 (0)