Skip to content

Commit c215696

Browse files
catilactychedelia
andauthored
light methods for python (#72)
Co-authored-by: charlotte 🌸 <charlotte.c.mcelwain@gmail.com>
1 parent 2098b83 commit c215696

File tree

3 files changed

+164
-2
lines changed

3 files changed

+164
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from processing import *
2+
3+
angle = 0.0
4+
5+
def setup():
6+
size(800, 600)
7+
mode_3d()
8+
9+
# Directional Light
10+
dir_light = create_directional_light(0.5, 0.24, 1.0, 1500.0)
11+
12+
# Point Lights
13+
point_light_a = create_point_light(1.0, 0.5, 0.25, 1000000.0, 200.0, 0.5)
14+
point_light_a.position(-25.0, 5.0, 51.0)
15+
point_light_a.look_at(0.0, 0.0, 0.0)
16+
17+
point_light_b = create_point_light(0.0, 0.5, 0.75, 2000000.0, 200.0, 0.25)
18+
point_light_b.position(0.0, 5.0, 50.5)
19+
point_light_b.look_at(0.0, 0.0, 0.0)
20+
21+
# Spot Light
22+
spot_light = create_spot_light(0.25, 0.8, 0.19, 15.0 * 1000000.0, 200.0, 0.84, 0.0, 0.7854)
23+
spot_light.position(40.0, 0.0, 70.0)
24+
spot_light.look_at(0.0, 0.0, 0.0)
25+
26+
def draw():
27+
global angle
28+
camera_position(100.0, 100.0, 300.0)
29+
camera_look_at(0.0, 0.0, 0.0)
30+
background(220)
31+
32+
push_matrix()
33+
rotate(angle)
34+
draw_box(100.0, 100.0, 100.0)
35+
pop_matrix()
36+
37+
angle += 0.02
38+
39+
40+
# TODO: this should happen implicitly on module load somehow
41+
run()
42+

crates/processing_pyo3/src/graphics.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ impl Drop for Surface {
2323
}
2424
}
2525

26+
#[pyclass]
27+
#[derive(Debug)]
28+
pub struct Light {
29+
entity: Entity,
30+
}
31+
32+
#[pymethods]
33+
impl Light {
34+
pub fn position(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
35+
transform_set_position(self.entity, x, y, z)
36+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
37+
}
38+
39+
pub fn look_at(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
40+
transform_look_at(self.entity, x, y, z).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
41+
}
42+
}
43+
44+
// TODO: implement `light_destroy`
45+
// impl Drop for Light {
46+
// fn drop(&mut self) {
47+
// let _ = light_destroy(self.entity);
48+
// }
49+
// }
50+
2651
#[pyclass]
2752
#[derive(Debug)]
2853
pub struct Image {
@@ -332,6 +357,56 @@ impl Graphics {
332357
graphics_ortho(self.entity, left, right, bottom, top, near, far)
333358
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
334359
}
360+
361+
pub fn light_directional(&self, r: f32, g: f32, b: f32, illuminance: f32) -> PyResult<Light> {
362+
let color = bevy::color::Color::srgb(r, g, b);
363+
match light_create_directional(self.entity, color, illuminance) {
364+
Ok(light) => Ok(Light { entity: light }),
365+
Err(e) => Err(PyRuntimeError::new_err(format!("{e}"))),
366+
}
367+
}
368+
369+
pub fn light_point(
370+
&self,
371+
r: f32,
372+
g: f32,
373+
b: f32,
374+
intensity: f32,
375+
range: f32,
376+
radius: f32,
377+
) -> PyResult<Light> {
378+
let color = bevy::color::Color::srgb(r, g, b);
379+
match light_create_point(self.entity, color, intensity, range, radius) {
380+
Ok(light) => Ok(Light { entity: light }),
381+
Err(e) => Err(PyRuntimeError::new_err(format!("{e}"))),
382+
}
383+
}
384+
385+
pub fn light_spot(
386+
&self,
387+
r: f32,
388+
g: f32,
389+
b: f32,
390+
intensity: f32,
391+
range: f32,
392+
radius: f32,
393+
inner_angle: f32,
394+
outer_angle: f32,
395+
) -> PyResult<Light> {
396+
let color = bevy::color::Color::srgb(r, g, b);
397+
match light_create_spot(
398+
self.entity,
399+
color,
400+
intensity,
401+
range,
402+
radius,
403+
inner_angle,
404+
outer_angle,
405+
) {
406+
Ok(light) => Ok(Light { entity: light }),
407+
Err(e) => Err(PyRuntimeError::new_err(format!("{e}"))),
408+
}
409+
}
335410
}
336411

