Skip to content
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

Fixing Clippy warnings, some refactors and optimizations #96

Merged
merged 6 commits into from
Sep 3, 2024
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 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ Cargo.lock
.vscode/

output/
.DS_Store
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ nodejs = ["wasm", "dep:js-sys", "dep:wasm-bindgen"]
[dependencies]
bimap = { version = "0.6" }
bytecheck = { version = "0.6", optional = true, default-features = false }
glam = { version = "0.28", features = [ "core-simd", "libm" ] }
glam = { version = "0.27", features = [ "core-simd", "libm" ] }
js-sys = { version = "0.3", optional = true }
rkyv = { version = "0.7", optional = true, features = [ "validation" ] }
serde = { version= "1.0", optional = true, features = [ "serde_derive" ] }
Expand Down
2 changes: 1 addition & 1 deletion demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ edition = "2021"
resolver = "2"

[dependencies]
bevy = "0.13"
bevy = "0.14"
ozz-animation-rs = { path = "../" }
reqwest = "0.11"
17 changes: 10 additions & 7 deletions demo/src/blend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub struct OzzBlend {
spine_trans: Vec<OzzTransform>,
}

unsafe impl Send for OzzBlend {}
unsafe impl Sync for OzzBlend {}

impl OzzBlend {
pub async fn new() -> Box<dyn OzzExample> {
let ((mut ar_skeleton, mut ar_animation1), (mut ar_animation2, mut ar_animation3)) = try_zip(
Expand Down Expand Up @@ -100,7 +103,7 @@ impl OzzBlend {

ob.bone_trans.reserve(bone_count);
ob.spine_trans.reserve(spine_count);
return Box::new(ob);
Box::new(ob)
}
}

Expand All @@ -110,11 +113,11 @@ impl OzzExample for OzzBlend {
}

fn bone_trans(&self) -> &[OzzTransform] {
return &self.bone_trans;
&self.bone_trans
}

fn spine_trans(&self) -> &[OzzTransform] {
return &self.spine_trans;
&self.spine_trans
}

fn update(&mut self, time: Time) {
Expand Down Expand Up @@ -184,26 +187,26 @@ impl OzzExample for OzzBlend {
let bone_rot = Quat::from_mat3(&Mat3::from_cols(bone_dir, bone_rot_y, bone_rot_z));

self.bone_trans.push(OzzTransform {
scale: scale,
scale,
rotation: bone_rot,
position: parent_pos,
});

let parent_rot = Quat::from_mat4(parent);
self.spine_trans.push(OzzTransform {
scale: scale,
scale,
rotation: parent_rot,
position: parent_pos,
});

if self.skeleton.is_leaf(i as i16) {
let current_rot = Quat::from_mat4(current);
self.spine_trans.push(OzzTransform {
scale: scale,
scale,
rotation: current_rot,
position: current_pos,
});
}
}
}
}
}
48 changes: 22 additions & 26 deletions demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ enum OzzType {
TwoBoneIK,
}

