diff --git a/Cargo.toml b/Cargo.toml index f8f2e1b343..c8e5adc180 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -69,4 +69,4 @@ type-uuid = "0.1" env_logger = "0.7" [profile.dev] -opt-level = 3 \ No newline at end of file +# opt-level = 3 \ No newline at end of file diff --git a/bevy_gltf/src/lib.rs b/bevy_gltf/src/lib.rs index fde298a562..512fb72ede 100644 --- a/bevy_gltf/src/lib.rs +++ b/bevy_gltf/src/lib.rs @@ -53,16 +53,35 @@ fn load_node(buffer_data: &[Vec], node: &gltf::Node, depth: i32) -> Result>()); + }); + return Ok(mesh); } } - println!(); for child in node.children() { return Ok(load_node(buffer_data, &child, depth + 1)?); diff --git a/examples/assets/Box.glb b/examples/assets/Box.glb deleted file mode 100644 index 95ec886b6b..0000000000 Binary files a/examples/assets/Box.glb and /dev/null differ diff --git a/examples/assets/Box.gltf b/examples/assets/Box.gltf deleted file mode 100644 index 7f603f07fc..0000000000 --- a/examples/assets/Box.gltf +++ /dev/null @@ -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" - } - ] -} diff --git a/examples/assets/Lantern.gltf b/examples/assets/Lantern.gltf deleted file mode 100644 index 74ef9165db..0000000000 --- a/examples/assets/Lantern.gltf +++ /dev/null @@ -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 - } - ] -} \ No newline at end of file diff --git a/examples/assets/Monkey.bin b/examples/assets/Monkey.bin new file mode 100644 index 0000000000..bf7d3426ec Binary files /dev/null and b/examples/assets/Monkey.bin differ diff --git a/examples/assets/Monkey.gltf b/examples/assets/Monkey.gltf new file mode 100644 index 0000000000..a6d9f0212e --- /dev/null +++ b/examples/assets/Monkey.gltf @@ -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" + } + ] +} diff --git a/examples/load_model.rs b/examples/load_model.rs index 7facb3c5f5..6c85434b9c 100644 --- a/examples/load_model.rs +++ b/examples/load_model.rs @@ -1,6 +1,52 @@ use bevy::{gltf, prelude::*}; fn main() { - let mesh = gltf::load_gltf("examples/assets/Box.gltf").unwrap().unwrap(); - // App::build().add_default_plugins().run(); + App::build() + .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::>().unwrap(); + let mesh_handle = mesh_storage.add(mesh); + + // create materials for our cube and plane + let mut material_storage = resources + .get_mut::>() + .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() + }); }