/src/away3d/animators/UVAnimator.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 214 lines · 138 code · 37 blank · 39 comment · 18 complexity · 56fdc22f1daeef5bc1005b217e516897 MD5 · raw file

  1. package away3d.animators
  2. {
  3. import away3d.animators.data.*;
  4. import away3d.animators.states.*;
  5. import away3d.animators.transitions.*;
  6. import away3d.arcane;
  7. import away3d.cameras.Camera3D;
  8. import away3d.core.base.*;
  9. import away3d.core.managers.*;
  10. import away3d.core.math.MathConsts;
  11. import away3d.materials.*;
  12. import away3d.materials.passes.*;
  13. import flash.display3D.Context3DProgramType;
  14. import flash.geom.Matrix;
  15. use namespace arcane;
  16. /**
  17. * Provides an interface for assigning uv-based animation data sets to mesh-based entity objects
  18. * and controlling the various available states of animation through an interative playhead that can be
  19. * automatically updated or manually triggered.
  20. */
  21. public class UVAnimator extends AnimatorBase implements IAnimator
  22. {
  23. private var _uvAnimationSet:UVAnimationSet;
  24. private var _deltaFrame:UVAnimationFrame = new UVAnimationFrame();
  25. private var _activeUVState:IUVAnimationState;
  26. private var _uvTransform:Matrix;
  27. private var _matrix2d:Vector.<Number>;
  28. private var _translate:Vector.<Number>;
  29. private var _autoRotation:Boolean;
  30. private var _rotationIncrease:Number = 1;
  31. private var _autoTranslate:Boolean;
  32. private var _translateIncrease:Vector.<Number>;
  33. /**
  34. * Creates a new <code>UVAnimator</code> object.
  35. *
  36. * @param uvAnimationSet The animation data set containing the uv animations used by the animator.
  37. */
  38. public function UVAnimator(uvAnimationSet:UVAnimationSet)
  39. {
  40. super(uvAnimationSet);
  41. _uvTransform = new Matrix();
  42. _matrix2d = Vector.<Number>([1, 0, 0, 0, 1, 0, 0, 0]);
  43. _translate = Vector.<Number>([0, 0, 0.5, 0.5]);
  44. _uvAnimationSet = uvAnimationSet;
  45. }
  46. /**
  47. * Defines if a rotation is performed automatically each update. The rotationIncrease value is added each iteration.
  48. */
  49. public function set autoRotation(b:Boolean):void
  50. {
  51. _autoRotation = b;
  52. }
  53. public function get autoRotation():Boolean
  54. {
  55. return _autoRotation;
  56. }
  57. /**
  58. * if autoRotation = true, the rotation is increased by the rotationIncrease value. Default is 1;
  59. */
  60. public function set rotationIncrease(value:Number):void
  61. {
  62. _rotationIncrease = value;
  63. }
  64. public function get rotationIncrease():Number
  65. {
  66. return _rotationIncrease;
  67. }
  68. /**
  69. * Defines if the animation is translated automatically each update. Ideal to scroll maps. Use setTranslateIncrease to define the offsets.
  70. */
  71. public function set autoTranslate(b:Boolean):void
  72. {
  73. _autoTranslate = b;
  74. if (b && !_translateIncrease)
  75. _translateIncrease = Vector.<Number>([0, 0]);
  76. }
  77. public function get autoTranslate():Boolean
  78. {
  79. return _autoTranslate;
  80. }
  81. /**
  82. * if autoTranslate = true, animation is translated automatically each update with the u and v values.
  83. * Note if value are integers, no visible update will be performed. Values are expected to be in 0-1 range.
  84. */
  85. public function setTranslateIncrease(u:Number, v:Number):void
  86. {
  87. if (!_translateIncrease)
  88. _translateIncrease = Vector.<Number>([0, 0]);
  89. _translateIncrease[0] = u;
  90. _translateIncrease[1] = v;
  91. }
  92. public function get translateIncrease():Vector.<Number>
  93. {
  94. return _translateIncrease;
  95. }
  96. /**
  97. * @inheritDoc
  98. */
  99. public function setRenderState(stage3DProxy:Stage3DProxy, renderable:IRenderable, vertexConstantOffset:int, vertexStreamOffset:int, camera:Camera3D):void
  100. {
  101. var material:TextureMaterial = renderable.material as TextureMaterial;
  102. var subMesh:SubMesh = renderable as SubMesh;
  103. if (!material || !subMesh)
  104. return;
  105. if (autoTranslate) {
  106. _deltaFrame.offsetU += _translateIncrease[0];
  107. _deltaFrame.offsetV += _translateIncrease[1];
  108. }
  109. _translate[0] = _deltaFrame.offsetU;
  110. _translate[1] = _deltaFrame.offsetV;
  111. stage3DProxy._context3D.setProgramConstantsFromVector(Context3DProgramType.VERTEX, vertexConstantOffset, _translate);
  112. _uvTransform.identity();
  113. if (_autoRotation)
  114. _deltaFrame.rotation += _rotationIncrease;
  115. if (_deltaFrame.rotation != 0)
  116. _uvTransform.rotate(_deltaFrame.rotation*MathConsts.DEGREES_TO_RADIANS);
  117. if (_deltaFrame.scaleU != 1 || _deltaFrame.scaleV != 1)
  118. _uvTransform.scale(_deltaFrame.scaleU, _deltaFrame.scaleV);
  119. _matrix2d[0] = _uvTransform.a;
  120. _matrix2d[1] = _uvTransform.b;
  121. _matrix2d[3] = _uvTransform.tx;
  122. _matrix2d[4] = _uvTransform.c;
  123. _matrix2d[5] = _uvTransform.d;
  124. _matrix2d[7] = _uvTransform.ty;
  125. stage3DProxy._context3D.setProgramConstantsFromVector(Context3DProgramType.VERTEX, vertexConstantOffset + 4, _matrix2d);
  126. }
  127. /**
  128. * @inheritDoc
  129. */
  130. public function play(name:String, transition:IAnimationTransition = null, offset:Number = NaN):void
  131. {
  132. transition = transition;
  133. offset = offset;
  134. if (_activeAnimationName == name)
  135. return;
  136. _activeAnimationName = name;
  137. if (!_animationSet.hasAnimation(name))
  138. throw new Error("Animation root node " + name + " not found!");
  139. _activeNode = _animationSet.getAnimation(name);
  140. _activeState = getAnimationState(_activeNode);
  141. _activeUVState = _activeState as IUVAnimationState;
  142. start();
  143. }
  144. /**
  145. * Applies the calculated time delta to the active animation state node.
  146. */
  147. override protected function updateDeltaTime(dt:Number):void
  148. {
  149. _absoluteTime += dt;
  150. _activeUVState.update(_absoluteTime);
  151. var currentUVFrame:UVAnimationFrame = _activeUVState.currentUVFrame;
  152. var nextUVFrame:UVAnimationFrame = _activeUVState.nextUVFrame;
  153. var blendWeight:Number = _activeUVState.blendWeight;
  154. if (currentUVFrame && nextUVFrame) {
  155. _deltaFrame.offsetU = currentUVFrame.offsetU + blendWeight*(nextUVFrame.offsetU - currentUVFrame.offsetU);
  156. _deltaFrame.offsetV = currentUVFrame.offsetV + blendWeight*(nextUVFrame.offsetV - currentUVFrame.offsetV);
  157. _deltaFrame.scaleU = currentUVFrame.scaleU + blendWeight*(nextUVFrame.scaleU - currentUVFrame.scaleU);
  158. _deltaFrame.scaleV = currentUVFrame.scaleV + blendWeight*(nextUVFrame.scaleV - currentUVFrame.scaleV);
  159. _deltaFrame.rotation = currentUVFrame.rotation + blendWeight*(nextUVFrame.rotation - currentUVFrame.rotation);
  160. }
  161. }
  162. /**
  163. * Verifies if the animation will be used on cpu. Needs to be true for all passes for a material to be able to use it on gpu.
  164. * Needs to be called if gpu code is potentially required.
  165. */
  166. public function testGPUCompatibility(pass:MaterialPassBase):void
  167. {
  168. }
  169. /**
  170. * @inheritDoc
  171. */
  172. public function clone():IAnimator
  173. {
  174. return new UVAnimator(_uvAnimationSet);
  175. }
  176. }
  177. }