Skip to content

Commit 3210baf

Browse files
committed
Can load scene based on command line parameter. Meshes now load properly. Issue was caused by bevyengine/bevy#3604
1 parent 055f2ef commit 3210baf

File tree

9 files changed

+41
-21
lines changed

9 files changed

+41
-21
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ assets:
55
rm -r assets || true
66
blender -b test_scenes/Cube.blend --python export.py -- --output-file="assets/scenes/Cube.scn" --log-level=DEBUG
77
blender -b test_scenes/PhysicsTest.blend --python export.py -- --output-file="assets/scenes/PhysicsTest.scn" --log-level=DEBUG
8+
blender -b test_scenes/Heirarchy.blend --python export.py -- --output-file="assets/scenes/Heirarchy.scn" --log-level=DEBUG
89

910
run:
10-
cargo run --example scenes
11+
cargo run --example scenes -- scenes/PhysicsTest.scn

blender_bevy_toolkit/core_definitions/mesh.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ def serialize_mesh(obj):
7676
if mesh.uv_layers:
7777

7878
uv_raw = mesh.uv_layers[0].data[loop_index].uv
79-
uv = (uv_raw[0], uv_raw[1], 0.0)
79+
uv = (uv_raw[0], uv_raw[1])
8080
else:
81-
uv = (0.0, 0.0, 0.0)
81+
uv = (0.0, 0.0)
8282

8383
dedup = (position, normal, uv)
8484
if dedup not in dedup_data_lookup:
@@ -112,7 +112,7 @@ def serialize_mesh(obj):
112112
for normal in normals:
113113
out_data += struct.pack("fff", *normal)
114114
for uv in uv0:
115-
out_data += struct.pack("fff", *uv)
115+
out_data += struct.pack("ff", *uv)
116116
for index in indices:
117117
out_data += struct.pack("III", *index)
118118

examples/scenes/main.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
use bevy::prelude::*;
33
use bevy_rapier3d::physics::{NoUserData, RapierPhysicsPlugin};
44
use blender_bevy_toolkit::BlendLoadPlugin;
5+
use std::env;
6+
57

68
fn spawn_scene(
79
mut commands: Commands,
@@ -23,12 +25,21 @@ fn spawn_scene(
2325
..Default::default()
2426
});
2527

26-
let scene_handle: Handle<DynamicScene> = asset_server.load("scenes/PhysicsTest.scn");
28+
let args: Vec<String> = env::args().collect();
29+
30+
if args.len() != 2 {
31+
println!("Please specify a scene file to load. For example:\n cargo run --example scenes -- scenes/Heirarchy.scn");
32+
std::process::exit(1);
33+
}
34+
35+
println!("Running scene: {}", args[1]);
36+
37+
let scene_handle: Handle<DynamicScene> = asset_server.load(args[1].as_str());
2738
scene_spawner.spawn_dynamic(scene_handle);
2839
}
2940

