PageRenderTime 504ms CodeModel.GetById 261ms app.highlight 6ms RepoModel.GetById 235ms app.codeStats 1ms

/src/away3d/animators/skeleton/SkeletonUtils.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 65 lines | 52 code | 10 blank | 3 comment | 4 complexity | f993e21ad3767959c851ecbbf04b5800 MD5 | raw file
 1/**
 2 * Author: David Lenaerts
 3 */
 4package away3d.animators.skeleton
 5{
 6	import away3d.animators.data.SkeletonAnimationSequence;
 7	import away3d.arcane;
 8
 9	import flash.geom.Matrix3D;
10	import flash.geom.Orientation3D;
11	import flash.geom.Vector3D;
12
13	use namespace arcane;
14
15	public class SkeletonUtils
16	{
17		public static function generateDifferenceClip(source : SkeletonAnimationSequence, referencePose : SkeletonPose) : SkeletonAnimationSequence
18		{
19			var diff : SkeletonAnimationSequence = new SkeletonAnimationSequence(source.name, true);
20			var numFrames : uint = source._frames.length;
21
22			for (var i : uint = 0; i < numFrames; ++i)
23				diff.addFrame(generateDifferencePose(source._frames[i], referencePose), source._durations[i]);
24
25			return diff;
26		}
27
28		public static function generateDifferencePose(source : SkeletonPose, reference : SkeletonPose) : SkeletonPose
29		{
30			if (source.numJointPoses != reference.numJointPoses)
31			{
32				throw new Error("joint counts don't match!");
33			}
34			
35			var numJoints : uint = source.numJointPoses;
36			var diff : SkeletonPose = new SkeletonPose();
37			var srcPose : JointPose;
38			var refPose : JointPose;
39			var diffPose : JointPose;			
40			var mtx : Matrix3D = new Matrix3D();
41			var tempMtx : Matrix3D = new Matrix3D();
42			var vec : Vector.<Vector3D>;
43
44			for (var i : int = 0; i < numJoints; ++i) {
45				srcPose = source.jointPoses[i];
46				refPose = reference.jointPoses[i];
47				diffPose = new JointPose();
48				diff.jointPoses[i] = diffPose;
49				diffPose.name = srcPose.name;
50
51				refPose.toMatrix3D(mtx);
52				mtx.invert();
53				mtx.append(srcPose.toMatrix3D(tempMtx));
54				vec = mtx.decompose(Orientation3D.QUATERNION);
55				diffPose.translation.copyFrom(vec[0]);
56				diffPose.orientation.x = vec[1].x;
57				diffPose.orientation.y = vec[1].y;
58				diffPose.orientation.z = vec[1].z;
59				diffPose.orientation.w = vec[1].w;
60			}
61
62			return diff;
63		}
64	}
65}