From d00e88b804c3444fefb03d1f3f973d9bfac319bc Mon Sep 17 00:00:00 2001 From: Imbris Date: Wed, 17 Jun 2020 03:49:14 -0400 Subject: [PATCH] Hotreload animations using libloading --- Cargo.lock | 12 ++ Cargo.toml | 1 + voxygen/Cargo.toml | 3 + voxygen/src/anim/Cargo.toml | 23 +++ voxygen/src/anim/mod.rs | 75 --------- .../src/anim/{ => src}/biped_large/idle.rs | 0 .../src/anim/{ => src}/biped_large/jump.rs | 0 voxygen/src/anim/{ => src}/biped_large/mod.rs | 3 +- voxygen/src/anim/{ => src}/biped_large/run.rs | 0 voxygen/src/anim/{ => src}/bird_medium/fly.rs | 0 .../src/anim/{ => src}/bird_medium/idle.rs | 0 voxygen/src/anim/{ => src}/bird_medium/mod.rs | 3 +- voxygen/src/anim/{ => src}/bird_medium/run.rs | 0 voxygen/src/anim/{ => src}/bird_small/idle.rs | 0 voxygen/src/anim/{ => src}/bird_small/jump.rs | 0 voxygen/src/anim/{ => src}/bird_small/mod.rs | 3 +- voxygen/src/anim/{ => src}/bird_small/run.rs | 0 voxygen/src/anim/{ => src}/character/alpha.rs | 0 voxygen/src/anim/{ => src}/character/beta.rs | 0 voxygen/src/anim/{ => src}/character/block.rs | 0 .../src/anim/{ => src}/character/blockidle.rs | 0 .../src/anim/{ => src}/character/charge.rs | 0 voxygen/src/anim/{ => src}/character/climb.rs | 0 voxygen/src/anim/{ => src}/character/dance.rs | 0 voxygen/src/anim/{ => src}/character/dash.rs | 0 voxygen/src/anim/{ => src}/character/equip.rs | 0 .../src/anim/{ => src}/character/gliding.rs | 0 voxygen/src/anim/{ => src}/character/idle.rs | 0 voxygen/src/anim/{ => src}/character/jump.rs | 0 voxygen/src/anim/{ => src}/character/mod.rs | 3 +- voxygen/src/anim/{ => src}/character/roll.rs | 0 voxygen/src/anim/{ => src}/character/run.rs | 0 voxygen/src/anim/{ => src}/character/shoot.rs | 0 voxygen/src/anim/{ => src}/character/sit.rs | 0 voxygen/src/anim/{ => src}/character/spin.rs | 0 voxygen/src/anim/{ => src}/character/stand.rs | 0 voxygen/src/anim/{ => src}/character/swim.rs | 0 voxygen/src/anim/{ => src}/character/wield.rs | 0 voxygen/src/anim/{ => src}/critter/idle.rs | 0 voxygen/src/anim/{ => src}/critter/jump.rs | 0 voxygen/src/anim/{ => src}/critter/mod.rs | 3 +- voxygen/src/anim/{ => src}/critter/run.rs | 0 voxygen/src/anim/{ => src}/dragon/fly.rs | 0 voxygen/src/anim/{ => src}/dragon/idle.rs | 0 voxygen/src/anim/{ => src}/dragon/mod.rs | 3 +- voxygen/src/anim/{ => src}/dragon/run.rs | 0 voxygen/src/anim/src/dyn_lib.rs | 109 ++++++++++++ .../src/anim/{ => src}/fish_medium/idle.rs | 0 .../src/anim/{ => src}/fish_medium/jump.rs | 0 voxygen/src/anim/{ => src}/fish_medium/mod.rs | 3 +- voxygen/src/anim/{ => src}/fish_medium/run.rs | 0 voxygen/src/anim/{ => src}/fish_small/idle.rs | 0 voxygen/src/anim/{ => src}/fish_small/jump.rs | 0 voxygen/src/anim/{ => src}/fish_small/mod.rs | 3 +- voxygen/src/anim/{ => src}/fish_small/run.rs | 0 voxygen/src/anim/{ => src}/fixture/mod.rs | 3 +- voxygen/src/anim/{ => src}/golem/idle.rs | 0 voxygen/src/anim/{ => src}/golem/jump.rs | 0 voxygen/src/anim/{ => src}/golem/mod.rs | 3 +- voxygen/src/anim/{ => src}/golem/run.rs | 0 voxygen/src/anim/src/lib.rs | 158 ++++++++++++++++++ voxygen/src/anim/{ => src}/object/mod.rs | 3 +- .../anim/{ => src}/quadruped_medium/idle.rs | 6 +- .../anim/{ => src}/quadruped_medium/jump.rs | 6 +- .../anim/{ => src}/quadruped_medium/mod.rs | 9 +- .../anim/{ => src}/quadruped_medium/run.rs | 6 +- .../anim/{ => src}/quadruped_small/idle.rs | 6 +- .../anim/{ => src}/quadruped_small/jump.rs | 6 +- .../src/anim/{ => src}/quadruped_small/mod.rs | 9 +- .../src/anim/{ => src}/quadruped_small/run.rs | 6 +- voxygen/src/lib.rs | 1 - voxygen/src/main.rs | 4 + voxygen/src/render/mod.rs | 1 - voxygen/src/render/pipelines/figure.rs | 6 +- voxygen/src/render/pipelines/mod.rs | 7 +- voxygen/src/render/pipelines/sprite.rs | 4 +- voxygen/src/render/util.rs | 10 -- voxygen/src/scene/figure/cache.rs | 2 +- voxygen/src/scene/figure/mod.rs | 42 ++++- voxygen/src/scene/mod.rs | 2 +- voxygen/src/scene/simple.rs | 10 +- 81 files changed, 411 insertions(+), 146 deletions(-) create mode 100644 voxygen/src/anim/Cargo.toml delete mode 100644 voxygen/src/anim/mod.rs rename voxygen/src/anim/{ => src}/biped_large/idle.rs (100%) rename voxygen/src/anim/{ => src}/biped_large/jump.rs (100%) rename voxygen/src/anim/{ => src}/biped_large/mod.rs (98%) rename voxygen/src/anim/{ => src}/biped_large/run.rs (100%) rename voxygen/src/anim/{ => src}/bird_medium/fly.rs (100%) rename voxygen/src/anim/{ => src}/bird_medium/idle.rs (100%) rename voxygen/src/anim/{ => src}/bird_medium/mod.rs (98%) rename voxygen/src/anim/{ => src}/bird_medium/run.rs (100%) rename voxygen/src/anim/{ => src}/bird_small/idle.rs (100%) rename voxygen/src/anim/{ => src}/bird_small/jump.rs (100%) rename voxygen/src/anim/{ => src}/bird_small/mod.rs (97%) rename voxygen/src/anim/{ => src}/bird_small/run.rs (100%) rename voxygen/src/anim/{ => src}/character/alpha.rs (100%) rename voxygen/src/anim/{ => src}/character/beta.rs (100%) rename voxygen/src/anim/{ => src}/character/block.rs (100%) rename voxygen/src/anim/{ => src}/character/blockidle.rs (100%) rename voxygen/src/anim/{ => src}/character/charge.rs (100%) rename voxygen/src/anim/{ => src}/character/climb.rs (100%) rename voxygen/src/anim/{ => src}/character/dance.rs (100%) rename voxygen/src/anim/{ => src}/character/dash.rs (100%) rename voxygen/src/anim/{ => src}/character/equip.rs (100%) rename voxygen/src/anim/{ => src}/character/gliding.rs (100%) rename voxygen/src/anim/{ => src}/character/idle.rs (100%) rename voxygen/src/anim/{ => src}/character/jump.rs (100%) rename voxygen/src/anim/{ => src}/character/mod.rs (99%) rename voxygen/src/anim/{ => src}/character/roll.rs (100%) rename voxygen/src/anim/{ => src}/character/run.rs (100%) rename voxygen/src/anim/{ => src}/character/shoot.rs (100%) rename voxygen/src/anim/{ => src}/character/sit.rs (100%) rename voxygen/src/anim/{ => src}/character/spin.rs (100%) rename voxygen/src/anim/{ => src}/character/stand.rs (100%) rename voxygen/src/anim/{ => src}/character/swim.rs (100%) rename voxygen/src/anim/{ => src}/character/wield.rs (100%) rename voxygen/src/anim/{ => src}/critter/idle.rs (100%) rename voxygen/src/anim/{ => src}/critter/jump.rs (100%) rename voxygen/src/anim/{ => src}/critter/mod.rs (98%) rename voxygen/src/anim/{ => src}/critter/run.rs (100%) rename voxygen/src/anim/{ => src}/dragon/fly.rs (100%) rename voxygen/src/anim/{ => src}/dragon/idle.rs (100%) rename voxygen/src/anim/{ => src}/dragon/mod.rs (99%) rename voxygen/src/anim/{ => src}/dragon/run.rs (100%) create mode 100644 voxygen/src/anim/src/dyn_lib.rs rename voxygen/src/anim/{ => src}/fish_medium/idle.rs (100%) rename voxygen/src/anim/{ => src}/fish_medium/jump.rs (100%) rename voxygen/src/anim/{ => src}/fish_medium/mod.rs (97%) rename voxygen/src/anim/{ => src}/fish_medium/run.rs (100%) rename voxygen/src/anim/{ => src}/fish_small/idle.rs (100%) rename voxygen/src/anim/{ => src}/fish_small/jump.rs (100%) rename voxygen/src/anim/{ => src}/fish_small/mod.rs (97%) rename voxygen/src/anim/{ => src}/fish_small/run.rs (100%) rename voxygen/src/anim/{ => src}/fixture/mod.rs (96%) rename voxygen/src/anim/{ => src}/golem/idle.rs (100%) rename voxygen/src/anim/{ => src}/golem/jump.rs (100%) rename voxygen/src/anim/{ => src}/golem/mod.rs (98%) rename voxygen/src/anim/{ => src}/golem/run.rs (100%) create mode 100644 voxygen/src/anim/src/lib.rs rename voxygen/src/anim/{ => src}/object/mod.rs (96%) rename voxygen/src/anim/{ => src}/quadruped_medium/idle.rs (95%) rename voxygen/src/anim/{ => src}/quadruped_medium/jump.rs (93%) rename voxygen/src/anim/{ => src}/quadruped_medium/mod.rs (96%) rename voxygen/src/anim/{ => src}/quadruped_medium/run.rs (96%) rename voxygen/src/anim/{ => src}/quadruped_small/idle.rs (93%) rename voxygen/src/anim/{ => src}/quadruped_small/jump.rs (91%) rename voxygen/src/anim/{ => src}/quadruped_small/mod.rs (95%) rename voxygen/src/anim/{ => src}/quadruped_small/run.rs (92%) delete mode 100644 voxygen/src/render/util.rs diff --git a/Cargo.lock b/Cargo.lock index 50414f1559..6d3b85f8e0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4594,11 +4594,23 @@ dependencies = [ "veloren-client", "veloren-common", "veloren-server", + "veloren-voxygen-anim", "veloren-world", "winit", "winres", ] +[[package]] +name = "veloren-voxygen-anim" +version = "0.6.0" +dependencies = [ + "lazy_static", + "libloading 0.6.2", + "notify", + "vek", + "veloren-common", +] + [[package]] name = "veloren-world" version = "0.6.0" diff --git a/Cargo.toml b/Cargo.toml index a5fa2b3da3..277aa6d984 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ members = [ "voxygen", "world", "network", + "voxygen/src/anim" ] # default profile for devs, fast to compile, okay enough to run, no debug information diff --git a/voxygen/Cargo.toml b/voxygen/Cargo.toml index fb9862bee2..bd96e5c9f4 100644 --- a/voxygen/Cargo.toml +++ b/voxygen/Cargo.toml @@ -12,6 +12,7 @@ default-run = "veloren-voxygen" gl = ["gfx_device_gl"] singleplayer = ["server"] tweak = ["const-tweaker"] +hot-anim = ["anim/use-dyn-lib"] default = ["gl", "singleplayer", "msgbox"] @@ -19,6 +20,8 @@ default = ["gl", "singleplayer", "msgbox"] common = { package = "veloren-common", path = "../common" } client = { package = "veloren-client", path = "../client" } +anim = { package = "veloren-voxygen-anim", path = "src/anim", default-features = false } + # Graphics gfx = "0.18.2" gfx_device_gl = { version = "0.16.2", optional = true } diff --git a/voxygen/src/anim/Cargo.toml b/voxygen/src/anim/Cargo.toml new file mode 100644 index 0000000000..b515860a7c --- /dev/null +++ b/voxygen/src/anim/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "veloren-voxygen-anim" +version = "0.6.0" +authors = ["Joshua Barretto ", "Imbris "] +edition = "2018" + +[lib] +name = "voxygen_anim" +crate-type = ["lib", "cdylib"] + +[features] +use-dyn-lib = ["libloading", "notify", "lazy_static"] +be-dyn-lib = [] + +default = ["be-dyn-lib"] + +[dependencies] +vek = { version = "0.11.2", features = ["serde"] } +common = { package = "veloren-common", path = "../../../common" } +libloading = { version = "0.6.2", optional = true } +notify = { version = "5.0.0-pre.2", optional = true } +lazy_static = { version = "1.4.0", optional = true } + diff --git a/voxygen/src/anim/mod.rs b/voxygen/src/anim/mod.rs deleted file mode 100644 index 3682db6374..0000000000 --- a/voxygen/src/anim/mod.rs +++ /dev/null @@ -1,75 +0,0 @@ -pub mod biped_large; -pub mod bird_medium; -pub mod bird_small; -pub mod character; -pub mod critter; -pub mod dragon; -pub mod fish_medium; -pub mod fish_small; -pub mod fixture; -pub mod golem; -pub mod object; -pub mod quadruped_medium; -pub mod quadruped_small; - -use crate::render::FigureBoneData; -use vek::*; - -#[derive(Copy, Clone, Debug)] -pub struct Bone { - pub offset: Vec3, - pub ori: Quaternion, - pub scale: Vec3, -} - -impl Default for Bone { - fn default() -> Self { - Self { - offset: Vec3::zero(), - ori: Quaternion::identity(), - scale: Vec3::broadcast(1.0 / 11.0), - } - } -} - -impl Bone { - pub fn compute_base_matrix(&self) -> Mat4 { - Mat4::::translation_3d(self.offset) - * Mat4::scaling_3d(self.scale) - * Mat4::from(self.ori) - } - - /// Change the current bone to be more like `target`. - fn interpolate(&mut self, target: &Bone, dt: f32) { - // TODO: Make configurable. - let factor = (15.0 * dt).min(1.0); - self.offset += (target.offset - self.offset) * factor; - self.ori = vek::Slerp::slerp(self.ori, target.ori, factor); - self.scale += (target.scale - self.scale) * factor; - } -} - -pub trait Skeleton: Send + Sync + 'static { - type Attr; - - fn bone_count(&self) -> usize { 16 } - - fn compute_matrices(&self) -> ([FigureBoneData; 16], Vec3); - - /// Change the current skeleton to be more like `target`. - fn interpolate(&mut self, target: &Self, dt: f32); -} - -pub trait Animation { - type Skeleton: Skeleton; - type Dependency; - - /// Returns a new skeleton that is generated by the animation. - fn update_skeleton( - skeleton: &Self::Skeleton, - dependency: Self::Dependency, - anim_time: f64, - rate: &mut f32, - skeleton_attr: &<::Skeleton as Skeleton>::Attr, - ) -> Self::Skeleton; -} diff --git a/voxygen/src/anim/biped_large/idle.rs b/voxygen/src/anim/src/biped_large/idle.rs similarity index 100% rename from voxygen/src/anim/biped_large/idle.rs rename to voxygen/src/anim/src/biped_large/idle.rs diff --git a/voxygen/src/anim/biped_large/jump.rs b/voxygen/src/anim/src/biped_large/jump.rs similarity index 100% rename from voxygen/src/anim/biped_large/jump.rs rename to voxygen/src/anim/src/biped_large/jump.rs diff --git a/voxygen/src/anim/biped_large/mod.rs b/voxygen/src/anim/src/biped_large/mod.rs similarity index 98% rename from voxygen/src/anim/biped_large/mod.rs rename to voxygen/src/anim/src/biped_large/mod.rs index e5b3c38776..fc85fd7b03 100644 --- a/voxygen/src/anim/biped_large/mod.rs +++ b/voxygen/src/anim/src/biped_large/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{idle::IdleAnimation, jump::JumpAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; diff --git a/voxygen/src/anim/biped_large/run.rs b/voxygen/src/anim/src/biped_large/run.rs similarity index 100% rename from voxygen/src/anim/biped_large/run.rs rename to voxygen/src/anim/src/biped_large/run.rs diff --git a/voxygen/src/anim/bird_medium/fly.rs b/voxygen/src/anim/src/bird_medium/fly.rs similarity index 100% rename from voxygen/src/anim/bird_medium/fly.rs rename to voxygen/src/anim/src/bird_medium/fly.rs diff --git a/voxygen/src/anim/bird_medium/idle.rs b/voxygen/src/anim/src/bird_medium/idle.rs similarity index 100% rename from voxygen/src/anim/bird_medium/idle.rs rename to voxygen/src/anim/src/bird_medium/idle.rs diff --git a/voxygen/src/anim/bird_medium/mod.rs b/voxygen/src/anim/src/bird_medium/mod.rs similarity index 98% rename from voxygen/src/anim/bird_medium/mod.rs rename to voxygen/src/anim/src/bird_medium/mod.rs index 9d561c477a..d0a40a351b 100644 --- a/voxygen/src/anim/bird_medium/mod.rs +++ b/voxygen/src/anim/src/bird_medium/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{fly::FlyAnimation, idle::IdleAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; diff --git a/voxygen/src/anim/bird_medium/run.rs b/voxygen/src/anim/src/bird_medium/run.rs similarity index 100% rename from voxygen/src/anim/bird_medium/run.rs rename to voxygen/src/anim/src/bird_medium/run.rs diff --git a/voxygen/src/anim/bird_small/idle.rs b/voxygen/src/anim/src/bird_small/idle.rs similarity index 100% rename from voxygen/src/anim/bird_small/idle.rs rename to voxygen/src/anim/src/bird_small/idle.rs diff --git a/voxygen/src/anim/bird_small/jump.rs b/voxygen/src/anim/src/bird_small/jump.rs similarity index 100% rename from voxygen/src/anim/bird_small/jump.rs rename to voxygen/src/anim/src/bird_small/jump.rs diff --git a/voxygen/src/anim/bird_small/mod.rs b/voxygen/src/anim/src/bird_small/mod.rs similarity index 97% rename from voxygen/src/anim/bird_small/mod.rs rename to voxygen/src/anim/src/bird_small/mod.rs index 61202ed213..7937e99b4b 100644 --- a/voxygen/src/anim/bird_small/mod.rs +++ b/voxygen/src/anim/src/bird_small/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{idle::IdleAnimation, jump::JumpAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; diff --git a/voxygen/src/anim/bird_small/run.rs b/voxygen/src/anim/src/bird_small/run.rs similarity index 100% rename from voxygen/src/anim/bird_small/run.rs rename to voxygen/src/anim/src/bird_small/run.rs diff --git a/voxygen/src/anim/character/alpha.rs b/voxygen/src/anim/src/character/alpha.rs similarity index 100% rename from voxygen/src/anim/character/alpha.rs rename to voxygen/src/anim/src/character/alpha.rs diff --git a/voxygen/src/anim/character/beta.rs b/voxygen/src/anim/src/character/beta.rs similarity index 100% rename from voxygen/src/anim/character/beta.rs rename to voxygen/src/anim/src/character/beta.rs diff --git a/voxygen/src/anim/character/block.rs b/voxygen/src/anim/src/character/block.rs similarity index 100% rename from voxygen/src/anim/character/block.rs rename to voxygen/src/anim/src/character/block.rs diff --git a/voxygen/src/anim/character/blockidle.rs b/voxygen/src/anim/src/character/blockidle.rs similarity index 100% rename from voxygen/src/anim/character/blockidle.rs rename to voxygen/src/anim/src/character/blockidle.rs diff --git a/voxygen/src/anim/character/charge.rs b/voxygen/src/anim/src/character/charge.rs similarity index 100% rename from voxygen/src/anim/character/charge.rs rename to voxygen/src/anim/src/character/charge.rs diff --git a/voxygen/src/anim/character/climb.rs b/voxygen/src/anim/src/character/climb.rs similarity index 100% rename from voxygen/src/anim/character/climb.rs rename to voxygen/src/anim/src/character/climb.rs diff --git a/voxygen/src/anim/character/dance.rs b/voxygen/src/anim/src/character/dance.rs similarity index 100% rename from voxygen/src/anim/character/dance.rs rename to voxygen/src/anim/src/character/dance.rs diff --git a/voxygen/src/anim/character/dash.rs b/voxygen/src/anim/src/character/dash.rs similarity index 100% rename from voxygen/src/anim/character/dash.rs rename to voxygen/src/anim/src/character/dash.rs diff --git a/voxygen/src/anim/character/equip.rs b/voxygen/src/anim/src/character/equip.rs similarity index 100% rename from voxygen/src/anim/character/equip.rs rename to voxygen/src/anim/src/character/equip.rs diff --git a/voxygen/src/anim/character/gliding.rs b/voxygen/src/anim/src/character/gliding.rs similarity index 100% rename from voxygen/src/anim/character/gliding.rs rename to voxygen/src/anim/src/character/gliding.rs diff --git a/voxygen/src/anim/character/idle.rs b/voxygen/src/anim/src/character/idle.rs similarity index 100% rename from voxygen/src/anim/character/idle.rs rename to voxygen/src/anim/src/character/idle.rs diff --git a/voxygen/src/anim/character/jump.rs b/voxygen/src/anim/src/character/jump.rs similarity index 100% rename from voxygen/src/anim/character/jump.rs rename to voxygen/src/anim/src/character/jump.rs diff --git a/voxygen/src/anim/character/mod.rs b/voxygen/src/anim/src/character/mod.rs similarity index 99% rename from voxygen/src/anim/character/mod.rs rename to voxygen/src/anim/src/character/mod.rs index bbc0d3bfa3..7aefc2f5bf 100644 --- a/voxygen/src/anim/character/mod.rs +++ b/voxygen/src/anim/src/character/mod.rs @@ -31,8 +31,7 @@ pub use self::{ wield::WieldAnimation, }; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp; use vek::{Vec3, Vec4}; diff --git a/voxygen/src/anim/character/roll.rs b/voxygen/src/anim/src/character/roll.rs similarity index 100% rename from voxygen/src/anim/character/roll.rs rename to voxygen/src/anim/src/character/roll.rs diff --git a/voxygen/src/anim/character/run.rs b/voxygen/src/anim/src/character/run.rs similarity index 100% rename from voxygen/src/anim/character/run.rs rename to voxygen/src/anim/src/character/run.rs diff --git a/voxygen/src/anim/character/shoot.rs b/voxygen/src/anim/src/character/shoot.rs similarity index 100% rename from voxygen/src/anim/character/shoot.rs rename to voxygen/src/anim/src/character/shoot.rs diff --git a/voxygen/src/anim/character/sit.rs b/voxygen/src/anim/src/character/sit.rs similarity index 100% rename from voxygen/src/anim/character/sit.rs rename to voxygen/src/anim/src/character/sit.rs diff --git a/voxygen/src/anim/character/spin.rs b/voxygen/src/anim/src/character/spin.rs similarity index 100% rename from voxygen/src/anim/character/spin.rs rename to voxygen/src/anim/src/character/spin.rs diff --git a/voxygen/src/anim/character/stand.rs b/voxygen/src/anim/src/character/stand.rs similarity index 100% rename from voxygen/src/anim/character/stand.rs rename to voxygen/src/anim/src/character/stand.rs diff --git a/voxygen/src/anim/character/swim.rs b/voxygen/src/anim/src/character/swim.rs similarity index 100% rename from voxygen/src/anim/character/swim.rs rename to voxygen/src/anim/src/character/swim.rs diff --git a/voxygen/src/anim/character/wield.rs b/voxygen/src/anim/src/character/wield.rs similarity index 100% rename from voxygen/src/anim/character/wield.rs rename to voxygen/src/anim/src/character/wield.rs diff --git a/voxygen/src/anim/critter/idle.rs b/voxygen/src/anim/src/critter/idle.rs similarity index 100% rename from voxygen/src/anim/critter/idle.rs rename to voxygen/src/anim/src/critter/idle.rs diff --git a/voxygen/src/anim/critter/jump.rs b/voxygen/src/anim/src/critter/jump.rs similarity index 100% rename from voxygen/src/anim/critter/jump.rs rename to voxygen/src/anim/src/critter/jump.rs diff --git a/voxygen/src/anim/critter/mod.rs b/voxygen/src/anim/src/critter/mod.rs similarity index 98% rename from voxygen/src/anim/critter/mod.rs rename to voxygen/src/anim/src/critter/mod.rs index 85268c3f06..80b9d3cd12 100644 --- a/voxygen/src/anim/critter/mod.rs +++ b/voxygen/src/anim/src/critter/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{idle::IdleAnimation, jump::JumpAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; diff --git a/voxygen/src/anim/critter/run.rs b/voxygen/src/anim/src/critter/run.rs similarity index 100% rename from voxygen/src/anim/critter/run.rs rename to voxygen/src/anim/src/critter/run.rs diff --git a/voxygen/src/anim/dragon/fly.rs b/voxygen/src/anim/src/dragon/fly.rs similarity index 100% rename from voxygen/src/anim/dragon/fly.rs rename to voxygen/src/anim/src/dragon/fly.rs diff --git a/voxygen/src/anim/dragon/idle.rs b/voxygen/src/anim/src/dragon/idle.rs similarity index 100% rename from voxygen/src/anim/dragon/idle.rs rename to voxygen/src/anim/src/dragon/idle.rs diff --git a/voxygen/src/anim/dragon/mod.rs b/voxygen/src/anim/src/dragon/mod.rs similarity index 99% rename from voxygen/src/anim/dragon/mod.rs rename to voxygen/src/anim/src/dragon/mod.rs index 8e4ea6ff40..e949f44a69 100644 --- a/voxygen/src/anim/dragon/mod.rs +++ b/voxygen/src/anim/src/dragon/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{fly::FlyAnimation, idle::IdleAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; diff --git a/voxygen/src/anim/dragon/run.rs b/voxygen/src/anim/src/dragon/run.rs similarity index 100% rename from voxygen/src/anim/dragon/run.rs rename to voxygen/src/anim/src/dragon/run.rs diff --git a/voxygen/src/anim/src/dyn_lib.rs b/voxygen/src/anim/src/dyn_lib.rs new file mode 100644 index 0000000000..b870ca1c26 --- /dev/null +++ b/voxygen/src/anim/src/dyn_lib.rs @@ -0,0 +1,109 @@ +use lazy_static::lazy_static; +use libloading::Library; +use notify::{ + event::{AccessKind, AccessMode}, + immediate_watcher, EventKind, RecursiveMode, Watcher, +}; +use std::{ + process::{Command, Stdio}, + sync::Mutex, +}; + +lazy_static! { + pub static ref LIB: Mutex> = Mutex::new(Some(LoadedLib::compile_load())); +} + +pub struct LoadedLib { + pub lib: Library, +} + +impl LoadedLib { + fn compile_load() -> Self { + // Compile + let _output = Command::new("cargo") + .stderr(Stdio::inherit()) + .stdout(Stdio::inherit()) + .arg("build") + .arg("--release") + .arg("--package") + .arg("veloren-voxygen-anim") + .output() + .unwrap(); + + Self::load() + } + + fn load() -> Self { + #[cfg(target_os = "windows")] + let lib = Library::new("../target/release/libvoxygen_anim.dll").unwrap(); + #[cfg(not(target_os = "windows"))] + let lib = Library::new("../target/release/libvoxygen_anim.so").unwrap(); + + Self { lib } + } +} + +// Starts up watcher test test2 test3 test4 test5 +pub fn init() { + // Start watcher + let mut watcher = immediate_watcher(event_fn).unwrap(); + watcher.watch("src/anim", RecursiveMode::Recursive).unwrap(); + + // Let the watcher live forever + std::mem::forget(watcher); +} + +// Recompiles and hotreloads the lib if the source is changed +// Note: designed with voxygen dir as working dir, could be made more flexible +fn event_fn(res: notify::Result) { + match res { + Ok(event) => match event.kind { + EventKind::Access(AccessKind::Close(AccessMode::Write)) => { + if event + .paths + .iter() + .any(|p| p.extension().map(|e| e == "rs").unwrap_or(false)) + { + println!( + "Hot reloading animations because these files were modified:\n{:?}", + event.paths + ); + reload(); + } + }, + _ => {}, + }, + Err(e) => println!("watch error: {:?}", e), + } +} + +fn reload() { + // Compile + let output = Command::new("cargo") + .stderr(Stdio::inherit()) + .stdout(Stdio::inherit()) + .arg("build") + .arg("--release") + .arg("--package") + .arg("veloren-voxygen-anim") + .output() + .unwrap(); + + // Stop if recompile failed + if !output.status.success() { + println!("Failed to compile anim crate"); + return; + } + + println!("Compile Success!!"); + + let mut lock = LIB.lock().unwrap(); + + // Close lib + lock.take().unwrap().lib.close().unwrap(); + + // Open new lib + *lock = Some(LoadedLib::load()); + + println!("Updated"); +} diff --git a/voxygen/src/anim/fish_medium/idle.rs b/voxygen/src/anim/src/fish_medium/idle.rs similarity index 100% rename from voxygen/src/anim/fish_medium/idle.rs rename to voxygen/src/anim/src/fish_medium/idle.rs diff --git a/voxygen/src/anim/fish_medium/jump.rs b/voxygen/src/anim/src/fish_medium/jump.rs similarity index 100% rename from voxygen/src/anim/fish_medium/jump.rs rename to voxygen/src/anim/src/fish_medium/jump.rs diff --git a/voxygen/src/anim/fish_medium/mod.rs b/voxygen/src/anim/src/fish_medium/mod.rs similarity index 97% rename from voxygen/src/anim/fish_medium/mod.rs rename to voxygen/src/anim/src/fish_medium/mod.rs index 4693f0fea9..04533f5a04 100644 --- a/voxygen/src/anim/fish_medium/mod.rs +++ b/voxygen/src/anim/src/fish_medium/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{idle::IdleAnimation, jump::JumpAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; diff --git a/voxygen/src/anim/fish_medium/run.rs b/voxygen/src/anim/src/fish_medium/run.rs similarity index 100% rename from voxygen/src/anim/fish_medium/run.rs rename to voxygen/src/anim/src/fish_medium/run.rs diff --git a/voxygen/src/anim/fish_small/idle.rs b/voxygen/src/anim/src/fish_small/idle.rs similarity index 100% rename from voxygen/src/anim/fish_small/idle.rs rename to voxygen/src/anim/src/fish_small/idle.rs diff --git a/voxygen/src/anim/fish_small/jump.rs b/voxygen/src/anim/src/fish_small/jump.rs similarity index 100% rename from voxygen/src/anim/fish_small/jump.rs rename to voxygen/src/anim/src/fish_small/jump.rs diff --git a/voxygen/src/anim/fish_small/mod.rs b/voxygen/src/anim/src/fish_small/mod.rs similarity index 97% rename from voxygen/src/anim/fish_small/mod.rs rename to voxygen/src/anim/src/fish_small/mod.rs index 547e376ccf..d201989b99 100644 --- a/voxygen/src/anim/fish_small/mod.rs +++ b/voxygen/src/anim/src/fish_small/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{idle::IdleAnimation, jump::JumpAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; diff --git a/voxygen/src/anim/fish_small/run.rs b/voxygen/src/anim/src/fish_small/run.rs similarity index 100% rename from voxygen/src/anim/fish_small/run.rs rename to voxygen/src/anim/src/fish_small/run.rs diff --git a/voxygen/src/anim/fixture/mod.rs b/voxygen/src/anim/src/fixture/mod.rs similarity index 96% rename from voxygen/src/anim/fixture/mod.rs rename to voxygen/src/anim/src/fixture/mod.rs index 89ddd153af..a95bcb9729 100644 --- a/voxygen/src/anim/fixture/mod.rs +++ b/voxygen/src/anim/src/fixture/mod.rs @@ -1,5 +1,4 @@ -use super::Skeleton; -use crate::render::FigureBoneData; +use super::{FigureBoneData, Skeleton}; use vek::Vec3; #[derive(Clone)] diff --git a/voxygen/src/anim/golem/idle.rs b/voxygen/src/anim/src/golem/idle.rs similarity index 100% rename from voxygen/src/anim/golem/idle.rs rename to voxygen/src/anim/src/golem/idle.rs diff --git a/voxygen/src/anim/golem/jump.rs b/voxygen/src/anim/src/golem/jump.rs similarity index 100% rename from voxygen/src/anim/golem/jump.rs rename to voxygen/src/anim/src/golem/jump.rs diff --git a/voxygen/src/anim/golem/mod.rs b/voxygen/src/anim/src/golem/mod.rs similarity index 98% rename from voxygen/src/anim/golem/mod.rs rename to voxygen/src/anim/src/golem/mod.rs index 415b93f631..7329bff0e8 100644 --- a/voxygen/src/anim/golem/mod.rs +++ b/voxygen/src/anim/src/golem/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{idle::IdleAnimation, jump::JumpAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; diff --git a/voxygen/src/anim/golem/run.rs b/voxygen/src/anim/src/golem/run.rs similarity index 100% rename from voxygen/src/anim/golem/run.rs rename to voxygen/src/anim/src/golem/run.rs diff --git a/voxygen/src/anim/src/lib.rs b/voxygen/src/anim/src/lib.rs new file mode 100644 index 0000000000..17bec61d54 --- /dev/null +++ b/voxygen/src/anim/src/lib.rs @@ -0,0 +1,158 @@ +// TODO: we could probably avoid the need for this +#[cfg(all(feature = "be-dyn-lib", feature = "use-dyn-lib"))] +compile_error!("Can't use both \"be-dyn-lib\" and \"use-dyn-lib\" features at once"); + +pub mod biped_large; +pub mod bird_medium; +pub mod bird_small; +pub mod character; +pub mod critter; +pub mod dragon; +#[cfg(feature = "use-dyn-lib")] pub mod dyn_lib; +pub mod fish_medium; +pub mod fish_small; +pub mod fixture; +pub mod golem; +pub mod object; +pub mod quadruped_medium; +pub mod quadruped_small; + +#[cfg(feature = "use-dyn-lib")] +pub use dyn_lib::init; + +use vek::*; + +// TODO: replace with inner type everywhere +pub struct FigureBoneData(pub Mat4); +impl FigureBoneData { + pub fn new(mat: Mat4) -> Self { Self(mat) } + + pub fn default() -> Self { Self(Mat4::identity()) } +} + +#[derive(Copy, Clone, Debug)] +pub struct Bone { + pub offset: Vec3, + pub ori: Quaternion, + pub scale: Vec3, +} + +impl Default for Bone { + fn default() -> Self { + Self { + offset: Vec3::zero(), + ori: Quaternion::identity(), + scale: Vec3::broadcast(1.0 / 11.0), + } + } +} + +impl Bone { + pub fn compute_base_matrix(&self) -> Mat4 { + Mat4::::translation_3d(self.offset) + * Mat4::scaling_3d(self.scale) + * Mat4::from(self.ori) + } + + /// Change the current bone to be more like `target`. + fn interpolate(&mut self, target: &Bone, dt: f32) { + // TODO: Make configurable. + let factor = (15.0 * dt).min(1.0); + self.offset += (target.offset - self.offset) * factor; + self.ori = vek::Slerp::slerp(self.ori, target.ori, factor); + self.scale += (target.scale - self.scale) * factor; + } +} + +pub trait Skeleton: Send + Sync + 'static { + type Attr; + + #[cfg(feature = "use-dyn-lib")] + const COMPUTE_FN: &'static [u8] = b"\0"; + + fn bone_count(&self) -> usize { 16 } + + fn compute_matrices(&self) -> ([FigureBoneData; 16], Vec3) { + #[cfg(not(feature = "use-dyn-lib"))] + { + Self::compute_matrices_inner(self) + } + #[cfg(feature = "use-dyn-lib")] + { + let lock = dyn_lib::LIB.lock().unwrap(); + let lib = &lock.as_ref().unwrap().lib; + + let compute_fn: libloading::Symbol ([FigureBoneData; 16], Vec3)> = + unsafe { lib.get(Self::COMPUTE_FN).unwrap() }; + + compute_fn(self) + } + } + + fn compute_matrices_inner(&self) -> ([FigureBoneData; 16], Vec3) { + panic!( + "Neither compute_matrices_inner nor compute_matrices override present in Animation \ + impl" + ) + } + + /// Change the current skeleton to be more like `target`. + fn interpolate(&mut self, target: &Self, dt: f32); +} + +pub trait Animation { + type Skeleton: Skeleton; + type Dependency; + + #[cfg(feature = "use-dyn-lib")] + const UPDATE_FN: &'static [u8] = b"\0"; + + /// Returns a new skeleton that is generated by the animation. + fn update_skeleton( + skeleton: &Self::Skeleton, + dependency: Self::Dependency, + anim_time: f64, + rate: &mut f32, + skeleton_attr: &<::Skeleton as Skeleton>::Attr, + ) -> Self::Skeleton { + #[cfg(not(feature = "use-dyn-lib"))] + { + Self::update_skeleton_inner(skeleton, dependency, anim_time, rate, skeleton_attr) + } + #[cfg(feature = "use-dyn-lib")] + { + let lock = dyn_lib::LIB.lock().unwrap(); + let lib = &lock.as_ref().unwrap().lib; + + let update_fn: libloading::Symbol< + fn( + &Self::Skeleton, + Self::Dependency, + f64, + &mut f32, + &::Attr, + ) -> Self::Skeleton, + > = unsafe { + //let start = std::time::Instant::now(); + // Overhead of 0.5-5 us (could use hashmap to mitigate if this is an issue) + let f = lib.get(Self::UPDATE_FN).unwrap(); + //println!("{}", start.elapsed().as_nanos()); + f + }; + + update_fn(skeleton, dependency, anim_time, rate, skeleton_attr) + } + } + + fn update_skeleton_inner( + _skeleton: &Self::Skeleton, + _dependency: Self::Dependency, + _anim_time: f64, + _rate: &mut f32, + _skeleton_attr: &<::Skeleton as Skeleton>::Attr, + ) -> Self::Skeleton { + panic!( + "Neither update_skeleton_inner nor update_skeleton override present in Animation impl" + ) + } +} diff --git a/voxygen/src/anim/object/mod.rs b/voxygen/src/anim/src/object/mod.rs similarity index 96% rename from voxygen/src/anim/object/mod.rs rename to voxygen/src/anim/src/object/mod.rs index ec84809741..8b5b213429 100644 --- a/voxygen/src/anim/object/mod.rs +++ b/voxygen/src/anim/src/object/mod.rs @@ -1,5 +1,4 @@ -use super::Skeleton; -use crate::render::FigureBoneData; +use super::{FigureBoneData, Skeleton}; use vek::*; #[derive(Clone)] diff --git a/voxygen/src/anim/quadruped_medium/idle.rs b/voxygen/src/anim/src/quadruped_medium/idle.rs similarity index 95% rename from voxygen/src/anim/quadruped_medium/idle.rs rename to voxygen/src/anim/src/quadruped_medium/idle.rs index d47848cca1..99815d01a3 100644 --- a/voxygen/src/anim/quadruped_medium/idle.rs +++ b/voxygen/src/anim/src/quadruped_medium/idle.rs @@ -8,7 +8,11 @@ impl Animation for IdleAnimation { type Dependency = f64; type Skeleton = QuadrupedMediumSkeleton; - fn update_skeleton( + #[cfg(feature = "use-dyn-lib")] + const UPDATE_FN: &'static [u8] = b"quadruped_medium_idle\0"; + + #[cfg_attr(feature = "be-dyn-lib", export_name = "quadruped_medium_idle")] + fn update_skeleton_inner( skeleton: &Self::Skeleton, global_time: Self::Dependency, anim_time: f64, diff --git a/voxygen/src/anim/quadruped_medium/jump.rs b/voxygen/src/anim/src/quadruped_medium/jump.rs similarity index 93% rename from voxygen/src/anim/quadruped_medium/jump.rs rename to voxygen/src/anim/src/quadruped_medium/jump.rs index da66316fa0..09be93ca59 100644 --- a/voxygen/src/anim/quadruped_medium/jump.rs +++ b/voxygen/src/anim/src/quadruped_medium/jump.rs @@ -7,7 +7,11 @@ impl Animation for JumpAnimation { type Dependency = (f32, f64); type Skeleton = QuadrupedMediumSkeleton; - fn update_skeleton( + #[cfg(feature = "use-dyn-lib")] + const UPDATE_FN: &'static [u8] = b"quadruped_medium_jump\0"; + + #[cfg_attr(feature = "be-dyn-lib", export_name = "quadruped_medium_jump")] + fn update_skeleton_inner( skeleton: &Self::Skeleton, _global_time: Self::Dependency, _anim_time: f64, diff --git a/voxygen/src/anim/quadruped_medium/mod.rs b/voxygen/src/anim/src/quadruped_medium/mod.rs similarity index 96% rename from voxygen/src/anim/quadruped_medium/mod.rs rename to voxygen/src/anim/src/quadruped_medium/mod.rs index e69cfe6fb1..bf65ab0750 100644 --- a/voxygen/src/anim/quadruped_medium/mod.rs +++ b/voxygen/src/anim/src/quadruped_medium/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{idle::IdleAnimation, jump::JumpAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; @@ -32,9 +31,13 @@ impl QuadrupedMediumSkeleton { impl Skeleton for QuadrupedMediumSkeleton { type Attr = SkeletonAttr; + #[cfg(feature = "use-dyn-lib")] + const COMPUTE_FN: &'static [u8] = b"quadruped_medium_compute_mats\0"; + fn bone_count(&self) -> usize { 11 } - fn compute_matrices(&self) -> ([FigureBoneData; 16], Vec3) { + #[cfg_attr(feature = "be-dyn-lib", export_name = "quadruped_medium_compute_mats")] + fn compute_matrices_inner(&self) -> ([FigureBoneData; 16], Vec3) { let ears_mat = self.ears.compute_base_matrix(); let head_upper_mat = self.head_upper.compute_base_matrix(); let head_lower_mat = self.head_lower.compute_base_matrix(); diff --git a/voxygen/src/anim/quadruped_medium/run.rs b/voxygen/src/anim/src/quadruped_medium/run.rs similarity index 96% rename from voxygen/src/anim/quadruped_medium/run.rs rename to voxygen/src/anim/src/quadruped_medium/run.rs index d87deabc56..07ca660220 100644 --- a/voxygen/src/anim/quadruped_medium/run.rs +++ b/voxygen/src/anim/src/quadruped_medium/run.rs @@ -8,7 +8,11 @@ impl Animation for RunAnimation { type Dependency = (f32, f64); type Skeleton = QuadrupedMediumSkeleton; - fn update_skeleton( + #[cfg(feature = "use-dyn-lib")] + const UPDATE_FN: &'static [u8] = b"quadruped_medium_run\0"; + + #[cfg_attr(feature = "be-dyn-lib", export_name = "quadruped_medium_run")] + fn update_skeleton_inner( skeleton: &Self::Skeleton, (_velocity, global_time): Self::Dependency, anim_time: f64, diff --git a/voxygen/src/anim/quadruped_small/idle.rs b/voxygen/src/anim/src/quadruped_small/idle.rs similarity index 93% rename from voxygen/src/anim/quadruped_small/idle.rs rename to voxygen/src/anim/src/quadruped_small/idle.rs index 38706e65c7..1aa53671b4 100644 --- a/voxygen/src/anim/quadruped_small/idle.rs +++ b/voxygen/src/anim/src/quadruped_small/idle.rs @@ -8,7 +8,11 @@ impl Animation for IdleAnimation { type Dependency = f64; type Skeleton = QuadrupedSmallSkeleton; - fn update_skeleton( + #[cfg(feature = "use-dyn-lib")] + const UPDATE_FN: &'static [u8] = b"quadruped_small_idle\0"; + + #[cfg_attr(feature = "be-dyn-lib", export_name = "quadruped_small_idle")] + fn update_skeleton_inner( skeleton: &Self::Skeleton, global_time: Self::Dependency, anim_time: f64, diff --git a/voxygen/src/anim/quadruped_small/jump.rs b/voxygen/src/anim/src/quadruped_small/jump.rs similarity index 91% rename from voxygen/src/anim/quadruped_small/jump.rs rename to voxygen/src/anim/src/quadruped_small/jump.rs index 9ad8c16499..fd52280d8e 100644 --- a/voxygen/src/anim/quadruped_small/jump.rs +++ b/voxygen/src/anim/src/quadruped_small/jump.rs @@ -7,7 +7,11 @@ impl Animation for JumpAnimation { type Dependency = (f32, f64); type Skeleton = QuadrupedSmallSkeleton; - fn update_skeleton( + #[cfg(feature = "use-dyn-lib")] + const UPDATE_FN: &'static [u8] = b"quadruped_small_jump\0"; + + #[cfg_attr(feature = "be-dyn-lib", export_name = "quadruped_small_jump")] + fn update_skeleton_inner( skeleton: &Self::Skeleton, (_velocity, _global_time): Self::Dependency, _anim_time: f64, diff --git a/voxygen/src/anim/quadruped_small/mod.rs b/voxygen/src/anim/src/quadruped_small/mod.rs similarity index 95% rename from voxygen/src/anim/quadruped_small/mod.rs rename to voxygen/src/anim/src/quadruped_small/mod.rs index 3254412fb7..4031671b40 100644 --- a/voxygen/src/anim/quadruped_small/mod.rs +++ b/voxygen/src/anim/src/quadruped_small/mod.rs @@ -5,8 +5,7 @@ pub mod run; // Reexports pub use self::{idle::IdleAnimation, jump::JumpAnimation, run::RunAnimation}; -use super::{Bone, Skeleton}; -use crate::render::FigureBoneData; +use super::{Bone, FigureBoneData, Skeleton}; use common::comp::{self}; use vek::Vec3; @@ -28,9 +27,13 @@ impl QuadrupedSmallSkeleton { impl Skeleton for QuadrupedSmallSkeleton { type Attr = SkeletonAttr; + #[cfg(feature = "use-dyn-lib")] + const COMPUTE_FN: &'static [u8] = b"quadruped_small_compute_mats\0"; + fn bone_count(&self) -> usize { 7 } - fn compute_matrices(&self) -> ([FigureBoneData; 16], Vec3) { + #[cfg_attr(feature = "be-dyn-lib", export_name = "quadruped_small_compute_mats")] + fn compute_matrices_inner(&self) -> ([FigureBoneData; 16], Vec3) { let chest_mat = self.chest.compute_base_matrix(); ( [ diff --git a/voxygen/src/anim/quadruped_small/run.rs b/voxygen/src/anim/src/quadruped_small/run.rs similarity index 92% rename from voxygen/src/anim/quadruped_small/run.rs rename to voxygen/src/anim/src/quadruped_small/run.rs index 7e6daa30a1..a2b26d938c 100644 --- a/voxygen/src/anim/quadruped_small/run.rs +++ b/voxygen/src/anim/src/quadruped_small/run.rs @@ -8,7 +8,11 @@ impl Animation for RunAnimation { type Dependency = (f32, f64); type Skeleton = QuadrupedSmallSkeleton; - fn update_skeleton( + #[cfg(feature = "use-dyn-lib")] + const UPDATE_FN: &'static [u8] = b"quadruped_small_run\0"; + + #[cfg_attr(feature = "be-dyn-lib", export_name = "quadruped_small_run")] + fn update_skeleton_inner( skeleton: &Self::Skeleton, (_velocity, _global_time): Self::Dependency, anim_time: f64, diff --git a/voxygen/src/lib.rs b/voxygen/src/lib.rs index de25dc82ed..02bc36d053 100644 --- a/voxygen/src/lib.rs +++ b/voxygen/src/lib.rs @@ -5,7 +5,6 @@ #[macro_use] pub mod ui; -pub mod anim; pub mod audio; pub mod controller; mod ecs; diff --git a/voxygen/src/main.rs b/voxygen/src/main.rs index 54fdd777dd..2f2b37ec20 100644 --- a/voxygen/src/main.rs +++ b/voxygen/src/main.rs @@ -162,6 +162,10 @@ fn main() { default_hook(panic_info); })); + // Initialise watcher for animation hotreloading + #[cfg(feature = "hot-anim")] + anim::init(); + // Set up the initial play state. let mut states: Vec> = vec![Box::new(MainMenuState::new(&mut global_state))]; states diff --git a/voxygen/src/render/mod.rs b/voxygen/src/render/mod.rs index 605ca1e4e2..9bf74d5aa9 100644 --- a/voxygen/src/render/mod.rs +++ b/voxygen/src/render/mod.rs @@ -7,7 +7,6 @@ pub mod model; pub mod pipelines; pub mod renderer; pub mod texture; -mod util; // Reexports pub use self::{ diff --git a/voxygen/src/render/pipelines/figure.rs b/voxygen/src/render/pipelines/figure.rs index 94d1272112..e0226d0aa0 100644 --- a/voxygen/src/render/pipelines/figure.rs +++ b/voxygen/src/render/pipelines/figure.rs @@ -1,5 +1,5 @@ use super::{ - super::{util::arr_to_mat, Pipeline, TgtColorFmt, TgtDepthStencilFmt}, + super::{Pipeline, TgtColorFmt, TgtDepthStencilFmt}, Globals, Light, Shadow, }; use gfx::{ @@ -81,7 +81,7 @@ impl Locals { flags |= is_player as u32; Self { - model_mat: arr_to_mat(model_mat.into_col_array()), + model_mat: model_mat.into_col_arrays(), model_col: col.into_array(), flags, } @@ -95,7 +95,7 @@ impl Default for Locals { impl BoneData { pub fn new(bone_mat: Mat4) -> Self { Self { - bone_mat: arr_to_mat(bone_mat.into_col_array()), + bone_mat: bone_mat.into_col_arrays(), } } diff --git a/voxygen/src/render/pipelines/mod.rs b/voxygen/src/render/pipelines/mod.rs index 3f661a2cdc..81ef76456f 100644 --- a/voxygen/src/render/pipelines/mod.rs +++ b/voxygen/src/render/pipelines/mod.rs @@ -6,7 +6,6 @@ pub mod sprite; pub mod terrain; pub mod ui; -use super::util::arr_to_mat; use crate::scene::camera::CameraMode; use common::terrain::BlockKind; use gfx::{self, gfx_constant_struct_meta, gfx_defines, gfx_impl_struct_meta}; @@ -64,9 +63,9 @@ impl Globals { sprite_render_distance: f32, ) -> Self { Self { - view_mat: arr_to_mat(view_mat.into_col_array()), - proj_mat: arr_to_mat(proj_mat.into_col_array()), - all_mat: arr_to_mat((proj_mat * view_mat).into_col_array()), + view_mat: view_mat.into_col_arrays(), + proj_mat: proj_mat.into_col_arrays(), + all_mat: (proj_mat * view_mat).into_col_arrays(), cam_pos: Vec4::from(cam_pos).into_array(), focus_pos: Vec4::from(focus_pos).into_array(), view_distance: [view_distance; 4], diff --git a/voxygen/src/render/pipelines/sprite.rs b/voxygen/src/render/pipelines/sprite.rs index c0747faa88..9eb562eb49 100644 --- a/voxygen/src/render/pipelines/sprite.rs +++ b/voxygen/src/render/pipelines/sprite.rs @@ -1,5 +1,5 @@ use super::{ - super::{util::arr_to_mat, Pipeline, TgtColorFmt, TgtDepthStencilFmt}, + super::{Pipeline, TgtColorFmt, TgtDepthStencilFmt}, Globals, Light, Shadow, }; use gfx::{ @@ -67,7 +67,7 @@ impl Vertex { impl Instance { pub fn new(mat: Mat4, col: Rgb, wind_sway: f32) -> Self { - let mat_arr = arr_to_mat(mat.into_col_array()); + let mat_arr = mat.into_col_arrays(); Self { inst_mat0: mat_arr[0], inst_mat1: mat_arr[1], diff --git a/voxygen/src/render/util.rs b/voxygen/src/render/util.rs deleted file mode 100644 index 68b52f671a..0000000000 --- a/voxygen/src/render/util.rs +++ /dev/null @@ -1,10 +0,0 @@ -// TODO: Get rid of this ugliness. -#[rustfmt::skip] -pub fn arr_to_mat(arr: [f32; 16]) -> [[f32; 4]; 4] { - [ - [arr[ 0], arr[ 1], arr[ 2], arr[ 3]], - [arr[ 4], arr[ 5], arr[ 6], arr[ 7]], - [arr[ 8], arr[ 9], arr[10], arr[11]], - [arr[12], arr[13], arr[14], arr[15]], - ] -} diff --git a/voxygen/src/scene/figure/cache.rs b/voxygen/src/scene/figure/cache.rs index cb89da0a1d..25fbeea417 100644 --- a/voxygen/src/scene/figure/cache.rs +++ b/voxygen/src/scene/figure/cache.rs @@ -1,10 +1,10 @@ use super::load::*; use crate::{ - anim::{self, Skeleton}, mesh::Meshable, render::{FigurePipeline, Mesh, Model, Renderer}, scene::camera::CameraMode, }; +use anim::{Skeleton}; use common::{ assets::watch::ReloadIndicator, comp::{ diff --git a/voxygen/src/scene/figure/mod.rs b/voxygen/src/scene/figure/mod.rs index c8f2fd3b27..50c1f1f630 100644 --- a/voxygen/src/scene/figure/mod.rs +++ b/voxygen/src/scene/figure/mod.rs @@ -5,13 +5,6 @@ pub use cache::FigureModelCache; pub use load::load_mesh; // TODO: Don't make this public. use crate::{ - anim::{ - self, biped_large::BipedLargeSkeleton, bird_medium::BirdMediumSkeleton, - bird_small::BirdSmallSkeleton, character::CharacterSkeleton, critter::CritterSkeleton, - dragon::DragonSkeleton, fish_medium::FishMediumSkeleton, fish_small::FishSmallSkeleton, - golem::GolemSkeleton, object::ObjectSkeleton, quadruped_medium::QuadrupedMediumSkeleton, - quadruped_small::QuadrupedSmallSkeleton, Animation, Skeleton, - }, ecs::comp::Interpolated, render::{Consts, FigureBoneData, FigureLocals, Globals, Light, Renderer, Shadow}, scene::{ @@ -19,6 +12,13 @@ use crate::{ SceneData, }, }; +use anim::{ + biped_large::BipedLargeSkeleton, bird_medium::BirdMediumSkeleton, + bird_small::BirdSmallSkeleton, character::CharacterSkeleton, critter::CritterSkeleton, + dragon::DragonSkeleton, fish_medium::FishMediumSkeleton, fish_small::FishSmallSkeleton, + golem::GolemSkeleton, object::ObjectSkeleton, quadruped_medium::QuadrupedMediumSkeleton, + quadruped_small::QuadrupedSmallSkeleton, Animation, Skeleton, +}; use common::{ comp::{ item::ItemKind, Body, CharacterState, Last, LightAnimation, LightEmitter, Loadout, Ori, @@ -2037,7 +2037,8 @@ pub struct FigureState { impl FigureState { pub fn new(renderer: &mut Renderer, skeleton: S) -> Self { - let (bone_consts, lantern_offset) = skeleton.compute_matrices(); + let (bone_mats, lantern_offset) = skeleton.compute_matrices(); + let bone_consts = figure_bone_data_from_anim(bone_mats); Self { bone_consts: renderer.create_consts(&bone_consts).unwrap(), locals: renderer.create_consts(&[FigureLocals::default()]).unwrap(), @@ -2081,7 +2082,9 @@ impl FigureState { let locals = FigureLocals::new(mat, col, is_player); renderer.update_consts(&mut self.locals, &[locals]).unwrap(); - let (new_bone_consts, lantern_offset) = self.skeleton.compute_matrices(); + let (new_bone_mats, lantern_offset) = self.skeleton.compute_matrices(); + let new_bone_consts = figure_bone_data_from_anim(new_bone_mats); + renderer .update_consts( &mut self.bone_consts, @@ -2097,3 +2100,24 @@ impl FigureState { pub fn skeleton_mut(&mut self) -> &mut S { &mut self.skeleton } } + +fn figure_bone_data_from_anim(mats: [anim::FigureBoneData; 16]) -> [FigureBoneData; 16] { + [ + FigureBoneData::new(mats[0].0), + FigureBoneData::new(mats[1].0), + FigureBoneData::new(mats[2].0), + FigureBoneData::new(mats[3].0), + FigureBoneData::new(mats[4].0), + FigureBoneData::new(mats[5].0), + FigureBoneData::new(mats[6].0), + FigureBoneData::new(mats[7].0), + FigureBoneData::new(mats[8].0), + FigureBoneData::new(mats[9].0), + FigureBoneData::new(mats[10].0), + FigureBoneData::new(mats[11].0), + FigureBoneData::new(mats[12].0), + FigureBoneData::new(mats[13].0), + FigureBoneData::new(mats[14].0), + FigureBoneData::new(mats[15].0), + ] +} diff --git a/voxygen/src/scene/mod.rs b/voxygen/src/scene/mod.rs index 914b5e98f0..31923d547a 100644 --- a/voxygen/src/scene/mod.rs +++ b/voxygen/src/scene/mod.rs @@ -9,7 +9,6 @@ use self::{ terrain::Terrain, }; use crate::{ - anim::character::SkeletonAttr, audio::{music::MusicMgr, sfx::SfxMgr, AudioFrontend}, render::{ create_pp_mesh, create_skybox_mesh, Consts, Globals, Light, Model, PostProcessLocals, @@ -17,6 +16,7 @@ use crate::{ }, window::{AnalogGameInput, Event}, }; +use anim::character::SkeletonAttr; use common::{ comp, state::State, diff --git a/voxygen/src/scene/simple.rs b/voxygen/src/scene/simple.rs index c4c8f4e235..22e971d1ed 100644 --- a/voxygen/src/scene/simple.rs +++ b/voxygen/src/scene/simple.rs @@ -1,9 +1,4 @@ use crate::{ - anim::{ - character::{CharacterSkeleton, IdleAnimation, SkeletonAttr}, - fixture::FixtureSkeleton, - Animation, Skeleton, - }, mesh::Meshable, render::{ create_pp_mesh, create_skybox_mesh, Consts, FigurePipeline, Globals, Light, Mesh, Model, @@ -15,6 +10,11 @@ use crate::{ }, window::{Event, PressState}, }; +use anim::{ + character::{CharacterSkeleton, IdleAnimation, SkeletonAttr}, + fixture::FixtureSkeleton, + Animation, Skeleton, +}; use common::{ comp::{humanoid, Body, Loadout}, figure::Segment,