veloren/voxygen/src/scene/mod.rs

259 lines
8.2 KiB
Rust
Raw Normal View History

pub mod camera;
pub mod figure;
pub mod sound;
2019-09-06 10:38:02 +00:00
pub mod terrain;
2019-07-24 14:27:13 +00:00
use self::{
camera::{Camera, CameraMode},
figure::FigureMgr,
sound::SoundMgr,
2019-09-06 10:38:02 +00:00
terrain::Terrain,
2019-07-24 14:27:13 +00:00
};
use crate::{
2019-09-06 10:38:02 +00:00
audio::AudioFrontend,
render::{
2019-07-21 15:04:36 +00:00
create_pp_mesh, create_skybox_mesh, Consts, Globals, Light, Model, PostProcessLocals,
PostProcessPipeline, Renderer, SkyboxLocals, SkyboxPipeline,
},
window::Event,
};
use client::Client;
2019-08-16 14:58:14 +00:00
use common::{comp, terrain::BlockKind, vol::ReadVol};
use specs::Join;
use vek::*;
// TODO: Don't hard-code this.
const CURSOR_PAN_SCALE: f32 = 0.005;
2019-07-21 15:37:41 +00:00
const MAX_LIGHT_COUNT: usize = 32;
2019-07-21 15:04:36 +00:00
const LIGHT_DIST_RADIUS: f32 = 64.0; // The distance beyond which lights may not be visible
struct Skybox {
model: Model<SkyboxPipeline>,
locals: Consts<SkyboxLocals>,
}
struct PostProcess {
model: Model<PostProcessPipeline>,
locals: Consts<PostProcessLocals>,
}
pub struct Scene {
globals: Consts<Globals>,
2019-07-21 15:04:36 +00:00
lights: Consts<Light>,
2019-01-15 15:13:11 +00:00
camera: Camera,
skybox: Skybox,
postprocess: PostProcess,
2019-01-15 15:13:11 +00:00
terrain: Terrain,
2019-06-05 16:32:33 +00:00
loaded_distance: f32,
figure_mgr: FigureMgr,
sound_mgr: SoundMgr,
}
impl Scene {
/// Create a new `Scene` with default parameters.
2019-07-04 12:02:26 +00:00
pub fn new(renderer: &mut Renderer) -> Self {
let resolution = renderer.get_resolution().map(|e| e as f32);
Self {
globals: renderer.create_consts(&[Globals::default()]).unwrap(),
2019-07-21 15:04:36 +00:00
lights: renderer.create_consts(&[Light::default(); 32]).unwrap(),
2019-07-22 19:11:00 +00:00
camera: Camera::new(resolution.x / resolution.y, CameraMode::ThirdPerson),
2019-01-15 15:13:11 +00:00
skybox: Skybox {
model: renderer.create_model(&create_skybox_mesh()).unwrap(),
locals: renderer.create_consts(&[SkyboxLocals::default()]).unwrap(),
},
postprocess: PostProcess {
model: renderer.create_model(&create_pp_mesh()).unwrap(),
locals: renderer
.create_consts(&[PostProcessLocals::default()])
.unwrap(),
},
2019-08-19 20:09:35 +00:00
terrain: Terrain::new(renderer),
2019-06-05 16:32:33 +00:00
loaded_distance: 0.0,
figure_mgr: FigureMgr::new(),
2019-09-06 10:38:02 +00:00
sound_mgr: SoundMgr::new(),
}
}
/// Get a reference to the scene's globals
pub fn globals(&self) -> &Consts<Globals> {
&self.globals
}
2019-01-23 22:39:31 +00:00
/// Get a reference to the scene's camera.
pub fn camera(&self) -> &Camera {
&self.camera
}
2019-01-23 22:39:31 +00:00
/// Get a mutable reference to the scene's camera.
pub fn camera_mut(&mut self) -> &mut Camera {
&mut self.camera
}
2019-01-23 22:39:31 +00:00
/// Handle an incoming user input event (e.g.: cursor moved, key pressed, window closed).
///
/// If the event is handled, return true.
pub fn handle_input_event(&mut self, event: Event) -> bool {
match event {
// When the window is resized, change the camera's aspect ratio
Event::Resize(dims) => {
self.camera.set_aspect_ratio(dims.x as f32 / dims.y as f32);
true
}
// Panning the cursor makes the camera rotate
Event::CursorPan(delta) => {
self.camera.rotate_by(Vec3::from(delta) * CURSOR_PAN_SCALE);
true
}
// Zoom the camera when a zoom event occurs
Event::Zoom(delta) => {
2019-08-13 17:54:13 +00:00
self.camera.zoom_switch(delta * 0.3);
true
}
// All other events are unhandled
_ => false,
}
}
2019-01-23 20:01:58 +00:00
/// Maintain data such as GPU constant buffers, models, etc. To be called once per tick.
2019-09-06 10:38:02 +00:00
pub fn maintain(
&mut self,
renderer: &mut Renderer,
audio: &mut AudioFrontend,
client: &Client,
) {
// Get player position.
let player_pos = client
.state()
.ecs()
.read_storage::<comp::Pos>()
.get(client.entity())
.map_or(Vec3::zero(), |pos| pos.0);
// Alter camera position to match player.
2019-06-10 10:50:48 +00:00
let tilt = self.camera.get_orientation().y;
let dist = self.camera.get_distance();
2019-08-13 17:54:13 +00:00
let up = if self.camera.get_mode() == CameraMode::FirstPerson {
1.5
} else {
1.2
};
2019-07-21 12:42:45 +00:00
self.camera.set_focus_pos(
player_pos + Vec3::unit_z() * (up + dist * 0.15 - tilt.min(0.0) * dist * 0.75),
2019-07-21 12:42:45 +00:00
);
// Tick camera for interpolation.
self.camera.update(client.state().get_time());
// Compute camera matrices.
let (view_mat, proj_mat, cam_pos) = self.camera.compute_dependents(client);
2019-06-05 16:32:33 +00:00
// Update chunk loaded distance smoothly for nice shader fog
2019-07-21 15:04:36 +00:00
let loaded_distance = client.loaded_distance().unwrap_or(0) as f32 * 32.0; // TODO: No magic!
2019-06-06 10:09:25 +00:00
self.loaded_distance = (0.98 * self.loaded_distance + 0.02 * loaded_distance).max(0.01);
2019-06-05 16:32:33 +00:00
2019-07-21 15:04:36 +00:00
// Update light constants
2019-07-21 16:50:13 +00:00
let mut lights = (
&client.state().ecs().read_storage::<comp::Pos>(),
2019-07-25 20:51:20 +00:00
client.state().ecs().read_storage::<comp::Ori>().maybe(),
2019-07-21 16:50:13 +00:00
&client.state().ecs().read_storage::<comp::LightEmitter>(),
)
2019-07-21 15:04:36 +00:00
.join()
2019-07-25 20:51:20 +00:00
.filter(|(pos, _, _)| {
2019-07-21 15:04:36 +00:00
(pos.0.distance_squared(player_pos) as f32)
< self.loaded_distance.powf(2.0) + LIGHT_DIST_RADIUS
})
2019-07-25 20:51:20 +00:00
.map(|(pos, ori, light_emitter)| {
let rot = {
if let Some(o) = ori {
Mat3::rotation_z(-o.0.x.atan2(o.0.y))
} else {
Mat3::identity()
}
};
2019-07-21 16:50:13 +00:00
Light::new(
2019-07-25 20:51:20 +00:00
pos.0 + (rot * light_emitter.offset),
2019-07-21 16:50:13 +00:00
light_emitter.col,
light_emitter.strength,
)
2019-07-25 20:51:20 +00:00
})
2019-07-21 15:04:36 +00:00
.collect::<Vec<_>>();
lights.sort_by_key(|light| {
Vec3::from(Vec4::from(light.pos)).distance_squared(player_pos) as i32
});
2019-07-21 15:37:41 +00:00
lights.truncate(MAX_LIGHT_COUNT);
2019-07-21 15:04:36 +00:00
renderer
.update_consts(&mut self.lights, &lights)
.expect("Failed to update light constants");
// Update global constants.
renderer
.update_consts(
&mut self.globals,
&[Globals::new(
view_mat,
proj_mat,
cam_pos,
self.camera.get_focus_pos(),
2019-06-05 16:32:33 +00:00
self.loaded_distance,
client.state().get_time_of_day(),
client.state().get_time(),
renderer.get_resolution(),
2019-07-21 15:04:36 +00:00
lights.len(),
2019-08-16 14:58:14 +00:00
client
.state()
.terrain()
.get(cam_pos.map(|e| e.floor() as i32))
.map(|b| b.kind())
.unwrap_or(BlockKind::Air),
)],
)
.expect("Failed to update global constants");
// Maintain the terrain.
2019-06-06 09:04:37 +00:00
self.terrain.maintain(
renderer,
client,
self.camera.get_focus_pos(),
self.loaded_distance,
view_mat,
proj_mat,
);
// Maintain the figures.
2019-07-24 14:27:13 +00:00
self.figure_mgr.maintain(renderer, client);
// Remove unused figures.
self.figure_mgr.clean(client.get_tick());
// Maintain audio
self.sound_mgr.maintain(audio, client);
}
/// Render the scene using the provided `Renderer`.
pub fn render(&mut self, renderer: &mut Renderer, client: &mut Client) {
// Render the skybox first (it appears over everything else so must be rendered first).
renderer.render_skybox(&self.skybox.model, &self.globals, &self.skybox.locals);
// Render terrain and figures.
2019-07-21 15:04:36 +00:00
self.figure_mgr
2019-07-24 14:27:13 +00:00
.render(renderer, client, &self.globals, &self.lights, &self.camera);
2019-08-19 21:54:16 +00:00
self.terrain.render(
renderer,
&self.globals,
&self.lights,
self.camera.get_focus_pos(),
);
renderer.render_post_process(
&self.postprocess.model,
&self.globals,
&self.postprocess.locals,
);
}
}