3041
fn main() {
31-
println!("Running example scenes");
42+
3243

3344
App::new()
3445
.add_plugins(DefaultPlugins)

src/blend_collection.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ use bevy::prelude::*;
22

33
/// This component loads another collection and spawns it as a child
44
/// of the entity with this component
5-
#[derive(Reflect, Default)]
5+
#[derive(Reflect, Default, Component)]
66
#[reflect(Component)]
7-
#[derive(Component)]
87
pub struct BlendCollectionLoader {
98
path: String,
109
}

src/blend_label.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use bevy::prelude::*;
22

33
/// Component that contains the name of the object
4-
#[derive(Reflect, Default)]
4+
#[derive(Reflect, Default, Component)]
55
#[reflect(Component)]
6-
#[derive(Component)]
76
pub struct BlendLabel {
87
pub name: String,
98
}

src/blend_mesh.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use bevy::{
66
};
77
use std::convert::TryInto;
88

9-
#[derive(Reflect, Default)]
9+
#[derive(Reflect, Default, Component)]
1010
#[reflect(Component)] // this tells the reflect derive to also reflect component behaviors
11-
#[derive(Component)]
1211
pub struct BlendMeshLoader {
1312
path: String,
1413
}
1514

1615
type FVec3Arr = Vec<[f32; 3]>;
16+
type FVec2Arr = Vec<[f32; 2]>;
1717

1818
pub fn blend_mesh_loader(
1919
mut commands: Commands,
@@ -79,14 +79,15 @@ pub fn load_mesh(data: &[u8]) -> Mesh {
7979
let indices = Indices::U32(indices);
8080

8181
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
82-
mesh.set_indices(Some(indices));
82+
8383
mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, positions);
8484
mesh.set_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
8585
mesh.set_attribute(Mesh::ATTRIBUTE_UV_0, uv0s);
86-
86+
mesh.set_indices(Some(indices));
8787
mesh
8888
}
8989

90+
9091
/// Reads a f32 from a buffer
9192
fn get_f32(arr: &[u8]) -> f32 {
9293
f32::from_le_bytes(arr[0..4].try_into().unwrap())
@@ -108,6 +109,17 @@ fn parse_vec3_array(data: &[u8], num_elements: usize) -> Vec<[f32; 3]> {
108109
}
109110
out_array
110111
}
112+
/// Converts a slice of u8's into a vec of f32;s
113+
fn parse_vec2_array(data: &[u8], num_elements: usize) -> Vec<[f32; 2]> {
114+
let mut out_array = Vec::with_capacity(num_elements);
115+
for i in 0..num_elements {
116+
out_array.push([
117+
get_f32(&data[i * 12..]),
118+
get_f32(&data[(i * 8 + 4)..]),
119+
]);
120+
}
121+
out_array
122+
}
111123
/// Converts a slice of u8's into a vec of u16's
112124
fn parse_u32_array(data: &[u8], num_elements: usize) -> Vec<u32> {
113125
let mut out_array = Vec::with_capacity(num_elements);
@@ -120,18 +132,18 @@ fn parse_u32_array(data: &[u8], num_elements: usize) -> Vec<u32> {
120132
/// Converts the bytes of a binary stl file into a vector of face indices,
121133
/// vertices and vertex normals.
122134
/// Expects correctly formatted STL files
123-
fn extact_buffers_from_mesh(mesh: &[u8]) -> (Vec<u32>, FVec3Arr, FVec3Arr, FVec3Arr) {
135+
fn extact_buffers_from_mesh(mesh: &[u8]) -> (Vec<u32>, FVec3Arr, FVec3Arr, FVec2Arr) {
124136
let num_verts = u16::from_le_bytes(mesh[0..2].try_into().unwrap()) as usize;
125137
let num_faces = u16::from_le_bytes(mesh[2..4].try_into().unwrap()) as usize;
126138

127139
let verts_start = 4;
128140
let normals_start = verts_start + num_verts * 4 * 3;
129141
let uv0_start = normals_start + num_verts * 4 * 3;
130-
let indices_start = uv0_start + num_verts * 4 * 3;
142+
let indices_start = uv0_start + num_verts * 4 * 2;
131143

132144
let positions = parse_vec3_array(&mesh[verts_start..], num_verts);
133145
let normals = parse_vec3_array(&mesh[normals_start..], num_verts);
134-
let uv0 = parse_vec3_array(&mesh[uv0_start..], num_verts);
146+
let uv0 = parse_vec2_array(&mesh[uv0_start..], num_verts);
135147
let indices = parse_u32_array(&mesh[indices_start..], num_faces * 3);
136148

137149
(indices, positions, normals, uv0)

src/rapier_physics.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ use std::convert::TryInto;
66

77
use smallvec;
88

9-
#[derive(Reflect, Default)]
9+
#[derive(Reflect, Default, Component)]
1010
#[reflect(Component)]
11-
#[derive(Component)]
1211
/// A RigidBodyDescription is only present until the body_description_to_builder system runs,
1312
/// upon which it is converted to a rapier::dynamics::RigidBodyBuilder with matching properties.
1413
/// Then the bevy rapier plugin converts that into a RigidBodyHandle component for the purpose
@@ -141,9 +140,8 @@ pub fn body_description_to_builder(
141140
}
142141

143142
/// Maps to a [`ColliderBundle`]
144-
#[derive(Reflect, Default, Debug)]
143+
#[derive(Reflect, Default, Debug, Component)]
145144
#[reflect(Component)]
146-
#[derive(Component)]
147145
pub struct ColliderDescription {
148146
/// Maps to [`ColliderMaterial`] friction
149147
friction: f32,

test_scenes/Cube.blend

-46.8 KB
Binary file not shown.

test_scenes/Heirarchy.blend

774 KB
Binary file not shown.

0 commit comments

Comments
 (0)