【rust/bevy】使用points构造ConvexMesh

说在前面

  • 操作系统:win11
  • rust版本:rustc 1.77.0-nightly
  • bevy版本:0.12
  • github:这里

问题提出

  • three.js中,可以通过使用ConvexGeometry从给定的三维点集合生成凸包(Convex Hull)

    import { ConvexGeometry } from 'three/addons/geometries/ConvexGeometry.js';
    

    例如:

    const geometry = new ConvexGeometry( points );
    const material = new THREE.MeshBasicMaterial( { color: 0x00ff00 } );
    const mesh = new THREE.Mesh( geometry, material );
    scene.add( mesh );
    

    在这里插入图片描述

  • 但是在bevy中如何实现呢?

Rapier

  • 在当前bevy的版本中,并没有类似的实现,目前支持的shape也比three.js少:
    • Box
    • Capsule
    • Circle
    • Cube
    • Cylinder
    • Icosphere
    • Plane
    • Quad
    • RegularPolygon
    • Torus
    • UVSphere
  • 所以,如果要构造ConvexHull,只能借助第三方库(或者自己实现,可以参考three.js实现)
  • 这里我们借助一个物理库rapier来实现
  • 关于rapier
    这里

具体实现

  • 首先我们通过点集构造ConvexHullrapier可以直接完成这一步:

    let collider = Collider::convex_hull(&points);
    

    但是该方法构造的是一个Collider,实际上是无法直接当成Mesh使用的

  • 然后我们将其转换成ConvexPolyhedron

    let convex = c.as_convex_polyhedron().unwrap();
    
  • 该结构中包含一个凸包的所有属性,例如点、边、面等,事实上,它提供了一个方法,能够直接转换成TriangleMesh所需要的数据:

    impl ConvexPolyhedron {
        /// Discretize the boundary of this convex polyhedron as a triangle-mesh.
        pub fn to_trimesh(&self) -> (Vec<Point3<Real>>, Vec<[u32; 3]>) {
            let mut indices = Vec::new();
    
            for face in self.faces() {
                let i1 = face.first_vertex_or_edge;
                let i2 = i1 + face.num_vertices_or_edges;
                let first_id = self.vertices_adj_to_face()[i1 as usize] as u32;
    
                for idx in self.vertices_adj_to_face()[i1 as usize + 1..i2 as usize].windows(2) {
                    indices.push([first_id, idx[0] as u32, idx[1] as u32]);
                }
            }
    
            (self.points().to_vec(), indices)
        }
    }
    

    但是不包含normal数据,导致光照对其没有作用
    在这里插入图片描述

  • 所以我们需要自己实现一个相似的方法,首先我们来看看测试数据,实际上它有20个点,共12个面,每个面是一个正五边形,所以使用TriangleMesh来表达的话,一共是36个三角形

  • 再让我们来看看bevy中构造一个TriangleMesh需要哪些数据:

    fn create_simple_parallelogram() -> Mesh {
        // Create a new mesh using a triangle list topology, where each set of 3 vertices composes a triangle.
        Mesh::new(PrimitiveTopology::TriangleList)
            // 顶点数据
            .with_inserted_attribute(
                Mesh::ATTRIBUTE_POSITION,
                vec![[0.0, 0.0, 0.0], [1.0, 2.0, 0.0], [2.0, 2.0, 0.0], [1.0, 0.0, 0.0]]
            )
            // uv数据 在本文中我们并不需要
            .with_inserted_attribute(
                Mesh::ATTRIBUTE_UV_0,
                vec![[0.0, 1.0], [0.5, 0.0], [1.0, 0.0], [0.5, 1.0]]
            )
            // 法线
            .with_inserted_attribute(
                Mesh::ATTRIBUTE_NORMAL,
                vec![[0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0], [0.0, 0.0, 1.0]]
            )
            // 三角形的顶点索引
            .with_indices(Some(Indices::U32(vec![
                0, 3, 1,
                1, 3, 2
            ])))
    }
    
  • 对比我们需要的数据,以及to_trimesh方法,发现我们少了normal数据,不过这个数据在face中就有,那么所有条件就具备了

  • 完整代码

    let plist: Vec<f32> = vec![0., 3.568220853805542, 9.341723442077637, 5.773502826690674, 5.773502826690674, 5.773502826690674, -5.773502826690674, 5.773502826690674, 5.773502826690674, 3.568220853805542, 9.341723442077637, 0., -3.568220853805542, 9.341723442077637, 0., 9.341723442077637, 0., 3.568220853805542, 9.341723442077637, 0., -3.568220853805542, 5.773502826690674, 5.773502826690674, -5.773502826690674, 5.773502826690674, -5.773502826690674, -5.773502826690674, 0., -3.568220853805542, -9.341723442077637, 0., 3.568220853805542, -9.341723442077637, -5.773502826690674, -5.773502826690674, -5.773502826690674, -9.341723442077637, 0., -3.568220853805542, -5.773502826690674, 5.773502826690674, -5.773502826690674, -3.568220853805542, -9.341723442077637, 0., -5.773502826690674, -5.773502826690674, 5.773502826690674, -9.341723442077637, 0., 3.568220853805542, 0., -3.568220853805542, 9.341723442077637, 3.568220853805542, -9.341723442077637, 0., 5.773502826690674, -5.773502826690674, 5.773502826690674];
    let points: Vec<Vec3> = plist
        .array_chunks()
        .into_iter()
        .map(|&v: &[f32; 3]| Vec3::from_array(v))
        .collect();
    // 通过点集构造convex hull
    let collider = Collider::convex_hull(&points);
    if let Some(c) = collider {
        let convex = c.as_convex_polyhedron().unwrap();
        // 取convex hull的所有面
        let faces = convex.raw.faces();
        // 取点集
        let points = convex.raw.points();
        // 取映射关系
        let face_to_vertices = convex.raw.vertices_adj_to_face();
    	
        let mut positions = Vec::new();
        // 法向量 用于处理光照
        let mut normals = Vec::new();
        let mut indices = Vec::new();
        // 遍历所有的面
        for face in faces {
            let i1 = face.first_vertex_or_edge;
            let i2 = i1 + face.num_vertices_or_edges;
    
            for idx in i1..i2 {
                let point = points[face_to_vertices[idx as usize] as usize];
                // 重新构造点集 points是原始点集
                positions.push([point.x, point.y, point.z]);
                // 面上的所有点的朝向与面相同
                normals.push([face.normal.x, face.normal.y, face.normal.z]);
            }
    
            for idx in i1 + 1..i2 - 1 {
            	// 构造顶点索引
                indices.push([i1, idx as u32, idx + 1 as u32]);
            }
        }
    	// 构造Mesh
        let mesh = Mesh::new(PrimitiveTopology::TriangleList)
            .with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions)
            .with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals)
            .with_indices(Some(Indices::U32(indices.concat())));
    
        commands.spawn(PbrBundle {
            mesh: meshes.add(mesh),
            material: materials.add(Color::rgb_u8(124, 144, 255).into()),
            transform: Transform::from_xyz(0.0, 1., 0.0).with_scale(Vec3::new(0.1, 0.1, 0.1)),
            ..default()
        });
    }
    
  • 结果(gif)
    在这里插入图片描述

