Git Product home page Git Product logo

skeletal-animation-system's Introduction

skeletal-animation-system npm version Build Status

A standalone, stateless, dual quaternion based skeletal animation system built with interactive applications in mind

View live demo

TODO: Create a demo site instead of just a demo. Embed a demo inside of the demo site

Tutorials

WebGL Skeletal Animation Sound Effects Tutorial

Attaching objects to bones

WebGL Skeletal Animation Tutorial

Background / Initial Motivation

skeletal-animation-system aims to give the user a flexible module for managing skeletal animations across different 3d models and bone groups.

skeletal-animation-system aims to provide a sane API for starting, stopping and interpolating skeletal animations.

It supports blending between your previous and current animation when you switch animations. It also supports splitting your model into different bone groups such as the upper and lower body, allowing you to, for example, play a walking animation for your legs while playing a punch animation for your upper body.

skeletal-animation-system does not maintain an internal state, but instead lets the modules consumer track things such as the current animation and the current clock time.

I use matrices and not dual quaternions

The first versions of skeletal-animation-system uses matrices instead of dual quaternions.

The issue there was that blending matrices can lead to unexpected artifacts.

So we switched to dual quaternions and completely dropped support for matrices.

However, if you use matrices you can still make use of skeletal-animation-system.

  1. Convert your matrices into dual quaternions once when you first load your model.
  2. Use skeletal-animation-system to determine your pose dual quaternions
  3. Convert your pose dual quaternions back into matrices before each render
  4. Use your newly created matrices for skinning

The 3rd step here means that you're doing some extra work on the CPU, but this hopefully bridges the gap for you until you can move to dual quaternion based skinning.

TODO: Example code demonstrating how to incorporate skeletal-animation-system into matrix based skinning application

This API is still experimental and will evolve as we use it and realize the kinks.

To Install

$ npm install --save skeletal-animation-system

Demo

To run the demo locally:

$ git clone https://github.com/chinedufn/skeletal-animation-system
$ cd skeletal-animation-system
$ npm install
$ npm run demo

Changes to the demo and src files will now live reload in your browser.


View live demo

Usage

var animationSystem = require('skeletal-animation-system')
// Parsed using collada-dae-parser or some other parser
var parsedColladaModel = require('./parsed-collada-model.json')

// Keyframe data for all joints.
// @see `github.com/chinedufn/blender-actions-to-json` for an example format
var lowerBodyKeyframes = {...}
var upperBodyKey = {...}

// Convert our joint names into their associated joint index number
// This number comes from collada-dae-parser
// (or your parser of choice)
var upperBodyJointNums = [0, 1, 5, 6, 8]
var lowerBodyJointNums = [2, 3, 4, 7, 9]

// Our options for animating our model's upper body
var upperBodyOptions = {
  currentTime: 28.24,
  jointNums: upperBodyJointsNums,
  blendFunction: function (dt) {
    // Blend animations linearly over 2.5 seconds
    return 1 / 2.5 * dt
  },
  currentAnimation: {
    keyframes: currentAnimKeyframes,
    startTime: 25
  },
  previousAnimation: {
    keyframes: previousAnimKeyframes,
    startTime: 24.5
  }
}

// Our options for animating our model's lower body
var lowerBodyOptions = {
  currentTime: 28.24,
  jointNums: lowerBodyJointNums,
  currentAnimation: {
    keyframes: currentAnimKeyframes,
    startTime: 24.3,
    noLoop: true
  }
}

var interpolatedUpperBodyJoints = animationSystem
.interpolateJoints(upperBodyOptions).joints

var lowerBodyData = animationSystem
.interpolateJoints(lowerBodyOptions)
var interpolatedLowerBodyJoints = lowerBodyData.joints

console.log(lowerBodyData.currentAnimationInfo)
// => {lowerKeyframeNumber: 5, upperKeyframeNumber: 6}

// You now have your interpolated upper and lower body dual quaternions (joints).
// You can pass these into any vertex shader that
// works with dual quaternions

// If you're just getting started and you still need matrices you
// can convert these into matrices using dual-quat-to-mat4
//  @see https://github.com/chinedufn/dual-quat-to-mat4

