Replies: 2 comments 1 reply
-
I'd be interested in seeing a |
Beta Was this translation helpful? Give feedback.
-
I think I was able to get something working. I'll put my code below in case it's useful to someone later.
commands.entity(npc)
.with_children(|parent| {
parent.spawn((
SpatialBundle::default(),
VisibleEntities::default(),
Frustum::default(),
PerspectiveProjection::default(),
NPCCameraMarker,
));
});
/// System updating the visibility of entities each frame.
/// Camera-free version of `check_visibility` from bevy_render.
pub fn mark_visible_entities(
mut thread_queues: Local<Parallel<Vec<Entity>>>,
mut view_query: Query<(&mut VisibleEntities, &Frustum), With<NPCCameraMarker>>,
mut visible_aabb_query: Query<(
Entity,
&InheritedVisibility,
Option<&Aabb>,
&GlobalTransform,
Has<NoFrustumCulling>,
)>,
deterministic_rendering_config: Res<DeterministicRenderingConfig>,
) { ... }
pub(crate) fn update_perception(
query: Query<(&VisibleEntities, &NPCCameraMarker)>,
players: Query<&Player>,
mut commands: Commands
) {
let mut num_cams = 0;
for (visible_entities, camera_marker) in query.iter() {
for visible_entity in visible_entities.iter() {
if players.get(*visible_entity).is_ok() {
num_cams += 1;
}
}
}
info!("player visible to {} npc cams", num_cams);
} I'm not super confident this is correct but it seems to be working in my local. One note for wayfarers: this code doesn't handle occlusions, just frustum culling, so it's really just a starting point for an NPC perception system. In my own version I think I'll run some ray casting on the culled entities to actually make a final decision on visibility. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
bevy::render::view::check_visibility
seems perfect for what I'm trying to do, which is to attach a Camera to each NPC and then use theViewVisibility
component to detect what objects the NPC can see.This seems to be almost working, but there's an issue I can't figure out. Basically, I get some warnings like
Another issue: with multiple Cameras rendering to the same RenderTarget, the render results look bad, and there's also a bunch of unnecessary rendering happening (I don't need to actually render the NPC perspective, just run object culling).
How can I add Cameras to the NPCs so that I get ViewVisibility without any further rendering? I didn't see a DummyrenderTarget or anything like that.
Beta Was this translation helpful? Give feedback.
All reactions