PageRenderTime 124ms CodeModel.GetById 27ms app.highlight 44ms RepoModel.GetById 18ms app.codeStats 0ms

/src/away3d/animators/skeleton/SkeletonAdditiveNode.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 100 lines | 76 code | 18 blank | 6 comment | 3 complexity | fd4c650d42a3c7c5405c80f08446a07a MD5 | raw file
  1/**
  2 * Author: David Lenaerts
  3 */
  4package away3d.animators.skeleton
  5{
  6
  7	import away3d.core.math.Quaternion;
  8
  9	import flash.geom.Vector3D;
 10
 11	public class SkeletonAdditiveNode extends SkeletonTreeNode
 12	{
 13		public var baseInput : SkeletonTreeNode;
 14		public var differenceInput : SkeletonTreeNode;
 15		private var _blendWeight : Number = 0;
 16
 17		private static var _tempQuat : Quaternion = new Quaternion();
 18
 19		public function SkeletonAdditiveNode()
 20		{
 21			super();
 22		}
 23
 24		override public function get duration() : Number
 25		{
 26			return baseInput.duration;
 27		}
 28
 29		public function get blendWeight() : Number
 30		{
 31			return _blendWeight;
 32		}
 33
 34		public function set blendWeight(value : Number) : void
 35		{
 36			_blendWeight = value;
 37			_duration = baseInput.duration;
 38		}
 39
 40		override public function set time(value : Number) : void
 41		{
 42			baseInput.time = value;
 43			differenceInput.time = value;
 44			super.time = value;
 45		}
 46
 47		override public function set direction(value : Number) : void
 48		{
 49			baseInput.direction = value;
 50			differenceInput.direction = value;
 51			super.direction = value;
 52		}
 53
 54		// todo: return whether or not update was performed
 55		override public function updatePose(skeleton : Skeleton) : void
 56		{
 57			// todo: should only update if blendWeight dirty, or if either child returns false
 58			baseInput.updatePose(skeleton);
 59			differenceInput.updatePose(skeleton);
 60
 61			var endPose : JointPose;
 62			var endPoses : Vector.<JointPose> = skeletonPose.jointPoses;
 63			var basePoses : Vector.<JointPose> = baseInput.skeletonPose.jointPoses;
 64			var diffPoses : Vector.<JointPose> = differenceInput.skeletonPose.jointPoses;
 65			var base : JointPose, diff : JointPose;
 66			var basePos : Vector3D, diffPos : Vector3D;
 67			var tr : Vector3D;
 68			var numJoints : uint = skeleton.numJoints;
 69
 70			// :s
 71			if (endPoses.length != numJoints) endPoses.length = numJoints;
 72
 73			for (var i : uint = 0; i < numJoints; ++i) {
 74				endPose = endPoses[i] ||= new JointPose();
 75				base = basePoses[i];
 76				diff = diffPoses[i];
 77				basePos = base.translation;
 78				diffPos = diff.translation;
 79
 80				_tempQuat.multiply(diff.orientation, base.orientation);
 81				endPose.orientation.lerp(base.orientation, _tempQuat, _blendWeight);
 82
 83				tr = endPose.translation;
 84				tr.x = basePos.x + _blendWeight*diffPos.x;
 85				tr.y = basePos.y + _blendWeight*diffPos.y;
 86				tr.z = basePos.z + _blendWeight*diffPos.z;
 87			}
 88		}
 89
 90		override public function updatePositionData() : void
 91		{
 92			var deltA : Vector3D = baseInput.rootDelta;
 93			var deltB : Vector3D = differenceInput.rootDelta;
 94
 95			rootDelta.x = deltA.x + _blendWeight*deltB.x;
 96			rootDelta.y = deltA.y + _blendWeight*deltB.y;
 97			rootDelta.z = deltA.z + _blendWeight*deltB.z;
 98		}
 99	}
100}