-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
199 lines (178 loc) · 5.5 KB
/
mod.rs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use std::path::Path;
use ash::vk;
use glam::vec4;
use glam::Mat4;
use glam::Vec2;
use glam::Vec3;
use log::info;
use log::trace;
use tobj;
use crate::config::Config;
use crate::render_graph::RenderableVertex;
use crate::vk_ctx::VkCtx;
use crate::vk_utils::*;
pub use self::bounding_box::*;
pub use self::camera::*;
pub use self::material::*;
pub use self::tressfx::*;
pub use self::world::*;
pub use self::world_entity::*;
mod bounding_box;
mod camera;
mod material;
mod tressfx;
mod world;
mod world_entity;
pub fn load_scene(vk_ctx: &VkCtx, config: &Config) -> World {
let scale = config.model_scale;
let frames_in_flight = config.frames_in_flight;
let model_matrix = Mat4::from_scale(Vec3::new(scale, scale, scale));
let sintel = load_sintel(vk_ctx, frames_in_flight, model_matrix);
let sintel_eyes = load_sintel_eyes(vk_ctx, frames_in_flight, model_matrix);
// tressfx
let sintel_tfx_file = load_tressfx_file(std::path::Path::new(
"./assets/sintel_lite_v2_1/GEO-sintel_hair_emit.002-sintel_hair.tfx",
));
let mut sintel_hair = TfxObject::from_file(
vk_ctx,
config,
"sintel_hair",
model_matrix,
&sintel_tfx_file,
);
sintel_hair.center_of_gravity.y = 9.0; // just below the eyes
sintel_hair.collision_capsule0 = vec4(0.0, 36.65, -1.3, 9.27);
sintel_hair.collision_capsule1 = vec4(0.0, 35.81, 2.38, 8.97);
sintel_hair.collision_capsule2 = vec4(0.0, 26.56, 2.75, 8.89);
sintel_hair.scale_debug_use_only = scale;
World {
camera: Camera::new(config, vk_ctx.window_size()),
entities: vec![sintel, sintel_eyes],
tressfx_objects: vec![sintel_hair],
}
}
fn load_sintel(vk_ctx: &VkCtx, frames_in_flight: usize, model_matrix: Mat4) -> WorldEntity {
let tex_diffuse = vk_ctx.create_texture_from_file(
Path::new("./assets/sintel_lite_v2_1/textures/sintel_skin_diff.jpg"),
vk::Format::R8G8B8A8_SRGB,
);
let specular_tex = vk_ctx.create_texture_from_file(
Path::new("./assets/sintel_lite_v2_1/textures/sintel_skin_spec.jpg"),
VkTexture::RAW_DATA_TEXTURE_FORMAT,
);
let hair_shadow_tex = vk_ctx.create_texture_from_file(
Path::new("./assets/sintel_lite_v2_1/textures/sintel_hair_shadow.jpg"),
VkTexture::RAW_DATA_TEXTURE_FORMAT,
);
let material = Material::new(tex_diffuse, Some(specular_tex), Some(hair_shadow_tex));
let (mesh, aabb) = load_obj_mesh(
vk_ctx,
Path::new("./assets/sintel_lite_v2_1/sintel.obj"),
&model_matrix,
);
let name = "sintel".to_string();
let model_ubo = allocate_model_ubo_vec(vk_ctx, frames_in_flight, &name);
WorldEntity {
name,
material,
vertex_buffer: mesh.vertex_buffer,
index_buffer: mesh.index_buffer,
vertex_count: mesh.vertex_count,
aabb,
model_matrix,
model_ubo,
}
}
fn load_sintel_eyes(vk_ctx: &VkCtx, frames_in_flight: usize, model_matrix: Mat4) -> WorldEntity {
let tex_diffuse = vk_ctx.create_texture_from_file(
Path::new("./assets/sintel_lite_v2_1/textures/sintel_eyeball_diff.jpg"),
vk::Format::R8G8B8A8_SRGB,
);
let mut material = Material::new(tex_diffuse, None, None);
material.specular_mul = 3.0; // shiny!
let (mesh, aabb) = load_obj_mesh(
vk_ctx,
Path::new("./assets/sintel_lite_v2_1/sintel_eyeballs.obj"),
&model_matrix,
);
let name = "sintel_eyes".to_string();
let model_ubo = allocate_model_ubo_vec(vk_ctx, frames_in_flight, &name);
WorldEntity {
name,
material,
vertex_buffer: mesh.vertex_buffer,
index_buffer: mesh.index_buffer,
vertex_count: mesh.vertex_count,
aabb,
model_matrix,
model_ubo,
}
}
struct Mesh {
pub vertex_buffer: VkBuffer,
pub index_buffer: VkBuffer,
pub vertex_count: u32,
}
fn load_obj_mesh(
vk_ctx: &VkCtx,
path: &std::path::Path,
model_matrix: &Mat4,
) -> (Mesh, BoundingBox) {
let (models, _) = tobj::load_obj(path, &tobj::GPU_LOAD_OPTIONS)
.expect(&format!("Failed to load OBJ file '{}'", path.display()));
if models.len() != 1 {
panic!(
"Expected 1 model in OBJ file, found {}. File: '{}'",
models.len(),
path.display()
)
}
let object = &models[0];
let mesh = &object.mesh;
info!(
"Loading OBJ file '{}'. Found object '{}' with {} vertices, {} normals, {} uv coordinates and {} triangles",
path.display(),
object.name,
mesh.positions.len() / 3,
mesh.normals.len() / 3,
mesh.texcoords.len() / 2,
mesh.indices.len() /3
);
let vertex_count = mesh.positions.len() / 3;
let mut vertices: Vec<RenderableVertex> = Vec::with_capacity(vertex_count);
let m_ps = &mesh.positions;
let m_n = &mesh.normals;
let m_uv = &mesh.texcoords;
(0..vertex_count).for_each(|i| {
let i3 = i * 3;
let i2 = i * 2;
vertices.push(RenderableVertex {
position: Vec3::new(m_ps[i3], m_ps[i3 + 1], m_ps[i3 + 2]),
normal: Vec3::new(m_n[i3], m_n[i3 + 1], m_n[i3 + 2]),
uv: Vec2::new(m_uv[i2], m_uv[i2 + 1]),
})
});
let aabb = BoundingBox::from_vertices(&vertices, *model_matrix);
trace!("{}", aabb);
// allocate
let vertices_bytes = bytemuck::cast_slice(&vertices);
let vertex_buffer = vk_ctx.create_buffer_from_data(
format!("{}_VertexBuffer", object.name),
vertices_bytes,
vk::BufferUsageFlags::VERTEX_BUFFER,
);
// index buffer
let indices = &mesh.indices;
let indices_bytes = bytemuck::cast_slice(&indices);
let index_buffer = vk_ctx.create_buffer_from_data(
format!("{}_IndexBuffer", object.name),
indices_bytes,
vk::BufferUsageFlags::INDEX_BUFFER,
);
let mesh = Mesh {
vertex_buffer,
index_buffer,
vertex_count: indices.len() as u32,
};
(mesh, aabb)
}