-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforward_model_ubo.rs
More file actions
74 lines (67 loc) · 2.2 KB
/
forward_model_ubo.rs
File metadata and controls
74 lines (67 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use bytemuck;
use glam::Mat4;
use crate::{
config::Config,
render_graph::shadow_map_pass::ShadowMapPass,
scene::{Camera, WorldEntity},
};
// All below must match shader-defined consts
const FLAG_IS_METALIC: i32 = 1;
const FLAG_USE_SPECULAR_TEXTURE: i32 = 2;
const FLAG_USE_HAIR_SHADOW_TEXTURE: i32 = 4;
// add mvp here? If we add ui, we have to reupload anyway cuz materials can change
#[derive(Copy, Clone, Debug)] // , bytemuck::Zeroable, bytemuck::Pod
#[repr(C)]
pub struct ForwardModelUBO {
pub u_model_matrix: Mat4,
/// model view projection matrix for current camera
pub u_model_view_projection_matrix: Mat4,
/// model view projection matrix for shadow-casting light
pub u_shadow_matrix_mvp: Mat4,
// material
pub u_specular: f32,
pub u_specular_mul: f32,
pub u_material_flags: i32,
pub u_sss_transluency: f32,
pub u_sss_width: f32,
pub u_sss_bias: f32,
pub u_sss_gain: f32,
pub u_sss_strength: f32,
}
unsafe impl bytemuck::Zeroable for ForwardModelUBO {}
unsafe impl bytemuck::Pod for ForwardModelUBO {}
fn flag_bits(cond: bool, bit: i32) -> i32 {
if cond {
bit
} else {
0
}
}
impl ForwardModelUBO {
pub fn new(config: &Config, entity: &WorldEntity, camera: &Camera) -> ForwardModelUBO {
let material = &entity.material;
let mut material_flags: i32 = 0;
material_flags |= flag_bits(material.is_metallic, FLAG_IS_METALIC);
material_flags |= flag_bits(material.specular_tex.is_some(), FLAG_USE_SPECULAR_TEXTURE);
material_flags |= flag_bits(
material.hair_shadow_tex.is_some(),
FLAG_USE_HAIR_SHADOW_TEXTURE,
);
ForwardModelUBO {
u_model_matrix: entity.model_matrix,
u_model_view_projection_matrix: camera.model_view_projection_matrix(entity.model_matrix),
u_shadow_matrix_mvp: ShadowMapPass::get_light_shadow_mvp(
&config.shadows.shadow_source,
entity.model_matrix,
),
u_specular: material.specular,
u_specular_mul: material.specular_mul,
u_material_flags: material_flags,
u_sss_transluency: material.sss_transluency,
u_sss_width: material.sss_width,
u_sss_bias: material.sss_bias,
u_sss_gain: material.sss_gain,
u_sss_strength: material.sss_strength,
}
}
}