Skip to content

Commit

Permalink
fly camera example
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynus committed May 18, 2019
1 parent 86c5dfd commit 80e7747
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/assets/prefab/fly_camera.ron
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Prefab (
(
data: (
graphics: (
mesh: Asset(File ("mesh/cube.obj", ObjFormat, ())),
mesh: Asset(File("mesh/cube.obj", ("OBJ", ()))),
material: (
albedo: Data(Rgba((1.0, 0.0, 0.0, 1.0,), (channel: Srgb),)),
albedo: Generate(Srgba(0.0, 0.0, 1.0, 1.0)),
),
),
transform: (
Expand Down
104 changes: 100 additions & 4 deletions examples/fly_camera/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,35 @@ use amethyst::{
core::transform::TransformBundle,
input::{is_key_down, is_mouse_button_down, InputBundle, StringBindings},
prelude::*,
renderer::{DrawShaded, MouseButton, PosNormTex, VirtualKeyCode},
ecs::{Resources, SystemData, ReadExpect},
renderer::{
pass::DrawShadedDesc,
rendy::{
factory::Factory,
graph::{
render::{RenderGroupDesc, SubpassBuilder},
GraphBuilder,
},
hal::format::Format,
mesh::{Normal, Position, TexCoord},
},
types::DefaultBackend,
GraphCreator, RenderingSystem,
},
winit::{VirtualKeyCode, MouseButton},
utils::{application_root_dir, scene::BasicScenePrefab},
window::{ScreenDimensions, Window, WindowBundle},
Error,
};

type MyPrefabData = BasicScenePrefab<Vec<PosNormTex>>;
type MyPrefabData = BasicScenePrefab<(Vec<Position>, Vec<Normal>, Vec<TexCoord>)>;

struct ExampleState;

impl SimpleState for ExampleState {
fn on_start(&mut self, data: StateData<'_, GameData<'_, '_>>) {
let prefab_handle = data.world.exec(|loader: PrefabLoader<'_, MyPrefabData>| {
loader.load("prefab/fly_camera.ron", RonFormat, (), ())
loader.load("prefab/fly_camera.ron", RonFormat, ())
});
data.world
.create_entity()
Expand Down Expand Up @@ -58,6 +74,7 @@ fn main() -> Result<(), Error> {
let key_bindings_path = app_root.join("examples/fly_camera/resources/input.ron");

let game_data = GameDataBuilder::default()
.with_bundle(WindowBundle::from_config_path(display_config_path))?
.with(PrefabLoaderSystem::<MyPrefabData>::default(), "", &[])
.with_bundle(
FlyControlBundle::<StringBindings>::new(
Expand All @@ -71,8 +88,87 @@ fn main() -> Result<(), Error> {
.with_bundle(
InputBundle::<StringBindings>::new().with_bindings_from_file(&key_bindings_path)?,
)?
.with_basic_renderer(display_config_path, DrawShaded::<PosNormTex>::new(), false)?;
.with_thread_local(RenderingSystem::<DefaultBackend, _>::new(
ExampleGraph::new(),
));
let mut game = Application::build(resources_directory, ExampleState)?.build(game_data)?;
game.run();
Ok(())
}

struct ExampleGraph {
last_dimensions: Option<ScreenDimensions>,
surface_format: Option<Format>,
dirty: bool,
}

impl ExampleGraph {
pub fn new() -> Self {
ExampleGraph {
last_dimensions: None,
surface_format: None,
dirty: true,
}
}
}

impl GraphCreator<DefaultBackend> for ExampleGraph {
fn rebuild(&mut self, res: &Resources) -> bool {
// Rebuild when dimensions change, but wait until at least two frames have the same.
let new_dimensions = res.try_fetch::<ScreenDimensions>();
use std::ops::Deref;
if self.last_dimensions.as_ref() != new_dimensions.as_ref().map(|d| d.deref()) {
self.dirty = true;
self.last_dimensions = new_dimensions.map(|d| d.clone());
return false;
}
return self.dirty;
}

fn builder(
&mut self,
factory: &mut Factory<DefaultBackend>,
res: &Resources,
) -> GraphBuilder<DefaultBackend, Resources> {
use amethyst::renderer::rendy::{
graph::present::PresentNode,
hal::command::{ClearDepthStencil, ClearValue},
};

self.dirty = false;
let window = <ReadExpect<'_, std::sync::Arc<Window>>>::fetch(res);
let surface = factory.create_surface(window.clone());
// cache surface format to speed things up
let surface_format = *self
.surface_format
.get_or_insert_with(|| factory.get_surface_format(&surface));

let mut graph_builder = GraphBuilder::new();
let color = graph_builder.create_image(
surface.kind(),
1,
surface_format,
Some(ClearValue::Color([0.34, 0.36, 0.52, 1.0].into())),
);

let depth = graph_builder.create_image(
surface.kind(),
1,
Format::D32Sfloat,
Some(ClearValue::DepthStencil(ClearDepthStencil(1.0, 0))),
);

let ui = graph_builder.add_node(
SubpassBuilder::new()
.with_group(DrawShadedDesc::new().builder())
.with_color(color)
.with_depth_stencil(depth)
.into_pass(),
);

let _present = graph_builder
.add_node(PresentNode::builder(factory, surface, color).with_dependency(ui));

graph_builder
}
}

0 comments on commit 80e7747

Please sign in to comment.