参考

  • 22
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Rust的async/await是一种异步编程模式,可以在代码中使用关键字async和await来创建和等待异步任务。 以下是使用async/await的基本步骤: 1. 在函数签名中添加async关键字,表示该函数是一个异步函数。 2. 在函数体内使用await关键字等待异步任务的结果。await关键字会暂停当前函数的执行,直到异步任务完成并返回结果。 3. 在异步函数内部,可以使用异步API来创建和启动异步任务。 例如,下面是一个简单的使用async/await的例子: ```rust async fn do_something_async() -> String { // 创建一个异步任务,等待1秒后返回结果 let result = tokio::time::delay_for(Duration::from_secs(1)).await; // 返回一个字符串 return "Hello, World!".to_string(); } fn main() { // 创建一个异步运行时 let rt = tokio::runtime::Runtime::new().unwrap(); // 在异步运行时中执行异步函数 let result = rt.block_on(async { // 等待异步任务完成并返回结果 let result = do_something_async().await; // 返回异步任务的结果 return result; }); // 输出结果 println!("{}", result); } ``` 在这个例子中,我们首先定义了一个异步函数do_something_async(),它创建了一个异步任务,等待1秒后返回一个字符串。然后,在main函数中,我们创建了一个异步运行时,并使用block_on函数在异步运行时中执行异步函数do_something_async(),并等待它的结果。最后,我们输出异步任务的结果。 需要注意的是,Rust的async/await需要配合异步运行时一起使用,例如上述例子中使用的tokio运行时。异步运行时提供了异步任务的执行环境和调度器,负责管理异步任务的执行和调度。因此,在使用async/await时需要选择一个合适的异步运行时,并将异步函数放入异步运行时中执行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值