Skip to content

Commit

Permalink
update demos
Browse files Browse the repository at this point in the history
  • Loading branch information
Nan committed Mar 9, 2024
1 parent eb113cf commit 798b7ac
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 103 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ozz-animation-rs"
version = "0.7.2"
version = "0.8.0"
authors = ["SlimeYummy <zzzcccnnn@outlook.com>"]
edition = "2021"
rust-version = "1.75"
Expand All @@ -10,7 +10,7 @@ homepage = "https://github.com/SlimeYummy/ozz-animation-rs"
repository = "https://github.com/SlimeYummy/ozz-animation-rs"
keywords = ["animation", "skeletal-animation", "fixed-point", "deterministic", "ozz-animation"]
categories = ["game-development", "rendering::data-formats", "api-bindings"]
exclude = ["/.circleci", "/.github", "/expected", "/output", "/resource"]
exclude = ["/.circleci", "/.github", "/demo", "/expected", "/output", "/resource"]
resolver = "2"

[features]
Expand Down
49 changes: 16 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Ozz-animation offline features are not supported, and no plans to support. Pleas

### Examples

A simple demo is in [./demo](https://github.com/FenQiDian/ozz-animation-rs/tree/master/demo) folder. Enter the folder and execute `cargo run`.

![demo](https://github.com/SlimeYummy/ozz-animation-rs/tree/master/demo/demo.jpg)

The test cases under [./tests](https://github.com/FenQiDian/ozz-animation-rs/tree/master/tests) can be viewed as examples.

Ozz-animation-rs keeps the same API styles with original ozz-animation library. Therefore, you can also refer to the ozz-animation [examples](https://github.com/guillaumeblanc/ozz-animation/tree/master/samples).
Expand All @@ -44,51 +48,30 @@ use ozz_animation_rs::math::*;
use std::rc::Rc;

// Load resources
let skeleton = Rc::new(Skeleton::from_file("./resource/skeleton.ozz").unwrap());
let animation1 = Rc::new(Animation::from_file("./resource/animation1.ozz").unwrap());
let animation2 = Rc::new(Animation::from_file("./resource/animation2.ozz").unwrap());

// Init sample job 1
let mut sample_job1: SamplingJob = SamplingJob::default();
sample_job1.set_animation(animation1.clone());
sample_job1.set_context(SamplingContext::new(animation1.num_tracks()));
let sample_out1 = ozz_buf(vec![SoaTransform::default(); skeleton.num_soa_joints()]);
sample_job1.set_output(sample_out1.clone());

// Init sample job 2
let mut sample_job2: SamplingJob = SamplingJob::default();
sample_job2.set_animation(animation2.clone());
sample_job2.set_context(SamplingContext::new(animation2.num_tracks()));
let sample_out2 = ozz_buf(vec![SoaTransform::default(); skeleton.num_soa_joints()]);
sample_job2.set_output(sample_out2.clone());

// Init blending job
let mut blending_job = BlendingJob::default();
blending_job.set_skeleton(skeleton.clone());
let blending_out = ozz_buf(vec![SoaTransform::default(); skeleton.num_soa_joints()]);
blending_job.set_output(blending_out.clone());
blending_job.layers_mut().push(BlendingLayer::with_weight(sample_out1.clone(), 0.5));
blending_job.layers_mut().push(BlendingLayer::with_weight(sample_out2.clone(), 0.5));
let skeleton = Rc::new(Skeleton::from_path("./resource/skeleton.ozz").unwrap());
let animation = Rc::new(Animation::from_path("./resource/animation.ozz").unwrap());

// Init sample job
let mut sample_job: SamplingJob = SamplingJob::default();
sample_job.set_animation(animation.clone());
sample_job.set_context(SamplingContext::new(animation.num_tracks()));
let sample_out = ozz_buf(vec![SoaTransform::default(); skeleton.num_soa_joints()]);
sample_job.set_output(sample_out.clone());

// Init local to model job
let mut l2m_job: LocalToModelJob = LocalToModelJob::default();
l2m_job.set_skeleton(skeleton.clone());
l2m_job.set_input(blending_out.clone());
l2m_job.set_input(sample_out.clone());
let l2m_out = ozz_buf(vec![Mat4::default(); skeleton.num_joints()]);
l2m_job.set_output(l2m_out.clone());

// Run the jobs
let ratio = 0.5;

sample_job1.set_ratio(ratio);
sample_job1.run().unwrap();
sample_job2.set_ratio(ratio);
sample_job2.run().unwrap();

blending_job.run().unwrap();
sample_job.set_ratio(ratio);
sample_job.run().unwrap();

l2m_job.run().unwrap();

l2m_out.vec().unwrap(); // Outputs here, are model-space matrices
```

Expand Down
Binary file added demo/demo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
80 changes: 43 additions & 37 deletions demo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials

// ground
commands.spawn(PbrBundle {
mesh: meshes.add(Rectangle::new(4.0, 16.0)),
material: materials.add(Color::rgb(0.35, 0.56, 0.45)),
mesh: meshes.add(Rectangle::new(20.0, 30.0)),
material: materials.add(Color::rgb(1.0, 0.96, 0.95)),
transform: Transform::from_rotation(Quat::from_rotation_x(-std::f32::consts::FRAC_PI_2))
.with_translation(Vec3::new(0.0, 0.0, 5.0)),
.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::WHITE);
let bone_material = materials.add(Color::rgb(0.68, 0.68, 0.8));
for i in 0..BONE_COUNT {
commands.spawn((
PbrBundle {
Expand All @@ -110,34 +110,8 @@ fn setup(mut commands: Commands, mut meshes: ResMut<Assets<Mesh>>, mut materials
}

// camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(1.5, 1.0, 3.0).looking_at(Vec3::new(0.0, 1.0, -0.0), Vec3::Y),
..default()
},
FogSettings {
color: Color::rgba(0.35, 0.48, 0.66, 1.0),
directional_light_color: Color::rgba(1.0, 0.95, 0.85, 0.5),
directional_light_exponent: 30.0,
falloff: FogFalloff::from_visibility_colors(
15.0, // distance in world units up to which objects retain visibility (>= 5% contrast)
Color::rgb(0.35, 0.5, 0.66), // atmospheric extinction color (after light is lost due to absorption by atmospheric particles)
Color::rgb(0.8, 0.844, 1.0), // atmospheric inscattering color (light gained due to scattering from the sun)
),
},
));

// Sun
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::rgb(0.98, 0.95, 0.82),
illuminance: 3000.0,
shadows_enabled: true,
shadow_depth_bias: 0.05,
shadow_normal_bias: 0.9,
..default()
},
transform: Transform::from_xyz(-3.0, 2.0, -4.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(1.5, 1.0, 3.0).looking_at(Vec3::new(0.0, 1.0, -0.0), Vec3::Y),
..default()
});

Expand All @@ -146,17 +120,31 @@ 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::hex("888888").unwrap(),
base_color: Color::rgb(0.4, 0.61, 0.98),
unlit: true,
cull_mode: None,
..default()
}),
transform: Transform::from_scale(Vec3::splat(20.0)),
transform: Transform::from_scale(Vec3::splat(30.0)),
..default()
},
NotShadowCaster,
));

// Sun
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::WHITE,
illuminance: 10000.0,
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()
});

// light
commands.spawn(PointLightBundle {
point_light: PointLight {
Expand Down Expand Up @@ -233,6 +221,7 @@ fn draw_spines(mut gizmos: Gizmos, oc: Query<&OzzComponent>) {
}
}
}
draw_ground(&mut gizmos);
}

#[rustfmt::skip]
Expand Down Expand Up @@ -280,6 +269,23 @@ fn build_bone_mesh() -> Mesh {
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),
);
}
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),
);
}
}

fn draw_gizmos(gizmos: &mut Gizmos, trans: &OzzTransform) {
let normal_x = trans.rotation.mul_vec3(Vec3::X).normalize();
let normal_y = trans.rotation.mul_vec3(Vec3::Y).normalize();
Expand All @@ -288,19 +294,19 @@ fn draw_gizmos(gizmos: &mut Gizmos, trans: &OzzTransform) {
trans.position,
Direction3d::new_unchecked(normal_x),
trans.scale * 0.25,
Color::rgba(1.0, 0.1, 0.1, 0.4),
Color::rgba(1.0, 0.1, 0.1, 0.5),
);
gizmos.circle(
trans.position,
Direction3d::new_unchecked(normal_y),
trans.scale * 0.25,
Color::rgba(0.1, 1.0, 0.1, 0.4),
Color::rgba(0.1, 1.0, 0.1, 0.5),
);
gizmos.circle(
trans.position,
Direction3d::new_unchecked(normal_z),
trans.scale * 0.25,
Color::rgba(0.1, 0.1, 1.0, 0.4),
Color::rgba(0.1, 0.1, 1.0, 0.5),
);
}

Expand Down
41 changes: 10 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,29 @@
//!
//! // Load resources
//! let skeleton = Rc::new(Skeleton::from_path("./resource/skeleton.ozz").unwrap());
//! let animation1 = Rc::new(Animation::from_path("./resource/animation1.ozz").unwrap());
//! let animation2 = Rc::new(Animation::from_path("./resource/animation2.ozz").unwrap());
//! let animation = Rc::new(Animation::from_path("./resource/animation.ozz").unwrap());
//!
//! // Init sample job 1
//! let mut sample_job1: SamplingJob = SamplingJob::default();
//! sample_job1.set_animation(animation1.clone());
//! sample_job1.set_context(SamplingContext::new(animation1.num_tracks()));
//! let sample_out1 = ozz_buf(vec![SoaTransform::default(); skeleton.num_soa_joints()]);
//! sample_job1.set_output(sample_out1.clone());
//!
//! // Init sample job 2
//! let mut sample_job2: SamplingJob = SamplingJob::default();
//! sample_job2.set_animation(animation2.clone());
//! sample_job2.set_context(SamplingContext::new(animation2.num_tracks()));
//! let sample_out2 = ozz_buf(vec![SoaTransform::default(); skeleton.num_soa_joints()]);
//! sample_job2.set_output(sample_out2.clone());
//!
//! // Init blending job
//! let mut blending_job = BlendingJob::default();
//! blending_job.set_skeleton(skeleton.clone());
//! let blending_out = ozz_buf(vec![SoaTransform::default(); skeleton.num_soa_joints()]);
//! blending_job.set_output(blending_out.clone());
//! blending_job.layers_mut().push(BlendingLayer::with_weight(sample_out1.clone(), 0.5));
//! blending_job.layers_mut().push(BlendingLayer::with_weight(sample_out2.clone(), 0.5));
//! // Init sample job
//! let mut sample_job: SamplingJob = SamplingJob::default();
//! sample_job.set_animation(animation.clone());
//! sample_job.set_context(SamplingContext::new(animation.num_tracks()));
//! let sample_out = ozz_buf(vec![SoaTransform::default(); skeleton.num_soa_joints()]);
//! sample_job.set_output(sample_out.clone());
//!
//! // Init local to model job
//! let mut l2m_job: LocalToModelJob = LocalToModelJob::default();
//! l2m_job.set_skeleton(skeleton.clone());
//! l2m_job.set_input(blending_out.clone());
//! l2m_job.set_input(sample_out.clone());
//! let l2m_out = ozz_buf(vec![Mat4::default(); skeleton.num_joints()]);
//! l2m_job.set_output(l2m_out.clone());
//!
//! // Run the jobs
//! let ratio = 0.5;
//!
//! sample_job1.set_ratio(ratio);
//! sample_job1.run().unwrap();
//! sample_job2.set_ratio(ratio);
//! sample_job2.run().unwrap();
//!
//! blending_job.run().unwrap();
//! sample_job.set_ratio(ratio);
//! sample_job.run().unwrap();
//!
//! l2m_job.run().unwrap();
//!
//! l2m_out.vec().unwrap(); // Outputs here, are model-space matrices
//! ```
//!
Expand Down

0 comments on commit 798b7ac

Please sign in to comment.