/Actionscripts/org/papervision3d/materials/MovieAssetMaterial.as

https://github.com/KBgood/AR-Proj
ActionScript | 125 lines | 68 code | 20 blank | 37 comment | 11 complexity | f859af1e775d14237e9bc52dd3734f7d MD5 | raw file
  1. package org.papervision3d.materials
  2. {
  3. import flash.display.MovieClip;
  4. import flash.display.Sprite;
  5. import flash.utils.getDefinitionByName;
  6. import org.papervision3d.core.log.PaperLogger;
  7. import org.papervision3d.core.render.draw.ITriangleDrawer;
  8. /**
  9. * The MovieAssetMaterial class creates a texture from a MovieClip library symbol.
  10. * <p/>
  11. * The texture can be animated and/or transparent.
  12. * <p/>
  13. * The MovieClip's content needs to be top left aligned with the registration point.
  14. * <p/>
  15. * Materials collects data about how objects appear when rendered.
  16. */
  17. public class MovieAssetMaterial extends MovieMaterial implements ITriangleDrawer
  18. {
  19. private static var _library :Object = new Object();
  20. private static var _count :Object = new Object();
  21. /**
  22. * By default, a MovieAssetMaterial is stored and resused, but there are times where a user may want a unique copy. set to true if you want a unique instance
  23. * created
  24. */
  25. public var createUnique:Boolean = false;
  26. /**
  27. * A texture object.
  28. */
  29. override public function get texture():Object
  30. {
  31. return this._texture;
  32. }
  33. /**
  34. * @private
  35. */
  36. override public function set texture( asset:Object ):void
  37. {
  38. if( asset is String == false )
  39. {
  40. PaperLogger.error("Error: MovieAssetMaterial.texture requires a String to be passed to create the MovieClip reference from the library");
  41. return;
  42. }
  43. movie = Sprite(createMovie( String( asset ) ));
  44. bitmap = createBitmapFromSprite( movie );
  45. _texture = asset;
  46. }
  47. // ______________________________________________________________________ NEW
  48. /**
  49. * The MovieAssetMaterial class creates a texture from a MovieClip library id.
  50. *
  51. * @param linkageID The linkage name of the MovieClip symbol in the library.
  52. * @param transparent [optional] - If it's not transparent, the empty areas of the MovieClip will be of fill32 color. Default value is false.
  53. */
  54. public function MovieAssetMaterial( linkageID:String="", transparent:Boolean=false, animated:Boolean=false, createUnique:Boolean=false, precise:Boolean = false)
  55. {
  56. movieTransparent = transparent;
  57. this.animated = animated;
  58. this.createUnique = createUnique;
  59. this.precise = precise;
  60. if( linkageID.length > 0 ) texture = linkageID;
  61. }
  62. // ______________________________________________________________________ CREATE BITMAP
  63. /*
  64. * since we need to pass a movieclip reference to MovieMaterial, I changed this method
  65. * from createBitmap, to createMovie. the super's constructor will take care of
  66. * creating the actual bitmap reference
  67. *
  68. */
  69. protected function createMovie( asset:* ):MovieClip
  70. {
  71. // Remove previous bitmap
  72. if( this._texture != asset )
  73. {
  74. _count[this._texture]--;
  75. var prevMovie:MovieClip = _library[this._texture];
  76. if( prevMovie && _count[this._texture] == 0 )
  77. {
  78. _library[this._texture] = null;
  79. }
  80. }
  81. // Retrieve from library or...
  82. var movie:MovieClip = _library[asset];
  83. var MovieAsset:Class;
  84. // ...attachMovie
  85. if( ! movie )
  86. {
  87. MovieAsset = getDefinitionByName( asset ) as Class;
  88. movie = new MovieAsset();
  89. _library[asset] = movie;
  90. _count[asset] = 0;
  91. }
  92. else if( createUnique )
  93. {
  94. MovieAsset = getDefinitionByName( asset ) as Class;
  95. movie = new MovieAsset();
  96. }
  97. else
  98. {
  99. _count[asset]++;
  100. }
  101. // Create Bitmap
  102. return movie;
  103. }
  104. }
  105. }