Added per-instance transformation matrix, variable wind swaying

This commit is contained in:
Joshua Barretto 2019-08-21 13:47:29 +01:00
parent 590ce1a987
commit 75e1ee3cd7
3 changed files with 53 additions and 16 deletions

View File

@ -6,8 +6,12 @@
in vec3 v_pos; in vec3 v_pos;
in vec3 v_norm; in vec3 v_norm;
in vec3 v_col; in vec3 v_col;
in vec3 inst_pos; in vec4 inst_mat0;
in vec4 inst_mat1;
in vec4 inst_mat2;
in vec4 inst_mat3;
in vec3 inst_col; in vec3 inst_col;
in float inst_wind_sway;
out vec3 f_pos; out vec3 f_pos;
flat out vec3 f_norm; flat out vec3 f_norm;
@ -17,10 +21,20 @@ out float f_light;
const float SCALE = 1.0 / 11.0; const float SCALE = 1.0 / 11.0;
void main() { void main() {
f_pos = inst_pos + v_pos * SCALE; mat4 inst_mat;
inst_mat[0] = inst_mat0;
inst_mat[1] = inst_mat1;
inst_mat[2] = inst_mat2;
inst_mat[3] = inst_mat3;
f_pos = (inst_mat * vec4(v_pos * SCALE, 1)).xyz;
// Wind waving // Wind waving
f_pos += vec3(sin(tick.x * 1.7 + inst_pos.y * 0.2), sin(tick.x * 1.3 + inst_pos.x * 0.2), 0) * sin(v_pos.z * 0.03) * 0.5; f_pos += inst_wind_sway * vec3(
sin(tick.x * 1.7 + f_pos.y * 0.2),
sin(tick.x * 1.3 + f_pos.x * 0.2),
0.0
) * sin(v_pos.z * 0.03) * 0.5;
f_norm = v_norm; f_norm = v_norm;

View File

@ -1,5 +1,5 @@
use super::{ use super::{
super::{Pipeline, TgtColorFmt, TgtDepthFmt}, super::{util::arr_to_mat, Pipeline, TgtColorFmt, TgtDepthFmt},
Globals, Light, Globals, Light,
}; };
use gfx::{ use gfx::{
@ -22,8 +22,12 @@ gfx_defines! {
} }
vertex Instance { vertex Instance {
inst_pos: [f32; 3] = "inst_pos", inst_mat0: [f32; 4] = "inst_mat0",
inst_mat1: [f32; 4] = "inst_mat1",
inst_mat2: [f32; 4] = "inst_mat2",
inst_mat3: [f32; 4] = "inst_mat3",
inst_col: [f32; 3] = "inst_col", inst_col: [f32; 3] = "inst_col",
inst_wind_sway: f32 = "inst_wind_sway",
} }
pipeline pipe { pipeline pipe {
@ -49,17 +53,22 @@ impl Vertex {
} }
impl Instance { impl Instance {
pub fn new(inst_pos: Vec3<f32>, col: Rgb<f32>) -> Self { pub fn new(mat: Mat4<f32>, col: Rgb<f32>, wind_sway: f32) -> Self {
let mat_arr = arr_to_mat(mat.into_col_array());
Self { Self {
inst_pos: inst_pos.into_array(), inst_mat0: mat_arr[0],
inst_mat1: mat_arr[1],
inst_mat2: mat_arr[2],
inst_mat3: mat_arr[3],
inst_col: col.into_array(), inst_col: col.into_array(),
inst_wind_sway: wind_sway,
} }
} }
} }
impl Default for Instance { impl Default for Instance {
fn default() -> Self { fn default() -> Self {
Self::new(Vec3::zero(), Rgb::broadcast(1.0)) Self::new(Mat4::identity(), Rgb::broadcast(1.0), 0.0)
} }
} }

View File

@ -47,6 +47,19 @@ struct MeshWorkerResponse {
started_tick: u64, started_tick: u64,
} }
struct SpriteConfig {
wind_sway: f32, // 1.0 is normal
}
fn sprite_config_for(kind: BlockKind) -> Option<SpriteConfig> {
match kind {
BlockKind::Wheat => Some(SpriteConfig { wind_sway: 1.0 }),
BlockKind::LongGrass => Some(SpriteConfig { wind_sway: 1.0 }),
BlockKind::Flowers => Some(SpriteConfig { wind_sway: 0.3 }),
_ => None,
}
}
/// Function executed by worker threads dedicated to chunk meshing. /// Function executed by worker threads dedicated to chunk meshing.
fn mesh_worker( fn mesh_worker(
pos: Vec2<i32>, pos: Vec2<i32>,
@ -73,16 +86,17 @@ fn mesh_worker(
) + Vec3::new(x, y, z); ) + Vec3::new(x, y, z);
let kind = volume.get(wpos).unwrap_or(&Block::empty()).kind(); let kind = volume.get(wpos).unwrap_or(&Block::empty()).kind();
match kind {
BlockKind::Wheat | BlockKind::LongGrass | BlockKind::Flowers => { if let Some(cfg) = sprite_config_for(kind) {
instances.entry(kind).or_insert_with(|| Vec::new()).push( instances.entry(kind).or_insert_with(|| Vec::new()).push(
SpriteInstance::new( SpriteInstance::new(
Mat4::identity().translated_3d(
wpos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0), wpos.map(|e| e as f32) + Vec3::new(0.5, 0.5, 0.0),
Rgb::broadcast(1.0),
), ),
) Rgb::broadcast(1.0),
} cfg.wind_sway,
_ => {} ),
);
} }
} }
} }