Here's a Bevy (Rust game engine) program that:
- Loads a 3D model (e.g.,
.glb or .gltf),
- Normalizes it to the origin and uniform size,
- Renders it from multiple camera angles,
- Saves screenshots of each angle.
✅ Features
- Model centering and scaling (auto-normalization).
- Multiple camera angles.
- Screenshot output using Bevy's render pipeline.
🦀 Rust Code (Bevy)
Make sure your Cargo.toml includes:
[package]
name = "model_screenshotter"
version = "0.1.0"
edition = "2021"
[dependencies]
bevy = { version = "0.13", features = ["jpeg", "ktx2", "hdr"] }
bevy_scene_hook = "0.3" # Optional helper for scene post-processing
📁 Structure
We'll create:
- A main file that loads the model and launches cameras.
- Screenshots are triggered after the scene is loaded.
🔧 main.rs
use bevy::{
prelude::*,
render::camera::RenderTarget,
window::WindowPlugin,
asset::{LoadedAsset, AssetApp},
};
use bevy::render::render_resource::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages};
use std::f32::consts::PI;
const MODEL_PATH: &str = "model.glb#Scene0"; // Replace with your model path
const OUTPUT_RESOLUTION: UVec2 = UVec2::new(512, 512); // Screenshot size
const ANGLES: usize = 8; // Number of views
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
resolution: (OUTPUT_RESOLUTION.x as f32, OUTPUT_RESOLUTION.y as f32).into(),
..default()
}),
..default()
}))
.init_asset::<SceneBoundingBox>()
.register_asset_loader(SceneBoundingBoxLoader)
.add_systems(Startup, setup)
.add_systems(Update, (
capture_screenshots.run_if(resource_exists::<CameraDone>()),
))
.run();
}
#[derive(Resource)]
struct NormalizedScene {
entity: Entity,
}
#[derive(Resource)]
struct CameraDone;
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
commands.spawn(DirectionalLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// Load 3D model
let scene_handle = asset_server.load(MODEL_PATH);
commands.spawn(SceneBundle {
scene: scene_handle.clone(),
..default()
});
// Camera container (to be moved later)
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 3.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
})
.insert(Name::new("MainCamera"));
}
/// Screenshot all angles after normalization
fn capture_screenshots(
mut commands: Commands,
mut camera_query: Query<&mut Transform, With<Camera>>,
mut done: ResMut<Option<CameraDone>>,
mut counter: Local<usize>,
time: Res<Time>,
asset_server: Res<AssetServer>,
scene_query: Query<Entity, With<Handle<Scene>>>,
mut meshes: ResMut<Assets<Mesh>>,
scene_entities: Query<(&Handle<Scene>, &GlobalTransform), Without<Camera>>,
) {
// Screenshot from ANGLES different horizontal angles:
if *counter >= ANGLES {
return;
}
let radius = 3.0;
let angle = 2.0 * PI * (*counter as f32) / ANGLES as f32;
let cam_x = radius * angle.cos();
let cam_z = radius * angle.sin();
if let Ok(mut cam_tf) = camera_query.get_single_mut() {
cam_tf.translation = Vec3::new(cam_x, 1.5, cam_z);
cam_tf.look_at(Vec3::ZERO, Vec3::Y);
}
// Save screenshot every N frames (simple timer)
if time.elapsed_seconds() > 0.5 {
let path = format!("screenshot_{}.png", *counter);
println!("📸 Capturing screenshot: {path}");
let _ = std::process::Command::new("cargo")
.arg("run")
.arg("--bin")
.arg("screenshot-saver")
.arg(&path)
.status();
*counter += 1;
}
if *counter >= ANGLES {
println!("✅ Done capturing screenshots");
}
}
📂 Model Normalization
To keep this example simple, you can normalize your model manually or use tools like Blender to center it:
However, for automatic centering and scaling, Bevy doesn't yet provide bounding box calculation for full scenes directly. You would use a plugin like bevy_mod_picking or compute AABBs.
You can approximate bounding by wrapping the loaded scene with a parent and applying offset/scale to center and fit into a unit cube.
💾 Screenshot Capture?
By default, Bevy doesn't have screenshot capture built-in. You can use this workaround:
Add Plugin + Crate: 💡 Bevy Screenshot
Add this to Cargo.toml:
bevy_screenshot = "0.4"
And update the system with:
use bevy_screenshot::ScreenshotManager;
// In App
.add_plugin(bevy_screenshot::ScreenshotPlugin)
In your screenshot system:
if let Ok(mut screenshot_manager) = commands.get_resource_mut::<ScreenshotManager>() {
screenshot_manager.take_screenshot(format!("outputs/view_{counter}.png"));
}
🧪 Run & Test
cargo run --release
Place your model.glb or model.gltf into the assets/ folder.
📸 Output
Result: outputs/view_0.png → outputs/view_7.png rendered from different camera angles around the model.
🔚 Notes
- Screenshot plugin is optional; you can also implement it manually using Bevy's render targets.
- Consider offloading rendering into headless or post-processing job for automation.
- You can export high-resolution thumbnails this way.
Would you like a version that loads multiple models in batch and renders them?