如何防止相机在 Bevy 中穿透墙壁?

问题描述

我使用 Bevy 制作了一个 3d 迷宫,但是当我尝试在迷宫中导航时,相机可以透过墙壁看到。从走廊往下看,前面的墙会出现,但当它靠近时,它就被切断了,我可以看到另一条走廊。

我正在为墙壁使用 Box 精灵,如下所示:

commands
    .spawn(PbrBundle {
        mesh: meshes.add(Mesh::from(shape::Box::new(1.0,1.0,0.1))),material: materials.add(Color::rgb(0.8,0.7,0.6).into()),transform: Transform::from_translation(Vec3::new(x,0.5,y+0.5)),..Default::default()
    });

我的相机是这样添加的:

commands
    .spawn(Camera3dBundle {
        transform: Transform::from_translation(Vec3::new(-2.0,0.0))
            .looking_at(Vec3::new(0.0,0.0),Vec3::unit_y()),..Default::default()
    })

我需要对透视图做些什么额外的事情来防止它看穿离它太近的物体吗?理想情况下,它永远不会看穿物体。

解决方法

事实证明,我的墙壁刚好间隔 1 个单位,默认的近场透视为 1.0。将相机视角的 near 参数降低到 0.01 可防止在距离墙壁太近时看穿墙壁。

在 main.rs 中:

use bevy_render::camera::PerspectiveProjection;

commands
    .spawn(Camera3dBundle {
        transform: Transform::from_translation(Vec3::new(-2.0,0.5,0.0))
            .looking_at(Vec3::new(0.0,0.0),Vec3::unit_y()),perspective_projection: PerspectiveProjection {
            near: 0.01,..Default::default()
        },..Default::default()
    })

在cargo.toml:

[dependencies]
bevy_render = "0.4"