Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions assets/shaders/custom_vertex.wesl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { processing::VertexOutput };
import { processing::mesh::{Vertex, vertex_output} };

@group(3) @binding(0) var<uniform> bulge: f32;

@vertex
fn vertex(vertex_in: Vertex) -> VertexOutput {
var vertex = vertex_in;
// `sphere()` draws a unit sphere scaled by its transform
vertex.position += vertex.normal * bulge * sin(vertex.position.y * 12.0);
return vertex_output(vertex);
}

@fragment
fn fragment(mesh: VertexOutput) -> @location(0) vec4<f32> {
let light_dir = normalize(vec3(0.4, -0.6, 0.7));
let diffuse = max(dot(normalize(mesh.world_normal), light_dir), 0.2);
// the fill arrives as the vertex color
return vec4(mesh.color.rgb * diffuse, mesh.color.a);
}
3 changes: 3 additions & 0 deletions crates/processing_core/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ pub const ITALIC: &str = "italic";
pub const BOLD: &str = "bold";
pub const BOLD_ITALIC: &str = "bold_italic";

pub const ENABLE_STROKE_PERSPECTIVE: &str = "enable_stroke_perspective"; // hint
pub const DISABLE_STROKE_PERSPECTIVE: &str = "disable_stroke_perspective";

pub const WORD: &str = "word"; // text wrap mode
pub const CHAR: &str = "char";

Expand Down
19 changes: 17 additions & 2 deletions crates/processing_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,15 @@ pub extern "C" fn processing_circle(graphics_id: u64, cx: f32, cy: f32, d: f32)
pub extern "C" fn processing_line(graphics_id: u64, x1: f32, y1: f32, x2: f32, y2: f32) {
error::clear_error();
let graphics_entity = Entity::from_bits(graphics_id);
error::check(|| graphics_record_command(graphics_entity, DrawCommand::Line { x1, y1, x2, y2 }));
error::check(|| {
graphics_record_command(
graphics_entity,
DrawCommand::Line {
from: Vec3::new(x1, y1, 0.0),
to: Vec3::new(x2, y2, 0.0),
},
)
});
}

/// Draw a triangle.
Expand Down Expand Up @@ -782,7 +790,14 @@ pub extern "C" fn processing_quad(
pub extern "C" fn processing_point(graphics_id: u64, x: f32, y: f32) {
error::clear_error();
let graphics_entity = Entity::from_bits(graphics_id);
error::check(|| graphics_record_command(graphics_entity, DrawCommand::Point { x, y }));
error::check(|| {
graphics_record_command(
graphics_entity,
DrawCommand::Point {
at: Vec3::new(x, y, 0.0),
},
)
});
}

/// Draw a square.
Expand Down
9 changes: 3 additions & 6 deletions crates/processing_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,10 +564,8 @@ pub fn js_line(graphics_id: BigInt, x1: f64, y1: f64, x2: f64, y2: f64) -> Resul
record(
graphics_id,
DrawCommand::Line {
x1: x1 as f32,
y1: y1 as f32,
x2: x2 as f32,
y2: y2 as f32,
from: vec3(x1, y1, 0.0),
to: vec3(x2, y2, 0.0),
},
)
}
Expand Down Expand Up @@ -627,8 +625,7 @@ pub fn js_point(graphics_id: BigInt, x: f64, y: f64) -> Result<()> {
record(
graphics_id,
DrawCommand::Point {
x: x as f32,
y: y as f32,
at: vec3(x, y, 0.0),
},
)
}
Expand Down
75 changes: 75 additions & 0 deletions crates/processing_pyo3/examples/Demos/Tests/first_frame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""One of everything, captured on the first frame.

