Skip to content
Merged
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ path = "examples/pbr.rs"
name = "midi"
path = "examples/midi.rs"

[[example]]
name = "gltf_load"
path = "examples/gltf_load.rs"

[profile.wasm-release]
inherits = "release"
opt-level = "z"
Expand Down
Binary file added assets/gltf/Duck.glb
Binary file not shown.
11 changes: 10 additions & 1 deletion crates/processing_ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,16 @@ pub extern "C" fn processing_perspective(
) {
error::clear_error();
let graphics_entity = Entity::from_bits(graphics_id);
error::check(|| graphics_perspective(graphics_entity, fov, aspect, near, far));
error::check(|| {
graphics_perspective(
graphics_entity,
fov,
aspect,
near,
far,
bevy::math::Vec4::new(0.0, 0.0, -1.0, -near),
)
});
}

#[unsafe(no_mangle)]
Expand Down
46 changes: 46 additions & 0 deletions crates/processing_pyo3/examples/gltf_load.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import math
from processing import *

gltf = None
duck_geo = None
duck_mat = None
light = None
frame = 0

def setup():
global gltf, duck_geo, duck_mat, light
size(800, 600)

gltf = load_gltf("gltf/Duck.glb")
duck_geo = gltf.geometry("LOD3spShape")
duck_mat = gltf.material("blinn3-fx")

mode_3d()
gltf.camera(0)
light = gltf.light(0)

def draw():
global frame
t = frame * 0.02

radius = 1.5
lx = math.cos(t) * radius
ly = 1.5
lz = math.sin(t) * radius
light.position(lx, ly, lz)
light.look_at(0.0, 0.8, 0.0)

r = math.sin(t * 8.0) * 0.5 + 0.5
g = math.sin(t * 8.0 + 2.0) * 0.5 + 0.5
b = math.sin(t * 8.0 + 4.0) * 0.5 + 0.5
duck_mat.set_float4("base_color", r, g, b, 1.0)

background(25)
use_material(duck_mat)
draw_geometry(duck_geo)

frame += 1


# TODO: this should happen implicitly on module load somehow
run()
53 changes: 53 additions & 0 deletions crates/processing_pyo3/src/gltf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use bevy::prelude::Entity;
use processing::prelude::*;
use pyo3::{exceptions::PyRuntimeError, prelude::*};

use crate::graphics::{Geometry, Light, get_graphics};
use crate::material::Material;

#[pyclass(unsendable)]
pub struct Gltf {
entity: Entity,
}

#[pymethods]
impl Gltf {
pub fn geometry(&self, name: &str) -> PyResult<Geometry> {
let entity = gltf_geometry(self.entity, name)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Geometry { entity })
}

pub fn material(&self, name: &str) -> PyResult<Material> {
let entity = gltf_material(self.entity, name)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Material { entity })
}

pub fn mesh_names(&self) -> PyResult<Vec<String>> {
gltf_mesh_names(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn material_names(&self) -> PyResult<Vec<String>> {
gltf_material_names(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn camera(&self, index: usize) -> PyResult<()> {
gltf_camera(self.entity, index).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

pub fn light(&self, index: usize) -> PyResult<Light> {
let entity =
gltf_light(self.entity, index).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Light { entity })
}
}

#[pyfunction]
#[pyo3(pass_module)]
pub fn load_gltf(module: &Bound<'_, PyModule>, path: &str) -> PyResult<Gltf> {
let graphics = get_graphics(module)?;
let entity =
gltf_load(graphics.entity, path).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Gltf { entity })
}
18 changes: 13 additions & 5 deletions crates/processing_pyo3/src/graphics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::math::Vec4;
use bevy::prelude::Entity;
use processing::prelude::*;
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyDict};
Expand Down Expand Up @@ -26,7 +27,7 @@ impl Drop for Surface {
#[pyclass]
#[derive(Debug)]
pub struct Light {
entity: Entity,
pub(crate) entity: Entity,
}

#[pymethods]
Expand Down Expand Up @@ -62,7 +63,7 @@ impl Drop for Image {

#[pyclass(unsendable)]
pub struct Geometry {
entity: Entity,
pub(crate) entity: Entity,
}

#[pyclass]
Expand Down Expand Up @@ -132,7 +133,7 @@ impl Geometry {

#[pyclass(unsendable)]
pub struct Graphics {
entity: Entity,
pub(crate) entity: Entity,
pub surface: Surface,
}

Expand Down Expand Up @@ -357,8 +358,15 @@ impl Graphics {
}

pub fn perspective(&self, fov: f32, aspect: f32, near: f32, far: f32) -> PyResult<()> {
graphics_perspective(self.entity, fov, aspect, near, far)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
graphics_perspective(
self.entity,
fov,
aspect,
near,
far,
Vec4::new(0.0, 0.0, -1.0, -near),
)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}

#[allow(clippy::too_many_arguments)]
Expand Down
4 changes: 4 additions & 0 deletions crates/processing_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//! To allow Python users to create a similar experience, we provide module-level
//! functions that forward to a singleton Graphics object pub(crate) behind the scenes.
mod glfw;
mod gltf;
mod graphics;
pub(crate) mod material;

Expand All @@ -21,6 +22,7 @@ use pyo3::{
};
use std::ffi::{CStr, CString};

use gltf::Gltf;
use std::env;

#[pymodule]
Expand All @@ -30,6 +32,8 @@ fn processing(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_class::<Light>()?;
m.add_class::<Topology>()?;
m.add_class::<Material>()?;
m.add_class::<Gltf>()?;
m.add_function(wrap_pyfunction!(gltf::load_gltf, m)?)?;
m.add_function(wrap_pyfunction!(size, m)?)?;
m.add_function(wrap_pyfunction!(run, m)?)?;
m.add_function(wrap_pyfunction!(mode_3d, m)?)?;
Expand Down
2 changes: 2 additions & 0 deletions crates/processing_render/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ pub enum ProcessingError {
MaterialNotFound,
#[error("Unknown material property: {0}")]
UnknownMaterialProperty(String),
#[error("GLTF load error: {0}")]
GltfLoadError(String),
}
Loading
Loading