Skip to content

Commit b957736

Browse files
authored
Fix projection transform that was leading to ~100 max draws. (#92)
1 parent 77b166a commit b957736

File tree

2 files changed

+14
-23
lines changed

2 files changed

+14
-23
lines changed

crates/processing_render/src/graphics.rs

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
Flush,
3030
image::{Image, create_readback_buffer, pixel_size, pixels_to_bytes},
3131
render::{
32-
RenderState,
32+
BATCH_INDEX_STEP, RenderState,
3333
command::{CommandBuffer, DrawCommand},
3434
},
3535
surface::Surface,
@@ -112,19 +112,18 @@ pub struct SurfaceSize(pub u32, pub u32);
112112
/// Custom orthographic projection for Processing's coordinate system.
113113
/// Origin at top-left, Y-axis down, in pixel units (aka screen space).
114114
#[derive(Debug, Clone, Reflect)]
115-
#[reflect(Default)]
116115
pub struct ProcessingProjection {
117116
pub width: f32,
118117
pub height: f32,
119118
pub near: f32,
120119
pub far: f32,
121120
}
122121

123-
impl Default for ProcessingProjection {
124-
fn default() -> Self {
122+
impl ProcessingProjection {
123+
pub fn new(width: f32, height: f32) -> Self {
125124
Self {
126-
width: 1.0,
127-
height: 1.0,
125+
width,
126+
height,
128127
near: 0.0,
129128
far: 1000.0,
130129
}
@@ -234,14 +233,9 @@ pub fn create(
234233
Tonemapping::None,
235234
// we need to be able to write to the texture
236235
CameraMainTextureUsages::default().with(TextureUsages::COPY_DST),
237-
Projection::custom(ProcessingProjection {
238-
width: width as f32,
239-
height: height as f32,
240-
near: 0.0,
241-
far: 1000.0,
242-
}),
236+
Projection::custom(ProcessingProjection::new(width as f32, height as f32)),
243237
Msaa::Off,
244-
Transform::from_xyz(0.0, 0.0, 999.9),
238+
Transform::from_xyz(0.0, 0.0, BATCH_INDEX_STEP),
245239
render_layer,
246240
CommandBuffer::new(),
247241
RenderState::default(),
@@ -340,18 +334,13 @@ pub fn mode_2d(
340334
.get_mut(entity)
341335
.map_err(|_| ProcessingError::GraphicsNotFound)?;
342336

343-
*projection = Projection::custom(ProcessingProjection {
344-
width: *width as f32,
345-
height: *height as f32,
346-
near: 0.0,
347-
far: 1000.0,
348-
});
337+
*projection = Projection::custom(ProcessingProjection::new(*width as f32, *height as f32));
349338

350339
let mut transform = transforms
351340
.get_mut(entity)
352341
.map_err(|_| ProcessingError::GraphicsNotFound)?;
353342

354-
*transform = Transform::from_xyz(0.0, 0.0, 999.9);
343+
*transform = Transform::from_xyz(0.0, 0.0, BATCH_INDEX_STEP);
355344

356345
Ok(())
357346
}

crates/processing_render/src/render/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use crate::{
2323
render::{material::UntypedMaterial, primitive::rect},
2424
};
2525

26+
pub(crate) const BATCH_INDEX_STEP: f32 = 0.001;
27+
2628
#[derive(Component)]
2729
#[relationship(relationship_target = TransientMeshes)]
2830
pub struct BelongsToGraphics(pub Entity);
@@ -350,7 +352,7 @@ pub fn flush_draw_commands(
350352

351353
flush_batch(&mut res, &mut batch, &p_material_handles);
352354

353-
let z_offset = -(batch.draw_index as f32 * 0.001);
355+
let z_offset = -(batch.draw_index as f32 * BATCH_INDEX_STEP);
354356
let mut transform = state.transform.to_bevy_transform();
355357

356358
// if the "source" geometry was parented in a gltf scene, we need to make sure that
@@ -565,7 +567,7 @@ fn flush_batch(
565567
material_handles: &Query<&UntypedMaterial>,
566568
) {
567569
if let Some(mesh) = batch.current_mesh.take() {
568-
let z_offset = -(batch.draw_index as f32 * 0.001);
570+
let z_offset = -(batch.draw_index as f32 * BATCH_INDEX_STEP);
569571
spawn_mesh(res, batch, mesh, z_offset, material_handles);
570572
batch.draw_index += 1;
571573
}
@@ -616,7 +618,7 @@ fn add_shape3d(
616618
}
617619
};
618620

619-
let z_offset = -(batch.draw_index as f32 * 0.001);
621+
let z_offset = -(batch.draw_index as f32 * BATCH_INDEX_STEP);
620622
let mut transform = state.transform.to_bevy_transform();
621623
transform.translation.z += z_offset;
622624

0 commit comments

Comments
 (0)