From 42b2d3d50fe0805b64955dd9f35128e650ef2b5c Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Wed, 21 Feb 2024 21:39:54 -0800 Subject: [PATCH] works --- .gitignore | 2 + src/lib.rs | 168 ++++++++++++++++------------------------------------- 2 files changed, 51 insertions(+), 119 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..3a044e0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target +*.lock +.idea/* \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 1360013..ef5549a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,34 +1,62 @@ +// 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; +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; #[derive(Component)] struct FjSolidWrapper{ handle: fj_core::storage::Handle, } +#[derive(Component, Debug)] +pub struct FjMeshWrapper { + pub mesh: FjMesh>, + pub handle: FjHandle, +} + #[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>) -> Vec<[f32; 2]> { let mut uvs = Vec::new(); @@ -95,103 +123,14 @@ pub fn convert_mesh(fj_mesh: &FjMesh>) -> Mesh { mesh } - -fn model(x: f64, y: f64, z: f64, services: &mut Services) -> Handle { - 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>, Handle) { - - 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>, + query: Query<(Entity, &FjMeshWrapper), Without>, mut meshes: ResMut>, mut materials: ResMut>, ) { 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,7 +139,6 @@ fn update_fj_model_system( transform: Transform::from_xyz(0.0, 0.0, 0.0), ..default() }, - FjMeshWrapper { data: mesh, data2: fj_model }, FjConvertedFlag )); @@ -208,13 +146,5 @@ fn update_fj_model_system( } -pub struct FjRenderPlugin; - -impl Plugin for FjRenderPlugin { - fn build(&self, app: &mut App) { - app - .add_systems(Update, update_fj_model_system); - } -}