/src/away3d/materials/VideoMaterial.as

http://github.com/away3d/away3d-core-fp11 · ActionScript · 208 lines · 115 code · 54 blank · 39 comment · 11 complexity · ab078e1fc0156507f486de125f4c1bd3 MD5 · raw file

  1. package away3d.materials
  2. {
  3. import away3d.materials.utils.IVideoPlayer;
  4. import away3d.materials.utils.SimpleVideoPlayer;
  5. import flash.display.BitmapData;
  6. import flash.display.Sprite;
  7. import flash.events.Event;
  8. import flash.geom.Rectangle;
  9. public class VideoMaterial extends BitmapMaterial
  10. {
  11. private var _broadcaster:Sprite;
  12. private var _autoPlay:Boolean;
  13. private var _autoUpdate:Boolean;
  14. private var _materialSize:int;
  15. private var _player:IVideoPlayer;
  16. private var _clippingRect:Rectangle;
  17. public function VideoMaterial(source:String, materialSize:int = 256, loop:Boolean = true, autoPlay:Boolean = false, player:IVideoPlayer = null)
  18. {
  19. // used to capture the onEnterFrame event
  20. _broadcaster = new Sprite();
  21. // validates the size of the video
  22. _materialSize = materialSize;
  23. // this clipping ensures the bimapdata size is valid.
  24. _clippingRect = new Rectangle(0, 0, _materialSize, _materialSize);
  25. // assigns the provided player or creates a simple player if null.
  26. _player = player || new SimpleVideoPlayer();
  27. _player.loop = loop;
  28. _player.source = source;
  29. _player.width = _player.height = _materialSize;
  30. // sets autplay
  31. _autoPlay = autoPlay;
  32. // Sets up the bitmap material
  33. super( new BitmapData(_materialSize, _materialSize, true, 0x00ffffff), true, false, false);
  34. // if autoplay start video
  35. if(autoPlay)
  36. _player.play();
  37. // auto update is true by default
  38. autoUpdate = true;
  39. }
  40. /**
  41. * Draws the video and updates the bitmap material
  42. * If this function is not called the bitmap material will not update!
  43. */
  44. public function update():void
  45. {
  46. if(_player.playing && !_player.paused)
  47. {
  48. bitmapData.lock();
  49. bitmapData.draw( _player.container, null, null, null, _clippingRect);
  50. updateTexture();
  51. bitmapData.unlock();
  52. }
  53. }
  54. override public function dispose(deep:Boolean):void
  55. {
  56. autoUpdate = false;
  57. _player.dispose();
  58. _player = null;
  59. _broadcaster = null;
  60. _clippingRect = null;
  61. super.dispose(deep);
  62. }
  63. private function autoUpdateHandler( e:Event ):void
  64. {
  65. update();
  66. }
  67. //////////////////////////////////////////////////////
  68. // private class methods (static)
  69. //////////////////////////////////////////////////////
  70. /**
  71. * Validates the size of the BitmapMaterial.
  72. * Supported size are 2, 4, 8, 16, 32, 64, 128, 512, 1024, 2048.
  73. * Example: 145 would return 128.
  74. * @param size
  75. * @return int A valid size.
  76. *
  77. */
  78. public static function validateMaterialSize( size:int ):int
  79. {
  80. var sizes:Array = [2, 4, 8, 16, 32, 64, 128, 512, 1024, 2048];
  81. var validSize:int = Math.abs( size );
  82. if( sizes.indexOf( validSize ) == -1 )
  83. {
  84. for(var i:uint = 1; i < sizes.length; ++i)
  85. {
  86. if( sizes[i] > validSize )
  87. {
  88. // finds the closest match
  89. validSize = ( Math.abs( sizes[i] - validSize ) < Math.abs( sizes[i-1] - validSize ) )? sizes[i] : sizes[i - 1];
  90. break;
  91. }
  92. }
  93. validSize = (validSize > 2048)? 2048 : validSize;
  94. trace("Warning: "+ size + " is not a valid material size. Updating to the closest supported resolution: " + validSize);
  95. }
  96. return validSize;
  97. }
  98. //////////////////////////////////////////////////////
  99. // get / set functions
  100. //////////////////////////////////////////////////////
  101. /**
  102. * Indicates whether the video will start playing on initialisation.
  103. * If false, only the first frame is displayed.
  104. */
  105. public function set autoPlay(b:Boolean):void
  106. {
  107. _autoPlay = b;
  108. }
  109. public function get autoPlay():Boolean
  110. {
  111. return _autoPlay;
  112. }
  113. /**
  114. * Size of the bitmap used to render the video. This must be a supported resolution
  115. * [2, 4, 8, 16, 32, 64, 128, 512, 1024, 2048]
  116. * The size will be adjusted if an invalid value is passed
  117. */
  118. public function set materialSize(value:int):void
  119. {
  120. _materialSize = validateMaterialSize( value );
  121. _player.width = _player.height = _materialSize;
  122. _clippingRect = new Rectangle(0, 0, _materialSize, _materialSize);
  123. }
  124. public function get materialSize():int
  125. {
  126. return _materialSize;
  127. }
  128. /**
  129. * Indicates whether the material will redraw onEnterFrame
  130. */
  131. public function set autoUpdate(value:Boolean):void
  132. {
  133. _autoUpdate = value;
  134. if(value)
  135. {
  136. if(!_broadcaster.hasEventListener(Event.ENTER_FRAME))
  137. _broadcaster.addEventListener(Event.ENTER_FRAME, autoUpdateHandler, false, 0, true);
  138. }
  139. else
  140. {
  141. if(_broadcaster.hasEventListener(Event.ENTER_FRAME))
  142. _broadcaster.removeEventListener(Event.ENTER_FRAME, autoUpdateHandler);
  143. }
  144. }
  145. public function get autoUpdate():Boolean
  146. {
  147. return _autoUpdate;
  148. }
  149. public function get player():IVideoPlayer
  150. {
  151. return _player;
  152. }
  153. }
  154. }