PageRenderTime 63ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 12ms app.codeStats 0ms

/src/away3d/animators/data/VertexAnimationState.as

http://github.com/away3d/away3d-core-fp11
ActionScript | 121 lines | 76 code | 18 blank | 27 comment | 12 complexity | 7abb24c6f90416fab6f41937bd02ab92 MD5 | raw file
  1package away3d.animators.data
  2{
  3	import away3d.arcane;
  4	import away3d.core.base.Geometry;
  5	import away3d.core.base.IRenderable;
  6	import away3d.core.base.SubGeometry;
  7	import away3d.core.base.SubMesh;
  8	import away3d.core.managers.Stage3DProxy;
  9	import away3d.materials.passes.MaterialPassBase;
 10
 11	import flash.display3D.Context3D;
 12	import flash.display3D.Context3DProgramType;
 13	import flash.display3D.Context3DVertexBufferFormat;
 14
 15	use namespace arcane;
 16
 17	/**
 18	 * VertexAnimationState defines the state for a given Mesh and VertexAnimation. The state consists out of the current
 19	 * poses as Geometry, and their respective blend weights.
 20	 *
 21	 * @see away3d.core.animation.vertex.VertexAnimation
 22	 */
 23	public class VertexAnimationState extends AnimationStateBase
 24	{
 25		arcane var _poses : Vector.<Geometry>;
 26
 27		// keep a narrowly typed reference
 28		private var _vertexAnimation : VertexAnimation;
 29		private var _weights : Vector.<Number>;
 30
 31		/**
 32		 * Creates a VertexAnimationState object.
 33		 * @param animation The animation object the state refers to.
 34		 */
 35		public function VertexAnimationState(animation : VertexAnimation)
 36		{
 37			super(animation);
 38			_vertexAnimation = animation;
 39			_weights = Vector.<Number>([1, 0, 0, 0]);
 40			_poses = new Vector.<Geometry>();
 41		}
 42
 43		/**
 44		 * The blend weights per pose, must be values between 0 and 1.
 45		 */
 46		public function get weights() : Vector.<Number>
 47		{
 48			return _weights;
 49		}
 50
 51		/**
 52		 * The blend poses that will be used to compute the final pose.
 53		 */
 54		public function get poses() : Vector.<Geometry>
 55		{
 56			return _poses;
 57		}
 58
 59		/**
 60		 * @inheritDoc
 61		 */
 62		override public function setRenderState(stage3DProxy : Stage3DProxy, pass : MaterialPassBase, renderable : IRenderable) : void
 63		{
 64			var i : uint;
 65			var len : uint = _vertexAnimation._numPoses;
 66			var index : uint = _vertexAnimation._streamIndex;
 67			var context : Context3D = stage3DProxy._context3D;
 68
 69			// if no poses defined, set temp data
 70			if (!_poses.length) {
 71				if (_vertexAnimation.blendMode == VertexAnimationMode.ABSOLUTE) {
 72					for (i = 1; i < len; ++i) {
 73						stage3DProxy.setSimpleVertexBuffer(index + (j++), renderable.getVertexBuffer(stage3DProxy), Context3DVertexBufferFormat.FLOAT_3);
 74						context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, pass.numUsedVertexConstants, _weights, 1);
 75
 76						if (_vertexAnimation._useNormals)
 77							stage3DProxy.setSimpleVertexBuffer(index + (j++), renderable.getVertexNormalBuffer(stage3DProxy), Context3DVertexBufferFormat.FLOAT_3);
 78					}
 79				}
 80					// todo: set temp data for additive?
 81				return;
 82			}
 83
 84			// this type of animation can only be SubMesh
 85			var subMesh : SubMesh = SubMesh(renderable);
 86			var subGeom : SubGeometry;
 87			var j : uint;
 88
 89			if (_vertexAnimation.blendMode == VertexAnimationMode.ABSOLUTE) {
 90				i = 1;
 91				subGeom = _poses[uint(0)].subGeometries[subMesh._index];
 92				if (subGeom) subMesh.subGeometry = subGeom;
 93			}
 94			else i = 0;
 95			// set the base sub-geometry so the material can simply pick up on this data
 96
 97
 98
 99			for (; i < len; ++i) {
100				subGeom = _poses[i].subGeometries[subMesh._index] || subMesh.subGeometry;
101				stage3DProxy.setSimpleVertexBuffer(index + (j++), subGeom.getVertexBuffer(stage3DProxy), Context3DVertexBufferFormat.FLOAT_3);
102				context.setProgramConstantsFromVector(Context3DProgramType.VERTEX, pass.numUsedVertexConstants, _weights, 1);
103
104				if (_vertexAnimation._useNormals)
105					stage3DProxy.setSimpleVertexBuffer(index + (j++), subGeom.getVertexNormalBuffer(stage3DProxy), Context3DVertexBufferFormat.FLOAT_3);
106
107			}
108		}
109
110		/**
111		 * @inheritDoc
112		 */
113		override public function clone() : AnimationStateBase
114		{
115			var clone : VertexAnimationState = new VertexAnimationState(_vertexAnimation);
116			clone._poses = _poses;
117			clone._weights = _weights;
118			return clone;
119		}
120	}
121}