Skip to content

Commit 88d0db7

Browse files
committed
Add texture commands and image fixes.
1 parent f49b4ba commit 88d0db7

7 files changed

Lines changed: 78 additions & 8 deletions

File tree

‎crates/processing_pyo3/src/graphics.rs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2169,6 +2169,21 @@ impl Graphics {
21692169
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
21702170
}
21712171

2172+
pub fn texture(&self, source: ImageRef) -> PyResult<()> {
2173+
graphics_record_command(self.entity, DrawCommand::Texture(source.entity))
2174+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
2175+
}
2176+
2177+
pub fn no_texture(&self) -> PyResult<()> {
2178+
graphics_record_command(self.entity, DrawCommand::NoTexture)
2179+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
2180+
}
2181+
2182+
pub fn texture_transform(&self, transform: crate::math::PyAffine2) -> PyResult<()> {
2183+
graphics_record_command(self.entity, DrawCommand::TextureTransform(transform.0))
2184+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
2185+
}
2186+
21722187
pub fn unlit(&self) -> PyResult<()> {
21732188
graphics_record_command(self.entity, DrawCommand::Unlit)
21742189
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))

‎crates/processing_pyo3/src/lib.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,27 @@ mod mewnala {
18241824
graphics!(module).emissive(args)
18251825
}
18261826

1827+
#[pyfunction]
1828+
#[pyo3(pass_module)]
1829+
fn texture(module: &Bound<'_, PyModule>, source: graphics::ImageRef) -> PyResult<()> {
1830+
graphics!(module).texture(source)
1831+
}
1832+
1833+
#[pyfunction]
1834+
#[pyo3(pass_module)]
1835+
fn no_texture(module: &Bound<'_, PyModule>) -> PyResult<()> {
1836+
graphics!(module).no_texture()
1837+
}
1838+
1839+
#[pyfunction]
1840+
#[pyo3(pass_module)]
1841+
fn texture_transform(
1842+
module: &Bound<'_, PyModule>,
1843+
transform: crate::math::PyAffine2,
1844+
) -> PyResult<()> {
1845+
graphics!(module).texture_transform(transform)
1846+
}
1847+
18271848
#[pyfunction]
18281849
#[pyo3(pass_module)]
18291850
fn unlit(module: &Bound<'_, PyModule>) -> PyResult<()> {

‎crates/processing_pyo3/src/webcam.rs‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ use processing_webcam::{
55
};
66
use pyo3::{exceptions::PyRuntimeError, prelude::*};
77

8-
use crate::graphics::Image;
9-
108
#[pyclass(unsendable)]
119
pub struct Webcam {
1210
entity: Entity,
1311
}
1412

13+
impl Webcam {
14+
pub(crate) fn image_entity(&self) -> PyResult<Entity> {
15+
webcam_image(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
16+
}
17+
}
18+
1519
#[pymethods]
1620
impl Webcam {
1721
#[new]
@@ -40,12 +44,6 @@ impl Webcam {
4044
pub fn resolution(&self) -> PyResult<(u32, u32)> {
4145
webcam_resolution(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
4246
}
43-
44-
pub fn image(&self) -> PyResult<Image> {
45-
let entity =
46-
webcam_image(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
47-
Ok(Image::from_entity(entity))
48-
}
4947
}
5048

5149
impl Drop for Webcam {

‎crates/processing_render/src/image.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ pub fn blit(
100100
Ok(())
101101
}
102102

103+
#[derive(Component)]
104+
#[relationship(relationship_target = LinkedImageTarget)]
105+
pub struct LinkedImage(pub Entity);
106+
107+
#[derive(Component)]
108+
#[relationship_target(relationship = LinkedImage)]
109+
pub struct LinkedImageTarget(Entity);
110+
103111
#[derive(Component)]
104112
pub struct Image {
105113
pub handle: Handle<bevy::image::Image>,

‎crates/processing_render/src/render/command.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bevy::math::Affine2;
12
use bevy::math::Affine3A;
23
use bevy::prelude::*;
34
use bevy::render::render_resource::{BlendComponent, BlendFactor, BlendOperation, BlendState};
@@ -515,6 +516,9 @@ pub enum DrawCommand {
515516
Roughness(f32),
516517
Metallic(f32),
517518
Emissive(Color),
519+
Texture(Entity),
520+
NoTexture,
521+
TextureTransform(Affine2),
518522
Unlit,
519523
Tint(Color),
520524
NoTint,

‎crates/processing_render/src/render/mod.rs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,25 @@ pub fn flush_draw_commands(
222222
pbr.blend_state = None;
223223
state.style.material_key = pbr.into();
224224
}
225+
DrawCommand::Texture(entity) => {
226+
if let Ok(img) = p_images.get(entity) {
227+
let mut pbr = state.style.material_key.as_pbr();
228+
pbr.base_color_texture = Some(img.handle.clone());
229+
state.style.material_key = pbr.into();
230+
}
231+
}
232+
DrawCommand::NoTexture => {
233+
if let MaterialKey::Pbr { .. } = &state.style.material_key {
234+
let mut pbr = state.style.material_key.as_pbr();
235+
pbr.base_color_texture = None;
236+
state.style.material_key = pbr.into();
237+
}
238+
}
239+
DrawCommand::TextureTransform(transform) => {
240+
let mut pbr = state.style.material_key.as_pbr();
241+
pbr.uv_transform = transform;
242+
state.style.material_key = pbr.into();
243+
}
225244
DrawCommand::Unlit => {
226245
state.style.material_key = MaterialKey::Color {
227246
transparent: state.fill_is_transparent(),

‎crates/processing_webcam/src/lib.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ fn create_with_format(In(format): In<WebcamFormat>, mut commands: Commands) -> E
3131
}
3232

3333
fn create_image(In(entity): In<Entity>, world: &mut World) -> Result<Entity> {
34+
if let Some(linked) = world.get::<image::LinkedImage>(entity) {
35+
return Ok(linked.0);
36+
}
37+
3438
let stream = world
3539
.get::<WebcamStream>(entity)
3640
.ok_or(ProcessingError::WebcamNotConnected)?;
@@ -39,6 +43,7 @@ fn create_image(In(entity): In<Entity>, world: &mut World) -> Result<Entity> {
3943
let child = world
4044
.run_system_once_with(image::from_handle, handle)
4145
.unwrap()?;
46+
world.entity_mut(entity).insert(image::LinkedImage(child));
4247
world.entity_mut(entity).add_child(child);
4348
Ok(child)
4449
}

0 commit comments

Comments
 (0)