Skip to content

Commit 269373d

Browse files
authored
Image resize, Graphics as an image source and material setters.
1 parent 775a785 commit 269373d

6 files changed

Lines changed: 306 additions & 49 deletions

File tree

‎crates/processing_pyo3/src/graphics.rs‎

Lines changed: 87 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -124,23 +124,6 @@ fn normalize_src_rect(entity: Entity, rect: Option<[f32; 4]>) -> PyResult<[f32;
124124
}
125125
}
126126

127-
/// Resolve a composite source (`Image`, `Webcam`, or `Graphics`) to a
128-
/// texture-bearing entity, plus the graphics entity that must be flushed first
129-
/// when the source is a graphics (so its latest content is what gets sampled).
130-
/// A `Graphics` source uses its render-target surface entity, which carries an
131-
/// `Image` component for offscreen graphics.
132-
fn resolve_composite_source(src: &Bound<'_, PyAny>) -> PyResult<(Entity, Option<Entity>)> {
133-
if let Ok(img) = src.extract::<ImageRef>() {
134-
return Ok((img.entity, None));
135-
}
136-
if let Ok(g) = src.extract::<PyRef<Graphics>>() {
137-
return Ok((g.surface.entity, Some(g.entity)));
138-
}
139-
Err(PyValueError::new_err(
140-
"composite source must be an Image, Webcam, or Graphics",
141-
))
142-
}
143-
144127
/// Run the built-in composite filter: blend the `src` texture into the `dst`
145128
/// graphics target using `mode`, with normalized src/dst rects. This is the
146129
/// shader-based composite pass shared by `copy`/`blend`/`mask`.
@@ -446,6 +429,36 @@ impl Light {
446429
let v = extract_vec3(args)?;
447430
transform_look_at(self.entity, v).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
448431
}
432+
433+
#[pyo3(signature = (*args))]
434+
pub fn translate(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> {
435+
let v = extract_vec3(args)?;
436+
transform_translate(self.entity, v).map_err(rt_err)
437+
}
438+
439+
pub fn rotate_x(&self, angle: f32) -> PyResult<()> {
440+
transform_rotate_x(self.entity, angle).map_err(rt_err)
441+
}
442+
443+
pub fn rotate_y(&self, angle: f32) -> PyResult<()> {
444+
transform_rotate_y(self.entity, angle).map_err(rt_err)
445+
}
446+
447+
pub fn rotate_z(&self, angle: f32) -> PyResult<()> {
448+
transform_rotate_z(self.entity, angle).map_err(rt_err)
449+
}
450+
451+
/// Rotate by `angle` radians around `axis`.
452+
#[pyo3(signature = (angle, *axis))]
453+
pub fn rotate_axis(&self, angle: f32, axis: &Bound<'_, PyTuple>) -> PyResult<()> {
454+
let axis = extract_vec3(axis)?;
455+
transform_rotate_axis(self.entity, angle, axis).map_err(rt_err)
456+
}
457+
458+
/// Back to the origin with no rotation.
459+
pub fn reset(&self) -> PyResult<()> {
460+
transform_reset(self.entity).map_err(rt_err)
461+
}
449462
}
450463

451464
impl Light {
@@ -565,16 +578,33 @@ impl Image {
565578
}
566579
}
567580

581+
/// Anything that can be sampled as an image: an `Image`, a `Webcam`, or an
582+
/// offscreen `Graphics`.
568583
pub(crate) struct ImageRef {
569-
pub entity: Entity,
584+
entity: Entity,
585+
graphics: Option<Entity>,
586+
}
587+
588+
impl ImageRef {
589+
/// The entity carrying the image. A `Graphics` source with pending draws is
590+
/// flushed first, so what it has drawn so far is what gets sampled.
591+
pub(crate) fn texture(&self) -> PyResult<Entity> {
592+
if let Some(graphics) = self.graphics {
593+
graphics_flush_pending(graphics).map_err(rt_err)?;
594+
}
595+
Ok(self.entity)
596+
}
570597
}
571598

