Skip to content

Commit 737b97e

Browse files
committed
Use the MaterialKey as the cannonical settings for current draw.
1 parent aaecde1 commit 737b97e

File tree

3 files changed

+23
-40
lines changed

3 files changed

+23
-40
lines changed

crates/processing_render/src/geometry/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ use std::collections::HashMap;
1111

1212
use bevy::{
1313
asset::RenderAssetUsages,
14-
mesh::{Indices, MeshVertexAttributeId, Meshable, VertexAttributeValues},
14+
mesh::{Indices, MeshVertexAttributeId, VertexAttributeValues},
1515
prelude::*,
1616
render::render_resource::PrimitiveTopology,
1717
};
1818

1919
use crate::error::{ProcessingError, Result};
20+
use crate::render::primitive::{box_mesh, sphere_mesh};
2021

2122
pub struct GeometryPlugin;
2223

@@ -158,9 +159,7 @@ pub fn create_box(
158159
mut meshes: ResMut<Assets<Mesh>>,
159160
builtins: Res<BuiltinAttributes>,
160161
) -> Entity {
161-
let cuboid = Cuboid::new(width, height, depth);
162-
let mesh = cuboid.mesh().build();
163-
let handle = meshes.add(mesh);
162+
let handle = meshes.add(box_mesh(width, height, depth));
164163

165164
let layout_entity = commands
166165
.spawn(VertexLayout::with_attributes(vec![
@@ -180,9 +179,7 @@ pub fn create_sphere(
180179
mut meshes: ResMut<Assets<Mesh>>,
181180
builtins: Res<BuiltinAttributes>,
182181
) -> Entity {
183-
let sphere = Sphere::new(radius);
184-
let mesh = sphere.mesh().uv(sectors, stacks);
185-
let handle = meshes.add(mesh);
182+
let handle = meshes.add(sphere_mesh(radius, sectors, stacks));
186183

187184
let layout_entity = commands
188185
.spawn(VertexLayout::with_attributes(vec![

crates/processing_render/src/graphics.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use crate::{
2929
Flush,
3030
error::{ProcessingError, Result},
3131
image::{Image, bytes_to_pixels, create_readback_buffer, pixel_size, pixels_to_bytes},
32-
material::DefaultMaterial,
3332
render::{
3433
RenderState,
3534
command::{CommandBuffer, DrawCommand},
@@ -187,7 +186,6 @@ pub fn create(
187186
mut layer_manager: ResMut<RenderLayersManager>,
188187
p_images: Query<&Image, With<Surface>>,
189188
render_device: Res<RenderDevice>,
190-
default_material: Res<DefaultMaterial>,
191189
) -> Result<Entity> {
192190
// find the surface entity, if it is an image, we will render to that image
193191
// otherwise we will render to the window
@@ -245,7 +243,7 @@ pub fn create(
245243
Transform::from_xyz(0.0, 0.0, 999.9),
246244
render_layer,
247245
CommandBuffer::new(),
248-
RenderState::new(default_material.0),
246+
RenderState::new(),
249247
SurfaceSize(width, height),
250248
Graphics {
251249
readback_buffer,
@@ -426,15 +424,11 @@ pub fn destroy(
426424
Ok(())
427425
}
428426

429-
pub fn begin_draw(
430-
In(entity): In<Entity>,
431-
mut state_query: Query<&mut RenderState>,
432-
default_material: Res<DefaultMaterial>,
433-
) -> Result<()> {
427+
pub fn begin_draw(In(entity): In<Entity>, mut state_query: Query<&mut RenderState>) -> Result<()> {
434428
let mut state = state_query
435429
.get_mut(entity)
436430
.map_err(|_| ProcessingError::GraphicsNotFound)?;
437-
state.reset(default_material.0);
431+
state.reset();
438432
Ok(())
439433
}
440434

crates/processing_render/src/render/mod.rs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,17 @@ pub struct RenderResources<'w, 's> {
4040
struct BatchState {
4141
current_mesh: Option<Mesh>,
4242
material_key: Option<MaterialKey>,
43-
active_material: Entity,
4443
transform: Affine3A,
4544
draw_index: u32,
4645
render_layers: RenderLayers,
4746
graphics_entity: Entity,
4847
}
4948

5049
impl BatchState {
51-
fn new(graphics_entity: Entity, render_layers: RenderLayers, active_material: Entity) -> Self {
50+
fn new(graphics_entity: Entity, render_layers: RenderLayers) -> Self {
5251
Self {
5352
current_mesh: None,
5453
material_key: None,
55-
active_material,
5654
transform: Affine3A::IDENTITY,
5755
draw_index: 0,
5856
render_layers,
@@ -68,11 +66,10 @@ pub struct RenderState {
6866
pub stroke_weight: f32,
6967
pub material_key: MaterialKey,
7068
pub transform: TransformStack,
71-
pub active_material: Entity,
7269
}
7370

7471
impl RenderState {
75-
pub fn new(default_material: Entity) -> Self {
72+
pub fn new() -> Self {
7673
Self {
7774
fill_color: Some(Color::WHITE),
7875
stroke_color: Some(Color::BLACK),
@@ -82,11 +79,10 @@ impl RenderState {
8279
background_image: None,
8380
},
8481
transform: TransformStack::new(),
85-
active_material: default_material,
8682
}
8783
}
8884

89-
pub fn reset(&mut self, default_material: Entity) {
85+
pub fn reset(&mut self) {
9086
self.fill_color = Some(Color::WHITE);
9187
self.stroke_color = Some(Color::BLACK);
9288
self.stroke_weight = 1.0;
@@ -95,7 +91,6 @@ impl RenderState {
9591
background_image: None,
9692
};
9793
self.transform = TransformStack::new();
98-
self.active_material = default_material;
9994
}
10095

10196
pub fn fill_is_transparent(&self) -> bool {
@@ -131,11 +126,7 @@ pub fn flush_draw_commands(
131126
let view_from_world = camera_transform.to_matrix().inverse();
132127
let world_from_clip = (clip_from_view * view_from_world).inverse();
133128
let draw_commands = std::mem::take(&mut cmd_buffer.commands);
134-
let mut batch = BatchState::new(
135-
graphics_entity,
136-
render_layers.clone(),
137-
state.active_material,
138-
);
129+
let mut batch = BatchState::new(graphics_entity, render_layers.clone());
139130

140131
for cmd in draw_commands {
141132
match cmd {
@@ -305,13 +296,16 @@ pub fn flush_draw_commands(
305296
continue;
306297
};
307298

308-
let Some(mat_handle) = p_material_handles.get(batch.active_material).ok()
309-
else {
310-
warn!(
311-
"Could not find material for entity {:?}",
312-
batch.active_material
313-
);
314-
continue;
299+
let material_key = material_key_with_fill(&state);
300+
let material_handle = match &material_key {
301+
MaterialKey::Custom(mat_entity) => {
302+
let Some(handle) = p_material_handles.get(*mat_entity).ok() else {
303+
warn!("Could not find material for entity {:?}", mat_entity);
304+
continue;
305+
};
306+
handle.0.clone()
307+
}
308+
_ => material_key.to_material(&mut res.materials),
315309
};
316310

317311
flush_batch(&mut res, &mut batch);
@@ -322,7 +316,7 @@ pub fn flush_draw_commands(
322316

323317
res.commands.spawn((
324318
Mesh3d(geometry.handle.clone()),
325-
UntypedMaterial(mat_handle.0.clone()),
319+
UntypedMaterial(material_handle),
326320
BelongsToGraphics(batch.graphics_entity),
327321
transform,
328322
batch.render_layers.clone(),
@@ -331,9 +325,7 @@ pub fn flush_draw_commands(
331325
batch.draw_index += 1;
332326
}
333327
DrawCommand::Material(entity) => {
334-
state.active_material = entity;
335-
batch.active_material = entity;
336-
flush_batch(&mut res, &mut batch);
328+
state.material_key = MaterialKey::Custom(entity);
337329
}
338330
DrawCommand::Box {
339331
width,

0 commit comments

Comments
 (0)