Skip to content

Commit 25afe12

Browse files
committed
Add rs example and polish sphere function
1 parent 5a24804 commit 25afe12

File tree

4 files changed

+150
-38
lines changed

4 files changed

+150
-38
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ path = "examples/animated_mesh.rs"
6464
name = "custom_attribute"
6565
path = "examples/custom_attribute.rs"
6666

67+
[[example]]
68+
name = "particles"
69+
path = "examples/particles.rs"
70+
6771
[profile.wasm-release]
6872
inherits = "release"
6973
opt-level = "z"

crates/processing_pyo3/examples/particles.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ def setup():
1212
def draw():
1313
global geometry
1414

15-
camera_position(150.0, 150.0, 150.0)
16-
camera_look_at( 0.0, 0.0, 0.0)
15+
camera_position(100.0, 100.0, 300.0)
16+
camera_look_at(0.0, 0.0, 0.0)
1717
background(220, 200, 140)
1818

1919
draw_geometry(geometry)
@@ -23,15 +23,9 @@ def create_geometry():
2323

2424
begin_geometry()
2525

26-
for i in range(60):
27-
x = gauss(400, 200)
28-
y = gauss(350, 175)
29-
z = gauss(0, 100)
30-
31-
push_matrix()
32-
translate_3d(x, y, z)
33-
sphere(10)
34-
pop_matrix()
26+
push_matrix()
27+
sphere(10)
28+
pop_matrix()
3529

3630
geometry = end_geometry()
3731

crates/processing_render/src/geometry/mod.rs

Lines changed: 87 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -385,10 +385,16 @@ pub fn begin(
385385
]))
386386
.id();
387387

388-
let mesh = Mesh::new(
388+
let mut mesh = Mesh::new(
389389
Topology::TriangleList.to_primitive_topology(),
390390
RenderAssetUsages::default(),
391391
);
392+
393+
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, Vec::<[f32; 3]>::new());
394+
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, Vec::<[f32; 3]>::new());
395+
mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, Vec::<[f32; 4]>::new());
396+
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, Vec::<[f32; 2]>::new());
397+
392398
let handle = meshes.add(mesh);
393399