572599
impl<'a, 'py> FromPyObject<'a, 'py> for ImageRef {
573600
type Error = PyErr;
574601

575602
fn extract(ob: pyo3::Borrowed<'a, 'py, PyAny>) -> PyResult<Self> {
576603
if let Ok(img) = ob.extract::<PyRef<Image>>() {
577-
return Ok(ImageRef { entity: img.entity });
604+
return Ok(ImageRef {
605+
entity: img.entity,
606+
graphics: None,
607+
});
578608
}
579609
// an offscreen Graphics renders into an image; a window has no texture to sample
580610
if let Ok(g) = ob.extract::<PyRef<Graphics>>() {
@@ -585,12 +615,14 @@ impl<'a, 'py> FromPyObject<'a, 'py> for ImageRef {
585615
}
586616
return Ok(ImageRef {
587617
entity: g.surface.entity,
618+
graphics: Some(g.entity),
588619
});
589620
}
590621
#[cfg(feature = "webcam")]
591622
if let Ok(cam) = ob.extract::<PyRef<crate::webcam::Webcam>>() {
592623
return Ok(ImageRef {
593624
entity: cam.image_entity()?,
625+
graphics: None,
594626
});
595627
}
596628
Err(pyo3::exceptions::PyTypeError::new_err(
@@ -629,6 +661,26 @@ impl Image {
629661
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
630662
}
631663

664+
/// Resize to `width` x `height` pixels. A 0 for either keeps the aspect ratio.
665+
pub fn resize(&self, width: u32, height: u32) -> PyResult<()> {
666+
let (w, h) = image_size(self.entity).map_err(rt_err)?;
667+
let (width, height) = match (width, height) {
668+
(0, 0) => return Err(PyValueError::new_err("resize() needs a non-zero size")),
669+
(0, height) => ((w as f32 * height as f32 / h as f32).round() as u32, height),
670+
(width, 0) => (width, (h as f32 * width as f32 / w as f32).round() as u32),
671+
size => size,
672+
};
673+
image_resize(
674+
self.entity,
675+
Extent3d {
676+
width: width.max(1),
677+
height: height.max(1),
678+
depth_or_array_layers: 1,
679+
},
680+
)
681+
.map_err(rt_err)
682+
}
683+
632684
/// Image width in pixels.
633685
#[getter]
634686
fn width(&self) -> PyResult<u32> {
@@ -1258,8 +1310,19 @@ impl Graphics {
12581310
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
12591311
}
12601312

1313+
/// A color (same arguments as `fill()`), or an `Image`, `Graphics` or
1314+
/// `Webcam` stretched over the canvas.
12611315
#[pyo3(signature = (*args))]
12621316
pub fn background(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> {
1317+
if args.len() == 1
1318+
&& let Ok(image) = args.get_item(0)?.extract::<ImageRef>()
1319+
{
1320+
return graphics_record_command(
1321+
self.entity,
1322+
DrawCommand::BackgroundImage(image.texture()?),
1323+
)
1324+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")));
1325+
}
12631326
let color = extract_color_with_mode(
12641327
args,
12651328
&graphics_get_color_mode(self.entity)
@@ -1269,11 +1332,6 @@ impl Graphics {
12691332
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
12701333
}
12711334

1272-
pub fn background_image(&self, image: &Image) -> PyResult<()> {
1273-
graphics_record_command(self.entity, DrawCommand::BackgroundImage(image.entity))
1274-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
1275-
}
1276-
12771335
pub fn clear(&self) -> PyResult<()> {
12781336
graphics_record_command(
12791337
self.entity,
@@ -1951,7 +2009,7 @@ impl Graphics {
19512009
graphics_record_command(
19522010
self.entity,
19532011
DrawCommand::Image {
1954-
entity: source.entity,
2012+
entity: source.texture()?,
19552013
dx,
19562014
dy,
19572015
d_width,
@@ -2340,7 +2398,7 @@ impl Graphics {
23402398
}
23412399

23422400
pub fn texture(&self, source: ImageRef) -> PyResult<()> {
2343-
graphics_record_command(self.entity, DrawCommand::Texture(source.entity))
2401+
graphics_record_command(self.entity, DrawCommand::Texture(source.texture()?))
23442402
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
23452403
}
23462404

@@ -2473,10 +2531,7 @@ impl Graphics {
24732531
MULTIPLY, SCREEN, REPLACE)",
24742532
)
24752533
})?;
2476-
let (src_entity, flush) = resolve_composite_source(src)?;
2477-
if let Some(g) = flush {
2478-
graphics_flush(g).map_err(rt_err)?;
2479-
}
2534+
let src_entity = src.extract::<ImageRef>()?.texture()?;
24802535
let sr = normalize_src_rect(src_entity, src_rect)?;
24812536
let dr = normalize_rect(dst_rect, self.width as f32, self.height as f32);
24822537
composite_apply(self.entity, src_entity, code, sr, dr, opacity)
@@ -2492,10 +2547,7 @@ impl Graphics {
24922547
src_rect: Option<[f32; 4]>,
24932548
dst_rect: Option<[f32; 4]>,
24942549
) -> PyResult<()> {
2495-
let (src_entity, flush) = resolve_composite_source(src)?;
2496-
if let Some(g) = flush {
2497-
graphics_flush(g).map_err(rt_err)?;
2498-
}
2550+
let src_entity = src.extract::<ImageRef>()?.texture()?;
24992551
let sr = normalize_src_rect(src_entity, src_rect)?;
25002552
let dr = normalize_rect(dst_rect, self.width as f32, self.height as f32);
25012553
composite_apply(self.entity, src_entity, COMPOSITE_MODE_REPLACE, sr, dr, 1.0)
@@ -2519,10 +2571,7 @@ impl Graphics {
25192571
/// Applies a mask source's brightness as this graphics' alpha channel
25202572
/// (Processing `mask`). `mask` may be an `Image`, webcam, or `Graphics`.
25212573
pub fn mask(&self, mask: &Bound<'_, PyAny>) -> PyResult<()> {
2522-
let (src_entity, flush) = resolve_composite_source(mask)?;
2523-
if let Some(g) = flush {
2524-
graphics_flush(g).map_err(rt_err)?;
2525-
}
2574+
let src_entity = mask.extract::<ImageRef>()?.texture()?;
25262575
composite_apply(
25272576
self.entity,
25282577
src_entity,

‎crates/processing_pyo3/src/lib.rs‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,13 +1687,7 @@ pub mod processing_py {
16871687
#[pyfunction]
16881688
#[pyo3(pass_module, signature = (*args))]
16891689
fn background(module: &Bound<'_, PyModule>, args: &Bound<'_, PyTuple>) -> PyResult<()> {
1690-
let graphics = graphics!(module);
1691-
let first = args.get_item(0)?;
1692-
if first.is_instance_of::<Image>() {
1693-
graphics.background_image(&*first.extract::<PyRef<Image>>()?)
1694-
} else {
1695-
graphics.background(args)
1696-
}
1690+
graphics!(module).background(args)
16971691
}
16981692

16991693
#[pyfunction]

‎crates/processing_pyo3/src/material.rs‎

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct Material {
1616

1717
pub(crate) fn py_to_shader_value(value: &Bound<'_, PyAny>) -> PyResult<shader_value::ShaderValue> {
1818
if let Ok(img_ref) = value.extract::<ImageRef>() {
19-
return Ok(shader_value::ShaderValue::Texture(img_ref.entity));
19+
return Ok(shader_value::ShaderValue::Texture(img_ref.texture()?));
2020
}
2121
if let Ok(int_val) = value.cast::<PyInt>() {
2222
if let Ok(v) = int_val.extract::<i32>() {
@@ -47,6 +47,16 @@ pub(crate) fn py_to_shader_value(value: &Bound<'_, PyAny>) -> PyResult<shader_va
4747
return Ok(shader_value::ShaderValue::Grid(grid.entity));
4848
}
4949

50+
// row-major like `get_matrix()`, WGSL wants columns
51+
if let Ok(rows) = value.extract::<[f32; 16]>() {
52+
let m = bevy::math::Mat4::from_cols_array(&rows).transpose();
53+
return Ok(shader_value::ShaderValue::Mat4(m.to_cols_array()));
54+
}
55+
if let Ok(rows) = value.extract::<[[f32; 4]; 4]>() {
56+
let m = bevy::math::Mat4::from_cols_array_2d(&rows).transpose();
57+
return Ok(shader_value::ShaderValue::Mat4(m.to_cols_array()));
58+
}
59+
5060
if let Ok(v) = value.extract::<[f32; 4]>() {
5161
return Ok(shader_value::ShaderValue::Float4(v));
5262
}
@@ -88,6 +98,32 @@ fn apply_albedo(entity: Entity, value: &Bound<'_, PyAny>) -> PyResult<()> {
8898
)))
8999
}
90100

101+
fn apply_emissive(entity: Entity, value: &Bound<'_, PyAny>) -> PyResult<()> {
102+
let rt = |e| PyRuntimeError::new_err(format!("{e}"));
103+
if let Ok(buf) = value.extract::<PyRef<Buffer>>() {
104+
return material_set_emissive_buffer(entity, buf.entity).map_err(rt);
105+
}
106+
// emissive is a linear radiance, not an sRGB color
107+
let linear: bevy::color::LinearRgba = if let Ok(c) = value.extract::<PyRef<PyColor>>() {
108+
c.0.into()
109+
} else if let Ok([r, g, b, a]) = value.extract::<[f32; 4]>() {
110+
bevy::color::LinearRgba::new(r, g, b, a)
111+
} else if let Ok([r, g, b]) = value.extract::<[f32; 3]>() {
112+
bevy::color::LinearRgba::new(r, g, b, 1.0)
113+
} else {
114+
return Err(PyRuntimeError::new_err(format!(
115+
"unsupported emissive type: {} (expected Color, Buffer, or [r,g,b,(a)])",
116+
value.get_type().name()?
117+
)));
118+
};
119+
material_set(
120+
entity,
121+
"emissive",
122+
shader_value::ShaderValue::Float4([linear.red, linear.green, linear.blue, linear.alpha]),
123+
)
124+
.map_err(rt)
125+
}
126+
91127
fn py_truthy(value: &Bound<'_, PyAny>) -> PyResult<bool> {
92128
value
93129
.extract::<bool>()
@@ -100,6 +136,7 @@ fn apply_kwargs(entity: Entity, kwargs: &Bound<'_, PyDict>) -> PyResult<()> {
100136
let rt = |e| PyRuntimeError::new_err(format!("{e}"));
101137
match name.as_str() {
102138
"albedo" => apply_albedo(entity, &value)?,
139+
"emissive" => apply_emissive(entity, &value)?,
103140
"unlit" => material_set_unlit(entity, py_truthy(&value)?).map_err(rt)?,
104141
"double_sided" => material_set_double_sided(entity, py_truthy(&value)?).map_err(rt)?,
105142
"depth_write" => material_set_depth_write(entity, py_truthy(&value)?).map_err(rt)?,
@@ -166,6 +203,70 @@ impl Material {
166203
};
167204
apply_kwargs(self.entity, kwargs)
168205
}
206+
207+
/// Base color: a `Color`, `[r, g, b, (a)]`, or a per-instance `Buffer`.
208+
pub fn albedo(&self, value: &Bound<'_, PyAny>) -> PyResult<()> {
209+
apply_albedo(self.entity, value)
210+
}
211+
212+
pub fn metalness(&self, value: f32) -> PyResult<()> {
213+
self.set_pbr("metallic", value)
214+
}
215+
216+
pub fn roughness(&self, value: f32) -> PyResult<()> {
217+
self.set_pbr("roughness", value)
218+
}
219+
220+
pub fn reflectance(&self, value: f32) -> PyResult<()> {
221+
self.set_pbr("reflectance", value)
222+
}
223+
224+
/// Emitted light: a `Color`, linear `[r, g, b, (a)]`, or a per-instance `Buffer`.
225+
pub fn emissive(&self, value: &Bound<'_, PyAny>) -> PyResult<()> {
226+
apply_emissive(self.entity, value)
227+
}
228+
229+
pub fn opaque(&self) -> PyResult<()> {
230+
material_set_alpha_mode(self.entity, 0, 0.0).map_err(rt)
231+
}
232+
233+
/// Discards fragments with alpha below `cutoff`.
234+
#[pyo3(signature = (cutoff=0.5))]
235+
pub fn mask(&self, cutoff: f32) -> PyResult<()> {
236+
material_set_alpha_mode(self.entity, 1, cutoff).map_err(rt)
237+
}
238+
239+
pub fn transparent(&self) -> PyResult<()> {
240+
material_set_alpha_mode(self.entity, 2, 0.0).map_err(rt)
241+
}
242+
243+
#[pyo3(signature = (on=true))]
244+
pub fn double_sided(&self, on: bool) -> PyResult<()> {
245+
material_set_double_sided(self.entity, on).map_err(rt)
246+
}
247+
248+
#[pyo3(signature = (on=true))]
249+
pub fn depth_write(&self, on: bool) -> PyResult<()> {
250+
material_set_depth_write(self.entity, on).map_err(rt)
251+
}
252+
253+
/// Blends with what's behind it using a `BlendMode` (`ADD`, `MULTIPLY`, a custom one, ...).
254+
pub fn blend_mode(&self, mode: &crate::graphics::PyBlendMode) -> PyResult<()> {
255+
let state = mode
256+
.blend_state
257+
.unwrap_or(bevy::render::render_resource::BlendState::ALPHA_BLENDING);
258+
material_set_custom_blend(self.entity, state).map_err(rt)
259+
}
260+
}
261+
262+
fn rt(e: impl std::fmt::Display) -> PyErr {
263+
PyRuntimeError::new_err(format!("{e}"))
264+
}
265+
266+
impl Material {
267+
fn set_pbr(&self, name: &str, value: f32) -> PyResult<()> {
268+
material_set(self.entity, name, shader_value::ShaderValue::Float(value)).map_err(rt)
269+
}
169270
}
170271

171272
impl Drop for Material {

‎crates/processing_render/src/graphics.rs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,15 @@ pub fn flush(app: &mut App, entity: Entity) -> Result<()> {
757757
Ok(())
758758
}
759759

760+
/// Brings the target up to date with what was drawn so far; no update when nothing is queued
761+
pub fn flush_pending(app: &mut App, entity: Entity) -> Result<()> {
762+
if has_commands(app, entity) {
763+
flush(app, entity)
764+
} else {
765+
upload_dirty(app, entity)
766+
}
767+
}
768+
760769
fn has_commands(app: &App, entity: Entity) -> bool {
761770
app.world()
762771
.get::<CommandBuffer>(entity)

0 commit comments

Comments
 (0)