MovieClipTexture3D

From Flare3D Wiki
Jump to: navigation, search

This example shows how to create a dynamic animated texture from a display object.

MovieClip texture class

package flare.core
{
	import flare.core.*;
	import flare.system.*;
	import flash.display.*;
	import flash.events.*;
 
	public class MCTexture3D extends Texture3D
	{
		private var _init:Boolean;
		private var _displayObject:DisplayObject;
		private var _transparent:Boolean;
		private var _forceClear:Boolean;
 
		public function MCTexture3D( displayObject:DisplayObject, width:int, height:int, transparent:Boolean = false, forceClear:Boolean = false ) 
		{
			super( new BitmapData( width, height, transparent, 0x0 ), true );
 
			super.mipMode = Texture3D.MIP_NONE;
			super.loaded = true;
 
			_transparent = transparent;
			_forceClear = forceClear;
			_displayObject = displayObject;
 
			start();
		}
 
		public function start():void
		{
			_displayObject.addEventListener( "enterFrame", updateEvent );
		}
 
		public function stop():void
		{
			_displayObject.removeEventListener( "enterFrame", updateEvent );
		}
 
		private function updateEvent(e:Event):void 
		{
                        if ( !scene ) return;
 
			if ( _forceClear ) bitmapData.fillRect( bitmapData.rect, 0x0 );
 
			bitmapData.draw( _displayObject, _displayObject.transform.matrix, _displayObject.transform.colorTransform );
 
			uploadTexture();
		}
 
		public function get displayObject():DisplayObject
		{
			return _displayObject;
		}
	} 
}

Use Example

package  
{
	import flare.basic.*;
	import flare.core.*;
	import flare.materials.*;
	import flare.materials.filters.*;
	import flare.primitives.*;
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;
 
	/**
	 * @author Ariel Nehmad
	 */
	public class MovieClipTexture extends Sprite 
	{
		private var scene:Viewer3D;
		private var loader:Loader;
 
		public function MovieClipTexture() 
		{
			scene = new Viewer3D(this);
			scene.autoResize = true;
			scene.antialias = 2;
			scene.camera = new Camera3D();
			scene.camera.setPosition( 0, 0, -1000 );
 
			loader = new Loader();
			loader.load( new URLRequest( "loading.swf" ) );
			var mcTexture:MCTexture3D = new MCTexture3D( loader, 850, 400, false, true );
 
			var material:Shader3D = new Shader3D( "mc", [new TextureMapFilter(mcTexture)] );
			material.twoSided = true;
 
			var plane:Plane = new Plane( "plane", 800, 450, 1, material );
			scene.addChild( plane );
 
			scene.addEventListener( Scene3D.UPDATE_EVENT, updateEvent );
		}
 
		private function updateEvent(e:Event):void 
		{
			loader.alpha = Math.cos( getTimer() / 200 ) * 0.5 + 0.5;
			loader.scaleX = 1 + Math.sin( getTimer() / 200 ) * 0.2;
 
			if ( loader.content )
				Object(loader.content).base.bar.gotoAndStop( int(Math.random() * 100) );
		}
	}
}
blog comments powered by Disqus