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 !

/***************************************************************************
* BitmapFileMaterial
*
* uses the functionality from the built in BitmapMaterial
* in PV3D to allow the user to assign an external image
* to a material in as2
*
* @author: Marc Pelland
* @date: April 13, 2008
*
* ***************************************************************************
* Example:
*     var myMaterial:BitmapFileMaterial = new BitmapFileMaterial();
*     myMaterial.loadImage(__applicationData.pages.page[i].image.text);
*     myMaterial.onLoadInit = function( material ) {
*         material.oneSide = false;    // double sided
*         material.smooth = true;        // smoothing
*     }
***************************************************************************
* credit to JLM at Justinfront DOT net for LoadArrayMaterials class
***************************************************************************/

import org.papervision3d.materials.BitmapMaterial;
import com.dynamicflash.utils.Delegate;
import flash.geom.Rectangle;
import flash.display.BitmapData;

/***************************************************************************
* The BitmapFileMaterial class creates a texture from an external file
*
*/
class com.marcpelland.as2.papervision.materials.BitmapFileMaterial  {

// store the material info
private var __bitmapMaterial:BitmapMaterial;
public function get bitmapMaterial():BitmapMaterial {
return __bitmapMaterial;
}

private var __materialHolder_mc:MovieClip;    // holder for the material
private var __loadListener_obj:Object;        // load listener
private var __loader_mcl:MovieClipLoader;    // movie clip loader
private var __path:String;                    // file path

// set a separate load init function
private var __onLoadInit:Function;
public function set onLoadInit(ARG_func:Function):Void {
__onLoadInit = ARG_func;
}

/***************************************************************************
* CONSTRUCTOR
****************************************************************************/

function BitmapFileMaterial() {
init();
}

/***************************************************************************
* PUBLIC FUNCTIONS
****************************************************************************/

/***************************************************************************
* load image function
*
* @param ARG_filePath - path to the image file
*
* creates a clip holder, moves it off screen and loads
* the image into the holder
*/
public function loadImage(ARG_filePath:String):Void {

// updates bitmap material ( designed to not overwrite material already assigned )
__materialHolder_mc = _root.createEmptyMovieClip( 'materialHolder__' + _root.getNextHighestDepth(), _root.getNextHighestDepth());

// place off screen
__materialHolder_mc._x = -5000;

// load the image into the holder
__loader_mcl.loadClip( ARG_filePath, __materialHolder_mc );

}

/***************************************************************************
* PRIVATE FUNCTIONS
****************************************************************************/

/***************************************************************************
* init function
*
* initialize the loader
*/
private function init():Void {

// set up the loader
__loader_mcl = new MovieClipLoader();

// set up the listener
__loadListener_obj = new Object();

// add the load int functionality
__loadListener_obj.onLoadInit = Delegate.create(this, loadInit);
__loader_mcl.addListener(__loadListener_obj);

}

/***************************************************************************
* loadInit function
*
* the image has been loaded and all is ready to move on
*/
private function loadInit():Void {

// get the material into a BitmapMaterial
__bitmapMaterial =  writeMaterial();

// run the custom load init command
__onLoadInit( __bitmapMaterial );

}

/***************************************************************************
* write material function
*
* create a bitmap and copy the data from the movieclip
* to the bitmap, return the bitmap
*/
private function writeMaterial():BitmapMaterial {

// draw movie on bitmap
var bitmap = new BitmapData(__materialHolder_mc._width, __materialHolder_mc._height, false, 0x000000);
bitmap.draw(__materialHolder_mc);

var rectangle:Rectangle = new Rectangle(0 , 0, __materialHolder_mc._width, __materialHolder_mc._height );
bitmap.copyPixels( bitmap, rectangle, rectangle );

// assign material
var material_bm:BitmapMaterial = new BitmapMaterial(bitmap);

// remove the temp clip
__materialHolder_mc.removeMovieClip();

return material_bm;
}

}

Leave a Reply

Your email address will not be published. Required fields are marked *