Skip to content

Bevy 0.6 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,913 changes: 847 additions & 1,066 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
name = "blender_bevy_toolkit"
version = "0.1.0"
version = "0.2.0"
authors = ["Geoffrey Irons <[email protected]>"]
edition = "2018"
edition = "2021"
license = "MIT"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -13,7 +13,7 @@ serde = {version = "1", features = ["derive"]}
smallvec = { version = "1.4", features = ["serde"] }

[dependencies.bevy]
version="0.5.0"
version="0.6.0"

[dependencies.bevy_rapier3d]
version="0.9.0"
version="0.12.0"
6 changes: 3 additions & 3 deletions blender_bevy_toolkit/core_definitions/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ def serialize_mesh(obj):
if mesh.uv_layers:

uv_raw = mesh.uv_layers[0].data[loop_index].uv
uv = (uv_raw[0], uv_raw[1], 0.0)
uv = (uv_raw[0], uv_raw[1])
else:
uv = (0.0, 0.0, 0.0)
uv = (0.0, 0.0)

dedup = (position, normal, uv)
if dedup not in dedup_data_lookup:
Expand Down Expand Up @@ -112,7 +112,7 @@ def serialize_mesh(obj):
for normal in normals:
out_data += struct.pack("fff", *normal)
for uv in uv0:
out_data += struct.pack("fff", *uv)
out_data += struct.pack("ff", *uv)
for index in indices:
out_data += struct.pack("III", *index)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"field": "body_status",
"type": "u8enum",
"default": ["dynamic", "static", "kinematic"],
"default": ["dynamic", "static", "kinematic (position)", "kinematic (velocity)"],
"description": "Dynamic bodies respond to forces. Static bodies do not move. Kinematic bodies must be moved manually but internally track velocity so they push other dynamic bodies properly"
},
{
Expand All @@ -33,18 +33,6 @@
"type": "bool",
"default": false,
"description": "allow this body to sleep when velocity is low - this increases performance"
},
{
"field": "mass_extra",
"type": "f32",
"default": 0.0,
"description": "mass of the body additional to the mass of the collision shapes"
},
{
"field": "inertia_extra",
"type": "vec3",
"default": [0.0, 0.0, 0.0],
"description": "inertia of the body additional to the mass of the collision shapes"
}
]
}
20 changes: 15 additions & 5 deletions examples/scenes/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! This example loads the various test scenes
use bevy::prelude::*;
use bevy_rapier3d::physics::RapierPhysicsPlugin;
use bevy_rapier3d::physics::{NoUserData, RapierPhysicsPlugin};
use bevy_rapier3d::prelude::*;
use blender_bevy_toolkit::BlendLoadPlugin;


fn spawn_scene(
mut commands: Commands,
asset_server: Res<AssetServer>,
Expand All @@ -18,7 +20,7 @@ fn spawn_scene(
});

