├── .gitignore
├── .travis.yml
├── README.md
├── benchmark
└── benchmark.js
├── index.js
├── package.json
├── site
├── character.png
├── index.html
├── main.js
├── styles.css
└── tomorrow.css
├── src
├── blend-dual-quaternions.js
├── get-previous-animation-data.js
└── skeletal-animation-system.js
└── test
├── blend-out-previous-anim.js
└── skeletal-animation-system.js
/.gitignore:
--------------------------------------------------------------------------------
1 | example-dist
2 | node_modules
3 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: node_js
2 | node_js:
3 | - '5.0'
4 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | skeletal-animation-system [](http://badge.fury.io/js/skeletal-animation-system) [](https://travis-ci.org/chinedufn/skeletal-animation-system)
2 | ===============
3 |
4 | > A standalone, stateless, dual quaternion based skeletal animation system built with interactive applications in mind
5 |
6 | [View live demo](http://chinedufn.github.io/skeletal-animation-system/)
7 |
8 | TODO: Create a demo site instead of just a demo. Embed a demo inside of the demo site
9 |
10 | ## Tutorials
11 |
12 | [WebGL Skeletal Animation Sound Effects Tutorial](http://chinedufn.com/webgl-skeletal-animation-sound-effect-tutorial/)
13 |
14 | [Attaching objects to bones](http://chinedufn.com/attaching-objects-to-bones/)
15 |
16 | [WebGL Skeletal Animation Tutorial](http://chinedufn.com/webgl-skeletal-animation-tutorial)
17 |
18 | ## Background / Initial Motivation
19 |
20 | skeletal-animation-system aims to give the user a flexible module for managing skeletal animations across different 3d models and bone groups.
21 |
22 | `skeletal-animation-system` aims to provide a sane API for starting, stopping and interpolating skeletal animations.
23 |
24 | It supports blending between
25 | your previous and current animation when you switch animations. It also supports splitting your model into different bone groups such as the upper
26 | and lower body, allowing you to, for example, play a walking animation for your legs while playing a punch animation for your upper body.
27 |
28 | `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.
29 |
30 | ## I use matrices and not dual quaternions
31 |
32 | The first versions of `skeletal-animation-system` uses matrices instead of dual quaternions.
33 |
34 | The issue there was that [blending matrices can lead to unexpected artifacts](http://chinedufn.com/dual-quaternion-shader-explained/).
35 |
36 | So we switched to dual quaternions and completely dropped support for matrices.
37 |
38 | However, if you use matrices you can still make use of `skeletal-animation-system`.
39 |
40 | 1. [Convert your matrices into dual quaternions](https://github.com/chinedufn/mat4-to-dual-quat) once when you first load your model.
41 | 2. Use `skeletal-animation-system` to determine your pose dual quaternions
42 | 3. [Convert your pose dual quaternions back into matrices before each render](https://github.com/chinedufn/dual-quat-to-mat4)
43 | 4. Use your newly created matrices for skinning
44 |
45 | 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
46 | based skinning.
47 |
48 | TODO: Example code demonstrating how to incorporate `skeletal-animation-system` into matrix based skinning application
49 |
50 | This API is still experimental and will evolve as we use it and realize the kinks.
51 |
52 | ## To Install
53 |
54 | ```sh
55 | $ npm install --save skeletal-animation-system
56 | ```
57 |
58 | ## Demo
59 |
60 | To run the demo locally:
61 |
62 | ```sh
63 | $ git clone https://github.com/chinedufn/skeletal-animation-system
64 | $ cd skeletal-animation-system
65 | $ npm install
66 | $ npm run demo
67 | ```
68 |
69 | Changes to the `demo` and `src` files will now live reload in your browser.
70 |
71 | ---
72 |
73 | [View live demo](http://chinedufn.github.io/skeletal-animation-system/)
74 |
75 | ## Usage
76 |
77 | ```js
78 | var animationSystem = require('skeletal-animation-system')
79 | // Parsed using collada-dae-parser or some other parser
80 | var parsedColladaModel = require('./parsed-collada-model.json')
81 |
82 | // Keyframe data for all joints.
83 | // @see `github.com/chinedufn/blender-actions-to-json` for an example format
84 | var lowerBodyKeyframes = {...}
85 | var upperBodyKey = {...}
86 |
87 | // Convert our joint names into their associated joint index number
88 | // This number comes from collada-dae-parser
89 | // (or your parser of choice)
90 | var upperBodyJointNums = [0, 1, 5, 6, 8]
91 | var lowerBodyJointNums = [2, 3, 4, 7, 9]
92 |
93 | // Our options for animating our model's upper body
94 | var upperBodyOptions = {
95 | currentTime: 28.24,
96 | jointNums: upperBodyJointsNums,
97 | blendFunction: function (dt) {
98 | // Blend animations linearly over 2.5 seconds
99 | return 1 / 2.5 * dt
100 | },
101 | currentAnimation: {
102 | keyframes: currentAnimKeyframes,
103 | startTime: 25
104 | },
105 | previousAnimation: {
106 | keyframes: previousAnimKeyframes,
107 | startTime: 24.5
108 | }
109 | }
110 |
111 | // Our options for animating our model's lower body
112 | var lowerBodyOptions = {
113 | currentTime: 28.24,
114 | jointNums: lowerBodyJointNums,
115 | currentAnimation: {
116 | keyframes: currentAnimKeyframes,
117 | startTime: 24.3,
118 | noLoop: true
119 | }
120 | }
121 |
122 | var interpolatedUpperBodyJoints = animationSystem
123 | .interpolateJoints(upperBodyOptions).joints
124 |
125 | var lowerBodyData = animationSystem
126 | .interpolateJoints(lowerBodyOptions)
127 | var interpolatedLowerBodyJoints = lowerBodyData.joints
128 |
129 | console.log(lowerBodyData.currentAnimationInfo)
130 | // => {lowerKeyframeNumber: 5, upperKeyframeNumber: 6}
131 |
132 | // You now have your interpolated upper and lower body dual quaternions (joints).
133 | // You can pass these into any vertex shader that
134 | // works with dual quaternions
135 |
136 | // If you're just getting started and you still need matrices you
137 | // can convert these into matrices using dual-quat-to-mat4
138 | // @see https://github.com/chinedufn/dual-quat-to-mat4
139 | ```
140 |
141 | ## Expected JSON model format
142 |
143 | TODO: [Link to collada-dae-parser README]()
144 |
145 | ## Benchmark
146 |
147 | ```sh
148 | npm run bench
149 | ```
150 |
151 | ## TODO:
152 |
153 | - [x] Handle rotation quaternion lerp when dot product is < 0
154 | - [ ] Implement more from the papers linked in `References` section below (whenever we need them)
155 | - [x] Add documentation about how to approach playing a sound effect on a keyframe in your game / simulation / program
156 | - [x] Benchmark
157 | - [ ] Allow consumer to provide the sampling function between keyframes. Currently we sample linearly between all keyframes. Could make use of [chromakode/fcurve](https://github.com/chromakode/fcurve) here
158 | - [ ] Create a new demo site and demo(s)
159 |
160 | ## API
161 |
162 | ### `animationSystem.interpolateJoints(options)` -> `Object`
163 |
164 | #### options
165 |
166 | *Optional*
167 |
168 | Type: `object`
169 |
170 | ```js
171 | // Example overrides
172 | var myOptions = {
173 | // TODO:
174 | }
175 | interpolatedJoints = animationSystem.interpolateJoints(myOptions)
176 | ```
177 |
178 | ##### currentTime
179 |
180 | Type: `Number`
181 |
182 | Default: `0`
183 |
184 | The current number of seconds elapsed. If you have an animation an loop,
185 | this will typically be the sum of all of your loops time deltas
186 |
187 | ```js
188 | // Example of tracking current time
189 | var currentTime = 0
190 | function animationLoop (dt) {
191 | currentTime += dt
192 | }
193 | ```
194 |
195 | ##### keyframes
196 |
197 | Type: `Object`
198 |
199 | Default: `{}`
200 |
201 | TODO: Link to collada-dae-parser README on keyframes for more info, but also put an example here
202 |
203 | ##### jointNums
204 |
205 | Type: `Array`
206 |
207 | An array of joint indices that you would like to interpolate.
208 |
209 | Say your model has 4 joints. To interpolate the entire model you would pass in [0, 1, 2, 3].
210 | To only interpolate two of the joints you might pass in [0, 2], or any desired combination.
211 |
212 | These joint indices are based on the order of the joints in your `keyframes`
213 |
214 | ##### blendFunction
215 |
216 | Type: `Function`
217 |
218 | Default: `Blend linearly over 0.2 seconds`
219 |
220 | A function that accepts a time elapsed in seconds
221 | and returns a value between `0` and `1`.
222 |
223 | This returned value represents the weight of the new animation.
224 |
225 | ```js
226 | function myBlendFunction (dt) {
227 | // Blend the old animation into the new one linearly over 5 seconds
228 | return 0.2 * dt)
229 | }
230 | ```
231 |
232 | ##### currentAnimation
233 |
234 | Type: `Object`
235 |
236 | An object containing parameters for the current animation
237 |
238 | If you supply a previous animation your current animation will be
239 | blended in using your `blendFunction`
240 |
241 | ```js
242 | var currentAnimation = {
243 | keyframes: {0: [..], 1.66666: [...]}
244 | startTime: 10
245 | }
246 | ```
247 |
248 | ###### currentAnimation.keyframes
249 |
250 | Type: `Array`
251 |
252 | ```js
253 | {
254 | "0": [
255 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
256 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
257 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
258 | ],
259 | "1.33333": [
260 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1],
261 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1],
262 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 5, 1]
263 | ]
264 | }
265 | ```
266 |
267 | Pose matrices for each joint in the model, organized by the animation time (`0` and `1.33333` are seconds)
268 |
269 | ###### currentAnimation.startTime
270 |
271 | Type: `Number`
272 |
273 | The time in seconds that your current animation was initiated. This gets compared with
274 | the `currentTime` in order to interpolate your joint data appropriately.
275 |
276 | ###### currentAnimation.noLoop
277 |
278 | Type: `Boolean`
279 |
280 | Whether or not your animation should loop. For example, let's say you are 13 seconds into a 4 second animation.
281 |
282 | If `noLoop === true` then you will be playing the frame at the 4th second.
283 |
284 | If `noLoop === false` then you will be playing the frame at the 1st second.
285 |
286 | ##### previousAnimation
287 |
288 | An object containing parameters for the previous animations.
289 | Your previous animation gets blended out using your `blendFunction`
290 | while your current animation gets blended in.
291 |
292 | Type: `Object`
293 |
294 | ###### previousAnimation.keyframes
295 |
296 | Type: `Array`
297 |
298 | ```js
299 | {
300 | "0": [
301 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
302 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],
303 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
304 | ],
305 | "1.33333": [
306 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1],
307 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 2, 1],
308 | [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 5, 1]
309 | ]
310 | }
311 | ```
312 |
313 | Pose matrices for each joint in the model, organized by the animation time (`0` and `1.33333` are seconds)
314 |
315 | ###### previousAnimation.startTime
316 |
317 | Type: `Number`
318 |
319 | The time in seconds that your previous animation was initiated.
320 | This is used in order to blend in the current animation.
321 |
322 | ## Returned data
323 |
324 | ```js
325 | // Example
326 | {
327 | joints: [...],
328 | currentAnimationInfo: {
329 | lowerKeyframeNumber: 0,
330 | upperKeyframeNumber:: 1
331 | }
332 | }
333 | ```
334 |
335 | `currentAnimationInfo` is the lower and upper keyframe time bounds of the current animation.
336 | 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).
337 |
338 | ## See Also
339 |
340 | - [collada-dae-parser](https://github.com/chinedufn/collada-dae-parser)
341 | - [blender-iks-to-fks](https://github.com/chinedufn/blender-iks-to-fks)
342 | - [blender-actions-to-json](https://github.com/chinedufn/blender-actions-to-json)
343 |
344 | ## References
345 |
346 | - [Anatomy of a skeletal animation system part 1](http://blog.demofox.org/2012/09/21/anatomy-of-a-skeletal-animation-system-part-1/), [part 2](http://blog.demofox.org/2012/09/21/anatomy-of-a-skeletal-animation-system-part-2/) and [part 3](http://blog.demofox.org/2012/09/21/anatomy-of-a-skeletal-animation-system-part-3/)
347 | - [Dual-Quaternions - From Classical Mechanics to Computer Graphics and Beyond](http://www.xbdev.net/misc_demos/demos/dual_quaternions_beyond/paper.pdf)
348 | - This taught us to negate one of the dual quaternions if the dot product of the rotation quaternions was less than 0
349 |
350 | ## License
351 |
352 | MIT
353 |
--------------------------------------------------------------------------------
/benchmark/benchmark.js:
--------------------------------------------------------------------------------
1 | var bench = require('nanobench')
2 | var animationSystem = require('../')
3 |
4 | /**
5 | * TODO: Threw in a benchmark, but need to be more deliberate about what we're benchmarking
6 | * and how to make things faster
7 | */
8 | bench('Interpolate 20 bones one million times', function (b) {
9 | b.start()
10 |
11 | for (var i = 0; i < 1000000; i++) {
12 | animationSystem.interpolateJoints({
13 | currentTime: i,
14 | keyframes: {
15 | 0: [
16 | [0, 0, 0, 0, 0, 0, 0, 0],
17 | [0, 0, 0, 0, 0, 0, 0, 0],
18 | [0, 0, 0, 0, 0, 0, 0, 0],
19 | [0, 0, 0, 0, 0, 0, 0, 0],
20 | [0, 0, 0, 0, 0, 0, 0, 0],
21 | [0, 0, 0, 0, 0, 0, 0, 0],
22 | [0, 0, 0, 0, 0, 0, 0, 0],
23 | [0, 0, 0, 0, 0, 0, 0, 0],
24 | [0, 0, 0, 0, 0, 0, 0, 0],
25 | [0, 0, 0, 0, 0, 0, 0, 0]
26 | ],
27 | 100: [
28 | [1, 1, 1, 1, 1, 1, 1, 1],
29 | [1, 1, 1, 1, 1, 1, 1, 1],
30 | [1, 1, 1, 1, 1, 1, 1, 1],
31 | [1, 1, 1, 1, 1, 1, 1, 1],
32 | [1, 1, 1, 1, 1, 1, 1, 1],
33 | [1, 1, 1, 1, 1, 1, 1, 1],
34 | [1, 1, 1, 1, 1, 1, 1, 1],
35 | [1, 1, 1, 1, 1, 1, 1, 1],
36 | [1, 1, 1, 1, 1, 1, 1, 1],
37 | [1, 1, 1, 1, 1, 1, 1, 1]
38 | ]
39 | },
40 | jointNums: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
41 | currentAnimation: {
42 | range: [0, 1],
43 | startTime: 0
44 | }
45 | })
46 | }
47 |
48 | b.end()
49 | })
50 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | module.exports = require('./src/skeletal-animation-system')
2 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "skeletal-animation-system",
3 | "version": "0.8.1",
4 | "description": "A standalone, stateless, dual quaternion based skeletal animation system built with interactive applications in mind",
5 | "main": "index.js",
6 | "scripts": {
7 | "bench": "node benchmark/benchmark.js",
8 | "demo": "budo --open --live --host=localhost --dir=demo/webgl/asset demo/webgl/browser-entry.js",
9 | "regl-demo": "budo --open --live --host=localhost --dir=demo/webgl/asset demo/regl/regl.js",
10 | "deploy": "mkdirp example-dist && npm run dist:index:html && browserify demo/browser.js > example-dist/bundle.js && cp ./demo/asset/* ./example-dist/ && gh-pages -d example-dist",
11 | "dist:index:html": "echo '
12 |
13 |
14 |
15 |
16 |
17 |
28 |
29 |
40 |
41 |
Skeletal-Animation-System
42 |
A standalone, stateless, dual quaternion based skeletal animation system built with interactive applications in mind.
43 |
44 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
59 |
60 |
61 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
Introduction
73 |
74 |
75 |
skeletal-animation-system aims to give the user a flexible module for managing skeletal animations across different 3d models and bone groups.
76 |
77 |
skeletal-animation-system aims to provide a sane API for starting, stopping and interpolating skeletal animations.
78 |
79 |
It supports blending between
80 | your previous and current animation when you switch animations. It also supports splitting your model into different bone groups such as the upper
81 | and lower body, allowing you to, for example, play a walking animation for your legs while playing a punch animation for your upper body.
82 |
83 |
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.
84 |
85 |
I use matrices and not dual quaternions
86 |
87 |
The first versions of skeletal-animation-system uses matrices instead of dual quaternions.
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
103 | based skinning.
104 |
105 |
This API is still experimental and will evolve as we use it and realize the kinks.
106 |
107 |
To Install
108 |
109 |
$ npm install --save skeletal-animation-system
110 |
111 |
Usage
112 |
113 |
var animationSystem = require('skeletal-animation-system')
114 | // Parsed using collada-dae-parser or some other parser
115 | var parsedColladaModel = require('./parsed-collada-model.json')
116 |
117 | // Keyframe data for all joints.
118 | // @see `github.com/chinedufn/blender-actions-to-json` for an example format
119 | var lowerBodyKeyframes = {...}
120 | var upperBodyKey = {...}
121 |
122 | // Convert our joint names into their associated joint index number
123 | // This number comes from collada-dae-parser
124 | // (or your parser of choice)
125 | var upperBodyJointNums = [0, 1, 5, 6, 8]
126 | var lowerBodyJointNums = [2, 3, 4, 7, 9]
127 |
128 | // Our options for animating our model's upper body
129 | var upperBodyOptions = {
130 | currentTime: 28.24,
131 | jointNums: upperBodyJointsNums,
132 | blendFunction: function (dt) {
133 | // Blend animations linearly over 2.5 seconds
134 | return 1 / 2.5 * dt
135 | },
136 | currentAnimation: {
137 | keyframes: currentAnimKeyframes,
138 | startTime: 25
139 | },
140 | previousAnimation: {
141 | keyframes: previousAnimKeyframes,
142 | startTime: 24.5
143 | }
144 | }
145 |
146 | // Our options for animating our model's lower body
147 | var lowerBodyOptions = {
148 | currentTime: 28.24,
149 | jointNums: lowerBodyJointNums,
150 | currentAnimation: {
151 | keyframes: currentAnimKeyframes,
152 | startTime: 24.3,
153 | noLoop: true
154 | }
155 | }
156 |
157 | var interpolatedUpperBodyJoints = animationSystem
158 | .interpolateJoints(upperBodyOptions).joints
159 |
160 | var lowerBodyData = animationSystem
161 | .interpolateJoints(lowerBodyOptions)
162 | var interpolatedLowerBodyJoints = lowerBodyData.joints
163 |
164 | console.log(lowerBodyData.currentAnimationInfo)
165 | // => {lowerKeyframeNumber: 5, upperKeyframeNumber: 6}
166 |
167 | // You now have your interpolated upper and lower body dual quaternions (joints).
168 | // You can pass these into any vertex shader that
169 | // works with dual quaternions
170 |
171 | // If you're just getting started and you still need matrices you
172 | // can convert these into matrices using dual-quat-to-mat4
173 | // @see https://github.com/chinedufn/dual-quat-to-mat4
The current number of seconds elapsed. If you have an animation an loop,
208 | this will typically be the sum of all of your loops time deltas
209 |
210 |
// Example of tracking current time
211 | var currentTime = 0
212 | function animationLoop (dt) {
213 | currentTime += dt
214 | }
215 |
216 |
keyframes
217 |
218 |
Type: Object
219 |
220 |
Default: {}
221 |
222 |
jointNums
223 |
224 |
Type: Array
225 |
226 |
An array of joint indices that you would like to interpolate.
227 |
228 |
Say your model has 4 joints. To interpolate the entire model you would pass in [0, 1, 2, 3].
229 | To only interpolate two of the joints you might pass in [0, 2], or any desired combination.
230 |
231 |
These joint indices are based on the order of the joints in your keyframes
232 |
233 |
blendFunction
234 |
235 |
Type: Function
236 |
237 |
Default: Blend linearly over 0.2 seconds
238 |
239 |
A function that accepts a time elapsed in seconds
240 | and returns a value between 0 and 1.
241 |
242 |
This returned value represents the weight of the new animation.
243 |
244 |
function myBlendFunction (dt) {
245 | // Blend the old animation into the new one linearly over 5 seconds
246 | return 0.2 * dt)
247 | }
248 |
249 |
currentAnimation
250 |
251 |
Type: Object
252 |
253 |
An object containing parameters for the current animation
254 |
255 |
If you supply a previous animation your current animation will be
256 | blended in using your blendFunction
Pose matrices for each joint in the model, organized by the animation time (0 and 1.33333 are seconds)
281 |
282 |
currentAnimation.startTime
283 |
284 |
Type: Number
285 |
286 |
The time in seconds that your current animation was initiated. This gets compared with
287 | the currentTime in order to interpolate your joint data appropriately.
288 |
289 |
currentAnimation.noLoop
290 |
291 |
Type: Boolean
292 |
293 |
Whether or not your animation should loop. For example, let's say you are 13 seconds into a 4 second animation.
294 |
295 |
If noLoop === true then you will be playing the frame at the 4th second.
296 |
297 |
If noLoop === false then you will be playing the frame at the 1st second.
298 |
299 |
previousAnimation
300 |
301 |
An object containing parameters for the previous animations.
302 | Your previous animation gets blended out using your blendFunction
303 | while your current animation gets blended in.