master
mitchellhansen 9 months ago
parent c642974997
commit 42b2d3d50f

2
.gitignore vendored

@ -1 +1,3 @@
/target /target
*.lock
.idea/*

@ -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)] #[derive(Component)]
struct FjSolidWrapper{ struct FjSolidWrapper{
handle: fj_core::storage::Handle<Solid>, handle: fj_core::storage::Handle<Solid>,
} }
#[derive(Component, Debug)]
pub struct FjMeshWrapper {
pub mesh: FjMesh<Point<3>>,
pub handle: FjHandle<Solid>,
}
#[derive(Component)] #[derive(Component)]
struct FjConvertedFlag; struct FjConvertedFlag;
pub struct FjRenderPlugin;
use fj_core::{ impl Plugin for FjRenderPlugin {
algorithms::sweep::Sweep, fn build(&self, app: &mut App) {
objects::{Region, Sketch, Solid}, app
operations::{BuildRegion, BuildSketch, Insert, UpdateSketch}, .add_systems(Update, update_fj_model_system);
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;
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]> { fn generate_uv_mapping(fj_mesh: &FjMesh<Point<3>>) -> Vec<[f32; 2]> {
let mut uvs = Vec::new(); let mut uvs = Vec::new();
@ -95,103 +123,14 @@ pub fn convert_mesh(fj_mesh: &FjMesh<Point<3>>) -> Mesh {
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( fn update_fj_model_system(
mut commands: Commands, mut commands: Commands,
query: Query<(Entity, &FjSolidWrapper), Without<FjConvertedFlag>>, query: Query<(Entity, &FjMeshWrapper), Without<FjConvertedFlag>>,
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
for (entity, solid) in &query { for (entity, solid) in &query {
let bevy_mesh = convert_mesh(&solid.mesh);
let (mesh, fj_model) = fornjot_stuff::build_mesh();
let bevy_mesh = fornjot_convert_mesh::convert_mesh(&mesh);
commands.entity(entity).insert( commands.entity(entity).insert(
( (
PbrBundle { PbrBundle {
@ -200,7 +139,6 @@ fn update_fj_model_system(
transform: Transform::from_xyz(0.0, 0.0, 0.0), transform: Transform::from_xyz(0.0, 0.0, 0.0),
..default() ..default()
}, },
FjMeshWrapper { data: mesh, data2: fj_model },
FjConvertedFlag 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);
}
}

Loading…
Cancel
Save