gltf
This commit is contained in:
@@ -13,8 +13,8 @@ shaderc = "0.5.0"
|
|||||||
cgmath = "0.17"
|
cgmath = "0.17"
|
||||||
winit = "0.19"
|
winit = "0.19"
|
||||||
image = "0.22.0"
|
image = "0.22.0"
|
||||||
collada = "0.11.0"
|
|
||||||
serde = "1.0.97"
|
serde = "1.0.97"
|
||||||
serde_derive = "1.0.97"
|
serde_derive = "1.0.97"
|
||||||
toml = "0.5.1"
|
toml = "0.5.1"
|
||||||
gilrs = "0.7.1"
|
gilrs = "0.7.1"
|
||||||
|
gltf = "0.13.0"
|
||||||
BIN
models/box.bin
Normal file
BIN
models/box.bin
Normal file
Binary file not shown.
117
models/box.gltf
Normal file
117
models/box.gltf
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
{
|
||||||
|
"asset" : {
|
||||||
|
"generator" : "Khronos glTF Blender I/O v0.9.36",
|
||||||
|
"version" : "2.0"
|
||||||
|
},
|
||||||
|
"scene" : 0,
|
||||||
|
"scenes" : [
|
||||||
|
{
|
||||||
|
"name" : "Scene",
|
||||||
|
"nodes" : [
|
||||||
|
0
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nodes" : [
|
||||||
|
{
|
||||||
|
"mesh" : 0,
|
||||||
|
"name" : "Cube"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"materials" : [
|
||||||
|
{
|
||||||
|
"doubleSided" : true,
|
||||||
|
"name" : "Material",
|
||||||
|
"pbrMetallicRoughness" : {
|
||||||
|
"baseColorFactor" : [
|
||||||
|
0.800000011920929,
|
||||||
|
0.800000011920929,
|
||||||
|
0.800000011920929,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"metallicFactor" : 0,
|
||||||
|
"roughnessFactor" : 0.4000000059604645
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"meshes" : [
|
||||||
|
{
|
||||||
|
"name" : "Cube",
|
||||||
|
"primitives" : [
|
||||||
|
{
|
||||||
|
"attributes" : {
|
||||||
|
"POSITION" : 0,
|
||||||
|
"NORMAL" : 1,
|
||||||
|
"TEXCOORD_0" : 2
|
||||||
|
},
|
||||||
|
"indices" : 3,
|
||||||
|
"material" : 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"accessors" : [
|
||||||
|
{
|
||||||
|
"bufferView" : 0,
|
||||||
|
"componentType" : 5126,
|
||||||
|
"count" : 24,
|
||||||
|
"max" : [
|
||||||
|
1,
|
||||||
|
1,
|
||||||
|
1
|
||||||
|
],
|
||||||
|
"min" : [
|
||||||
|
-1,
|
||||||
|
-1,
|
||||||
|
-1
|
||||||
|
],
|
||||||
|
"type" : "VEC3"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bufferView" : 1,
|
||||||
|
"componentType" : 5126,
|
||||||
|
"count" : 24,
|
||||||
|
"type" : "VEC3"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bufferView" : 2,
|
||||||
|
"componentType" : 5126,
|
||||||
|
"count" : 24,
|
||||||
|
"type" : "VEC2"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bufferView" : 3,
|
||||||
|
"componentType" : 5123,
|
||||||
|
"count" : 36,
|
||||||
|
"type" : "SCALAR"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"bufferViews" : [
|
||||||
|
{
|
||||||
|
"buffer" : 0,
|
||||||
|
"byteLength" : 288,
|
||||||
|
"byteOffset" : 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"buffer" : 0,
|
||||||
|
"byteLength" : 288,
|
||||||
|
"byteOffset" : 288
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"buffer" : 0,
|
||||||
|
"byteLength" : 192,
|
||||||
|
"byteOffset" : 576
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"buffer" : 0,
|
||||||
|
"byteLength" : 72,
|
||||||
|
"byteOffset" : 768
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"buffers" : [
|
||||||
|
{
|
||||||
|
"byteLength" : 840,
|
||||||
|
"uri" : "box.bin"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
111
models/cube.dae
111
models/cube.dae
@@ -1,111 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<COLLADA xmlns="http://www.collada.org/2005/11/COLLADASchema" version="1.4.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
|
||||||
<asset>
|
|
||||||
<contributor>
|
|
||||||
<author>Blender User</author>
|
|
||||||
<authoring_tool>Blender 2.80.74 commit date:2019-07-11, commit time:13:50, hash:06312c6d2db8</authoring_tool>
|
|
||||||
</contributor>
|
|
||||||
<created>2019-07-29T12:00:25</created>
|
|
||||||
<modified>2019-07-29T12:00:25</modified>
|
|
||||||
<unit name="meter" meter="1"/>
|
|
||||||
<up_axis>Z_UP</up_axis>
|
|
||||||
</asset>
|
|
||||||
<library_effects>
|
|
||||||
<effect id="Material-effect">
|
|
||||||
<profile_COMMON>
|
|
||||||
<technique sid="common">
|
|
||||||
<lambert>
|
|
||||||
<emission>
|
|
||||||
<color sid="emission">0 0 0 1</color>
|
|
||||||
</emission>
|
|
||||||
<diffuse>
|
|
||||||
<color sid="diffuse">0.8 0.8 0.8 1</color>
|
|
||||||
</diffuse>
|
|
||||||
<shininess>
|
|
||||||
<float sid="shininess">0.4</float>
|
|
||||||
</shininess>
|
|
||||||
<reflectivity>
|
|
||||||
<float sid="specular">0</float>
|
|
||||||
</reflectivity>
|
|
||||||
<transparency>
|
|
||||||
<float sid="alpha">1</float>
|
|
||||||
</transparency>
|
|
||||||
<index_of_refraction>
|
|
||||||
<float sid="ior">1.45</float>
|
|
||||||
</index_of_refraction>
|
|
||||||
</lambert>
|
|
||||||
</technique>
|
|
||||||
</profile_COMMON>
|
|
||||||
</effect>
|
|
||||||
</library_effects>
|
|
||||||
<library_images/>
|
|
||||||
<library_materials>
|
|
||||||
<material id="Material-material" name="Material">
|
|
||||||
<instance_effect url="#Material-effect"/>
|
|
||||||
</material>
|
|
||||||
</library_materials>
|
|
||||||
<library_geometries>
|
|
||||||
<geometry id="Cube-mesh" name="Cube">
|
|
||||||
<mesh>
|
|
||||||
<source id="Cube-mesh-positions">
|
|
||||||
<float_array id="Cube-mesh-positions-array" count="24">1 1 1 1 1 -1 1 -1 1 1 -1 -1 -1 1 1 -1 1 -1 -1 -1 1 -1 -1 -1</float_array>
|
|
||||||
<technique_common>
|
|
||||||
<accessor source="#Cube-mesh-positions-array" count="8" stride="3">
|
|
||||||
<param name="X" type="float"/>
|
|
||||||
<param name="Y" type="float"/>
|
|
||||||
<param name="Z" type="float"/>
|
|
||||||
</accessor>
|
|
||||||
</technique_common>
|
|
||||||
</source>
|
|
||||||
<source id="Cube-mesh-normals">
|
|
||||||
<float_array id="Cube-mesh-normals-array" count="18">0 0 1 0 -1 0 -1 0 0 0 0 -1 1 0 0 0 1 0</float_array>
|
|
||||||
<technique_common>
|
|
||||||
<accessor source="#Cube-mesh-normals-array" count="6" stride="3">
|
|
||||||
<param name="X" type="float"/>
|
|
||||||
<param name="Y" type="float"/>
|
|
||||||
<param name="Z" type="float"/>
|
|
||||||
</accessor>
|
|
||||||
</technique_common>
|
|
||||||
</source>
|
|
||||||
<source id="Cube-mesh-map-0">
|
|
||||||
<float_array id="Cube-mesh-map-0-array" count="72">0.625 0 0.375 0.25 0.375 0 0.625 0.25 0.375 0.5 0.375 0.25 0.625 0.5 0.375 0.75 0.375 0.5 0.625 0.75 0.375 1 0.375 0.75 0.375 0.5 0.125 0.75 0.125 0.5 0.875 0.5 0.625 0.75 0.625 0.5 0.625 0 0.625 0.25 0.375 0.25 0.625 0.25 0.625 0.5 0.375 0.5 0.625 0.5 0.625 0.75 0.375 0.75 0.625 0.75 0.625 1 0.375 1 0.375 0.5 0.375 0.75 0.125 0.75 0.875 0.5 0.875 0.75 0.625 0.75</float_array>
|
|
||||||
<technique_common>
|
|
||||||
<accessor source="#Cube-mesh-map-0-array" count="36" stride="2">
|
|
||||||
<param name="S" type="float"/>
|
|
||||||
<param name="T" type="float"/>
|
|
||||||
</accessor>
|
|
||||||
</technique_common>
|
|
||||||
</source>
|
|
||||||
<vertices id="Cube-mesh-vertices">
|
|
||||||
<input semantic="POSITION" source="#Cube-mesh-positions"/>
|
|
||||||
</vertices>
|
|
||||||
<triangles material="Material-material" count="12">
|
|
||||||
<input semantic="VERTEX" source="#Cube-mesh-vertices" offset="0"/>
|
|
||||||
<input semantic="NORMAL" source="#Cube-mesh-normals" offset="1"/>
|
|
||||||
<input semantic="TEXCOORD" source="#Cube-mesh-map-0" offset="2" set="0"/>
|
|
||||||
<p>4 0 0 2 0 1 0 0 2 2 1 3 7 1 4 3 1 5 6 2 6 5 2 7 7 2 8 1 3 9 7 3 10 5 3 11 0 4 12 3 4 13 1 4 14 4 5 15 1 5 16 5 5 17 4 0 18 6 0 19 2 0 20 2 1 21 6 1 22 7 1 23 6 2 24 4 2 25 5 2 26 1 3 27 3 3 28 7 3 29 0 4 30 2 4 31 3 4 32 4 5 33 0 5 34 1 5 35</p>
|
|
||||||
</triangles>
|
|
||||||
</mesh>
|
|
||||||
</geometry>
|
|
||||||
</library_geometries>
|
|
||||||
<library_controllers/>
|
|
||||||
<library_visual_scenes>
|
|
||||||
<visual_scene id="Scene" name="Scene">
|
|
||||||
<node id="Cube" name="Cube" type="NODE">
|
|
||||||
<matrix sid="transform">1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1</matrix>
|
|
||||||
<instance_geometry url="#Cube-mesh" name="Cube">
|
|
||||||
<bind_material>
|
|
||||||
<technique_common>
|
|
||||||
<instance_material symbol="Material-material" target="#Material-material">
|
|
||||||
<bind_vertex_input semantic="UVMap" input_semantic="TEXCOORD" input_set="0"/>
|
|
||||||
</instance_material>
|
|
||||||
</technique_common>
|
|
||||||
</bind_material>
|
|
||||||
</instance_geometry>
|
|
||||||
</node>
|
|
||||||
</visual_scene>
|
|
||||||
</library_visual_scenes>
|
|
||||||
<scene>
|
|
||||||
<instance_visual_scene url="#Scene"/>
|
|
||||||
</scene>
|
|
||||||
</COLLADA>
|
|
||||||
@@ -29,7 +29,7 @@ impl Game for TestGame {
|
|||||||
|
|
||||||
impl TestGame {
|
impl TestGame {
|
||||||
fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) {
|
fn game_start(self: &mut Self, renderer: &mut VulkanRenderer) {
|
||||||
self.cube_mesh = Some(renderer.upload_mesh(mesh::load_mesh("models/cube.dae", self.log_config.mesh_load_info).into_iter().nth(0).unwrap()));
|
self.cube_mesh = Some(renderer.upload_mesh(mesh::load_mesh("models/box.gltf", self.log_config.mesh_load_info).unwrap().into_iter().nth(0).unwrap()));
|
||||||
println!("Game started.");
|
println!("Game started.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
100
src/mesh.rs
100
src/mesh.rs
@@ -1,62 +1,64 @@
|
|||||||
use crate::vulkan::Vertex;
|
use crate::vulkan::Vertex;
|
||||||
use std::path::Path;
|
|
||||||
|
use std::time::SystemTime;
|
||||||
|
|
||||||
pub struct CPUMesh {
|
pub struct CPUMesh {
|
||||||
pub(crate) vertices: Vec<Vertex>,
|
pub(crate) vertices: Vec<Vertex>,
|
||||||
pub(crate) indices: Vec<u32>,
|
pub(crate) indices: Vec<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn load_mesh(mesh_path: &str, print_status: bool) -> Vec<CPUMesh> {
|
pub fn load_mesh(mesh_path: &str, print_status: bool) -> Result<Vec<CPUMesh>, gltf::Error> {
|
||||||
struct TempVertex {
|
let mut start_time = None;
|
||||||
pos: Option<[f32; 3]>,
|
let mut total_vertices = 0;
|
||||||
uv: Option<[f32; 2]>,
|
let mut total_indices = 0;
|
||||||
normal: Option<[f32; 3]>,
|
|
||||||
|
if print_status {
|
||||||
|
start_time = Some(SystemTime::now());
|
||||||
|
println!("Loading mesh file {}", mesh_path);
|
||||||
|
}
|
||||||
|
let (document, buffers, _textures) = gltf::import(mesh_path)?;
|
||||||
|
let mut meshes = vec![];
|
||||||
|
|
||||||
|
if print_status { println!("Mesh file loaded after {} seconds, processing...", start_time.unwrap().elapsed().unwrap().as_secs()); }
|
||||||
|
|
||||||
|
|
||||||
|
for mesh in document.meshes() {
|
||||||
|
for primitive in mesh.primitives() {
|
||||||
|
let reader = primitive.reader(|buffer| Some(&buffers[buffer.index()]));
|
||||||
|
match (reader.read_indices(), reader.read_positions(), reader.read_tex_coords(0), reader.read_normals()) {
|
||||||
|
(Some(indices), Some(positions), Some(tex_coords), Some(normals)) => {
|
||||||
|
let vertices = positions.zip(tex_coords.into_f32()).zip(normals).map(|((p, t), n)| Vertex {
|
||||||
|
position: p,
|
||||||
|
uv: t,
|
||||||
|
normal: n
|
||||||
|
}).collect();
|
||||||
|
let cpu_mesh = CPUMesh { vertices, indices: indices.into_u32().collect() };
|
||||||
|
if print_status {
|
||||||
|
let vert_count = cpu_mesh.vertices.len();
|
||||||
|
let index_count = cpu_mesh.indices.len();
|
||||||
|
|
||||||
|
println!("Loaded mesh {} after {} seconds: {} Vertices, {} Indices",
|
||||||
|
mesh.name().unwrap_or(""),
|
||||||
|
start_time.unwrap().elapsed().unwrap().as_secs(),
|
||||||
|
vert_count,
|
||||||
|
index_count);
|
||||||
|
|
||||||
|
total_vertices += vert_count;
|
||||||
|
total_indices += index_count;
|
||||||
|
}
|
||||||
|
meshes.push(cpu_mesh);
|
||||||
|
},
|
||||||
|
(None, _, _, _) => println!("Indices missing!"),
|
||||||
|
(_, None, _, _) => println!("Vertex positions missing!"),
|
||||||
|
(_, _, None, _) => println!("Tex coords missing!"),
|
||||||
|
(_, _, _, None) => println!("Normals missing!"),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if print_status {
|
if print_status {
|
||||||
println!("Start loading file {}", mesh_path);
|
println!("Finished loading file, total vertices: {}, total indices: {}", total_vertices, total_indices);
|
||||||
}
|
}
|
||||||
let file = collada::document::ColladaDocument::from_path(Path::new(mesh_path)).unwrap();
|
|
||||||
file.get_obj_set().unwrap().objects.iter().map(|model| {
|
|
||||||
if print_status {
|
|
||||||
println!("Loading mesh {}: {} Vertices", model.name, model.vertices.len());
|
|
||||||
}
|
|
||||||
let mut vertices: Vec<TempVertex> = model.vertices.iter().map(|v| {
|
|
||||||
TempVertex {
|
|
||||||
pos: Some([v.x as f32, v.y as f32, v.z as f32]),
|
|
||||||
uv: None,
|
|
||||||
normal: None
|
|
||||||
}
|
|
||||||
}).collect();
|
|
||||||
|
|
||||||
let mut indices = Vec::new();
|
Ok(meshes)
|
||||||
|
|
||||||
model.geometry.iter().for_each(|geometry| geometry.mesh.iter().for_each(|primitive| {
|
|
||||||
if let collada::PrimitiveElement::Triangles(tris) = primitive {
|
|
||||||
tris.vertices.iter().for_each(|tri| {
|
|
||||||
[tri.0, tri.1, tri.2].iter().for_each(|(vertex_index, texture_position_index, normal_index)| {
|
|
||||||
indices.push(*vertex_index as u32);
|
|
||||||
vertices[*vertex_index].uv = texture_position_index.or(None).map(|tpi| {
|
|
||||||
let tex_vertex = model.tex_vertices[tpi];
|
|
||||||
[tex_vertex.x as f32, 1.0 - tex_vertex.y as f32]
|
|
||||||
});
|
|
||||||
vertices[*vertex_index].normal = normal_index.or(None).map(|ni| {
|
|
||||||
let normal = model.normals[ni];
|
|
||||||
[normal.x as f32, normal.y as f32, normal.z as f32]
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
panic!("Mesh format Polylist not supported!");
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
|
|
||||||
let finished_vertices = vertices.iter().map(|v| Vertex {
|
|
||||||
position: v.pos.unwrap(),
|
|
||||||
uv: v.uv.unwrap(),
|
|
||||||
normal: v.normal.unwrap(),
|
|
||||||
}).collect();
|
|
||||||
|
|
||||||
CPUMesh { vertices: finished_vertices, indices }
|
|
||||||
}).collect()
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user