0

我面临一个实体已经存在的问题,但我想向它添加一个 PbrBundle。为了解决这个问题,我试图与 PbrBundle 一起生成一个子实体。希望能根据父类进行改造,但是改造部分好像不行。

fn spawn_parent(commands: &mut Commands) -> Entity {
    let entity = commands.spawn((Transform::from_translation(vec3(1.0, 0.0, 0.0)),)) ...

我希望代码绘制的线不会位于屏幕中间(应该向右翻译,更改父级似乎根本没有任何效果): 在此处输入图像描述

我从下面的代码中得到以下输出:

Spawned camera: 0v0
Spawned parent: 1v0
Spawned child: 2v0
0v0 changed global transform: Vec3(0.0, 0.0, 10.0)
2v0 changed parent: Parent(1v0)
1v0 changed children: Children([2v0])
2v0 changed global transform: Vec3(0.0, 0.0, 0.0)

编码:

use bevy::prelude::*;
use bevy::math::vec3;
use bevy::render::mesh::Indices;
use bevy::render::pipeline::PrimitiveTopology;

fn main() {
    App::build()
        .add_plugins(DefaultPlugins)
        .add_system(debug_global_transform.system())
        .add_system(debug_children.system())
        .add_system(debug_parent.system())
        .add_startup_system(setup.system())
        .run();
}

fn setup(commands: &mut Commands, meshes: ResMut<Assets<Mesh>>, materials: ResMut<Assets<StandardMaterial>>) {
    spawn_camera(commands);
    let parent_entity = spawn_parent(commands);
    spawn_child(commands, meshes, materials, parent_entity);
}

fn spawn_camera(commands: &mut Commands) {
    commands.spawn(Camera3dBundle {
        perspective_projection: Default::default(),
        transform: Transform::from_translation(Vec3::new(0.0, 0.0, 10.0))
            .looking_at(Vec3::zero(), Vec3::unit_y()),
        ..Default::default()
    });
    println!("Spawned camera: {:?}", commands.current_entity().unwrap());
}

fn spawn_parent(commands: &mut Commands) -> Entity {
    let entity = commands.spawn((Transform::from_translation(vec3(1.0, 0.0, 0.0)),)).current_entity().unwrap();
    println!("Spawned parent: {:?}", entity);
    entity
}

fn spawn_child(commands: &mut Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials: ResMut<Assets<StandardMaterial>>, parent_entity: Entity) {
    let mut mesh = Mesh::new(PrimitiveTopology::LineList);
    mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, vec![[0.0, 0.0, 0.0], [0.0, 1.0, 0.0]]);
    mesh.set_attribute(Mesh::ATTRIBUTE_NORMAL, vec![[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]);
    mesh.set_attribute(Mesh::ATTRIBUTE_UV_0, vec![[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]);
    mesh.set_indices(Some(Indices::U32(vec![0, 1])));
    let child_entity = commands.spawn(PbrBundle {
            mesh:  meshes.add(mesh),
            material: materials.add(Color::rgb(1.0, 1.0, 1.0).into()),
            // Default transform, let the parent decide where this is drawn
            ..Default::default()
        })
        //.with(Parent(parent_entity)) // Doesn't seem to work well either
        .current_entity()
        .unwrap();
    println!("Spawned child: {:?}", child_entity);
    commands.push_children(parent_entity, &[child_entity]);
}

fn debug_global_transform(query: Query<(Entity, &GlobalTransform), Changed<GlobalTransform>>) {
    query.iter()
        .for_each(|(entity, transform)| {
            println!("{:?} changed global transform: {:?}", entity, transform.translation);
        });
}

fn debug_children(children_query: Query<(Entity, &Children), Changed<Children>>) {
    for (entity, children) in children_query.iter() {
        println!("{:?} changed children: {:?}", entity, children);
    }
}

fn debug_parent(parent_query: Query<(Entity, &Parent), Changed<Parent>>) {
    for (entity, parent) in parent_query.iter() {
        println!("{:?} changed parent: {:?}", entity, parent);
    }
}

4

1 回答 1

0

似乎向父级添加 GlobalTransform 可以解决问题:)

fn spawn_parent(commands: &mut Commands) -> Entity {
    let entity = commands.spawn((GlobalTransform::default(), Transform::from_translation(vec3(1.0, 0.0, 0.0)))).current_entity().unwrap();
    println!("Spawned parent: {:?}", entity);
    entity
}
于 2021-03-21T02:53:48.573 回答