标签: bevy

如何手动创建带有顶点的网格?

我需要做什么才能创建具有以下顶点的网格:

let mut vertices : Vec<[f32; 3]> = Vec::new();

    vertices.push([0.0, 0.0, 0.0]);
    vertices.push([1.0, 2.0, 1.0]);
    vertices.push([2.0, 0.0, 0.0]);

Run Code Online (Sandbox Code Playgroud)

然后我想像这样生成一个 MeshBundle

commands
    .spawn(MeshBundle {
        mesh: mesh,
        transform: Transform::from_translation(Vec3::new(0.0, 0.0, 0.0)),
        ..Default::default()
    });
Run Code Online (Sandbox Code Playgroud)

rust bevy

18
推荐指数
1
解决办法
1万
查看次数

如何在没有 GPU 的情况下使用默认插件运行 Bevy 应用程序?

我的笔记本电脑没有专用 GPU。我在这台带有集成 GPU 的笔记本电脑上运行基本 OS 5 。当我尝试运行初学者的 Bevy 应用程序时,

use bevy::prelude::*;

fn main() {
    App::build()
        .add_default_plugins()
        .run();
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误 -

thread 'main' panicked at 'Unable to find a GPU! Make sure you have installed required drivers!', /home/actuday/.cargo/registry/src/github.com-1ecc6299db9ec823/bevy_wgpu-0.1.3/src/wgpu_renderer.rs:22:23
Run Code Online (Sandbox Code Playgroud)

我已经安装并升级了 GPU 的驱动程序。输出

glxinfo | grep "OpenGL"
Run Code Online (Sandbox Code Playgroud)

包含该行

OpenGL version string: 4.6 (Compatibility Profile) Mesa 20.3.0-devel (git-98e866c 2020-09-03 bionic-oibaf-ppa)
Run Code Online (Sandbox Code Playgroud)

OpenGL renderer string: Mesa Intel(R) UHD Graphics 620 (KBL GT2)
Run Code Online (Sandbox Code Playgroud)

我安装了 vulkan,错误现在替换为 -

     Running `target/debug/bevy_app`
WARNING: vallium/llvmpipe is not …
Run Code Online (Sandbox Code Playgroud)

rust bevy

9
推荐指数
0
解决办法
1202
查看次数

如何在Bevy系统中进行嵌套查询?

我正在与 Bevy 一起制作一个小boids玩具,每个 boids 的速度/加速度取决于它周围 boids 的位置和速度值。这意味着对于每个 boid,我想运行一些依赖于其他 boid 的某个子集的逻辑。

这看起来基本上可能是一个嵌套的 for 循环:

for boid in boids {
    for other_boid in boids {
        if boid.id == other_boid.id {
            continue;
        }
        
        if boid.position.distance_to(other_boid.position) < PERCEPTION_DISTANCE {
            // change boid's velocity / acceleration
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我不确定如何在 Bevy 中进行查询。假设我有一个系统move_boids

fn move_boids(mut query: Query<&Boid>) {
    for boid in &mut query.iter() {
        // I can't iterate over *other* boids here
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到类似这样的错误,因为我query在两个循环中都可变地借用:

error[E0499]: cannot borrow `query` as …
Run Code Online (Sandbox Code Playgroud)

bevy

8
推荐指数
1
解决办法
3075
查看次数

Bevy 如何根据参数的类型“范围”其系统?

Bevy是一个新的 Rust 游戏引擎和 ECS,它有一个功能,它根据参数的类型“确定”其系统。从它的文档

我们传递给“系统函数”的参数定义了系统在哪些实体上运行。在这种情况下,greet_people 将在具有 Person 和 Name 组件的所有实体上运行。

它看起来像这样:

struct Person;
struct Name(String);

fn greet_people(person: &Person, name: &Name) {
    println!("hello {}", name.0);
}
Run Code Online (Sandbox Code Playgroud)

Bevy 是如何做到这一点的?我以为我在某处读到 Rust 不支持这种方式的反射。

rust bevy

7
推荐指数
1
解决办法
675
查看次数

Rust/Bevy 中的点零调用计时器?

在 Bevy 书中使用了以下代码:

struct GreetTimer(Timer);

fn greet_people(
    time: Res<Time>, mut timer: ResMut<GreetTimer>, query: Query<&Name, With<Person>>) {
    // update our timer with the time elapsed since the last update
    // if that caused the timer to finish, we say hello to everyone
    if timer.0.tick(time.delta()).just_finished() {
        for name in query.iter() {
            println!("hello {}!", name.0);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

timer.0和电话在做什么name.0?这本书没有解决这个问题,我看到它Timer有一个勾选方法,那么既然已经是一个,.0那么这里在做什么?timerTimer

rust bevy

7
推荐指数
1
解决办法
593
查看次数

Bevy 的默认字体无法使用吗?

当尝试打印“Hello, world!”时 使用 Bevy 到屏幕上时,文本不会显示,直到我导入第三方字体文件并将其作为资源加载以用作组件TextStyle值中的字体值Text。在使用该字体之前,我没有明确指定该字体;我刚刚使用了来自 的值Default::default(),即WeakHandle<Font>(Id(97059ac6-c9ba-4da9-95b6-bed82c3ce198, 0))。这似乎意味着 Bevy 找到了错误的字体,或者至少是默认情况下无法使用的字体。这更有可能是我的系统提供的字体问题还是 Bevy 默认字体功能的问题?

这是一个最小的可重现示例:

use bevy::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup)
        .run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn_bundle(UiCameraBundle::default());
    commands.spawn_bundle(TextBundle {
        text: Text::with_section(
            "Hello, world!",
            TextStyle {
                font_size: 60.0,
                color: Color::WHITE,
                font: asset_server.load("FiraSans-Bold.ttf")
            },
            Default::default()
        ),
        ..Default::default()
    });
}
Run Code Online (Sandbox Code Playgroud)

该代码有效。当我将字体值从 更改为 时asset_server.load("FiraSans-Bold.ttf")Default::default()屏幕上没有显示任何内容。

game-development rust bevy

7
推荐指数
1
解决办法
1782
查看次数

如何在bevy游戏引擎中实现异步系统功能?

我目前正在开发一款基于 3D 体素的游戏,我希望能够根据玩家的运动生成程序块。

但在简单的系统中运行块生成会导致 FPS 大幅下降。

我已经尝试使用 来创建一个独立于其他所有内容运行的任务池std::sync::mpsc::channel,以生成块数据和网格,然后由正常的集群系统请求、缓冲,然后使用 生成commands.spawn(PbrBundle{...})

fn chunk_loader(
    pool: Res<Pool>,
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
    mut chunkmap: ResMut<ChunkMap>,
    mut buffer: ResMut<Buffer>, // buffer of chunk data
) {
    let mut chunks = pool.request_chunks();
    buffer.0.append(&mut chunks);

    for _ in 0..CHUNK_UPDATES_PER_FRAME {
        if let Some( (chunk, mesh) ) = buffer.0.pop() {
            chunkmap.map.insert([chunk.x, chunk.y, chunk.z], chunk);
    
            let mesh = mesh.clone();
        
            commands.spawn_bundle(PbrBundle {
                mesh: meshes.add(mesh),
                transform: Transform::from_matrix(Mat4::from_scale_rotation_translation(
                    Vec3::splat(1.0),
                    Quat::from_rotation_x(0.0),
                    Vec3::new((chunk.x * CHUNK_SIZE as …
Run Code Online (Sandbox Code Playgroud)

asynchronous rust bevy

7
推荐指数
1
解决办法
4314
查看次数

如何更改 SpriteComponent 的颜色?

我有一个查询系统,可以找到鼠标悬停在其中的对象。这不是一个按钮,但是,我想改变颜色。我不知道从哪里开始。我要查询什么属性以及如何更改它?目前,我有以下内容:

fn mouse_move(mut commands: Commands, cursor: Res<Cursor>, mut query: Query<(&Translation,&mut Sprite,&Box,&Name)>) 
{
    for (translation,mut sprite,_box,name) in &mut query.iter() {
        let cursor_tup = translate_cursor ((cursor.0,cursor.1));
        let cursor_vec = Vec3::new(cursor_tup.0,cursor_tup.1,0.0);
        if collides(cursor_vec,Vec2::new(1.0,1.0),translation.0,sprite.size) {
            println!("{}",name.0);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

rust bevy

6
推荐指数
1
解决办法
1489
查看次数

有没有办法在bevy游戏引擎中检测精灵的碰撞?

目前我使用两个物体(彗星和飞船)的位置来检测它们是否发生碰撞。

fn collision_detection(
    comets: Query<&Transform, With<Comet>>,
    mut ships: Query<(&Transform, &mut ShipStatus), With<Ship>>,
) {
    for comet in comets.iter() {
        for (ship, mut status) in ships.iter_mut() {
            if comet.translation.x < ship.translation.x + 80.0 && comet.translation.y < ship.translation.y + 80.0
            && comet.translation.x > ship.translation.x - 80.0 && comet.translation.y > ship.translation.y - 80.0 {
                status.dead = true;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但必须有更好的方法来做到这一点。

game-development rust bevy

6
推荐指数
1
解决办法
7424
查看次数

获取实体的所有组件

是否可以通过让实体处于锈迹中来获取组件列表?例如用于调试目的。

use bevy::prelude::*;
fn main()
{
    App::build()
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup.system())
        .add_system(first.system())
        .add_system(first_no_second.system())
        .add_system(first_and_second.system())
        .run()
}

fn setup(mut commands: Commands)
{
    commands.spawn().insert(FirstComponent(0.0));
    commands.spawn().insert(FirstComponent(1.0));
    commands.spawn().insert(SecondComponent::StateA);
    commands.spawn().insert(SecondComponent::StateB);
    commands.spawn().insert(SecondComponent::StateA).insert(FirstComponent(3.0));
    commands.spawn().insert(SecondComponent::StateB).insert(FirstComponent(4.0));
}

#[derive(Debug)]
struct FirstComponent(f32);

#[derive(Debug)]
enum SecondComponent
{
    StateA,
    StateB
}

fn first(query: Query<&FirstComponent>)
{
    for entity in query.iter()
    {
        println!("First: {:?}", entity)
    }
}

fn first_no_second(query: Query<&FirstComponent, Without<SecondComponent>>)
{
    for entity in query.iter()
    {
        println!("First without Second: {:?}", entity)
    }
}

fn first_and_second(query: Query<&FirstComponent, With<SecondComponent>>)
{
    for entity in query.iter()
    {
        println!("First with Second: …
Run Code Online (Sandbox Code Playgroud)

bevy

6
推荐指数
1
解决办法
7624
查看次数

标签 统计

bevy ×10

rust ×8

game-development ×2

asynchronous ×1