Expected JSON model format

TODO: Link to collada-dae-parser README

Benchmark

npm run bench

TODO:

  • Handle rotation quaternion lerp when dot product is < 0
  • Implement more from the papers linked in References section below (whenever we need them)
  • Add documentation about how to approach playing a sound effect on a keyframe in your game / simulation / program
  • Benchmark
  • Allow consumer to provide the sampling function between keyframes. Currently we sample linearly between all keyframes. Could make use of chromakode/fcurve here
  • Create a new demo site and demo(s)

API

animationSystem.interpolateJoints(options) -> Object

options

Optional

Type: object

// Example overrides
var myOptions = {
  // TODO:
}
interpolatedJoints = animationSystem.interpolateJoints(myOptions)
currentTime

Type: Number

Default: 0

The current number of seconds elapsed. If you have an animation an loop, this will typically be the sum of all of your loops time deltas

// Example of tracking current time
var currentTime = 0
function animationLoop (dt) {
 currentTime += dt
}
keyframes

Type: Object

Default: {}

TODO: Link to collada-dae-parser README on keyframes for more info, but also put an example here

jointNums

Type: Array

An array of joint indices that you would like to interpolate.

Say your model has 4 joints. To interpolate the entire model you would pass in [0, 1, 2, 3]. To only interpolate two of the joints you might pass in [0, 2], or any desired combination.

These joint indices are based on the order of the joints in your keyframes

blendFunction

Type: Function

Default: Blend linearly over 0.2 seconds

A function that accepts a time elapsed in seconds and returns a value between 0 and 1.

This returned value represents the weight of the new animation.

function myBlendFunction (dt) {
  // Blend the old animation into the new one linearly over 5 seconds
  return 0.2 * dt)
}
currentAnimation

Type: Object

An object containing parameters for the current animation

If you supply a previous animation your current animation will be blended in using your blendFunction

var currentAnimation = {
  keyframes: {0: [..], 1.66666: [...]}
  startTime: 10
}
currentAnimation.keyframes

Type: Array

{
  "0": [
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
  ],
  "1.33333": [
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 5, 1]
  ]
}

Pose matrices for each joint in the model, organized by the animation time (0 and 1.33333 are seconds)

currentAnimation.startTime

Type: Number

The time in seconds that your current animation was initiated. This gets compared with the currentTime in order to interpolate your joint data appropriately.

currentAnimation.noLoop

Type: Boolean

Whether or not your animation should loop. For example, let's say you are 13 seconds into a 4 second animation.

If noLoop === true then you will be playing the frame at the 4th second.

If noLoop === false then you will be playing the frame at the 1st second.

previousAnimation

An object containing parameters for the previous animations. Your previous animation gets blended out using your blendFunction while your current animation gets blended in.

Type: Object

previousAnimation.keyframes

Type: Array

{
  "0": [
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
  ],
  "1.33333": [
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1],
    [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 5, 1]
  ]
}

Pose matrices for each joint in the model, organized by the animation time (0 and 1.33333 are seconds)

previousAnimation.startTime

Type: Number

The time in seconds that your previous animation was initiated. This is used in order to blend in the current animation.

Returned data

// Example
{
  joints: [...],
  currentAnimationInfo: {
    lowerKeyframeNumber: 0,
    upperKeyframeNumber:: 1
  }
}

currentAnimationInfo is the lower and upper keyframe time bounds of the current animation. If you have three keyframes at 1 8 and 19 seconds and you are currently 12 seconds into your animation then your lower keyframe is 1 (8) and your upper keyframe is 2 (19).

See Also

References

License

MIT

skeletal-animation-system's People

Contributors

chinedufn avatar kevzettler avatar mdkq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

skeletal-animation-system's Issues

debugging joint interpolation not updating

Hi @chinedufn,

Appreciate your thoughts on this. I have a setup where I have a modular character, and I'm trying to render 1-to-1 mesh to joint setup. I'm currently isolating the Head to get things working and go to the other body parts. Amazingly I have the Head mesh rendering and it looks correctly transformed for like the first keyframe of the animation. Screenshot below.

screenshot 2017-09-28 16 04 31