337412
// TODO: a real color type. or color parser? idk. color is confusing. let's think

crates/processing_pyo3/src/lib.rs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
mod glfw;
1212
mod graphics;
1313

14-
use graphics::{Geometry, Graphics, Image, Topology, get_graphics, get_graphics_mut};
14+
use graphics::{Geometry, Graphics, Image, Light, Topology, get_graphics, get_graphics_mut};
1515
use pyo3::{
1616
exceptions::PyRuntimeError,
1717
prelude::*,
@@ -25,7 +25,7 @@ use std::env;
2525
fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
2626
m.add_class::<Graphics>()?;
2727
m.add_class::<Image>()?;
28-
m.add_class::<Geometry>()?;
28+
m.add_class::<Light>()?;
2929
m.add_class::<Topology>()?;
3030
m.add_function(wrap_pyfunction!(size, m)?)?;
3131
m.add_function(wrap_pyfunction!(run, m)?)?;
@@ -45,6 +45,9 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
4545
m.add_function(wrap_pyfunction!(rect, m)?)?;
4646
m.add_function(wrap_pyfunction!(image, m)?)?;
4747
m.add_function(wrap_pyfunction!(draw_geometry, m)?)?;
48+
m.add_function(wrap_pyfunction!(create_directional_light, m)?)?;
49+
m.add_function(wrap_pyfunction!(create_point_light, m)?)?;
50+
m.add_function(wrap_pyfunction!(create_spot_light, m)?)?;
4851

4952
Ok(())
5053
}
@@ -275,3 +278,45 @@ fn rect(
275278
fn image(module: &Bound<'_, PyModule>, image_file: &str) -> PyResult<Image> {
276279
get_graphics(module)?.image(image_file)
277280
}
281+
282+
#[pyfunction]
283+
#[pyo3(pass_module, signature = (r, g, b, illuminance))]
284+
fn create_directional_light(
285+
module: &Bound<'_, PyModule>,
286+
r: f32,
287+
g: f32,
288+
b: f32,
289+
illuminance: f32,
290+
) -> PyResult<Light> {
291+
get_graphics(module)?.light_directional(r, g, b, illuminance)
292+
}
293+
294+
#[pyfunction]
295+
#[pyo3(pass_module, signature = (r, g, b, intensity, range, radius))]
296+
fn create_point_light(
297+
module: &Bound<'_, PyModule>,
298+
r: f32,
299+
g: f32,
300+
b: f32,
301+
intensity: f32,
302+
range: f32,
303+
radius: f32,
304+
) -> PyResult<Light> {
305+
get_graphics(module)?.light_point(r, g, b, intensity, range, radius)
306+
}
307+
308+
#[pyfunction]
309+
#[pyo3(pass_module, signature = (r, g, b, intensity, range, radius, inner_angle, outer_angle))]
310+
fn create_spot_light(
311+
module: &Bound<'_, PyModule>,
312+
r: f32,
313+
g: f32,
314+
b: f32,
315+
intensity: f32,
316+
range: f32,
317+
radius: f32,
318+
inner_angle: f32,
319+
outer_angle: f32,
320+
) -> PyResult<Light> {
321+
get_graphics(module)?.light_spot(r, g, b, intensity, range, radius, inner_angle, outer_angle)
322+
}

0 commit comments

Comments
 (0)