PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/away3d/animators/skeleton/SkeletonDirectionalNode.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 123 lines | 99 code | 16 blank | 8 comment | 13 complexity | 48a6bdb2c191c6243383e1439c2613e0 MD5 | raw file
  1/**
  2 * Author: David Lenaerts
  3 */
  4package away3d.animators.skeleton
  5{
  6
  7	import flash.geom.Vector3D;
  8
  9	public class SkeletonDirectionalNode extends SkeletonTreeNode
 10	{
 11		/**
 12		 * The weights for each joint. The total needs to equal 1.
 13		 */
 14		public var forward : SkeletonTreeNode;
 15		public var backward : SkeletonTreeNode;
 16		public var left : SkeletonTreeNode;
 17		public var right : SkeletonTreeNode;
 18		private var _inputA : SkeletonTreeNode;
 19		private var _inputB : SkeletonTreeNode;
 20		private var _blendWeight : Number;
 21		private var _blendDirty : Boolean;
 22
 23		public function SkeletonDirectionalNode()
 24		{
 25			super();
 26		}
 27
 28		override public function set time(value : Number) : void
 29		{
 30			forward.time = value;
 31			backward.time = value;
 32			left.time = value;
 33			right.time = value;
 34			super.time = value;
 35		}
 36
 37		// between 0 - 360
 38		override public function set direction(value : Number) : void
 39		{
 40			forward.direction = value;
 41			backward.direction = value;
 42			left.direction = value;
 43			right.direction = value;
 44			_blendDirty = true;
 45			super.direction = value;
 46		}
 47
 48		override public function updatePose(skeleton : Skeleton) : void
 49		{
 50			if (_blendDirty) updateBlend();
 51			_inputA.updatePose(skeleton);
 52			_inputB.updatePose(skeleton);
 53
 54			var durA : Number = _inputA.duration;
 55			var endPose : JointPose;
 56			var endPoses : Vector.<JointPose> = skeletonPose.jointPoses;
 57			var poses1 : Vector.<JointPose> = _inputA.skeletonPose.jointPoses;
 58			var poses2 : Vector.<JointPose> = _inputB.skeletonPose.jointPoses;
 59			var pose1 : JointPose, pose2 : JointPose;
 60			var p1 : Vector3D, p2 : Vector3D;
 61			var tr : Vector3D;
 62			var numJoints : uint = skeleton.numJoints;
 63
 64			// :s
 65			if (endPoses.length != numJoints) endPoses.length = numJoints;
 66
 67			_duration = durA + _blendWeight*(_inputB.duration - durA);
 68
 69			for (var i : uint = 0; i < numJoints; ++i) {
 70				endPose = endPoses[i] ||= new JointPose();
 71				pose1 = poses1[i];
 72				pose2 = poses2[i];
 73				p1 = pose1.translation; p2 = pose2.translation;
 74
 75				endPose.orientation.lerp(pose1.orientation, pose2.orientation, _blendWeight);
 76
 77				tr = endPose.translation;
 78				tr.x = p1.x + _blendWeight*(p2.x - p1.x);
 79				tr.y = p1.y + _blendWeight*(p2.y - p1.y);
 80				tr.z = p1.z + _blendWeight*(p2.z - p1.z);
 81			}
 82		}
 83
 84		private function updateBlend() : void
 85		{
 86			_blendDirty = false;
 87
 88			while (_direction < 0) _direction += 360;
 89			while (_direction >= 360) _direction -= 360;
 90
 91			if (_direction < 90) {
 92				_inputA = forward;
 93				_inputB = right;
 94				_blendWeight = _direction/90;
 95			}
 96			else if (_direction < 180) {
 97				_inputA = right;
 98				_inputB = backward;
 99				_blendWeight = (_direction-90)/90;
100			}
101			else if (_direction < 270) {
102				_inputA = backward;
103				_inputB = left;
104				_blendWeight = (_direction-180)/90;
105			}
106			else {
107				_inputA = left;
108				_inputB = forward;
109				_blendWeight = (_direction-270)/90;
110			}
111		}
112
113		override public function updatePositionData() : void
114		{
115			if (_blendDirty) updateBlend();
116			var deltA : Vector3D = _inputA.rootDelta;
117			var deltB : Vector3D = _inputB.rootDelta;
118			rootDelta.x = deltA.x + _blendWeight*(deltB.x - deltA.x);
119			rootDelta.y = deltA.y + _blendWeight*(deltB.y - deltA.y);
120			rootDelta.z = deltA.z + _blendWeight*(deltB.z - deltA.z);
121		}
122	}
123}