The problem I am having is that the joint data doesn't seem to update on further interpolateJoints calls. You can see the code I have instrumented below:

      animTime = (action.payload.elapsed + action.payload.dt) / 1000;

      interpolatedJoints = animationSystem.interpolateJoints({
        currentTime: animTime,
        jointNums: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
        currentAnimation: {
          keyframes: player.animation.actions.WalkCycle,
          startTime: 0,
          noLoop: false
        }
      });

     console.log(animTime, interpolatedJoints.joints[7]);

The console log output is in the screenshot above so you can see that the animTime is increasing, however, the joint output is not. Hopefully, I'm overlooking or misunderstanding something here.

New demo code

Hey so I've cleaned up the demo site quite a bit:

  • Removed all content but demo and links
  • Links now go straight to the appropriate section of the readme on github
  • Cleaned up unused classes/css etc.

I was going to just copy the old demo and fit it into the new page, but I remember you (@chinedufn) saying that you were considering redoing the demo. Is that still something you want to do or is it cool if I just go ahead and try to use the old stuff?

Landing page mockup

mockup

Ok thoughts?

Color scheme can definitely change

could be cool to go for a more white/black/grey/reddish-pink accent scheme given that its a pretty low level tool and doesn't require the flashiness that's currently there... in fact I'm gonna experiment with that I have an idea in mind.

Not sure about single page vs multipage. I think having the demo separate is good since it could be GPU intensive for crappier computers or could break on computers that have JS disabled. But also maybe that doesn't really need to be considered given this is a JS project?

I'm thinking keep link structure as is, and getting started button auto scrolls the page to an anchor lower on the page.

update for Landon?

Hi @chinedufn, I imagine you are no longer using this in your Rust projects. Does it makes sense to update this to support the Landon export format? I'm happy to take a stab at it.

I currently have a large ugly translation file that does massaging on the Landon format to make it work with skeletal-animation-system. I might make sense to move some of this into the skeletal-animation-system module. If blender-actions-to-json is being deprecated it might make sense to make this focused on landon support

Support for dynamic mesh to bone binding?

@chinedufn I'm really excited about your work here. I've been hacking on a WebGL game for some time and I'm not happy with any of the full featured engines I have tried. The Blender -> WebGL pipeline is unstable, borderline unusable in other engines. I think skeletal animation is a critical missing piece of the stack-gl ecosystem.

Can I manually create a mesh elsewhere, or import a separate mesh and bind it to a bone? Is that possible with this library? Like if I have a equipment like swords that I need to attach to a character. The workflow would be:

  1. create armature and animations in blender with 'place holder meshes'
  2. export to collada and import to WebGL environment
  3. animate the armature in WebGL
  4. create another mesh and bind to existing armature

Let me know if that example makes sense.

cannot find module when run demo

Hellothere, thanks for making awesome modules, but i got error while trying to demo.

what i did

$ git clone https://github.com/chinedufn/skeletal-animation-system
$ cd skeletal-animation-system
$ npm install
$ npm run demo

get error msg like

Cannot find module 'C:\Users\Jay\Desktop\prototypes\skeletons\skeletal-animation-system\demo\webgl\browser-entry.js'
skeletal-animation-system\

can anyone help?

Thanks

clarification on `currentAnimationInfo` return data

Hi chinedfun,

I'm confused on what lowerKeyframeNumber and upperKeyframeNumber actually represent.

{
  joints: [...],
  currentAnimationInfo: {
    lowerKeyframeNumber: 0,
    upperKeyframeNumber:: 1
  }
}

The lower/upper distinction could mean that it's for the lower/upper dual quaternions but following the example from the README you would call interpolateJoints twice giving you 2 sets of lower/upperKeframeNumber

var interpolatedUpperBodyJoints = animationSystem
.interpolateJoints(upperBodyOptions).joints
console.log(upperBodyData.currentAnimationInfo)
// => {lowerKeyframeNumber: 5, upperKeyframeNumber: 6}

var lowerBodyData = animationSystem
.interpolateJoints(lowerBodyOptions)
console.log(lowerBodyData.currentAnimationInfo)
// => {lowerKeyframeNumber: 5, upperKeyframeNumber: 6}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.