4个版本 (破坏性更新)

0.6.0 2024年3月9日
0.5.0 2023年7月10日
0.4.0 2023年3月13日
0.3.0 2023年2月3日

#2107游戏开发

Download history 156/week @ 2024-03-09 4/week @ 2024-03-16 37/week @ 2024-03-30 7/week @ 2024-04-06

183 每月下载量

MIT/Apache

3MB
361

Bevy的导航网格

MIT/Apache 2.0 Release Doc Crate

⚠️ 此包已重命名为 vleue_navigator。有关更新和持续支持,请更改依赖项!

使用BevyPolyanya构建的导航网格。

map with many points finding their paths

查看WASM演示

用法

从gltf文件加载网格,然后从中构建PathMesh,并用于获取随机点之间的路径。

use bevy::{
    gltf::{Gltf, GltfMesh},
    prelude::*,
};

use bevy_pathmesh::{PathMesh, PathMeshPlugin};
use rand::Rng;

fn main() {
    App::new()
        .add_plugins((DefaultPlugins, PathMeshPlugin))
        .add_systems(Startup, load)
        .add_systems(Update, get_path)
        .run()
}

#[derive(Resource)]
struct Handles(Handle<Gltf>, Option<Handle<PathMesh>>);

fn load(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.insert_resource(Handles(asset_server.load("navmesh.glb"), None));
}

fn get_path(
    mut handles: ResMut<Handles>,
    gltfs: Res<Assets<Gltf>>,
    gltf_meshes: Res<Assets<GltfMesh>>,
    meshes: Res<Assets<Mesh>>,
    mut path_meshes: ResMut<Assets<PathMesh>>,
) {
    if handles.1.is_none() {
        // Get the gltf struct loaded from the file
        let Some(gltf) = gltfs.get(&handles.0) else {
            return
         };
        // Get the mesh called `navmesh`
        let Some(gltf_mesh) = gltf_meshes.get(&gltf.named_meshes["navmesh"]) else {
            return
         };
        // Get the actual mesh
        let Some(mesh) = meshes.get(&gltf_mesh.primitives[0].mesh) else {
            return
        };
        // Build a `PathMesh` from that mesh, then save it as an asset
        handles.1 = Some(path_meshes.add(PathMesh::from_bevy_mesh(mesh)));
    } else {
        // Get the path mesh, then search for a path
        let Some(path_mesh) = path_meshes.get(handles.1.as_ref().unwrap()) else {
            return
        };
        // Find two random point
        let from = Vec2::new(
            rand::thread_rng().gen_range(-50.0..50.0),
            rand::thread_rng().gen_range(-50.0..50.0),
        );
        let to = Vec2::new(
            rand::thread_rng().gen_range(-50.0..50.0),
            rand::thread_rng().gen_range(-50.0..50.0),
        );
        if let Some(path) = path_mesh.path(from, to) {
            info!("path from {} to {}: {:?}", from, to, path);
        } else {
            info!("no path between {} and {}", from, to)
        }
    }
}
Bevy bevy_pathmesh
0.13 0.6
0.11 0.5
0.10 0.4

依赖项

~50–91MB
~1.5M SLoC