[Papervision3D] LoadArrayMaterials AS2

jlm jlm at justinfront.net
Mon Apr 2 15:09:14 EDT 2007


Enclosed is a class I am using, since it is not totally specific to the 
project I have opensourced it, hopefully it will prove usefull for people
using the AS2 version of papervision prior to all the classes being
formulated.

suggested changes welcome and let me know if I should change ammend
copyrights etc...

[code]

/*
Copyright (c) 2007 Justin L Mills
JLM at Justinfront DOT net

LoadArrayMaterials class for use with AS2 papervision.
 - creates an array of materials from a list of jpg's or movies to be loaded
in.

// special thanks to Tink for pointing out I needed to use bitmap material
and suggesting I resuse symbols.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*

// example use
// hope to provide better example use when current project finishes.

		materialsLoader  = new LoadArrayMaterials()
		materialsLoader.load( moviesToLoad );
		materialsLoader.onLoadInit = function( material, counter ){
			
			material.oneSide 			= 	false;
			material.smooth 			= 	true;
	
		};
		materialsLoader.onFinished = Delegate.create(this, createPlanes);
*/


// _______________________________________________________________________
// LoadArrayMaterials


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

// class: LoadArrayMaterials
// 	composite class for generating an array of materials from a list of
materials
class net.justinfront.LoadArrayMaterials{
	
	
	
	private var __holder:		 		MovieClip;
	private var __d:					Number;
	private var __arrayBitmapMaterials: 	Array; 
	private var __loadListener:			Object;
	private var __loader:				MovieClipLoader;
	private var __counter:				Number;
	private var __onLoadInit:			Function;
	private var __onLoadFinished:		Function;
	private var __moviePaths:			Array;
	private var __length:				Number;
	private var __lastLoadedMaterial:		BitmapMaterial;
	
	// constructor: LoadArrayMaterials
	public function LoadArrayMaterials()
	{
		
		init();
		
	}
	
	// method: init 
	// 	initialises class
	private function init(): Void
	{

		__arrayBitmapMaterials = [];
		
		__loader = new MovieClipLoader();
		__loadListener = {};
		__loadListener.onLoadInit = Delegate.create( this, storeAndLoad );
		__loader.addListener( __loadListener );
		
	
	}
	
	// method: load
	// 	starts loading the moviePaths.
	public function load( moviePaths: Array ): Void
	{	
		
		// reuse the same depth
		__d = _root.getNextHighestDepth();
		__moviePaths = moviePaths;
		__length = moviePaths.length;
		__counter = -1;
		
		loadNext();
		
	}
	
	
	// method: storeAndLoad
	// 	loads and stores BitmapMaterial
	private function storeAndLoad(): Void
	{
		trace('storeAndLoad');
		__lastLoadedMaterial =  copyToMaterial();
		__arrayBitmapMaterials[ __counter ] = __lastLoadedMaterial;
		__onLoadInit( __lastLoadedMaterial, __counter);
		loadNext();
		
	}
	
	
	// method: loadNext
	// 	loads next image
	private function loadNext( Void ): Void
	{
		
		var isDone: Boolean 	=	Boolean( __counter < __length -1 );
		while( __moviePaths[ ++__counter ] == undefined && __counter < __length
-1)
		{
			trace('image not found');
			// increment counter skip undefined entres assuming already set.
		}
		if( isDone )
		{ 
		
			// updates bitmap material ( designed to not overwrite material already
assigned ) 
			__holder 		=	 _root.createEmptyMovieClip( 'holder__' + __d, __d );
			// place off screen
			__holder._x = -1000;
			// load in next material
			trace('holder '+__moviePaths[__counter]);
			
			__loader.loadClip( __moviePaths[__counter], __holder );
	
			
		} else 
		{
			
			__holder.removeMovieClip();
			__onLoadFinished();	
			
		}

	}
	
	
	// property: materialArray
	// 	returns an array of material
	public function get materialArray():Array{
		
		return __arrayBitmapMaterials
	
	}
	
	
	// property: onLoadInit
	// 	allows you to get last loaded material and its index number.
	public function set onLoadInit( func: Function )
	{
		
		__onLoadInit = func
		
	}
	
	
	// method: copyToMaterial
	// copies __holder to a BitmapMaterial
	private function copyToMaterial( ): BitmapMaterial
	{
		trace(copyToMaterial);
		// short cut _holder for faster access
		var mc: MovieClip 			= __holder;
		
		// draw movie on bitmap
		var bitmap 				= new BitmapData( mc._width, mc._height, false, 0x000000);
		bitmap.draw( mc );
		var rectangle:Rectangle		= new Rectangle(0 , 0, mc._width, mc._height );
		bitmap.copyPixels( bitmap, rectangle, rectangle );
		
		// assign material 
		var material: BitmapMaterial		= 	new BitmapMaterial( bitmap );
		trace('material '+ material);
		return material
		
	}
	
	
	// property: onFinished
	// 	sets event for 
	public function set onFinished( func: Function )
	{
	
		__onLoadFinished = func;
	
	}
	
	
}
[/code]

have fun coding ;J

(ps my webmail is messing me about hopefully I have only posted this once!
if not sorry)



More information about the Papervision3D mailing list