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
513 changes: 480 additions & 33 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ glam = "0.30.9"
rayon = { version = "1.11.0", optional = true }

tobj = {version = "4.0.3", optional = true}
bevy = { version = "0.16.1", optional = true }
bevy = { version = "0.16.1", optional = true, features = ["wayland"] }
bevy_panorbit_camera = { version = "0.26.0", optional = true }
thiserror = "2.0.18"
serde = { version = "1.0.228", features = ["derive"], optional = true }
geo = "0.32"
geo-bevy = { version = "8", optional = true }

[features]
default = []
verbose = []
f32 = []
rayon = ["dep:rayon"]
serde = ["dep:serde", "glam/serde"]

bevy = [
"dep:tobj",
"dep:bevy",
"dep:bevy_panorbit_camera",
"dep:geo-bevy"
]

[[example]]
Expand Down
223 changes: 223 additions & 0 deletions examples/extrusion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
//--- Copyright (C) 2025 Saki Komikado <komietty@gmail.com>,
//--- This Source Code Form is subject to the terms of the Mozilla Public License v.2.0.

use std::f64::consts::PI;

use bevy::asset::RenderAssetUsages;
use bevy::color::palettes::css::*;
use bevy::pbr::wireframe::{Wireframe, WireframeColor, WireframePlugin};
use bevy::prelude::*;
use bevy::render::mesh::{PrimitiveTopology, VertexAttributeValues};
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin};
use boolmesh::{prelude::*, Real, Vec2};
use geo::{BooleanOps, Coord, MultiPolygon, Rect, Translate};

#[derive(Default, Reflect, GizmoConfigGroup)]
struct MyRoundGizmos {}

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(WireframePlugin::default())
.add_plugins(PanOrbitCameraPlugin)
.init_gizmo_group::<MyRoundGizmos>()
.add_systems(Startup, setup)
.run();
}