A shader or pipeline that is not ready on frame 1 silently drops its draws, so each kind
of draw gets its own part of the canvas: whatever is missing is the kind that dropped.
"""
from processing_py import *
from random import seed, uniform

points = None
mat = None


def setup():
global points, mat
size(800, 600)
mode_3d()

mat = create_material(load_shader("shaders/custom_material.wesl"), color=[0.3, 0.6, 1.0, 1.0])

seed(1)
count = 2000
points = create_particles(count, attributes=[Attribute.position()])
positions = []
for _ in range(count):
positions.append([220 + uniform(-60, 60), 120 + uniform(-60, 60), uniform(-60, 60)])
points.buffer("position").write(positions)


def draw():
camera_position(0.0, 0.0, 500.0)
camera_look_at(0.0, 0.0, 0.0)
background(20)

# a 2D shape
no_stroke()
fill(255, 150, 40)
rect(-340, -240, 160, 120)

# strokes
no_fill()
stroke(255)
stroke_weight(6)
ellipse(0, -180, 140, 110)
line(-60, -180, 60, -180)

# text
no_stroke()
fill(255)
text_size(40)
text_align(CENTER, CENTER)
text("frame 1", 250, -180)

# a 3D primitive
fill(60, 200, 110)
push_matrix()
translate(-250, 120, 0)
rotate_y(0.6)
rotate_x(0.4)
box(110)
pop_matrix()

# a custom material
push_style()
material(mat)
push_matrix()
translate(0, 120, 0)
sphere(65)
pop_matrix()
pop_style()

# particle points
particles(points)


run()
36 changes: 36 additions & 0 deletions crates/processing_pyo3/examples/custom_vertex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from processing_py import *

mat = None

def setup():
global mat
size(800, 600)
mode_3d()

shader = load_shader("shaders/custom_vertex.wesl")
mat = create_material(shader, bulge=0.15)

def draw():
camera_position(0.0, 0.0, 500.0)
camera_look_at(0.0, 0.0, 0.0)
background(12, 12, 18)

no_stroke()
material(mat)

fill(255, 80, 40)
push_matrix()
translate(-200, 0, 0)
sphere(70)
pop_matrix()

fill(40, 200, 120)
sphere(70)

fill(60, 120, 255)
push_matrix()
translate(200, 0, 0)
sphere(70)
pop_matrix()

run()
1 change: 1 addition & 0 deletions crates/processing_pyo3/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> {
add!(m, TOP, BOTTOM, BASELINE);
add!(m, NORMAL, ITALIC, BOLD, BOLD_ITALIC);
add!(m, IMAGE);
add!(m, ENABLE_STROKE_PERSPECTIVE, DISABLE_STROKE_PERSPECTIVE);
add!(m, WORD, CHAR);
add!(m, NEAREST, CLAMP, REPEAT, MIRROR);
add!(m, SRGB, LINEAR, HSL, HSV, HWB, OKLAB, OKLCH, LAB, LCH, XYZ);
Expand Down
52 changes: 47 additions & 5 deletions crates/processing_pyo3/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,8 +1522,28 @@ impl Graphics {
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn line(&self, x1: f32, y1: f32, x2: f32, y2: f32) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::Line { x1, y1, x2, y2 })
/// `line(x1, y1, x2, y2)`, or in 3D `line(x1, y1, z1, x2, y2, z2)`.
#[pyo3(signature = (x1, y1, x2, y2, *args))]
pub fn line(
&self,
x1: f32,
y1: f32,
x2: f32,
y2: f32,
args: &Bound<'_, PyTuple>,
) -> PyResult<()> {
let rest: Vec<f32> = args.extract()?;
let (from, to) = match rest[..] {
[] => (Vec3::new(x1, y1, 0.0), Vec3::new(x2, y2, 0.0)),
// six numbers read as two points
[a, b] => (Vec3::new(x1, y1, x2), Vec3::new(y2, a, b)),
_ => {
return Err(pyo3::exceptions::PyTypeError::new_err(
"line() takes 4 numbers, or 6 in 3D",
));
}
};
graphics_record_command(self.entity, DrawCommand::Line { from, to })
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

Expand Down Expand Up @@ -1569,9 +1589,15 @@ impl Graphics {
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn point(&self, x: f32, y: f32) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::Point { x, y })
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
#[pyo3(signature = (x, y, z=0.0))]
pub fn point(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
graphics_record_command(
self.entity,
DrawCommand::Point {
at: Vec3::new(x, y, z),
},
)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn square(&self, x: f32, y: f32, s: f32) -> PyResult<()> {
Expand Down Expand Up @@ -1710,6 +1736,22 @@ impl Graphics {
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

/// Changes a renderer setting (Processing `hint`).
pub fn hint(&self, which: &str) -> PyResult<()> {
use processing::prelude::constants as consts;
let perspective = match () {
_ if which.eq_ignore_ascii_case(consts::ENABLE_STROKE_PERSPECTIVE) => true,
_ if which.eq_ignore_ascii_case(consts::DISABLE_STROKE_PERSPECTIVE) => false,
_ => {
return Err(PyValueError::new_err(format!(
"unsupported hint: {which:?}"
)));
}
};
graphics_record_command(self.entity, DrawCommand::StrokePerspective(perspective))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

/// `IMAGE` reads `vertex()` texture coordinates in pixels of the texture, `NORMAL` from 0 to 1.
pub fn texture_mode(&self, mode: &str) -> PyResult<()> {
let mode = processing::prelude::TextureMode::parse(mode)
Expand Down
25 changes: 19 additions & 6 deletions crates/processing_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2498,9 +2498,16 @@ pub mod processing_py {
}

#[pyfunction]
#[pyo3(pass_module)]
fn line(module: &Bound<'_, PyModule>, x1: f32, y1: f32, x2: f32, y2: f32) -> PyResult<()> {
graphics!(module).line(x1, y1, x2, y2)
#[pyo3(pass_module, signature = (x1, y1, x2, y2, *args))]
fn line(
module: &Bound<'_, PyModule>,
x1: f32,
y1: f32,
x2: f32,
y2: f32,
args: &Bound<'_, PyTuple>,
) -> PyResult<()> {
graphics!(module).line(x1, y1, x2, y2, args)
}

#[pyfunction]
Expand Down Expand Up @@ -2534,9 +2541,9 @@ pub mod processing_py {
}

#[pyfunction]
#[pyo3(pass_module)]
fn point(module: &Bound<'_, PyModule>, x: f32, y: f32) -> PyResult<()> {
graphics!(module).point(x, y)
#[pyo3(pass_module, signature = (x, y, z=0.0))]
fn point(module: &Bound<'_, PyModule>, x: f32, y: f32, z: f32) -> PyResult<()> {
graphics!(module).point(x, y, z)
}

#[pyfunction]
Expand Down Expand Up @@ -2621,6 +2628,12 @@ pub mod processing_py {
graphics!(module).normal(args)
}

#[pyfunction]
#[pyo3(pass_module)]
fn hint(module: &Bound<'_, PyModule>, which: &str) -> PyResult<()> {
graphics!(module).hint(which)
}

#[pyfunction]
#[pyo3(pass_module)]
fn texture_mode(module: &Bound<'_, PyModule>, mode: &str) -> PyResult<()> {
Expand Down
Loading
Loading