diff --git a/crates/processing_ffi/src/lib.rs b/crates/processing_ffi/src/lib.rs index 39b31413..afe8823e 100644 --- a/crates/processing_ffi/src/lib.rs +++ b/crates/processing_ffi/src/lib.rs @@ -2405,7 +2405,7 @@ pub extern "C" fn processing_geometry_normal(geo_id: u64, nx: f32, ny: f32, nz: pub extern "C" fn processing_geometry_color(geo_id: u64, r: f32, g: f32, b: f32, a: f32) { error::clear_error(); let entity = Entity::from_bits(geo_id); - error::check(|| geometry_color(entity, Vec4::new(r, g, b, a))); + error::check(|| geometry_color(entity, bevy::color::Color::srgba(r, g, b, a))); } #[unsafe(no_mangle)] diff --git a/crates/processing_node/src/lib.rs b/crates/processing_node/src/lib.rs index 7fa56a9b..2f2e9593 100644 --- a/crates/processing_node/src/lib.rs +++ b/crates/processing_node/src/lib.rs @@ -1715,7 +1715,7 @@ pub fn js_geometry_normal(geo_id: BigInt, nx: f64, ny: f64, nz: f64) -> Result<( pub fn js_geometry_color(geo_id: BigInt, r: f64, g: f64, b: f64, a: f64) -> Result<()> { check(geometry_color( ent(geo_id), - Vec4::new(r as f32, g as f32, b as f32, a as f32), + bevy::color::Color::srgba(r as f32, g as f32, b as f32, a as f32), )) } diff --git a/crates/processing_pyo3/examples/Demos/Graphics/depth_sort.py b/crates/processing_pyo3/examples/Demos/Graphics/depth_sort.py index 17fd1a65..9a66de52 100644 --- a/crates/processing_pyo3/examples/Demos/Graphics/depth_sort.py +++ b/crates/processing_pyo3/examples/Demos/Graphics/depth_sort.py @@ -17,11 +17,11 @@ def setup(): for i in range(10): c = hsva(remap(i, 0, 10, 0, 360), 1.0, 1.0, 0.3) tri = create_geometry(topology=TRIANGLES) - tri.color(c.r, c.g, c.b, c.a) + tri.color(c) tri.vertex(200, 50, -50) - tri.color(c.r, c.g, c.b, c.a) + tri.color(c) tri.vertex(100, 100, 50) - tri.color(c.r, c.g, c.b, c.a) + tri.color(c) tri.vertex(100, 0, 20) tri.index(0) tri.index(1) diff --git a/crates/processing_pyo3/examples/Demos/Graphics/ribbons.py b/crates/processing_pyo3/examples/Demos/Graphics/ribbons.py index 0ed0eb19..5025c077 100644 --- a/crates/processing_pyo3/examples/Demos/Graphics/ribbons.py +++ b/crates/processing_pyo3/examples/Demos/Graphics/ribbons.py @@ -47,7 +47,11 @@ def draw(): arcball.run() translate(model_shift.x, model_shift.y, model_shift.z) - no_stroke() + if render_mode == 0: + # Lines take the stroke color, multiplied by the vertex colors + stroke(1.0) + else: + no_stroke() for i in range(len(models)): draw_geometry(models[i]) @@ -462,7 +466,7 @@ def create_ribbon_model(residues, trj): for line in (vertices0, vertices1, vertices2): model = create_geometry(topology=LINE_STRIP) for pos_vec in line: - model.color(ribbon_color.r, ribbon_color.g, ribbon_color.b, ribbon_color.a) + model.color(ribbon_color) model.vertex(pos_vec.x, pos_vec.y, pos_vec.z) trj.append(model) else: @@ -473,7 +477,7 @@ def create_ribbon_model(residues, trj): for i in range(len(vertices)): pos_vec = vertices[i] norm_vec = normals[i] - model.color(ribbon_color.r, ribbon_color.g, ribbon_color.b, ribbon_color.a) + model.color(ribbon_color) model.normal(-norm_vec.x, -norm_vec.y, -norm_vec.z) model.vertex(pos_vec.x, pos_vec.y, pos_vec.z) trj.append(model) diff --git a/crates/processing_pyo3/examples/Demos/Performance/cubic_grid_retained.py b/crates/processing_pyo3/examples/Demos/Performance/cubic_grid_retained.py index 5d255f25..75cb06bd 100644 --- a/crates/processing_pyo3/examples/Demos/Performance/cubic_grid_retained.py +++ b/crates/processing_pyo3/examples/Demos/Performance/cubic_grid_retained.py @@ -70,7 +70,7 @@ def add_box(g, x, y, z, s, c): h = s / 2 base = g.vertex_count() for (dx, dy, dz) in [(-1, -1, -1), (1, -1, -1), (1, 1, -1), (-1, 1, -1), (-1, -1, 1), (1, -1, 1), (1, 1, 1), (-1, 1, 1)]: - g.color(c.r, c.g, c.b, c.a) + g.color(c) g.vertex(x + dx * h, y + dy * h, z + dz * h) for (a, b, d) in [(0, 2, 1), (0, 3, 2), (4, 5, 6), (4, 6, 7), (0, 1, 5), (0, 5, 4), (2, 3, 7), (2, 7, 6), (1, 2, 6), (1, 6, 5), (0, 4, 7), (0, 7, 3)]: g.index(base + a) diff --git a/crates/processing_pyo3/examples/Demos/Performance/esfera.py b/crates/processing_pyo3/examples/Demos/Performance/esfera.py index 3e839c13..79a4ff16 100644 --- a/crates/processing_pyo3/examples/Demos/Performance/esfera.py +++ b/crates/processing_pyo3/examples/Demos/Performance/esfera.py @@ -51,6 +51,8 @@ def draw(): for i in range(len(lista)): lista[i].dibujar() + # Lines take the stroke color, multiplied by the vertex colors + stroke(1.0) stroke_weight(1) draw_geometry(pelos) diff --git a/crates/processing_pyo3/examples/Topics/Create Shapes/polygon_pshape.py b/crates/processing_pyo3/examples/Topics/Create Shapes/polygon_pshape.py index 94aef707..e4e2b2b8 100644 --- a/crates/processing_pyo3/examples/Topics/Create Shapes/polygon_pshape.py +++ b/crates/processing_pyo3/examples/Topics/Create Shapes/polygon_pshape.py @@ -15,8 +15,7 @@ def setup(): star = create_geometry(topology=TRIANGLES) # You can set fill and stroke star.color(0.4, 0.4, 0.4, 1.0) - stroke(1.0) # GAP: Geometry has no stroke; the outline is not drawn - stroke_weight(2) # GAP: Geometry has no stroke_weight + no_stroke() # GAP: stroke() on a Geometry draws every triangle edge; the polygon outline needs a Shape # Here, we are hardcoding a series of vertices; # the first one is the center, so the star can be built as a triangle fan star.vertex(0, 0, 0) diff --git a/crates/processing_pyo3/examples/Topics/Create Shapes/wiggle_pshape.py b/crates/processing_pyo3/examples/Topics/Create Shapes/wiggle_pshape.py index da439039..5c3fc466 100644 --- a/crates/processing_pyo3/examples/Topics/Create Shapes/wiggle_pshape.py +++ b/crates/processing_pyo3/examples/Topics/Create Shapes/wiggle_pshape.py @@ -43,8 +43,7 @@ def __init__(self): # Now make the geometry with those vertices self.s = create_geometry(topology=TRIANGLES) self.s.color(0.5, 0.5, 0.5, 1.0) - stroke(0.0) # GAP: Geometry has no stroke; the outline is not drawn - stroke_weight(2) # GAP: Geometry has no stroke_weight + no_stroke() # GAP: stroke() on a Geometry draws every triangle edge; the polygon outline needs a Shape for v in self.original: self.s.vertex(v.x, v.y, 0) # The circle is convex, so a triangle fan from the first vertex fills it diff --git a/crates/processing_pyo3/examples/Topics/Geometry/icosahedra.py b/crates/processing_pyo3/examples/Topics/Geometry/icosahedra.py index 1410d75f..aa3b448f 100644 --- a/crates/processing_pyo3/examples/Topics/Geometry/icosahedra.py +++ b/crates/processing_pyo3/examples/Topics/Geometry/icosahedra.py @@ -30,7 +30,7 @@ def draw(): rotate_y(frame_count * PI / -200) stroke(0.67, 0.0, 0.0) no_fill() - ico1.create() # GAP: stroke()/no_fill() wireframe outline on draw_geometry + ico1.create() pop_matrix() push_matrix() @@ -38,7 +38,7 @@ def draw(): rotate_y(frame_count * PI / 300) stroke(0.59, 0.0, 0.71) fill(0.67, 0.67, 0.0) - ico2.create() # GAP: stroke() outline on draw_geometry + ico2.create() pop_matrix() push_matrix() diff --git a/crates/processing_pyo3/examples/Topics/Geometry/noise_sphere.py b/crates/processing_pyo3/examples/Topics/Geometry/noise_sphere.py index fcbee87b..47f119c6 100644 --- a/crates/processing_pyo3/examples/Topics/Geometry/noise_sphere.py +++ b/crates/processing_pyo3/examples/Topics/Geometry/noise_sphere.py @@ -40,8 +40,8 @@ def draw(): for i in range(cuantos): lista[i].dibujar() - # Lines take the fill color, multiplied by the vertex colors - fill(1.0) # GAP: LINES geometry ignores stroke(); it is colored by fill() times vertex colors + # Lines take the stroke color, multiplied by the vertex colors + stroke(1.0) draw_geometry(pelos) diff --git a/crates/processing_pyo3/examples/Topics/Geometry/rgb_cube.py b/crates/processing_pyo3/examples/Topics/Geometry/rgb_cube.py index d11724c1..e6e13784 100644 --- a/crates/processing_pyo3/examples/Topics/Geometry/rgb_cube.py +++ b/crates/processing_pyo3/examples/Topics/Geometry/rgb_cube.py @@ -50,35 +50,35 @@ def draw(): def build_cube(): g = create_geometry(topology=TRIANGLES) - g.color(0, 1, 1, 1); g.vertex(-1, 1, 1) - g.color(1, 1, 1, 1); g.vertex(1, 1, 1) - g.color(1, 0, 1, 1); g.vertex(1, -1, 1) - g.color(0, 0, 1, 1); g.vertex(-1, -1, 1) - - g.color(1, 1, 1, 1); g.vertex(1, 1, 1) - g.color(1, 1, 0, 1); g.vertex(1, 1, -1) - g.color(1, 0, 0, 1); g.vertex(1, -1, -1) - g.color(1, 0, 1, 1); g.vertex(1, -1, 1) - - g.color(1, 1, 0, 1); g.vertex(1, 1, -1) - g.color(0, 1, 0, 1); g.vertex(-1, 1, -1) - g.color(0, 0, 0, 1); g.vertex(-1, -1, -1) - g.color(1, 0, 0, 1); g.vertex(1, -1, -1) - - g.color(0, 1, 0, 1); g.vertex(-1, 1, -1) - g.color(0, 1, 1, 1); g.vertex(-1, 1, 1) - g.color(0, 0, 1, 1); g.vertex(-1, -1, 1) - g.color(0, 0, 0, 1); g.vertex(-1, -1, -1) - - g.color(0, 1, 0, 1); g.vertex(-1, 1, -1) - g.color(1, 1, 0, 1); g.vertex(1, 1, -1) - g.color(1, 1, 1, 1); g.vertex(1, 1, 1) - g.color(0, 1, 1, 1); g.vertex(-1, 1, 1) - - g.color(0, 0, 0, 1); g.vertex(-1, -1, -1) - g.color(1, 0, 0, 1); g.vertex(1, -1, -1) - g.color(1, 0, 1, 1); g.vertex(1, -1, 1) - g.color(0, 0, 1, 1); g.vertex(-1, -1, 1) + g.color(0.0, 1.0, 1.0, 1.0); g.vertex(-1, 1, 1) + g.color(1.0, 1.0, 1.0, 1.0); g.vertex(1, 1, 1) + g.color(1.0, 0.0, 1.0, 1.0); g.vertex(1, -1, 1) + g.color(0.0, 0.0, 1.0, 1.0); g.vertex(-1, -1, 1) + + g.color(1.0, 1.0, 1.0, 1.0); g.vertex(1, 1, 1) + g.color(1.0, 1.0, 0.0, 1.0); g.vertex(1, 1, -1) + g.color(1.0, 0.0, 0.0, 1.0); g.vertex(1, -1, -1) + g.color(1.0, 0.0, 1.0, 1.0); g.vertex(1, -1, 1) + + g.color(1.0, 1.0, 0.0, 1.0); g.vertex(1, 1, -1) + g.color(0.0, 1.0, 0.0, 1.0); g.vertex(-1, 1, -1) + g.color(0.0, 0.0, 0.0, 1.0); g.vertex(-1, -1, -1) + g.color(1.0, 0.0, 0.0, 1.0); g.vertex(1, -1, -1) + + g.color(0.0, 1.0, 0.0, 1.0); g.vertex(-1, 1, -1) + g.color(0.0, 1.0, 1.0, 1.0); g.vertex(-1, 1, 1) + g.color(0.0, 0.0, 1.0, 1.0); g.vertex(-1, -1, 1) + g.color(0.0, 0.0, 0.0, 1.0); g.vertex(-1, -1, -1) + + g.color(0.0, 1.0, 0.0, 1.0); g.vertex(-1, 1, -1) + g.color(1.0, 1.0, 0.0, 1.0); g.vertex(1, 1, -1) + g.color(1.0, 1.0, 1.0, 1.0); g.vertex(1, 1, 1) + g.color(0.0, 1.0, 1.0, 1.0); g.vertex(-1, 1, 1) + + g.color(0.0, 0.0, 0.0, 1.0); g.vertex(-1, -1, -1) + g.color(1.0, 0.0, 0.0, 1.0); g.vertex(1, -1, -1) + g.color(1.0, 0.0, 1.0, 1.0); g.vertex(1, -1, 1) + g.color(0.0, 0.0, 1.0, 1.0); g.vertex(-1, -1, 1) # Each group of four vertices is a quad: two triangles for face in range(6): diff --git a/crates/processing_pyo3/examples/Topics/Geometry/toroid.py b/crates/processing_pyo3/examples/Topics/Geometry/toroid.py index 710d2ef1..d87002b5 100644 --- a/crates/processing_pyo3/examples/Topics/Geometry/toroid.py +++ b/crates/processing_pyo3/examples/Topics/Geometry/toroid.py @@ -55,7 +55,7 @@ def draw(): # wireframe or solid if is_wire_frame: stroke(1.0, 1.0, 0.59) - no_fill() # GAP: stroke()/no_fill() wireframe outline on draw_geometry + no_fill() else: no_stroke() fill(0.59, 0.76, 0.49) diff --git a/crates/processing_pyo3/examples/animated_mesh.py b/crates/processing_pyo3/examples/animated_mesh.py index 44030f72..1a7a5bc6 100644 --- a/crates/processing_pyo3/examples/animated_mesh.py +++ b/crates/processing_pyo3/examples/animated_mesh.py @@ -46,6 +46,7 @@ def draw(): camera_position(150.0, -150.0, 150.0) camera_look_at( 0.0, 0.0, 0.0) background(220, 200, 140) + no_stroke() for z in range(grid_size): for x in range(grid_size): diff --git a/crates/processing_pyo3/examples/geometry_methods.py b/crates/processing_pyo3/examples/geometry_methods.py index 53081d50..9179eb66 100644 --- a/crates/processing_pyo3/examples/geometry_methods.py +++ b/crates/processing_pyo3/examples/geometry_methods.py @@ -30,6 +30,7 @@ def setup(): def draw(): background(220) + no_stroke() camera_position(0.0, 0.0, 200.0) camera_look_at(0.0, 0.0, 0.0) draw_geometry(geometry) diff --git a/crates/processing_pyo3/src/graphics.rs b/crates/processing_pyo3/src/graphics.rs index cc8277de..01c95b17 100644 --- a/crates/processing_pyo3/src/graphics.rs +++ b/crates/processing_pyo3/src/graphics.rs @@ -1,7 +1,7 @@ use crate::color::{ColorMode, ColorSpace, extract_color_with_mode, parse_numeric}; use crate::glfw::GlfwContext; use crate::input; -use crate::math::{Vec3Like, extract_vec2, extract_vec3, extract_vec4}; +use crate::math::{Vec3Like, extract_vec2, extract_vec3}; use bevy::{ color::{ColorToPacked, LinearRgba, Srgba}, math::{Affine3A, Mat4, Vec3, Vec4}, @@ -830,8 +830,8 @@ impl Geometry { #[pyo3(signature = (*args))] pub fn color(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> { - let v = extract_vec4(args)?; - geometry_color(self.entity, v).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) + let color = extract_color_with_mode(args, &ColorMode::default())?; + geometry_color(self.entity, color).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) } #[pyo3(signature = (*args))] diff --git a/crates/processing_pyo3/src/math.rs b/crates/processing_pyo3/src/math.rs index 8f86c519..3e788bf5 100644 --- a/crates/processing_pyo3/src/math.rs +++ b/crates/processing_pyo3/src/math.rs @@ -60,21 +60,6 @@ impl Vec3Arg { } } -#[derive(FromPyObject)] -pub(crate) enum Vec4Arg { - Instance((PyVec4,)), - Components(f32, f32, f32, f32), -} - -impl Vec4Arg { - pub fn into_vec4(self) -> Vec4 { - match self { - Vec4Arg::Instance((v,)) => v.0, - Vec4Arg::Components(x, y, z, w) => Vec4::new(x, y, z, w), - } - } -} - // Vec3Like for single-object extraction (e.g., Quat::from_axis_angle(axis, angle)) #[derive(FromPyObject)] pub(crate) enum Vec3Like { @@ -101,10 +86,6 @@ pub(crate) fn extract_vec3(args: &Bound<'_, PyTuple>) -> PyResult { Ok(args.extract::()?.into_vec3()) } -pub(crate) fn extract_vec4(args: &Bound<'_, PyTuple>) -> PyResult { - Ok(args.extract::()?.into_vec4()) -} - // Implements a PyVecN class with the given name, fields, and underlying glam type, // including arithmetic operations, indexing, iteration, and common vector methods, etc. // The `extra` block can be used to add additional methods specific to certain vector types diff --git a/crates/processing_render/src/geometry/mod.rs b/crates/processing_render/src/geometry/mod.rs index b10f6a6f..1ad6e5db 100644 --- a/crates/processing_render/src/geometry/mod.rs +++ b/crates/processing_render/src/geometry/mod.rs @@ -75,6 +75,17 @@ impl Topology { } } +/// Which edges of a triangle mesh the stroke draws. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Edges { + #[default] + Triangles, + /// built-in primitives, whose triangle pairs are faces + Quads, + /// an imported model, which is never stroked + None, +} + #[derive(Component)] pub struct Geometry { pub handle: Handle, @@ -85,6 +96,7 @@ pub struct Geometry { pub custom_current: HashMap, /// set once a color with alpha below one is used; never cleared pub translucent: bool, + pub edges: Edges, } impl Geometry { @@ -97,8 +109,14 @@ impl Geometry { current_uv: [0.0, 0.0], custom_current: HashMap::new(), translucent: false, + edges: Edges::default(), } } + + pub fn with_edges(mut self, edges: Edges) -> Self { + self.edges = edges; + self + } } pub fn create( @@ -187,7 +205,9 @@ pub fn create_box( ])) .id(); - commands.spawn(Geometry::new(handle, layout_entity)).id() + commands + .spawn(Geometry::new(handle, layout_entity).with_edges(Edges::Quads)) + .id() } pub fn create_sphere( @@ -207,7 +227,9 @@ pub fn create_sphere( ])) .id(); - commands.spawn(Geometry::new(handle, layout_entity)).id() + commands + .spawn(Geometry::new(handle, layout_entity).with_edges(Edges::Quads)) + .id() } pub fn create_grid( @@ -257,12 +279,12 @@ pub fn normal(world: &mut World, entity: Entity, normal: Vec3) -> Result<()> { Ok(()) } -pub fn color(world: &mut World, entity: Entity, color: Vec4) -> Result<()> { +pub fn color(world: &mut World, entity: Entity, color: Color) -> Result<()> { let mut geometry = world .get_mut::(entity) .ok_or(ProcessingError::GeometryNotFound)?; - geometry.current_color = color.to_array(); - if color.w < 1.0 { + geometry.current_color = color.to_linear().to_f32_array(); + if color.alpha() < 1.0 { geometry.translucent = true; } Ok(()) diff --git a/crates/processing_render/src/gltf.rs b/crates/processing_render/src/gltf.rs index dca5ef38..add89361 100644 --- a/crates/processing_render/src/gltf.rs +++ b/crates/processing_render/src/gltf.rs @@ -14,7 +14,7 @@ use bevy::{ world_serialization::{InstanceId, WorldInstanceSpawner}, }; -use crate::geometry::{BuiltinAttributes, Geometry, layout::VertexLayout}; +use crate::geometry::{BuiltinAttributes, Edges, Geometry, layout::VertexLayout}; use crate::graphics; use crate::light::{LightOf, ProcessingLight}; use crate::material::ProcessingMaterial; @@ -208,7 +208,7 @@ pub fn geometry( let layout_entity = world.spawn(VertexLayout::with_attributes(attrs)).id(); let entity = world .spawn(( - Geometry::new(mesh_handle, layout_entity), + Geometry::new(mesh_handle, layout_entity).with_edges(Edges::None), GltfNodeTransform(global_transform), )) .id(); diff --git a/crates/processing_render/src/lib.rs b/crates/processing_render/src/lib.rs index 9efb920b..6f4c5a50 100644 --- a/crates/processing_render/src/lib.rs +++ b/crates/processing_render/src/lib.rs @@ -1593,7 +1593,7 @@ pub fn geometry_normal(entity: Entity, normal: Vec3) -> error::Result<()> { app_mut(|app| geometry::normal(app.world_mut(), entity, normal)) } -pub fn geometry_color(entity: Entity, color: Vec4) -> error::Result<()> { +pub fn geometry_color(entity: Entity, color: Color) -> error::Result<()> { app_mut(|app| geometry::color(app.world_mut(), entity, color)) } diff --git a/crates/processing_render/src/render/mod.rs b/crates/processing_render/src/render/mod.rs index cb742170..288b236d 100644 --- a/crates/processing_render/src/render/mod.rs +++ b/crates/processing_render/src/render/mod.rs @@ -15,7 +15,9 @@ use bevy::{ }, ecs::{query::Has, system::SystemParam}, math::{Affine2, Affine3A, Vec3A}, + mesh::PrimitiveTopology, pbr::gpu_instance_batch::GpuBatchedMesh3d, + pbr::wireframe::{Wireframe, WireframeColor, WireframeLineWidth, WireframeTopology}, prelude::*, render::render_resource::BlendState, window::WindowRef, @@ -33,7 +35,7 @@ use transform::TransformStack; use crate::{ Flush, - geometry::Geometry, + geometry::{Edges, Geometry}, gltf::GltfNodeTransform, image::Image, light::ProcessingLight, @@ -1006,52 +1008,39 @@ pub fn flush_draw_commands( continue; }; - let mut material_key = material_key_with_fill(&state); - if geometry.translucent { - material_key = material_key.transparent(); - } - let material_handle = match &material_key { - MaterialKey::Custom { - entity: mat_entity, - blend_state, - } => { - let Some(untyped) = p_material_handles.get(*mat_entity).ok() else { - warn!("Could not find material for entity {:?}", mat_entity); - continue; - }; - clone_custom_material_with_blend( - &mut res.custom_materials, - &untyped.0, - *blend_state, - ) + let topology = res + .meshes + .get(&geometry.handle) + .map(|mesh| mesh.primitive_topology()) + .unwrap_or_default(); + let triangles = topology.is_triangles(); + let paint = if triangles { + MeshPaint::Surface { + edges: match geometry.edges { + Edges::Triangles => Some(WireframeTopology::Triangles), + Edges::Quads => Some(WireframeTopology::Quads), + Edges::None => None, + }, } - _ => material_key - .to_material(&mut res.materials, sort_bias(batch.draw_index)), + } else { + MeshPaint::Lines }; - flush_batch(&mut res, &mut batch, &p_material_handles); - - let z_offset = layer_z(batch.draw_index); - let mut transform = state.transform.to_bevy_transform(); - - // if the "source" geometry was parented in a gltf scene, we need to make sure that - // we apply the parent transform here to ensure the correct final transform - // TODO: think about how hierarchies should work, especially for retained - if let Some(nt) = node_transform { - transform = - Transform::from_matrix(transform.to_matrix() * nt.0.to_matrix()); - } - transform.translation.z += z_offset; - - res.commands.spawn(( - Mesh3d(geometry.handle.clone()), - UntypedMaterial(material_handle), - FlushTransient(batch.graphics_entity), - transform, - batch.render_layers.clone(), - )); - - batch.draw_index += 1; + draw_mesh3d( + &mut res, + &mut batch, + &state, + Mesh3dDraw { + mesh: geometry.handle.clone(), + translucent: geometry.translucent, + // a geometry parented in a gltf scene keeps its node transform + // TODO: think about how hierarchies should work, especially for retained + local: node_transform.map(|nt| nt.0), + paint, + hairline: topology == PrimitiveTopology::TriangleStrip, + }, + &p_material_handles, + ); } DrawCommand::Particles { particles, @@ -1684,77 +1673,146 @@ fn add_shape3d( mesh: Mesh, material_handles: &Query<&UntypedMaterial>, ) { - use bevy::pbr::wireframe::{Wireframe, WireframeColor, WireframeLineWidth, WireframeTopology}; + let mesh = res.meshes.add(mesh); + draw_mesh3d( + res, + batch, + state, + Mesh3dDraw { + mesh, + translucent: false, + local: None, + paint: MeshPaint::Surface { + edges: Some(WireframeTopology::Quads), + }, + hairline: false, + }, + material_handles, + ); +} + +struct Mesh3dDraw { + mesh: Handle, + /// blend even with an opaque fill (translucent vertex colors) + translucent: bool, + /// applied under the current matrix + local: Option, + paint: MeshPaint, + /// the wide wireframe pass only draws triangle lists, so a strip is stroked one pixel wide + hairline: bool, +} +/// How the current style colors a mesh. +enum MeshPaint { + /// fill colors the surface, stroke draws these edges + Surface { edges: Option }, + /// lines and points take the stroke color + Lines, +} + +/// Draws a mesh colored by the current fill and stroke. +fn draw_mesh3d( + res: &mut RenderResources, + batch: &mut BatchState, + state: &RenderState, + draw: Mesh3dDraw, + material_handles: &Query<&UntypedMaterial>, +) { flush_batch(res, batch, material_handles); - let mesh_handle = res.meshes.add(mesh); - let fill_color = state.style.fill.color().unwrap_or(Color::WHITE); - let material_handle = match &state.style.material_key { - MaterialKey::Custom { entity, .. } => { - let Some(untyped) = material_handles.get(*entity).ok() else { - warn!("Custom material entity {:?} not found", entity); - return; + let (color, stroke) = match draw.paint { + MeshPaint::Surface { edges } => { + let color = match state.style.fill { + Fill::None => None, + Fill::Color(color) => Some(color), + Fill::Buffer(_) => Some(Color::WHITE), }; - clone_custom_material_with_blend( - &mut res.custom_materials, - &untyped.0, - state.style.blend_state, - ) - } - // TODO: in 2d, we use vertex colors. `to_material` becomes complicated if we also encode - // a base color in the material, so for simplicity we just create a new material here - // that is unlit and uses the fill color as the base color - MaterialKey::Color { .. } => { - let base = StandardMaterial { - base_color: fill_color, - unlit: true, - cull_mode: None, - alpha_mode: if state.style.blend_state.is_some() || state.fill_is_transparent() { - AlphaMode::Blend - } else { - AlphaMode::Opaque - }, - depth_bias: sort_bias(batch.draw_index), - ..default() - }; - let extended = ProcessingExtendedMaterial { - base, - extension: ProcessingMaterial { - blend_state: state.style.blend_state, - depth_write: None, - }, - }; - res.materials.add(extended).untyped() - } - _ => { - let key = material_key_with_fill(state); - key.to_material(&mut res.materials, sort_bias(batch.draw_index)) + (color, edges.zip(state.style.stroke_color)) } + MeshPaint::Lines => (state.style.stroke_color, None), + }; + if color.is_none() && stroke.is_none() { + return; + } + + let material_handle = match color { + None => None, + Some(color) => Some(match &state.style.material_key { + MaterialKey::Custom { entity, .. } => { + let Some(untyped) = material_handles.get(*entity).ok() else { + warn!("Custom material entity {:?} not found", entity); + return; + }; + clone_custom_material_with_blend( + &mut res.custom_materials, + &untyped.0, + state.style.blend_state, + ) + } + // TODO: in 2d, we use vertex colors. `to_material` becomes complicated if we also encode + // a base color in the material, so for simplicity we just create a new material here + // that is unlit and uses the fill color as the base color + MaterialKey::Color { .. } => { + let blended = + state.style.blend_state.is_some() || color.alpha() < 1.0 || draw.translucent; + let base = StandardMaterial { + base_color: color, + unlit: true, + cull_mode: None, + alpha_mode: if blended { + AlphaMode::Blend + } else { + AlphaMode::Opaque + }, + depth_bias: sort_bias(batch.draw_index), + ..default() + }; + let extended = ProcessingExtendedMaterial { + base, + extension: ProcessingMaterial { + blend_state: state.style.blend_state, + depth_write: None, + }, + }; + res.materials.add(extended).untyped() + } + key @ MaterialKey::Pbr { .. } => { + let mut key = material_key_with_color(key, color, state.style.blend_state); + if draw.translucent { + key = key.transparent(); + } + key.to_material(&mut res.materials, sort_bias(batch.draw_index)) + } + }), }; - let z_offset = layer_z(batch.draw_index); let mut transform = state.transform.to_bevy_transform(); - transform.translation.z += z_offset; + if let Some(local) = draw.local { + transform = Transform::from_matrix(transform.to_matrix() * local.to_matrix()); + } + transform.translation.z += layer_z(batch.draw_index); let mut entity = res.commands.spawn(( - Mesh3d(mesh_handle), - UntypedMaterial(material_handle), + Mesh3d(draw.mesh), FlushTransient(batch.graphics_entity), transform, batch.render_layers.clone(), )); - - if let Some(stroke_color) = state.style.stroke_color { + if let Some(material_handle) = material_handle { + entity.insert(UntypedMaterial(material_handle)); + } + if let Some((edges, color)) = stroke { entity.insert(( Wireframe, - WireframeColor { - color: stroke_color, - }, + WireframeColor { color }, WireframeLineWidth { - width: state.style.stroke_weight, + width: if draw.hairline { + 1.0 + } else { + state.style.stroke_weight + }, }, - WireframeTopology::Quads, + edges, )); } diff --git a/crates/processing_wasm/src/lib.rs b/crates/processing_wasm/src/lib.rs index f6f7331e..a9d33b36 100644 --- a/crates/processing_wasm/src/lib.rs +++ b/crates/processing_wasm/src/lib.rs @@ -1347,7 +1347,10 @@ pub fn js_geometry_normal(geo_id: u64, nx: f32, ny: f32, nz: f32) -> Result<(), #[wasm_bindgen(js_name = "geometryColor")] pub fn js_geometry_color(geo_id: u64, r: f32, g: f32, b: f32, a: f32) -> Result<(), JsValue> { let entity = Entity::from_bits(geo_id); - check(geometry_color(entity, Vec4::new(r, g, b, a))) + check(geometry_color( + entity, + bevy::color::Color::srgba(r, g, b, a), + )) } #[wasm_bindgen(js_name = "geometryUv")] diff --git a/examples/animated_mesh.rs b/examples/animated_mesh.rs index 8d5f7e84..01944415 100644 --- a/examples/animated_mesh.rs +++ b/examples/animated_mesh.rs @@ -1,6 +1,7 @@ use processing_glfw::GlfwContext; -use bevy::math::{Vec3, Vec4}; +use bevy::color::Color; +use bevy::math::Vec3; use processing::prelude::*; use processing_render::geometry::Topology; use processing_render::render::command::DrawCommand; @@ -39,7 +40,7 @@ fn sketch() -> error::Result<()> { let pz = z as f32 * spacing - offset; geometry_color( mesh, - Vec4::new( + Color::srgba( x as f32 / grid_size as f32, 0.5, z as f32 / grid_size as f32, @@ -90,6 +91,7 @@ fn sketch() -> error::Result<()> { graphics, DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.05, 0.05, 0.1)), )?; + graphics_record_command(graphics, DrawCommand::NoStroke)?; graphics_record_command(graphics, DrawCommand::Geometry(mesh))?; graphics_end_draw(graphics)?; diff --git a/examples/box.rs b/examples/box.rs index e687228f..9cb419eb 100644 --- a/examples/box.rs +++ b/examples/box.rs @@ -40,6 +40,7 @@ fn sketch() -> error::Result<()> { graphics, DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.1, 0.1, 0.15)), )?; + graphics_record_command(graphics, DrawCommand::NoStroke)?; graphics_record_command(graphics, DrawCommand::PushMatrix)?; graphics_record_command( diff --git a/examples/camera_controllers.rs b/examples/camera_controllers.rs index 16f21bb1..e49492df 100644 --- a/examples/camera_controllers.rs +++ b/examples/camera_controllers.rs @@ -62,6 +62,7 @@ fn sketch() -> error::Result<()> { graphics, DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.05, 0.05, 0.07)), )?; + graphics_record_command(graphics, DrawCommand::NoStroke)?; if mode < 2 { graphics_record_command( diff --git a/examples/custom_attribute.rs b/examples/custom_attribute.rs index 3bb2454c..ec690d61 100644 --- a/examples/custom_attribute.rs +++ b/examples/custom_attribute.rs @@ -1,6 +1,7 @@ use processing_glfw::GlfwContext; -use bevy::math::{Vec3, Vec4}; +use bevy::color::Color; +use bevy::math::Vec3; use processing::prelude::*; use processing_render::geometry::{AttributeFormat, Topology}; use processing_render::render::command::DrawCommand; @@ -37,16 +38,16 @@ fn sketch() -> error::Result<()> { let mesh = geometry_create_with_layout(layout, Topology::LineStrip)?; - geometry_color(mesh, Vec4::new(1.0, 0.0, 0.0, 1.0))?; + geometry_color(mesh, Color::srgba(1.0, 0.0, 0.0, 1.0))?; geometry_normal(mesh, Vec3::new(0.0, 0.0, 1.0))?; geometry_attribute_float(mesh, custom_attr, 0.0)?; geometry_vertex(mesh, Vec3::new(-50.0, 50.0, 0.0))?; - geometry_color(mesh, Vec4::new(0.0, 1.0, 0.0, 1.0))?; + geometry_color(mesh, Color::srgba(0.0, 1.0, 0.0, 1.0))?; geometry_attribute_float(mesh, custom_attr, 0.5)?; geometry_vertex(mesh, Vec3::new(50.0, 50.0, 0.0))?; - geometry_color(mesh, Vec4::new(0.0, 0.0, 1.0, 1.0))?; + geometry_color(mesh, Color::srgba(0.0, 0.0, 1.0, 1.0))?; geometry_attribute_float(mesh, custom_attr, 1.0)?; geometry_vertex(mesh, Vec3::new(0.0, -50.0, 0.0))?; @@ -65,6 +66,7 @@ fn sketch() -> error::Result<()> { graphics, DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.1, 0.1, 0.12)), )?; + graphics_record_command(graphics, DrawCommand::StrokeColor(Color::WHITE))?; graphics_record_command(graphics, DrawCommand::Geometry(mesh))?; graphics_end_draw(graphics)?; } diff --git a/examples/custom_material.rs b/examples/custom_material.rs index 533a8124..1693e6e4 100644 --- a/examples/custom_material.rs +++ b/examples/custom_material.rs @@ -48,6 +48,7 @@ fn sketch() -> error::Result<()> { graphics, DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.1, 0.1, 0.15)), )?; + graphics_record_command(graphics, DrawCommand::NoStroke)?; graphics_record_command(graphics, DrawCommand::PushMatrix)?; graphics_record_command( diff --git a/examples/lights.rs b/examples/lights.rs index c5bb3ff7..657c849b 100644 --- a/examples/lights.rs +++ b/examples/lights.rs @@ -85,6 +85,7 @@ fn sketch() -> error::Result<()> { graphics, DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.18, 0.20, 0.15)), )?; + graphics_record_command(graphics, DrawCommand::NoStroke)?; graphics_record_command(graphics, DrawCommand::Fill(bevy::color::Color::WHITE))?; graphics_record_command(graphics, DrawCommand::Material(pbr_mat))?; diff --git a/examples/materials.rs b/examples/materials.rs index c7c0294c..ecf01210 100644 --- a/examples/materials.rs +++ b/examples/materials.rs @@ -75,6 +75,7 @@ fn sketch() -> error::Result<()> { graphics, DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.05, 0.05, 0.07)), )?; + graphics_record_command(graphics, DrawCommand::NoStroke)?; graphics_record_command(graphics, DrawCommand::Fill(base_color))?; diff --git a/examples/text_3d.rs b/examples/text_3d.rs index 99abe875..b4c82f25 100644 --- a/examples/text_3d.rs +++ b/examples/text_3d.rs @@ -54,6 +54,7 @@ fn sketch() -> error::Result<()> { graphics, DrawCommand::BackgroundColor(bevy::color::Color::srgb(0.02, 0.02, 0.03)), )?; + graphics_record_command(graphics, DrawCommand::NoStroke)?; graphics_record_command( graphics,