bevy camera input, using the nokhwa crate
app.add_plugins((
DefaultPlugins,
BevyWebcamPlugin::default(),
));
app.add_systems(
Update,
setup_ui,
);
// ...
fn setup_ui(
mut commands: Commands,
stream: Res<WebcamStream>,
) {
commands.spawn(Camera2d);
commands.spawn((
ImageNode {
image: stream.frame.clone(),
..default()
},
Node {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
..default()
},
));
}- native camera capture via
nokhwa's native backends - threaded frame decoding on native targets, so the Bevy
Updatestage stays responsive - wasm32 (browser) capture via the DOM
MediaStreamTrackProcessorfeeding pixels into the exportedframe_inputbinding
- Native: frames are decoded on a dedicated worker thread and sent to the main Bevy world through a channel before being uploaded to the GPU.
- Wasm:
www/index.htmlacquires the webcam stream withgetUserMedia, processes frames withMediaStreamTrackProcessor, and forwards RGBA pixels into the wasm module viaframe_input. The Bevy plugin simply consumes those frames eachUpdate, so there is no blockingnokhwapath on the browser. - Camera selection on web: the browser decides which device backs the stream the user grants; the
CameraIndexsetting currently applies to native builds only.