Compare commits
2 Commits
c642974997
...
0.0.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 783d4999a7 | |||
| 42b2d3d50f |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
/target
|
||||
*.lock
|
||||
.idea/*
|
||||
@@ -16,3 +16,4 @@ fj-math = "0.47.0"
|
||||
fj-interop = "0.47.0"
|
||||
fj-core = "0.47.0"
|
||||
fj-export = "0.47.0"
|
||||
rand = "0.8.5"
|
||||
|
||||
294
src/lib.rs
294
src/lib.rs
@@ -1,34 +1,80 @@
|
||||
// Importing Bevy ECS components and systems
|
||||
use bevy::ecs::component::Component;
|
||||
use bevy::ecs::system::{Commands, Query};
|
||||
use bevy::ecs::entity::Entity;
|
||||
use bevy::ecs::query::Without;
|
||||
use bevy::ecs::system::ResMut;
|
||||
|
||||
// Importing Bevy assets and rendering related components
|
||||
use bevy::prelude::{Mesh, shape};
|
||||
use bevy::render::mesh::{Indices, PrimitiveTopology};
|
||||
use bevy::asset::Assets;
|
||||
use bevy::pbr::{PbrBundle, StandardMaterial};
|
||||
use bevy::render::color::Color;
|
||||
use bevy::transform::components::Transform;
|
||||
use bevy::utils::default;
|
||||
|
||||
// Importing Bevy application related components
|
||||
use bevy::app::{App, Plugin, Update};
|
||||
|
||||
// Importing Fj-core functionalities
|
||||
use fj_core::algorithms::approx::Tolerance;
|
||||
use fj_core::algorithms::bounding_volume::BoundingVolume;
|
||||
use fj_core::algorithms::sweep::Sweep;
|
||||
use fj_core::algorithms::triangulate::Triangulate;
|
||||
use fj_core::objects::{Cycle, Region, Shell, Sketch, Solid};
|
||||
use fj_core::operations::{BuildCycle, BuildRegion, BuildSketch, Insert, Reverse, UpdateRegion, UpdateSketch};
|
||||
use fj_core::services::Services;
|
||||
use fj_core::storage::Handle as FjHandle;
|
||||
|
||||
// Importing Fj-interop mesh
|
||||
use fj_interop::mesh::Mesh as FjMesh;
|
||||
|
||||
// Importing Fj-math and other standard functionalities
|
||||
use fj_math::{Aabb, Point, Scalar, Vector};
|
||||
use std::ops::Deref;
|
||||
use bevy::math::Vec3;
|
||||
use fj_core::geometry::curve::GlobalPath;
|
||||
use rand::Rng;
|
||||
|
||||
trait ToVec3 {
|
||||
fn to_vec3(&self) -> bevy::prelude::Vec3;
|
||||
}
|
||||
|
||||
impl ToVec3 for fj_math::Point<3> {
|
||||
fn to_vec3(&self) -> bevy::prelude::Vec3 {
|
||||
bevy::prelude::Vec3::new(self.x.into_f32(), self.y.into_f32(), self.z.into_f32())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToVec3 for fj_math::Vector<3> {
|
||||
fn to_vec3(&self) -> bevy::prelude::Vec3 {
|
||||
bevy::prelude::Vec3::new(self.x.into_f32(), self.y.into_f32(), self.z.into_f32())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct FjSolidWrapper{
|
||||
handle: fj_core::storage::Handle<Solid>,
|
||||
}
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct FjMeshWrapper {
|
||||
pub mesh: FjMesh<Point<3>>,
|
||||
pub handle: FjHandle<Solid>,
|
||||
}
|
||||
|
||||
#[derive(Component)]
|
||||
struct FjConvertedFlag;
|
||||
|
||||
pub struct FjRenderPlugin;
|
||||
|
||||
use fj_core::{
|
||||
algorithms::sweep::Sweep,
|
||||
objects::{Region, Sketch, Solid},
|
||||
operations::{BuildRegion, BuildSketch, Insert, UpdateSketch},
|
||||
services::Services,
|
||||
storage::Handle,
|
||||
};
|
||||
use fj_core::algorithms::approx::Tolerance;
|
||||
use fj_core::algorithms::bounding_volume::BoundingVolume;
|
||||
use fj_core::algorithms::triangulate::Triangulate;
|
||||
use fj_core::objects::{Cycle, Shell};
|
||||
use fj_core::operations::{BuildCycle, Reverse, UpdateRegion};
|
||||
use fj_interop::mesh::Mesh;
|
||||
use fj_math::{Aabb, Point, Scalar, Vector};
|
||||
use std::ops::Deref;
|
||||
impl Plugin for FjRenderPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.add_systems(Update, update_fj_model_system);
|
||||
}
|
||||
}
|
||||
|
||||
use bevy::prelude::Mesh;
|
||||
use bevy::render::mesh::{Indices, PrimitiveTopology};
|
||||
use fj_interop::mesh::Mesh as FjMesh;
|
||||
use fj_math::Point;
|
||||
|
||||
fn generate_uv_mapping(fj_mesh: &FjMesh<Point<3>>) -> Vec<[f32; 2]> {
|
||||
let mut uvs = Vec::new();
|
||||
@@ -44,7 +90,6 @@ fn generate_uv_mapping(fj_mesh: &FjMesh<Point<3>>) -> Vec<[f32; 2]> {
|
||||
uvs.push([x, y]);
|
||||
uvs.push([x, y]);
|
||||
}
|
||||
|
||||
uvs
|
||||
}
|
||||
|
||||
@@ -95,103 +140,14 @@ pub fn convert_mesh(fj_mesh: &FjMesh<Point<3>>) -> Mesh {
|
||||
mesh
|
||||
}
|
||||
|
||||
|
||||
fn model(x: f64, y: f64, z: f64, services: &mut Services) -> Handle<Solid> {
|
||||
let sketch = Sketch::empty()
|
||||
.add_region(
|
||||
Region::polygon(
|
||||
[
|
||||
[-x / 2., -y / 2.],
|
||||
[x / 2., -y / 2.],
|
||||
[x / 2., y / 2.],
|
||||
[-x / 2., y / 2.],
|
||||
],
|
||||
services,
|
||||
)
|
||||
.add_interiors(
|
||||
[
|
||||
Cycle::polygon(
|
||||
[
|
||||
[0.1, 0.1],
|
||||
[0.1, 2.0],
|
||||
[2.0, 2.0],
|
||||
[2.0, 0.1],
|
||||
],
|
||||
services,
|
||||
).insert(services),
|
||||
// Cycle::circle([0.1,0.75], 0.05, services).reverse(services).insert(services),
|
||||
// MinnesotaCycle::circle([0.1,0.75], 0.05, services).reverse(services).insert(services)
|
||||
]
|
||||
)
|
||||
.insert(services),
|
||||
)
|
||||
// .add_region(
|
||||
// Region::polygon(
|
||||
// [
|
||||
// [1.5, 2.25],
|
||||
// [3.0, 2.25],
|
||||
// [3.0, 4.5],
|
||||
// [1.5, 4.5],
|
||||
// ],
|
||||
// services,
|
||||
// ).insert(services)
|
||||
// )
|
||||
.insert(services);
|
||||
|
||||
let surface = services.objects.surfaces.xy_plane();
|
||||
|
||||
|
||||
|
||||
let path = Vector::from([0., 0., z]);
|
||||
|
||||
// let mut iter = sketch.faces(surface, services).into_iter();
|
||||
// let face = iter.next().unwrap();
|
||||
// let face = iter.next().unwrap();
|
||||
|
||||
// services.drop_and_validate();
|
||||
|
||||
// face.sweep(path, services)
|
||||
|
||||
// let surface = services.objects.surfaces.xy_plane();
|
||||
|
||||
(sketch, surface).sweep(path, services)
|
||||
}
|
||||
|
||||
|
||||
pub fn build_mesh() -> (Mesh<Point<3>>, Handle<Solid>) {
|
||||
|
||||
let mut services = Services::new();
|
||||
|
||||
let model = model(4., 8., (1.0 / 12.0 * 0.75), &mut services);
|
||||
let aabb = model.aabb().unwrap_or(Aabb {
|
||||
min: Point::origin(),
|
||||
max: Point::origin(),
|
||||
});
|
||||
|
||||
println!("{:?}", model);
|
||||
|
||||
let mut min_extent = Scalar::MAX;
|
||||
for extent in aabb.size().components {
|
||||
if extent > Scalar::ZERO && extent < min_extent {
|
||||
min_extent = extent;
|
||||
}
|
||||
}
|
||||
let tolerance = min_extent / Scalar::from_f64(1000.);
|
||||
let tolerance = Tolerance::from_scalar(tolerance).unwrap();
|
||||
|
||||
((model.deref(), tolerance).triangulate(), model)
|
||||
}
|
||||
|
||||
fn update_fj_model_system(
|
||||
mut commands: Commands,
|
||||
query: Query<(Entity, &FjSolidWrapper), Without<FjConvertedFlag>>,
|
||||
query: Query<(Entity, &FjMeshWrapper), Without<FjConvertedFlag>>,
|
||||
mut meshes: ResMut<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
for (entity, solid) in &query {
|
||||
|
||||
let (mesh, fj_model) = fornjot_stuff::build_mesh();
|
||||
let bevy_mesh = fornjot_convert_mesh::convert_mesh(&mesh);
|
||||
let bevy_mesh = convert_mesh(&solid.mesh);
|
||||
commands.entity(entity).insert(
|
||||
(
|
||||
PbrBundle {
|
||||
@@ -200,20 +156,114 @@ fn update_fj_model_system(
|
||||
transform: Transform::from_xyz(0.0, 0.0, 0.0),
|
||||
..default()
|
||||
},
|
||||
FjMeshWrapper { data: mesh, data2: fj_model },
|
||||
FjConvertedFlag
|
||||
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct FjRenderPlugin;
|
||||
|
||||
impl Plugin for FjRenderPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
app
|
||||
.add_systems(Update, update_fj_model_system);
|
||||
add_debug_info_to_entity(&mut commands, solid, &mut meshes, &mut materials);
|
||||
}
|
||||
}
|
||||
|
||||
fn add_debug_info_to_entity(mut commands: &mut Commands,
|
||||
fj_model: &FjMeshWrapper,
|
||||
mut meshes: &mut ResMut<Assets<Mesh>>,
|
||||
mut materials: &mut ResMut<Assets<StandardMaterial>>
|
||||
) {
|
||||
|
||||
for shell in fj_model.handle.shells() {
|
||||
println!("{:?}. shell", shell);
|
||||
for face in shell.faces() {
|
||||
println!("{:?}. face", face);
|
||||
let surface = face.surface();
|
||||
let geom = surface.geometry();
|
||||
let geom_sweep_vector = geom.v;
|
||||
match geom.u {
|
||||
GlobalPath::Circle(x) => {
|
||||
x.aabb();
|
||||
}
|
||||
GlobalPath::Line(x) => {
|
||||
let origin = x.origin();
|
||||
let direction = x.direction();
|
||||
let opposite_corner = Vec3::new(
|
||||
origin.x.into_f32() + direction.x.into_f32() + 0.01,
|
||||
origin.y.into_f32() + direction.y.into_f32() + 0.01,
|
||||
origin.z.into_f32() + direction.z.into_f32() + 0.01,
|
||||
);
|
||||
let mut rng = rand::thread_rng();
|
||||
let red: f32 = rng.gen_range(0.0..1.0);
|
||||
let green: f32 = rng.gen_range(0.0..1.0);
|
||||
let blue: f32 = rng.gen_range(0.0..1.0);
|
||||
commands.spawn(( // line on base of sweep
|
||||
PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Box::from_corners(opposite_corner.into(), origin.to_vec3()))),
|
||||
material: materials.add(Color::rgb(red, green, blue).into()),
|
||||
transform: Transform::from_xyz(0.0, 0.0, 0.0),
|
||||
..default()
|
||||
},
|
||||
// RaycastPickTarget::default(), // <- Needed for the raycast backend.
|
||||
// PickableBundle::default() // <- This one too
|
||||
));
|
||||
commands.spawn(( // line on top of sweep, just offset by the sweep vector
|
||||
PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Box::from_corners(opposite_corner.into(), origin.to_vec3()))),
|
||||
material: materials.add(Color::rgb(red, green, blue).into()),
|
||||
transform: Transform::from_xyz(geom_sweep_vector.x.into_f32(), geom_sweep_vector.y.into_f32(), geom_sweep_vector.z.into_f32()),
|
||||
..default()
|
||||
},
|
||||
// RaycastPickTarget::default(), // <- Needed for the raycast backend.
|
||||
// PickableBundle::default() // <- This one too
|
||||
));
|
||||
commands.spawn(( // line following the sweep
|
||||
PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Box::from_corners(origin.to_vec3() + geom_sweep_vector.to_vec3(), origin.to_vec3() + 0.01))),
|
||||
material: materials.add(Color::rgb(red, green, blue).into()),
|
||||
transform: Transform::from_xyz(0.0, 0.0, 0.0),
|
||||
..default()
|
||||
},
|
||||
// RaycastPickTarget::default(), // <- Needed for the raycast backend.
|
||||
// PickableBundle::default() // <- This one too
|
||||
));
|
||||
commands.spawn(( // vertex
|
||||
PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.025 })),
|
||||
material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()),
|
||||
transform: Transform::from_xyz(origin.to_xyz().x.into_f32(), origin.to_xyz().y.into_f32(), origin.to_xyz().z.into_f32()),
|
||||
..default()
|
||||
},
|
||||
// RaycastPickTarget::default(), // <- Needed for the raycast backend.
|
||||
// PickableBundle::default() // <- This one too
|
||||
));
|
||||
commands.spawn(( // swept vertex
|
||||
PbrBundle {
|
||||
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.025 })),
|
||||
material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()),
|
||||
transform: Transform::from_xyz(
|
||||
origin.to_xyz().x.into_f32() + geom_sweep_vector.to_vec3().x,
|
||||
origin.to_xyz().y.into_f32() + geom_sweep_vector.to_vec3().y,
|
||||
origin.to_xyz().z.into_f32() + geom_sweep_vector.to_vec3().z,
|
||||
),
|
||||
// transform: Transform::from_scale(origin.to_vec3() + geom_sweep_vector.to_vec3()),
|
||||
..default()
|
||||
},
|
||||
// RaycastPickTarget::default(), // <- Needed for the raycast backend.
|
||||
// PickableBundle::default(), // <- This one too
|
||||
|
||||
// I want to insert a component when dragging starts, that contains
|
||||
// data about the exact ray hit location.
|
||||
// On::<Pointer<DragStart>>::run(run),
|
||||
// On::<Pointer<DragStart>>::target_insert(DragCaster::default()),
|
||||
// On::<Pointer<Drag>>::target_component_mut::<DragCaster>(|drag, mut caster| {
|
||||
// caster.hit_location = drag.hit.position.unwrap();
|
||||
//
|
||||
// // (*transform).translation = transform.translation + Vec3::new(-drag.delta.x / 100.0, 0.0, drag.delta.y / 100.0);
|
||||
// }),
|
||||
// On::<Pointer<Drag>>::target_component_mut::<DragCaster>(|drag, mut caster| {
|
||||
// drag.distance
|
||||
// // (*transform).translation = transform.translation + Vec3::new(-drag.delta.x / 100.0, 0.0, drag.delta.y / 100.0);
|
||||
// }),
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user