fn setup(
mut cmds: Commands,
mut mats: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
spawn_model(
&mut cmds,
&mut meshes,
&mut mats,
Vec3::new(-6.0, 0.0, 0.0),
|| {
let square = Rect::new(Coord { x: -0.5, y: -0.5 }, Coord { x: 0.5, y: 0.5 });
square.to_polygon().into()
},
|polygon| polygon.extrude(1.0, 1, 0.0, Vec2::new(1.0, 1.0)).unwrap(),
);

spawn_model(
&mut cmds,
&mut meshes,
&mut mats,
Vec3::new(-4.0, 0.0, 0.0),
|| {
let square =
Rect::new(Coord { x: -0.5, y: -0.5 }, Coord { x: 0.5, y: 0.5 }).to_polygon();
let inner_square =
Rect::new(Coord { x: -0.25, y: -0.25 }, Coord { x: 0.25, y: 0.25 }).to_polygon();
square.boolean_op(&inner_square, geo::OpType::Difference)
},
|polygon| polygon.extrude(1.0, 1, 0.0, Vec2::new(1.0, 1.0)).unwrap(),
);

spawn_model(
&mut cmds,
&mut meshes,
&mut mats,
Vec3::new(-2.0, 0.0, 0.0),
|| {
let square =
Rect::new(Coord { x: -0.5, y: -0.5 }, Coord { x: 0.5, y: 0.5 }).to_polygon();
let inner_square =
Rect::new(Coord { x: -0.25, y: -0.25 }, Coord { x: 0.25, y: 0.25 }).to_polygon();
square.boolean_op(&inner_square, geo::OpType::Difference)
},
|polygon| {
polygon
.extrude(1.0, 20, PI as Real, Vec2::new(1.0, 1.0))
.unwrap()
},
);

spawn_model(
&mut cmds,
&mut meshes,
&mut mats,
Vec3::new(0.0, 0.0, 0.0),
|| {
let square =
Rect::new(Coord { x: -0.5, y: -0.5 }, Coord { x: 0.5, y: 0.5 }).to_polygon();
let inner_square =
Rect::new(Coord { x: -0.25, y: -0.25 }, Coord { x: 0.25, y: 0.25 }).to_polygon();
square.boolean_op(&inner_square, geo::OpType::Difference)
},
|polygon| polygon.extrude(1.0, 1, 0.0, Vec2::new(0.5, 0.5)).unwrap(),
);

spawn_model(
&mut cmds,
&mut meshes,
&mut mats,
Vec3::new(2.0, 0.0, 0.0),
|| {
let square =
Rect::new(Coord { x: -0.5, y: -0.5 }, Coord { x: 0.5, y: 0.5 }).to_polygon();
let inner_square =
Rect::new(Coord { x: -0.25, y: -0.25 }, Coord { x: 0.25, y: 0.25 }).to_polygon();
square.boolean_op(&inner_square, geo::OpType::Difference)
},
|polygon| polygon.extrude(1.0, 1, 0.0, Vec2::new(0.0, 0.0)).unwrap(),
);

spawn_model(
&mut cmds,
&mut meshes,
&mut mats,
Vec3::new(4.0, 0.0, 0.0),
|| {
let square =
Rect::new(Coord { x: -0.5, y: -0.5 }, Coord { x: 0.5, y: 0.5 }).to_polygon();
let inner_square =
Rect::new(Coord { x: -0.25, y: -0.25 }, Coord { x: 0.25, y: 0.25 }).to_polygon();
square.boolean_op(&inner_square, geo::OpType::Difference)
},
|polygon| {
polygon
.extrude(1.0, 50, PI as Real, Vec2::new(0.0, 0.0))
.unwrap()
},
);

spawn_model(
&mut cmds,
&mut meshes,
&mut mats,
Vec3::new(6.0, 0.0, 0.0),
|| {
let square =
Rect::new(Coord { x: -0.5, y: -0.5 }, Coord { x: 0.5, y: 0.5 }).to_polygon();
let square2 =
Rect::new(Coord { x: -0.25, y: -0.25 }, Coord { x: 0.25, y: 0.25 }).to_polygon();
let square2_hole = Rect::new(
Coord {
x: -0.124,
y: -0.125,
},
Coord { x: 0.125, y: 0.125 },
)
.to_polygon();
let square2 = square2.difference(&square2_hole).translate(0.0, 2.0);

square.boolean_op(&square2, geo::OpType::Union)
},
|polygon| polygon.extrude(1.0, 1, 0.0, Vec2::new(1.0, 1.0)).unwrap(),
);

cmds.spawn((PointLight::default(), Transform::from_xyz(2., 5., 2.)));
cmds.spawn((
Transform::from_translation(Vec3::new(0., 2., 3.)),
PanOrbitCamera::default(),
));
}

fn spawn_model(
cmds: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>,
mats: &mut ResMut<Assets<StandardMaterial>>,
translation: Vec3,
build_polygon: impl FnOnce() -> MultiPolygon<Real>,
build_model: impl FnOnce(MultiPolygon<Real>) -> Manifold,
) {
let polygon = build_polygon();

let polygon_meshes = geo_bevy::multi_polygon_to_mesh(&polygon).unwrap();

for mesh in polygon_meshes {
let mut mesh = mesh.mesh;
if let VertexAttributeValues::Float32x3(values) =
mesh.attribute_mut(Mesh::ATTRIBUTE_NORMAL).unwrap()
{
values.iter_mut().for_each(|value| *value = [0.0, 0.0, 1.0]);
}

cmds.spawn((
Mesh3d(meshes.add(mesh).clone()),
MeshMaterial3d(mats.add(StandardMaterial { ..default() })),
Transform::from_translation(translation + Vec3::new(0.0, 0.0, 2.0)),
Wireframe,
WireframeColor {
color: BLACK.into(),
},
));
}

let model = build_model(polygon);

let mut m = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
);
let mut pos = vec![];
let mut vns = vec![];
for (fid, hs) in model.hs.chunks(3).enumerate() {
let p0 = model.ps[hs[0].tail];
let p1 = model.ps[hs[1].tail];
let p2 = model.ps[hs[2].tail];
let n = model.face_normals[fid];
pos.push([p0.x as f32, p0.y as f32, p0.z as f32]);
pos.push([p1.x as f32, p1.y as f32, p1.z as f32]);
pos.push([p2.x as f32, p2.y as f32, p2.z as f32]);
vns.push([n.x as f32, n.y as f32, n.z as f32]);
vns.push([n.x as f32, n.y as f32, n.z as f32]);
vns.push([n.x as f32, n.y as f32, n.z as f32]);
}
m.insert_attribute(Mesh::ATTRIBUTE_POSITION, pos);
m.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vns);

