veloren/voxygen/examples/character_renderer.rs
Imbris 4a0c474be1 Remove Client dependency from Scene types and audio managers, add an
example for using voxygen as a library to renderer images of characters
2020-03-05 23:12:51 -05:00

59 lines
1.8 KiB
Rust

use common::{assets, comp};
use gfx_window_glutin::init_headless;
use vek::*;
use veloren_voxygen::{render, scene::simple as scene};
fn main() {
// Setup renderer
let dim = (200u16, 300u16, 1, gfx::texture::AaMode::Single);
let events_loop = glutin::EventsLoop::new();
let context = glutin::ContextBuilder::new()
.with_gl(glutin::GlRequest::Specific(glutin::Api::OpenGl, (3, 2)))
.build_headless(&events_loop, (dim.0 as u32, dim.1 as u32).into())
.expect("Failed to build headless context");
let (_context, device, factory, color_view, depth_view) = init_headless(context, dim);
let mut renderer = render::Renderer::new(
device,
factory,
color_view,
depth_view,
render::AaMode::SsaaX4,
render::CloudMode::Regular,
render::FluidMode::Shiny,
)
.unwrap();
// Create character
let body = comp::humanoid::Body::random();
const STARTER_BOW: &str = "common.items.weapons.starter_bow";
let equipment = comp::Equipment {
main: assets::load_cloned(STARTER_BOW).ok(),
alt: None,
};
// Setup scene (using the character selection screen `Scene`)
let mut scene = scene::Scene::new(&mut renderer, None);
let scene_data = scene::SceneData {
time: 1.0,
delta_time: 1.0,
tick: 0,
body: Some(body.clone()),
gamma: 1.0,
};
scene.camera_mut().set_focus_pos(Vec3::unit_z() * 0.8);
scene.camera_mut().set_distance(1.5);
scene.camera_mut().update(0.0);
scene.maintain(&mut renderer, scene_data);
// Render
renderer.clear();
scene.render(&mut renderer, 0, Some(body), &equipment);
renderer.flush();
// Get image
let img = renderer.create_screenshot().unwrap();
img.save("character.png").unwrap();
}