// Create a Light
commands.spawn().insert_bundle(LightBundle {
commands.spawn().insert_bundle(PointLightBundle {
transform: Transform::from_translation(Vec3::new(0.0, 8.0, 0.0)),
..Default::default()
});
Expand All @@ -27,13 +29,21 @@ fn spawn_scene(
scene_spawner.spawn_dynamic(scene_handle);
}


fn setup_physics(mut physics_config: ResMut<RapierConfiguration>) {
physics_config.gravity.y = 0.0;
physics_config.gravity.z = -9.8;
}


fn main() {
println!("Running example scenes");

App::build()
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(RapierPhysicsPlugin)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.add_plugin(BlendLoadPlugin::default())
.add_startup_system(spawn_scene.system())
.add_system(setup_physics.system())
.run();
}
2 changes: 1 addition & 1 deletion src/blend_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bevy::prelude::*;

/// This component loads another collection and spawns it as a child
/// of the entity with this component
#[derive(Reflect, Default)]
#[derive(Reflect, Default, Component)]
#[reflect(Component)]
pub struct BlendCollectionLoader {
path: String,
Expand Down
2 changes: 1 addition & 1 deletion src/blend_label.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::prelude::*;

/// Component that contains the name of the object
#[derive(Reflect, Default)]
#[derive(Reflect, Default, Component, Debug)]
#[reflect(Component)]
pub struct BlendLabel {
pub name: String,
Expand Down
29 changes: 19 additions & 10 deletions src/blend_mesh.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use bevy::{
asset::{AssetLoader, LoadContext},
prelude::*,
render::{mesh::Indices, pipeline::PrimitiveTopology},
render::{mesh::Indices, render_resource::PrimitiveTopology},
utils::BoxedFuture,
};
use std::convert::TryInto;

#[derive(Reflect, Default)]
#[derive(Reflect, Default, Component)]
#[reflect(Component)] // this tells the reflect derive to also reflect component behaviors
pub struct BlendMeshLoader {
path: String,
}

type FVec3Arr = Vec<[f32; 3]>;
type FVec2Arr = Vec<[f32; 2]>;

pub fn blend_mesh_loader(
mut commands: Commands,
Expand Down Expand Up @@ -40,10 +41,8 @@ pub fn blend_mesh_loader(
commands.entity(entity).insert_bundle((
bundle.mesh,
bundle.material,
bundle.main_pass,
bundle.draw,
bundle.visible,
bundle.render_pipelines,
bundle.visibility,
bundle.computed_visibility,
));
}
}
Expand Down Expand Up @@ -80,7 +79,6 @@ pub fn load_mesh(data: &[u8]) -> Mesh {
mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, positions);
mesh.set_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
mesh.set_attribute(Mesh::ATTRIBUTE_UV_0, uv0s);

mesh
}

Expand All @@ -105,6 +103,17 @@ fn parse_vec3_array(data: &[u8], num_elements: usize) -> Vec<[f32; 3]> {
}
out_array
}
/// Converts a slice of u8's into a vec of f32;s
fn parse_vec2_array(data: &[u8], num_elements: usize) -> Vec<[f32; 2]> {
let mut out_array = Vec::with_capacity(num_elements);
for i in 0..num_elements {
out_array.push([
get_f32(&data[i * 12..]),
get_f32(&data[(i * 8 + 4)..]),
]);
}
out_array
}
/// Converts a slice of u8's into a vec of u16's
fn parse_u32_array(data: &[u8], num_elements: usize) -> Vec<u32> {
let mut out_array = Vec::with_capacity(num_elements);
Expand All @@ -117,18 +126,18 @@ fn parse_u32_array(data: &[u8], num_elements: usize) -> Vec<u32> {
/// Converts the bytes of a binary stl file into a vector of face indices,
/// vertices and vertex normals.
/// Expects correctly formatted STL files
fn extact_buffers_from_mesh(mesh: &[u8]) -> (Vec<u32>, FVec3Arr, FVec3Arr, FVec3Arr) {
fn extact_buffers_from_mesh(mesh: &[u8]) -> (Vec<u32>, FVec3Arr, FVec3Arr, FVec2Arr) {
let num_verts = u16::from_le_bytes(mesh[0..2].try_into().unwrap()) as usize;
let num_faces = u16::from_le_bytes(mesh[2..4].try_into().unwrap()) as usize;

let verts_start = 4;
let normals_start = verts_start + num_verts * 4 * 3;
let uv0_start = normals_start + num_verts * 4 * 3;
let indices_start = uv0_start + num_verts * 4 * 3;
let indices_start = uv0_start + num_verts * 4 * 2;

let positions = parse_vec3_array(&mesh[verts_start..], num_verts);
let normals = parse_vec3_array(&mesh[normals_start..], num_verts);
let uv0 = parse_vec3_array(&mesh[uv0_start..], num_verts);
let uv0 = parse_vec2_array(&mesh[uv0_start..], num_verts);
let indices = parse_u32_array(&mesh[indices_start..], num_faces * 3);

(indices, positions, normals, uv0)
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ impl BlendLoadPlugin {
}

impl Plugin for BlendLoadPlugin {
fn build(&self, app: &mut AppBuilder) {
fn build(&self, app: &mut App) {
app.register_type::<blend_label::BlendLabel>();
app.register_type::<blend_collection::BlendCollectionLoader>();
app.register_type::<blend_mesh::BlendMeshLoader>();
app.register_type::<rapier_physics::RigidBodyDescription>();
app.register_type::<rapier_physics::ColliderDescription>();

app.init_asset_loader::<blend_mesh::BlendMeshAssetLoader>();

app.add_system(blend_collection::blend_collection_loader.system());
Expand Down
Loading