cmds.spawn((
Mesh3d(meshes.add(m).clone()),
MeshMaterial3d(mats.add(StandardMaterial { ..default() })),
Transform::from_translation(translation),
Wireframe,
WireframeColor {
color: BLACK.into(),
},
));
}
113 changes: 113 additions & 0 deletions examples/projection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//--- Copyright (C) 2025 Saki Komikado <komietty@gmail.com>,
//--- This Source Code Form is subject to the terms of the Mozilla Public License v.2.0.

use std::f64::consts::PI;

use bevy::asset::RenderAssetUsages;
use bevy::color::palettes::css::*;
use bevy::pbr::wireframe::{Wireframe, WireframeColor, WireframePlugin};
use bevy::prelude::*;
use bevy::render::mesh::{PrimitiveTopology, VertexAttributeValues};
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin};
use boolmesh::{compute_projection, prelude::*};

#[derive(Default, Reflect, GizmoConfigGroup)]
struct MyRoundGizmos {}

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(WireframePlugin::default())
.add_plugins(PanOrbitCameraPlugin)
.init_gizmo_group::<MyRoundGizmos>()
.add_systems(Startup, setup)
.run();
}

fn setup(
mut cmds: Commands,
mut mats: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
let model = "examples/models/double-torus.obj";
let (gargoyle, _) = tobj::load_obj(
model,
&tobj::LoadOptions {
..Default::default()
},
)
.expect("Failed to load obj file");

let m = &gargoyle[0].mesh;

let mut model = Manifold::new(
&m.positions.iter().map(|&v| v as f64).collect::<Vec<_>>(),
&m.indices.iter().map(|&v| v as usize).collect::<Vec<_>>(),
)
.unwrap();

model.translate(-6.0, 0.0, 0.0);
for _ in 0..5 {
model.rotate(PI / 4.0, 0.0, 0.0);
model.translate(2.0, 0.0, 0.0);

let mut m = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::default(),
);
let mut pos = vec![];
let mut vns = vec![];
for (fid, hs) in model.hs.chunks(3).enumerate() {
let p0 = model.ps[hs[0].tail];
let p1 = model.ps[hs[1].tail];
let p2 = model.ps[hs[2].tail];
let n = model.face_normals[fid];
pos.push([p0.x as f32, p0.y as f32, p0.z as f32]);
pos.push([p1.x as f32, p1.y as f32, p1.z as f32]);
pos.push([p2.x as f32, p2.y as f32, p2.z as f32]);
vns.push([n.x as f32, n.y as f32, n.z as f32]);
vns.push([n.x as f32, n.y as f32, n.z as f32]);
vns.push([n.x as f32, n.y as f32, n.z as f32]);
}
m.insert_attribute(Mesh::ATTRIBUTE_POSITION, pos);
m.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vns);

cmds.spawn((
Mesh3d(meshes.add(m).clone()),
MeshMaterial3d(mats.add(StandardMaterial { ..default() })),
Transform::default(),
Wireframe,
WireframeColor {
color: BLACK.into(),
},
));

let projection = compute_projection(&model).unwrap();
let projection_meshes = geo_bevy::multi_polygon_to_mesh(&projection).unwrap();

for mesh in projection_meshes {
let mut mesh = mesh.mesh;
if let VertexAttributeValues::Float32x3(values) =
mesh.attribute_mut(Mesh::ATTRIBUTE_NORMAL).unwrap()
{
values.iter_mut().for_each(|value| *value = [0.0, 0.0, 1.0]);
}

cmds.spawn((
Mesh3d(meshes.add(mesh).clone()),
MeshMaterial3d(mats.add(StandardMaterial { ..default() })),
Transform::from_translation(Vec3::new(0.0, 0.0, 2.0)),
Wireframe,
WireframeColor {
color: BLACK.into(),
},
));
}
}

cmds.spawn((PointLight::default(), Transform::from_xyz(2., 5., 2.)));
cmds.spawn((
Transform::from_translation(Vec3::new(0., 2., 3.)),
PanOrbitCamera::default(),
));
}
Loading