mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
151 lines
4.2 KiB
Rust
151 lines
4.2 KiB
Rust
|
use vek::*;
|
||
|
use client::Client;
|
||
|
use common::{
|
||
|
comp::Character,
|
||
|
figure::Segment,
|
||
|
};
|
||
|
use crate::{
|
||
|
render::{
|
||
|
Renderer,
|
||
|
Consts,
|
||
|
Globals,
|
||
|
Model,
|
||
|
SkyboxLocals,
|
||
|
SkyboxPipeline,
|
||
|
create_skybox_mesh,
|
||
|
PostProcessLocals,
|
||
|
PostProcessPipeline,
|
||
|
create_pp_mesh,
|
||
|
FigurePipeline,
|
||
|
},
|
||
|
scene::{
|
||
|
camera::Camera,
|
||
|
figure::{FigureModelCache, FigureState},
|
||
|
},
|
||
|
anim::{
|
||
|
Skeleton,
|
||
|
Animation,
|
||
|
character::{
|
||
|
CharacterSkeleton,
|
||
|
IdleAnimation,
|
||
|
},
|
||
|
},
|
||
|
};
|
||
|
|
||
|
struct Skybox {
|
||
|
model: Model<SkyboxPipeline>,
|
||
|
locals: Consts<SkyboxLocals>,
|
||
|
}
|
||
|
|
||
|
struct PostProcess {
|
||
|
model: Model<PostProcessPipeline>,
|
||
|
locals: Consts<PostProcessLocals>,
|
||
|
}
|
||
|
|
||
|
pub struct Scene {
|
||
|
globals: Consts<Globals>,
|
||
|
camera: Camera,
|
||
|
|
||
|
skybox: Skybox,
|
||
|
postprocess: PostProcess,
|
||
|
backdrop_model: Model<FigurePipeline>,
|
||
|
backdrop_state: FigureState<CharacterSkeleton>,
|
||
|
|
||
|
figure_model_cache: FigureModelCache,
|
||
|
figure_state: FigureState<CharacterSkeleton>,
|
||
|
}
|
||
|
|
||
|
impl Scene {
|
||
|
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(),
|
||
|
camera: Camera::new(resolution.x / resolution.y),
|
||
|
|
||
|
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(),
|
||
|
},
|
||
|
figure_model_cache: FigureModelCache::new(),
|
||
|
figure_state: FigureState::new(renderer, CharacterSkeleton::new()),
|
||
|
|
||
|
backdrop_model: renderer.create_model(&FigureModelCache::load_mesh("knight.vox", Vec3::zero())).unwrap(),
|
||
|
backdrop_state: FigureState::new(renderer, CharacterSkeleton::new()),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
pub fn maintain(&mut self, renderer: &mut Renderer, client: &Client) {
|
||
|
self.camera.set_focus_pos(Vec3::unit_z() * 1.75);
|
||
|
self.camera.update(client.state().get_time());
|
||
|
self.camera.set_distance(4.0);
|
||
|
self.camera.set_orientation(Vec3::new(
|
||
|
client.state().get_time() as f32 * 0.2,
|
||
|
0.3,
|
||
|
0.0,
|
||
|
));
|
||
|
|
||
|
let (view_mat, proj_mat, cam_pos) = self.camera.compute_dependents(client);
|
||
|
|
||
|
renderer.update_consts(
|
||
|
&mut self.globals,
|
||
|
&[Globals::new(
|
||
|
view_mat,
|
||
|
proj_mat,
|
||
|
cam_pos,
|
||
|
self.camera.get_focus_pos(),
|
||
|
100.0,
|
||
|
client.state().get_time_of_day(),
|
||
|
client.state().get_time(),
|
||
|
renderer.get_resolution(),
|
||
|
)],
|
||
|
);
|
||
|
|
||
|
self.figure_model_cache.clean(client.get_tick());
|
||
|
|
||
|
let tgt_skeleton = IdleAnimation::update_skeleton(
|
||
|
self.figure_state.skeleton_mut(),
|
||
|
client.state().get_time(),
|
||
|
client.state().get_time(),
|
||
|
);
|
||
|
self.figure_state.skeleton_mut().interpolate(&tgt_skeleton);
|
||
|
|
||
|
self.figure_state.update(renderer, Vec3::zero(), -Vec3::unit_y());
|
||
|
}
|
||
|
|
||
|
pub fn render(&mut self, renderer: &mut Renderer, client: &Client) {
|
||
|
renderer.render_skybox(&self.skybox.model, &self.globals, &self.skybox.locals);
|
||
|
|
||
|
let model = self.figure_model_cache.get_or_create_model(
|
||
|
renderer,
|
||
|
Character::random(),
|
||
|
client.get_tick(),
|
||
|
);
|
||
|
renderer.render_figure(
|
||
|
model,
|
||
|
&self.globals,
|
||
|
self.figure_state.locals(),
|
||
|
self.figure_state.bone_consts(),
|
||
|
);
|
||
|
|
||
|
renderer.render_figure(
|
||
|
&self.backdrop_model,
|
||
|
&self.globals,
|
||
|
self.backdrop_state.locals(),
|
||
|
self.backdrop_state.bone_consts(),
|
||
|
);
|
||
|
|
||
|
renderer.render_post_process(
|
||
|
&self.postprocess.model,
|
||
|
&self.globals,
|
||
|
&self.postprocess.locals,
|
||
|
);
|
||
|
}
|
||
|
}
|