/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

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