model loading works!

This commit is contained in:
Carter Anderson 2020-04-19 19:29:33 -07:00
parent 75ab0e21e5
commit 1143f16743
8 changed files with 171 additions and 616 deletions

View file

@ -69,4 +69,4 @@ type-uuid = "0.1"
env_logger = "0.7" env_logger = "0.7"
[profile.dev] [profile.dev]
opt-level = 3 # opt-level = 3

View file

@ -53,16 +53,35 @@ fn load_node(buffer_data: &[Vec<u8>], node: &gltf::Node, depth: i32) -> Result<M
reader reader
.read_positions() .read_positions()
.map(|v| VertexAttribute { .map(|v| VertexAttribute {
name: "position".into(), name: "Vertex_Position".into(),
values: VertexAttributeValues::Float3(v.collect()), values: VertexAttributeValues::Float3(v.collect()),
}) })
.map(|vertex_attribute| mesh.attributes.push(vertex_attribute)); .map(|vertex_attribute| mesh.attributes.push(vertex_attribute));
// let indices = reader.read_indices().unwrap(); reader
.read_normals()
.map(|v| VertexAttribute {
name: "Vertex_Normal".into(),
values: VertexAttributeValues::Float3(v.collect()),
})
.map(|vertex_attribute| mesh.attributes.push(vertex_attribute));
reader
.read_tex_coords(0)
.map(|v| VertexAttribute {
name: "Vertex_Uv".into(),
values: VertexAttributeValues::Float2(v.into_f32().collect()),
})
.map(|vertex_attribute| mesh.attributes.push(vertex_attribute));
reader.read_indices().map(|indices| {
mesh.indices = Some(indices.into_u32().collect::<Vec<u32>>());
});
return Ok(mesh); return Ok(mesh);
} }
} }
println!();
for child in node.children() { for child in node.children() {
return Ok(load_node(buffer_data, &child, depth + 1)?); return Ok(load_node(buffer_data, &child, depth + 1)?);

Binary file not shown.

View file

@ -1,142 +0,0 @@
{
"asset": {
"generator": "COLLADA2GLTF",
"version": "2.0"
},
"scene": 0,
"scenes": [
{
"nodes": [
0
]
}
],
"nodes": [
{
"children": [
1
],
"matrix": [
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
-1.0,
0.0,
0.0,
1.0,
0.0,
0.0,
0.0,
0.0,
0.0,
1.0
]
},
{
"mesh": 0
}
],
"meshes": [
{
"primitives": [
{
"attributes": {
"NORMAL": 1,
"POSITION": 2
},
"indices": 0,
"mode": 4,
"material": 0
}
],
"name": "Mesh"
}
],
"accessors": [
{
"bufferView": 0,
"byteOffset": 0,
"componentType": 5123,
"count": 36,
"max": [
23
],
"min": [
0
],
"type": "SCALAR"
},
{
"bufferView": 1,
"byteOffset": 0,
"componentType": 5126,
"count": 24,
"max": [
1.0,
1.0,
1.0
],
"min": [
-1.0,
-1.0,
-1.0
],
"type": "VEC3"
},
{
"bufferView": 1,
"byteOffset": 288,
"componentType": 5126,
"count": 24,
"max": [
0.5,
0.5,
0.5
],
"min": [
-0.5,
-0.5,
-0.5
],
"type": "VEC3"
}
],
"materials": [
{
"pbrMetallicRoughness": {
"baseColorFactor": [
0.800000011920929,
0.0,
0.0,
1.0
],
"metallicFactor": 0.0
},
"name": "Red"
}
],
"bufferViews": [
{
"buffer": 0,
"byteOffset": 576,
"byteLength": 72,
"target": 34963
},
{
"buffer": 0,
"byteOffset": 0,
"byteLength": 576,
"byteStride": 12,
"target": 34962
}
],
"buffers": [
{
"byteLength": 648,
"uri": "Box0.bin"
}
]
}

View file

@ -1,468 +0,0 @@
{
"accessors": [
{
"bufferView": 0,
"componentType": 5126,
"count": 926,
"type": "VEC2",
"max": [
0.992169142,
-0.007998445
],
"min": [
0.0107366741,
-0.992534757
]
},
{
"bufferView": 1,
"componentType": 5126,
"count": 926,
"type": "VEC3",
"max": [
1.0,
1.0,
1.0
],
"min": [
-1.0,
-1.0,
-1.0
]
},
{
"bufferView": 2,
"componentType": 5126,
"count": 926,
"type": "VEC4",
"max": [
1.0,
0.9999658,
1.0,
1.0
],
"min": [
-1.0,
-0.962171,
-1.0,
1.0
]
},
{
"bufferView": 3,
"componentType": 5126,
"count": 926,
"type": "VEC3",
"max": [
7.74559927,
12.8321095,
2.31570983
],
"min": [
-7.74559927,
-12.8321095,
-2.31570983
]
},
{
"bufferView": 4,
"componentType": 5123,
"count": 2616,
"type": "SCALAR",
"max": [
925
],
"min": [
0
]
},
{
"bufferView": 5,
"componentType": 5126,
"count": 756,
"type": "VEC2",
"max": [
0.8007193,
-0.0581455827
],
"min": [
0.351246148,
-0.361011624
]
},
{
"bufferView": 6,
"componentType": 5126,
"count": 756,
"type": "VEC3",
"max": [
1.0,
1.0,
0.999998
],
"min": [
-1.0,
-1.0,
-0.999998
]
},
{
"bufferView": 7,
"componentType": 5126,
"count": 756,
"type": "VEC4",
"max": [
0.9997526,
1.0,
0.999752462,
1.0
],
"min": [
-0.9995128,
-1.0,
-0.999752462,
1.0
]
},
{
"bufferView": 8,
"componentType": 5126,
"count": 756,
"type": "VEC3",
"max": [
0.129208073,
0.6523504,
0.129208073
],
"min": [
-0.129208073,
-0.6523504,
-0.129208073
]
},
{
"bufferView": 9,
"componentType": 5123,
"count": 3744,
"type": "SCALAR",
"max": [
755
],
"min": [
0
]
},
{
"bufferView": 10,
"componentType": 5126,
"count": 2463,
"type": "VEC2",
"max": [
0.9923278,
-0.00728821754
],
"min": [
0.3673209,
-0.413352221
]
},
{
"bufferView": 11,
"componentType": 5126,
"count": 2463,
"type": "VEC3",
"max": [
1.0,
1.0,
1.0
],
"min": [
-1.0,
-1.0,
-1.0
]
},
{
"bufferView": 12,
"componentType": 5126,
"count": 2463,
"type": "VEC4",
"max": [
1.0,
1.0,
1.0,
1.0
],
"min": [
-1.0,
-1.0,
-1.0,
-1.0
]
},
{
"bufferView": 13,
"componentType": 5126,
"count": 2463,
"type": "VEC3",
"max": [
1.03408229,
2.529281,
1.03408468
],
"min": [
-1.03408229,
-2.529281,
-1.03408468
]
},
{
"bufferView": 14,
"componentType": 5123,
"count": 9822,
"type": "SCALAR",
"max": [
2462
],
"min": [
0
]
}
],
"asset": {
"generator": "glTF Tools for Unity",
"version": "2.0"
},
"bufferViews": [
{
"buffer": 0,
"byteLength": 7408
},
{
"buffer": 0,
"byteOffset": 7408,
"byteLength": 11112
},
{
"buffer": 0,
"byteOffset": 18520,
"byteLength": 14816
},
{
"buffer": 0,
"byteOffset": 33336,
"byteLength": 11112
},
{
"buffer": 0,
"byteOffset": 44448,
"byteLength": 5232
},
{
"buffer": 0,
"byteOffset": 49680,
"byteLength": 6048
},
{
"buffer": 0,
"byteOffset": 55728,
"byteLength": 9072
},
{
"buffer": 0,
"byteOffset": 64800,
"byteLength": 12096
},
{
"buffer": 0,
"byteOffset": 76896,
"byteLength": 9072
},
{
"buffer": 0,
"byteOffset": 85968,
"byteLength": 7488
},
{
"buffer": 0,
"byteOffset": 93456,
"byteLength": 19704
},
{
"buffer": 0,
"byteOffset": 113160,
"byteLength": 29556
},
{
"buffer": 0,
"byteOffset": 142716,
"byteLength": 39408
},
{
"buffer": 0,
"byteOffset": 182124,
"byteLength": 29556
},
{
"buffer": 0,
"byteOffset": 211680,
"byteLength": 19644
}
],
"buffers": [
{
"uri": "Lantern.bin",
"byteLength": 231324
}
],
"images": [
{
"uri": "Lantern_baseColor.png"
},
{
"uri": "Lantern_roughnessMetallic.png"
},
{
"uri": "Lantern_normal.png"
},
{
"uri": "Lantern_emissive.png"
}
],
"meshes": [
{
"primitives": [
{
"attributes": {
"TEXCOORD_0": 0,
"NORMAL": 1,
"TANGENT": 2,
"POSITION": 3
},
"indices": 4,
"material": 0
}
],
"name": "LanternPole_Body"
},
{
"primitives": [
{
"attributes": {
"TEXCOORD_0": 5,
"NORMAL": 6,
"TANGENT": 7,
"POSITION": 8
},
"indices": 9,
"material": 0
}
],
"name": "LanternPole_Chain"
},
{
"primitives": [
{
"attributes": {
"TEXCOORD_0": 10,
"NORMAL": 11,
"TANGENT": 12,
"POSITION": 13
},
"indices": 14,
"material": 0
}
],
"name": "LanternPole_Lantern"
}
],
"materials": [
{
"pbrMetallicRoughness": {
"baseColorFactor": [
0.214041144,
0.214041144,
0.214041144,
1.0
],
"baseColorTexture": {
"index": 0
},
"metallicRoughnessTexture": {
"index": 1
}
},
"normalTexture": {
"index": 2
},
"emissiveFactor": [
1.0,
1.0,
1.0
],
"emissiveTexture": {
"index": 3
},
"name": "LanternPost_Mat"
}
],
"nodes": [
{
"mesh": 0,
"translation": [
-3.82315421,
13.01603,
0.0
],
"name": "LanternPole_Body"
},
{
"mesh": 1,
"translation": [
-9.582001,
21.0378723,
0.0
],
"name": "LanternPole_Chain"
},
{
"mesh": 2,
"translation": [
-9.582007,
18.0091515,
0.0
],
"name": "LanternPole_Lantern"
},
{
"children": [
0,
1,
2
],
"name": "Lantern"
}
],
"scene": 0,
"scenes": [
{
"nodes": [
3
]
}
],
"textures": [
{
"source": 0
},
{
"source": 1
},
{
"source": 2
},
{
"source": 3
}
]
}

BIN
examples/assets/Monkey.bin Normal file

Binary file not shown.

100
examples/assets/Monkey.gltf Normal file
View file

@ -0,0 +1,100 @@
{
"asset" : {
"generator" : "Khronos glTF Blender I/O v1.1.46",
"version" : "2.0"
},
"scene" : 0,
"scenes" : [
{
"name" : "Scene",
"nodes" : [
0
]
}
],
"nodes" : [
{
"mesh" : 0,
"name" : "Suzanne"
}
],
"meshes" : [
{
"name" : "Suzanne",
"primitives" : [
{
"attributes" : {
"POSITION" : 0,
"NORMAL" : 1,
"TEXCOORD_0" : 2
},
"indices" : 3
}
]
}
],
"accessors" : [
{
"bufferView" : 0,
"componentType" : 5126,
"count" : 1968,
"max" : [
1.3671875,
0.8515625,
0.984375
],
"min" : [
-1.3671875,
-0.8515625,
-0.984375
],
"type" : "VEC3"
},
{
"bufferView" : 1,
"componentType" : 5126,
"count" : 1968,
"type" : "VEC3"
},
{
"bufferView" : 2,
"componentType" : 5126,
"count" : 1968,
"type" : "VEC2"
},
{
"bufferView" : 3,
"componentType" : 5123,
"count" : 2904,
"type" : "SCALAR"
}
],
"bufferViews" : [
{
"buffer" : 0,
"byteLength" : 23616,
"byteOffset" : 0
},
{
"buffer" : 0,
"byteLength" : 23616,
"byteOffset" : 23616
},
{
"buffer" : 0,
"byteLength" : 15744,
"byteOffset" : 47232
},
{
"buffer" : 0,
"byteLength" : 5808,
"byteOffset" : 62976
}
],
"buffers" : [
{
"byteLength" : 68784,
"uri" : "Monkey.bin"
}
]
}

View file

@ -1,6 +1,52 @@
use bevy::{gltf, prelude::*}; use bevy::{gltf, prelude::*};
fn main() { fn main() {
let mesh = gltf::load_gltf("examples/assets/Box.gltf").unwrap().unwrap(); App::build()
// App::build().add_default_plugins().run(); .add_default_plugins()
.add_startup_system(setup)
.add_system_init(bevy::input::system::exit_on_esc_system)
.run();
}
/// set up a simple scene
fn setup(world: &mut World, resources: &mut Resources) {
let mesh = gltf::load_gltf("examples/assets/Monkey.gltf")
.unwrap()
.unwrap();
// create a cube and a plane mesh
let mut mesh_storage = resources.get_mut::<AssetStorage<Mesh>>().unwrap();
let mesh_handle = mesh_storage.add(mesh);
// create materials for our cube and plane
let mut material_storage = resources
.get_mut::<AssetStorage<StandardMaterial>>()
.unwrap();
let material_handle = material_storage.add(StandardMaterial {
albedo: Color::rgb(0.5, 0.4, 0.3),
..Default::default()
});
// add entities to the world
world
.build()
// mesh
.add_entity(MeshEntity {
mesh: mesh_handle,
material: material_handle,
..Default::default()
})
// light
.add_entity(LightEntity {
translation: Translation::new(4.0, -4.0, 5.0),
..Default::default()
})
// camera
.add_entity(CameraEntity {
local_to_world: LocalToWorld(Mat4::look_at_rh(
Vec3::new(2.0, -6.0, 2.0),
Vec3::new(0.0, 0.0, 0.0),
Vec3::new(0.0, 0.0, 1.0),
)),
..Default::default()
});
} }