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([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;


function BitmapFileMaterial() {


* 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 );



* 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);


* 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);

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

return material_bm;


Flash In The Can – Toronto

Flash in the CanFlash in the Can Toronto 2008 is quickly approaching, and I am getting very anxious to go. I used to attend the conference every year but missed it last year since I now live in Calgary. Critical Mass is nice enough to send me to the conference this year in hopes that i pick up a couple tips and tricks to show off when I get back. So I get to go back to Toronto!

If you are considering going to the conference, I highly recommend it if not just for inspiration. Being in the presence of the industry leaders is usually enough to get my mind going a mile a minute. Another incentive is that the great folks at FITC appear to be giving a couple last minute ticket discounts. I came across one such discount on moockblog.

While at the conference I will be attending the Papervision 3d workshop which I hope gives me the final push and confidence to start really getting into pv3d. I hope to take in a lot of the technical presentations that are being offered and I will be posting my notes and work throughout the conference.

Flash in the Can