const OZZ_TYPES: [(OzzType, &'static str); 3] = [
const OZZ_TYPES: [(OzzType, &str); 3] = [
(OzzType::Playback, "Playback"),
(OzzType::Blend, "Blend"),
(OzzType::TwoBoneIK, "TwoBoneIK"),
Expand Down Expand Up @@ -87,15 +87,15 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials
// ground
commands.spawn(PbrBundle {
mesh: meshes.add(Rectangle::new(20.0, 30.0)),
material: materials.add(Color::rgb(1.0, 0.96, 0.95)),
material: materials.add(Color::srgb(1.0, 0.96, 0.95)),
transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2))
.with_translation(Vec3::new(-2.0, 0.0, -5.0)),
..default()
});

// bones
let bone_mesh = meshes.add(build_bone_mesh());
let bone_material = materials.add(Color::rgb(0.68, 0.68, 0.8));
let bone_material = materials.add(Color::srgb(0.68, 0.68, 0.8));
for i in 0..BONE_COUNT {
commands.spawn((
PbrBundle {
Expand All @@ -120,7 +120,7 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials
PbrBundle {
mesh: meshes.add(Cuboid::new(2.0, 1.0, 1.0)),
material: materials.add(StandardMaterial {
base_color: Color::rgb(0.4, 0.61, 0.98),
base_color: Color::srgb(0.4, 0.61, 0.98),
unlit: true,
cull_mode: None,
..default()
Expand All @@ -139,7 +139,6 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials
shadows_enabled: true,
shadow_depth_bias: 0.05,
shadow_normal_bias: 0.9,
..default()
},
transform: Transform::from_xyz(-3.0, 1.6, -4.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
..default()
Expand Down Expand Up @@ -196,7 +195,7 @@ fn update_bones(mut query: Query<(&mut Transform, &mut Visibility, &BoneIndex)>,
if let Some(example) = &oc.iter().last().unwrap().example {
// only one OzzComponent
let bone_trans = example.bone_trans();
if bone_trans.len() > 0 {
if !bone_trans.is_empty() {
for (mut transform, mut visibility, idx) in query.iter_mut() {
if idx.0 < bone_trans.len() {
*visibility = Visibility::Visible;
Expand All @@ -215,7 +214,7 @@ fn draw_spines(mut gizmos: Gizmos, oc: Query<&OzzComponent>) {
if let Some(example) = &oc.iter().last().unwrap().example {
// only one OzzComponent
let spine_trans = example.spine_trans();
if spine_trans.len() > 0 {
if !spine_trans.is_empty() {
for trans in spine_trans {
draw_gizmos(&mut gizmos, trans);
}
Expand All @@ -226,26 +225,23 @@ fn draw_spines(mut gizmos: Gizmos, oc: Query<&OzzComponent>) {

#[rustfmt::skip]
fn build_bone_mesh() -> Mesh {
let c = vec![
Vec3::new(1.0, 0.0, 0.0),
let c = [Vec3::new(1.0, 0.0, 0.0),
Vec3::new(0.2, 0.1, 0.1),
Vec3::new(0.2, 0.1, -0.1),
Vec3::new(0.2, -0.1, -0.1),
Vec3::new(0.2, -0.1, 0.1),
Vec3::new(0.0, 0.0, 0.0),
];
let n = vec![
Vec3::cross(c[2] - c[1], c[2] - c[0]).normalize(),
Vec3::new(0.0, 0.0, 0.0)];
let n = [Vec3::cross(c[2] - c[1], c[2] - c[0]).normalize(),
Vec3::cross(c[1] - c[2], c[1] - c[5]).normalize(),
Vec3::cross(c[3] - c[2], c[3] - c[0]).normalize(),
Vec3::cross(c[2] - c[3], c[2] - c[5]).normalize(),
Vec3::cross(c[4] - c[3], c[4] - c[0]).normalize(),
Vec3::cross(c[3] - c[4], c[3] - c[5]).normalize(),
Vec3::cross(c[1] - c[4], c[1] - c[0]).normalize(),
Vec3::cross(c[4] - c[1], c[4] - c[5]).normalize(),
];
Vec3::cross(c[4] - c[1], c[4] - c[5]).normalize()];


let mesh = Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default())
Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::default())
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, vec![
c[0], c[2], c[1],
c[5], c[1], c[2],
Expand All @@ -265,23 +261,22 @@ fn build_bone_mesh() -> Mesh {
n[5], n[5], n[5],
n[6], n[6], n[6],
n[7], n[7], n[7],
]);
return mesh;
])
}

fn draw_ground(gizmos: &mut Gizmos) {
for i in -12..=8 {
gizmos.line(
Vec3::new(i as f32, 0.0, -20.0),
Vec3::new(i as f32, 0.0, 10.0),
Color::rgba(0.25, 0.25, 0.25, 0.4),
Color::srgba(0.25, 0.25, 0.25, 0.4),
);
}
for i in -20..=10 {
gizmos.line(
Vec3::new(-12.0, 0.0, i as f32),
Vec3::new(8.0, 0.0, i as f32),
Color::rgba(0.25, 0.25, 0.25, 0.4),
Color::srgba(0.25, 0.25, 0.25, 0.4),
);
}
}
Expand All @@ -292,21 +287,22 @@ fn draw_gizmos(gizmos: &mut Gizmos, trans: &OzzTransform) {
let normal_z = trans.rotation.mul_vec3(Vec3::Z).normalize();
gizmos.circle(
trans.position,
Direction3d::new_unchecked(normal_x),
Dir3::new_unchecked(normal_x),
trans.scale * 0.25,
Color::rgba(1.0, 0.1, 0.1, 0.5),
Color::srgba(1.0, 0.1, 0.1, 0.5),
);

gizmos.circle(
trans.position,
Direction3d::new_unchecked(normal_y),
Dir3::new_unchecked(normal_y),
trans.scale * 0.25,
Color::rgba(0.1, 1.0, 0.1, 0.5),
Color::srgba(0.1, 1.0, 0.1, 0.5),
);
gizmos.circle(
trans.position,
Direction3d::new_unchecked(normal_z),
Dir3::new_unchecked(normal_z),
trans.scale * 0.25,
Color::rgba(0.1, 0.1, 1.0, 0.5),
Color::srgba(0.1, 0.1, 1.0, 0.5),
);
}

Expand Down
15 changes: 9 additions & 6 deletions demo/src/playback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pub struct OzzPlayback {
spine_trans: Vec<OzzTransform>,
}

unsafe impl Send for OzzPlayback {}
unsafe impl Sync for OzzPlayback {}

impl OzzPlayback {
pub async fn new() -> Box<dyn OzzExample> {
let (mut ar_skeleton, mut ar_animation) = try_zip(
Expand Down Expand Up @@ -60,7 +63,7 @@ impl OzzPlayback {

oc.bone_trans.reserve(bone_count);
oc.spine_trans.reserve(spine_count);
return Box::new(oc);
Box::new(oc)
}
}

Expand All @@ -70,11 +73,11 @@ impl OzzExample for OzzPlayback {
}

fn bone_trans(&self) -> &[OzzTransform] {
return &self.bone_trans;
&self.bone_trans
}

fn spine_trans(&self) -> &[OzzTransform] {
return &self.spine_trans;
&self.spine_trans
}

fn update(&mut self, time: Time) {
Expand Down Expand Up @@ -113,22 +116,22 @@ impl OzzExample for OzzPlayback {
let bone_rot = Quat::from_mat3(&Mat3::from_cols(bone_dir, bone_rot_y, bone_rot_z));

self.bone_trans.push(OzzTransform {
scale: scale,
scale,
rotation: bone_rot,
position: parent_pos,
});

let parent_rot = Quat::from_mat4(parent);
self.spine_trans.push(OzzTransform {
scale: scale,
scale,
rotation: parent_rot,
position: parent_pos,
});

if self.skeleton.is_leaf(i as i16) {
let current_rot = Quat::from_mat4(current);
self.spine_trans.push(OzzTransform {
scale: scale,
scale,
rotation: current_rot,
position: current_pos,
});
Expand Down
21 changes: 12 additions & 9 deletions demo/src/two_bone_ik.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub struct OzzTwoBoneIK {
spine_trans: Vec<OzzTransform>,
}

unsafe impl Send for OzzTwoBoneIK {}
unsafe impl Sync for OzzTwoBoneIK {}

impl OzzTwoBoneIK {
pub async fn new() -> Box<dyn OzzExample> {
let mut ar_skeleton = load_archive("/two_bone_ik/skeleton.ozz").await.unwrap();
Expand Down Expand Up @@ -61,7 +64,7 @@ impl OzzTwoBoneIK {

oc.bone_trans.reserve(bone_count);
oc.spine_trans.reserve(spine_count);
return Box::new(oc);
Box::new(oc)
}
}

Expand All @@ -71,11 +74,11 @@ impl OzzExample for OzzTwoBoneIK {
}

fn bone_trans(&self) -> &[OzzTransform] {
return &self.bone_trans;
&self.bone_trans
}

fn spine_trans(&self) -> &[OzzTransform] {
return &self.spine_trans;
&self.spine_trans
}

fn update(&mut self, time: Time) {
Expand Down Expand Up @@ -108,11 +111,11 @@ impl OzzExample for OzzTwoBoneIK {
self.ik_job.set_twist_angle(0.0);
self.ik_job.set_pole_vector(Vec3::new(0.0, 1.0, 0.0).into());
self.ik_job
.set_start_joint(self.models1.buf().unwrap()[start_joint as usize].into());
.set_start_joint(self.models1.buf().unwrap()[start_joint as usize]);
self.ik_job
.set_mid_joint(self.models1.buf().unwrap()[mid_joint as usize].into());
.set_mid_joint(self.models1.buf().unwrap()[mid_joint as usize]);
self.ik_job
.set_end_joint(self.models1.buf().unwrap()[end_joint as usize].into());
.set_end_joint(self.models1.buf().unwrap()[end_joint as usize]);

self.ik_job.run().unwrap();

Expand Down Expand Up @@ -169,22 +172,22 @@ impl OzzExample for OzzTwoBoneIK {
let bone_rot = Quat::from_mat3(&Mat3::from_cols(bone_dir, bone_rot_y, bone_rot_z));

self.bone_trans.push(OzzTransform {
scale: scale,
scale,
rotation: bone_rot,
position: parent_pos,
});

let parent_rot = Quat::from_mat4(parent);
self.spine_trans.push(OzzTransform {
scale: scale,
scale,
rotation: parent_rot,
position: parent_pos,
});

if self.skeleton.is_leaf(i as i16) {
let current_rot = Quat::from_mat4(current);
self.spine_trans.push(OzzTransform {
scale: scale,
scale,
rotation: current_rot,
position: current_pos,
});
Expand Down
Loading
Loading