394400
let mut state = state_query
@@ -403,6 +409,7 @@ pub fn end(
403409
In(entity): In<Entity>,
404410
mut commands: Commands,
405411
mut state_query: Query<&mut RenderState>,
412+
meshes: Res<Assets<Mesh>>,
406413
) -> Result<Entity> {
407414
let geometry = state_query
408415
.get_mut(entity)
@@ -411,53 +418,94 @@ pub fn end(
411418
.take()
412419
.ok_or(ProcessingError::GeometryNotFound)?;
413420

421+
if let Some(mesh) = meshes.get(&geometry.handle) {
422+
println!("End geometry: {} vertices, {} indices",
423+
mesh.count_vertices(),
424+
mesh.indices().map(|i| i.len()).unwrap_or(0)
425+
);
426+
}
427+
414428
Ok(commands.spawn(geometry).id())
415429
}
416430

417431
pub fn sphere(
418432
In((entity, radius)): In<(Entity, f32)>,
419433
state_query: Query<&mut RenderState>,
434+
layouts: Query<&VertexLayout>,
435+
builtins: Res<BuiltinAttributes>,
420436
mut meshes: ResMut<Assets<Mesh>>,
421437
) -> Result<()> {
422-
let geometry = state_query
438+
let state = state_query
423439
.get(entity)
424-
.map_err(|_| ProcessingError::GraphicsNotFound)?
440+
.map_err(|_| ProcessingError::GraphicsNotFound)?;
441+
442+
let geometry = state
425443
.running_geometry
426444
.as_ref()
427445
.ok_or(ProcessingError::GeometryNotFound)?;
428446

447+
let layout = layouts
448+
.get(geometry.layout)
449+
.map_err(|_| ProcessingError::LayoutNotFound)?;
450+
429451
let mesh = meshes
430452
.get_mut(&geometry.handle)
431453
.ok_or(ProcessingError::GeometryNotFound)?;
432454

433455
let base_index = mesh.count_vertices() as u32;
434456
let sphere_mesh = Sphere::new(radius).mesh().build();
457+
let vertex_count = sphere_mesh.count_vertices();
458+
459+
// Get current transformation matrix from render state
460+
let transform = state.transform.current();
435461

436-
// Append positions
462+
// Append positions with transformation applied
437463
if let Some(VertexAttributeValues::Float32x3(new_pos)) =
438464
sphere_mesh.attribute(Mesh::ATTRIBUTE_POSITION)
439465
&& let Some(VertexAttributeValues::Float32x3(positions)) =
440466
mesh.attribute_mut(Mesh::ATTRIBUTE_POSITION)
441467
{
442-
positions.extend_from_slice(new_pos);
468+
for pos in new_pos {
469+
let transformed = transform.transform_point3(Vec3::from_array(*pos));
470+
positions.push(transformed.to_array());
471+
}
443472
}
444473

445-
// Append normals
446-
if let Some(VertexAttributeValues::Float32x3(new_normals)) =
447-
sphere_mesh.attribute(Mesh::ATTRIBUTE_NORMAL)
448-
&& let Some(VertexAttributeValues::Float32x3(normals)) =
449-
mesh.attribute_mut(Mesh::ATTRIBUTE_NORMAL)
450-
{
451-
normals.extend_from_slice(new_normals);
474+
// Append normals with rotation applied (no translation/scale)
475+
if layout.has_attribute(builtins.normal) {
476+
if let Some(VertexAttributeValues::Float32x3(new_normals)) =
477+
sphere_mesh.attribute(Mesh::ATTRIBUTE_NORMAL)
478+
&& let Some(VertexAttributeValues::Float32x3(normals)) =
479+
mesh.attribute_mut(Mesh::ATTRIBUTE_NORMAL)
480+
{
481+
let normal_matrix = transform.matrix3;
482+
for normal in new_normals {
483+
let transformed = normal_matrix.mul_vec3(Vec3::from_array(*normal)).normalize();
484+
normals.push(transformed.to_array());
485+
}
486+
}
452487
}
453488

454-
// Append UVs
455-
if let Some(VertexAttributeValues::Float32x2(new_uvs)) =
456-
sphere_mesh.attribute(Mesh::ATTRIBUTE_UV_0)
457-
&& let Some(VertexAttributeValues::Float32x2(uvs)) =
458-
mesh.attribute_mut(Mesh::ATTRIBUTE_UV_0)
459-
{
460-
uvs.extend_from_slice(new_uvs);
489+
// Append colors if in layout - fill with current color
490+
if layout.has_attribute(builtins.color) {
491+
if let Some(VertexAttributeValues::Float32x4(colors)) =
492+
mesh.attribute_mut(Mesh::ATTRIBUTE_COLOR)
493+
{
494+
for _ in 0..vertex_count {
495+
colors.push(geometry.current_color);
496+
}
497+
}
498+
}
499+
500+
// Append UVs if in layout
501+
if layout.has_attribute(builtins.uv) {
502+
if let Some(VertexAttributeValues::Float32x2(new_uvs)) =
503+
sphere_mesh.attribute(Mesh::ATTRIBUTE_UV_0)
504+
&& let Some(VertexAttributeValues::Float32x2(uvs)) =
505+
mesh.attribute_mut(Mesh::ATTRIBUTE_UV_0)
506+
{
507+
uvs.extend_from_slice(new_uvs);
508+
}
461509
}
462510

463511
// Append indices with offset
@@ -467,15 +515,27 @@ pub fn sphere(
467515
None => Vec::new(),
468516
};
469517

470-
match mesh.indices_mut() {
471-
Some(Indices::U32(indices)) => indices.extend(new_indices),
472-
Some(Indices::U16(indices)) => {
473-
indices.extend(new_indices.iter().map(|&i| i as u16));
474-
}
475-
None => {
476-
mesh.insert_indices(Indices::U32(new_indices));
518+
if !new_indices.is_empty() {
519+
match mesh.indices_mut() {
520+
Some(Indices::U32(indices)) => {
521+
indices.extend(new_indices);
522+
}
523+
None => {
524+
mesh.insert_indices(Indices::U32(new_indices));
525+
}
526+
Some(Indices::U16(indices)) => {
527+
if base_index + vertex_count as u32 > u16::MAX as u32 {
528+
// Need to upgrade to U32
529+
let existing: Vec<u32> = indices.iter().map(|&i| i as u32).collect();
530+
mesh.insert_indices(Indices::U32(
531+
existing.into_iter().chain(new_indices).collect()
532+
));
533+
} else {
534+
indices.extend(new_indices.iter().map(|&i| i as u16));
535+
}
536+
}
477537
}
478538
}
479539

480540
Ok(())
481-
}
541+
}

examples/particles.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
mod glfw;
2+
3+
use glfw::GlfwContext;
4+
use processing::prelude::*;
5+
use processing_render::{render::command::DrawCommand};
6+
7+
fn main() {
8+
match sketch() {
9+
Ok(_) => {
10+
eprintln!("Sketch completed successfully");
11+
exit(0).unwrap();
12+
}
13+
Err(e) => {
14+
eprintln!("Sketch error: {:?}", e);
15+
exit(1).unwrap();
16+
}
17+
};
18+
}
19+
20+
fn sketch() -> error::Result<()> {
21+
let mut glfw_ctx = GlfwContext::new(400, 400)?;
22+
init(Config::default())?;
23+
24+
let width = 400;
25+
let height = 400;
26+
let scale_factor = 1.0;
27+
28+
let surface = glfw_ctx.create_surface(width, height, scale_factor)?;
29+
let graphics = graphics_create(surface, width, height)?;
30+
31+
graphics_mode_3d(graphics)?;
32+
graphics_camera_position(graphics, 100.0, 100.0, 300.0)?;
33+
graphics_camera_look_at(graphics, 0.0, 0.0, 0.0)?;
34+
35+
while glfw_ctx.poll_events() {
36+
graphics_begin_draw(graphics)?;
37+
38+
graphics_record_command(
39+
graphics,
40+
DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.1, 0.1, 0.15)),
41+
)?;
42+
43+
geometry_begin(graphics)?;
44+
45+
graphics_record_command(graphics, DrawCommand::PushMatrix)?;
46+
geometry_sphere(graphics, 10.)?;
47+
graphics_record_command(graphics, DrawCommand::PopMatrix)?;
48+
49+
let geometry = geometry_end(graphics)?;
50+
graphics_record_command(graphics, DrawCommand::Geometry(geometry))?;
51+
graphics_end_draw(graphics)?;
52+
}
53+
Ok(())
54+
}

0 commit comments

Comments
 (0)