forked from SaschaWillems/Vulkan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
73 lines (65 loc) · 1.87 KB
/
lib.rs
File metadata and controls
73 lines (65 loc) · 1.87 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
#![cfg_attr(target_arch = "spirv", no_std)]
use spirv_std::{
glam::{Mat3, Mat4, Vec2, Vec3, Vec4},
num_traits::Float,
spirv,
};
#[repr(C)]
#[derive(Copy, Clone)]
pub struct UBO {
pub projection: Mat4,
pub view: Mat4,
pub model: Mat4,
}
#[spirv(vertex)]
pub fn main_vs(
in_pos: Vec4,
in_uv: Vec2,
in_color: Vec3,
in_normal: Vec3,
#[spirv(uniform, descriptor_set = 0, binding = 0)] ubo: &UBO,
#[spirv(position)] out_position: &mut Vec4,
out_normal: &mut Vec3,
out_uv: &mut Vec2,
out_color: &mut Vec3,
out_view_vec: &mut Vec3,
out_light_vec: &mut Vec3,
) {
*out_normal = in_normal;
*out_color = in_color;
*out_uv = in_uv;
*out_position = ubo.projection * ubo.view * ubo.model * in_pos;
let light_pos = Vec3::new(-5.0, -5.0, 0.0);
let pos = ubo.view * ubo.model * in_pos;
let normal_matrix = Mat3::from_mat4(ubo.view * ubo.model);
*out_normal = normal_matrix * in_normal;
*out_light_vec = light_pos - Vec3::new(pos.x, pos.y, pos.z);
*out_view_vec = -Vec3::new(pos.x, pos.y, pos.z);
}
#[spirv(fragment)]
pub fn main_fs(
in_normal: Vec3,
in_uv: Vec2,
in_color: Vec3,
in_view_vec: Vec3,
in_light_vec: Vec3,
out_frag_color: &mut Vec4,
) {
let mut ambient = Vec3::ZERO;
// Adjust light calculations for glow color
if in_color.x >= 0.9 || in_color.y >= 0.9 || in_color.z >= 0.9 {
ambient = in_color * 0.25;
}
let n = in_normal.normalize();
let l = in_light_vec.normalize();
let v = in_view_vec.normalize();
let r = (-l).reflect(n);
let diffuse = n.dot(l).max(0.0) * in_color;
let specular = r.dot(v).max(0.0).powf(8.0) * Vec3::new(0.75, 0.75, 0.75);
*out_frag_color = Vec4::new(
ambient.x + diffuse.x + specular.x,
ambient.y + diffuse.y + specular.y,
ambient.z + diffuse.z + specular.z,
1.0,
);
}