Papervision Actionscript 2 BitmapFileMaterial

I have kept it no secret that I would really like to become much more familiar with Papervision. This means playing around with it. While I would ultimately like to be able to use AS3, I decided that I would like to learn a little bit about PV3d in a more familiar language.

Upon decided to try a project in AS2, I instantly discovered that there are many short-comings when comparing the capabilities the AS2 version to the AS3 version. One thing that I felt I needed to do was load external images as materials for objects. I searched and I searched and didn’t really find much to support my need. I did manage to find a class called LoadArrayMaterials written by JLM at Justinfront DOT net. This was very helpful, but I thought that I might like to load one image at a time.

I decided to write the class myself using the guidance of JLM’s classfile. The result, a quick class that does just what I want !

/***************************************************************************
  1. * BitmapFileMaterial
  2. *
  3. * uses the functionality from the built in BitmapMaterial
  4. * in PV3D to allow the user to assign an external image
  5. * to a material in as2
  6. *
  7. * @author: Marc Pelland
  8. * @date: April 13, 2008
  9. *
  10. * ***************************************************************************
  11. * Example:
  12. *     var myMaterial:BitmapFileMaterial = new BitmapFileMaterial();
  13. *     myMaterial.loadImage(__applicationData.pages.page[i].image.text);
  14. *     myMaterial.onLoadInit = function( material ) {
  15. *         material.oneSide = false;    // double sided
  16. *         material.smooth = true;        // smoothing
  17. *     }
  18. ***************************************************************************
  19. * credit to JLM at Justinfront DOT net for LoadArrayMaterials class
  20. ***************************************************************************/
  21.  
  22. import org.papervision3d.materials.BitmapMaterial;
  23. import com.dynamicflash.utils.Delegate;
  24. import flash.geom.Rectangle;
  25. import flash.display.BitmapData;
  26.  
  27. /***************************************************************************
  28. * The BitmapFileMaterial class creates a texture from an external file
  29. *
  30. */
  31. class com.marcpelland.as2.papervision.materials.BitmapFileMaterial  {
  32.  
  33. // store the material info
  34. private var __bitmapMaterial:BitmapMaterial;
  35. public function get bitmapMaterial():BitmapMaterial {
  36. return __bitmapMaterial;
  37. }
  38.  
  39. private var __materialHolder_mc:MovieClip;    // holder for the material
  40. private var __loadListener_obj:Object;        // load listener
  41. private var __loader_mcl:MovieClipLoader;    // movie clip loader
  42. private var __path:String;                    // file path
  43.  
  44. // set a separate load init function
  45. private var __onLoadInit:Function;
  46. public function set onLoadInit(ARG_func:Function):Void {
  47. __onLoadInit = ARG_func;
  48. }
  49.  
  50. /***************************************************************************
  51. * CONSTRUCTOR
  52. ****************************************************************************/
  53.  
  54. function BitmapFileMaterial() {
  55. init();
  56. }
  57.  
  58. /***************************************************************************
  59. * PUBLIC FUNCTIONS
  60. ****************************************************************************/
  61.  
  62. /***************************************************************************
  63. * load image function
  64. *
  65. * @param ARG_filePath - path to the image file
  66. *
  67. * creates a clip holder, moves it off screen and loads
  68. * the image into the holder
  69. */
  70. public function loadImage(ARG_filePath:String):Void {
  71.  
  72. // updates bitmap material ( designed to not overwrite material already assigned )
  73. __materialHolder_mc = _root.createEmptyMovieClip( 'materialHolder__' + _root.getNextHighestDepth(), _root.getNextHighestDepth());
  74.  
  75. // place off screen
  76. __materialHolder_mc._x = -5000;
  77.  
  78. // load the image into the holder
  79. __loader_mcl.loadClip( ARG_filePath, __materialHolder_mc );
  80.  
  81. }
  82.  
  83. /***************************************************************************
  84. * PRIVATE FUNCTIONS
  85. ****************************************************************************/
  86.  
  87. /***************************************************************************
  88. * init function
  89. *
  90. * initialize the loader
  91. */
  92. private function init():Void {
  93.  
  94. // set up the loader
  95. __loader_mcl = new MovieClipLoader();
  96.  
  97. // set up the listener
  98. __loadListener_obj = new Object();
  99.  
  100. // add the load int functionality
  101. __loadListener_obj.onLoadInit = Delegate.create(this, loadInit);
  102. __loader_mcl.addListener(__loadListener_obj);
  103.  
  104. }
  105.  
  106. /***************************************************************************
  107. * loadInit function
  108. *
  109. * the image has been loaded and all is ready to move on
  110. */
  111. private function loadInit():Void {
  112.  
  113. // get the material into a BitmapMaterial
  114. __bitmapMaterial =  writeMaterial();
  115.  
  116. // run the custom load init command
  117. __onLoadInit( __bitmapMaterial );
  118.  
  119. }
  120.  
  121. /***************************************************************************
  122. * write material function
  123. *
  124. * create a bitmap and copy the data from the movieclip
  125. * to the bitmap, return the bitmap
  126. */
  127. private function writeMaterial():BitmapMaterial {
  128.  
  129. // draw movie on bitmap
  130. var bitmap = new BitmapData(__materialHolder_mc._width, __materialHolder_mc._height, false, 0×000000);
  131. bitmap.draw(__materialHolder_mc);
  132.  
  133. var rectangle:Rectangle = new Rectangle(0 , 0, __materialHolder_mc._width, __materialHolder_mc._height );
  134. bitmap.copyPixels( bitmap, rectangle, rectangle );
  135.  
  136. // assign material
  137. var material_bm:BitmapMaterial = new BitmapMaterial(bitmap);
  138.  
  139. // remove the temp clip
  140. __materialHolder_mc.removeMovieClip();
  141.  
  142. return material_bm;
  143. }
  144.  
  145. }

Leave a Reply




Captcha
Enter the letters you see above.