2019-06-06 14:48:41 +00:00
|
|
|
use super::{
|
2020-06-17 07:49:14 +00:00
|
|
|
super::{Pipeline, TgtColorFmt, TgtDepthStencilFmt},
|
2019-09-25 12:00:00 +00:00
|
|
|
Globals, Light, Shadow,
|
2019-06-06 14:48:41 +00:00
|
|
|
};
|
2019-01-13 20:53:55 +00:00
|
|
|
use gfx::{
|
2020-02-01 20:39:39 +00:00
|
|
|
self, gfx_constant_struct_meta, gfx_defines, gfx_impl_struct_meta, gfx_pipeline,
|
|
|
|
gfx_pipeline_inner, gfx_vertex_struct_meta,
|
2020-04-04 19:36:55 +00:00
|
|
|
state::{ColorMask, Comparison, Stencil, StencilOp},
|
2019-01-13 20:53:55 +00:00
|
|
|
};
|
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
gfx_defines! {
|
|
|
|
vertex Vertex {
|
2020-04-25 14:07:58 +00:00
|
|
|
pos_norm: u32 = "v_pos_norm",
|
|
|
|
col: u32 = "v_col",
|
|
|
|
// BBBBBBAA
|
|
|
|
// B = Bone
|
|
|
|
// A = AO
|
|
|
|
ao_bone: u8 = "v_ao_bone",
|
2019-01-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constant Locals {
|
|
|
|
model_mat: [[f32; 4]; 4] = "model_mat",
|
2019-05-13 13:58:01 +00:00
|
|
|
model_col: [f32; 4] = "model_col",
|
2020-04-04 19:36:55 +00:00
|
|
|
flags: u32 = "flags",
|
2019-01-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constant BoneData {
|
|
|
|
bone_mat: [[f32; 4]; 4] = "bone_mat",
|
|
|
|
}
|
|
|
|
|
|
|
|
pipeline pipe {
|
|
|
|
vbuf: gfx::VertexBuffer<Vertex> = (),
|
|
|
|
|
|
|
|
locals: gfx::ConstantBuffer<Locals> = "u_locals",
|
|
|
|
globals: gfx::ConstantBuffer<Globals> = "u_globals",
|
|
|
|
bones: gfx::ConstantBuffer<BoneData> = "u_bones",
|
2019-07-21 15:04:36 +00:00
|
|
|
lights: gfx::ConstantBuffer<Light> = "u_lights",
|
2019-09-25 12:00:00 +00:00
|
|
|
shadows: gfx::ConstantBuffer<Shadow> = "u_shadows",
|
2019-01-13 20:53:55 +00:00
|
|
|
|
2019-11-17 22:41:00 +00:00
|
|
|
noise: gfx::TextureSampler<f32> = "t_noise",
|
|
|
|
|
2020-04-04 19:36:55 +00:00
|
|
|
tgt_color: gfx::BlendTarget<TgtColorFmt> = ("tgt_color", ColorMask::all(), gfx::preset::blend::ALPHA),
|
|
|
|
tgt_depth_stencil: gfx::DepthStencilTarget<TgtDepthStencilFmt> = (gfx::preset::depth::LESS_EQUAL_WRITE,Stencil::new(Comparison::Always,0xff,(StencilOp::Keep,StencilOp::Keep,StencilOp::Replace))),
|
2019-01-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Vertex {
|
2020-06-11 17:06:11 +00:00
|
|
|
#[allow(clippy::collapsible_if)]
|
2020-04-17 20:58:36 +00:00
|
|
|
pub fn new(pos: Vec3<f32>, norm: Vec3<f32>, col: Rgb<f32>, ao: f32, bone_idx: u8) -> Self {
|
2020-04-25 14:07:58 +00:00
|
|
|
let norm_bits = if norm.x != 0.0 {
|
|
|
|
if norm.x < 0.0 { 0 } else { 1 }
|
|
|
|
} else if norm.y != 0.0 {
|
|
|
|
if norm.y < 0.0 { 2 } else { 3 }
|
|
|
|
} else {
|
|
|
|
if norm.z < 0.0 { 4 } else { 5 }
|
|
|
|
};
|
2019-01-13 20:53:55 +00:00
|
|
|
Self {
|
2020-04-25 14:07:58 +00:00
|
|
|
pos_norm: pos
|
2020-04-29 14:22:08 +00:00
|
|
|
.map2(Vec3::new(0, 9, 18), |e, shift| {
|
|
|
|
(((e * 2.0 + 256.0) as u32) & 0x3FF) << shift
|
2020-04-25 14:38:17 +00:00
|
|
|
})
|
|
|
|
.reduce_bitor()
|
2020-04-29 14:22:08 +00:00
|
|
|
| (norm_bits << 29),
|
2020-04-25 14:07:58 +00:00
|
|
|
col: col
|
|
|
|
.map2(Rgb::new(0, 8, 16), |e, shift| ((e * 255.0) as u32) << shift)
|
|
|
|
.reduce_bitor(),
|
|
|
|
ao_bone: (bone_idx << 2) | ((ao * 3.9999) as u8),
|
2019-01-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_bone_idx(mut self, bone_idx: u8) -> Self {
|
2020-04-25 14:07:58 +00:00
|
|
|
self.ao_bone = (self.ao_bone & 0b11) | (bone_idx << 2);
|
2019-01-13 20:53:55 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Locals {
|
2020-04-04 19:36:55 +00:00
|
|
|
pub fn new(model_mat: Mat4<f32>, col: Rgba<f32>, is_player: bool) -> Self {
|
|
|
|
let mut flags = 0;
|
|
|
|
flags |= is_player as u32;
|
|
|
|
|
2019-01-13 20:53:55 +00:00
|
|
|
Self {
|
2020-06-17 07:49:14 +00:00
|
|
|
model_mat: model_mat.into_col_arrays(),
|
2019-05-13 13:58:01 +00:00
|
|
|
model_col: col.into_array(),
|
2020-04-04 19:36:55 +00:00
|
|
|
flags,
|
2019-01-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-13 13:58:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Locals {
|
2020-04-04 19:36:55 +00:00
|
|
|
fn default() -> Self { Self::new(Mat4::identity(), Rgba::broadcast(1.0), false) }
|
2019-01-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BoneData {
|
|
|
|
pub fn new(bone_mat: Mat4<f32>) -> Self {
|
|
|
|
Self {
|
2020-06-17 07:49:14 +00:00
|
|
|
bone_mat: bone_mat.into_col_arrays(),
|
2019-01-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn default() -> Self { Self::new(Mat4::identity()) }
|
2019-01-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct FigurePipeline;
|
|
|
|
|
|
|
|
impl Pipeline for FigurePipeline {
|
|
|
|
type Vertex = Vertex;
|
|
|
|
}
|