topical media & game development

talk show tell print

flex-video3d-scene.ax

flex-video3d-scene.ax [swf] flex


  /*
   * ----------------------------------
   * Flash Video on 3D
   * Adobe DevNet Tutorial Sample Code
   * ----------------------------------
   * Paul Spitzer
   * www.ActionScriptArchitect.com
   * www.fluid.com
   * ----------------------------------
   */
  package 
  {
          import flash.display.DisplayObject;
          import flash.display.Loader;
          import flash.display.LoaderInfo;
          import flash.display.Sprite;
          import flash.events.Event;
          import flash.net.URLRequest;
          
          import mx.core.Container;
          
          import org.papervision3d.cameras.FreeCamera3D;
          import org.papervision3d.materials.ColorMaterial;
          import org.papervision3d.materials.MaterialsList;
          import org.papervision3d.objects.DisplayObject3D;
          import org.papervision3d.scenes.Scene3D;
          import org.papervision3d.materials.BitmapMaterial;
          import flash.display.BitmapData;
          import flash.events.ProgressEvent;
          
          
The Video3DScene is responsible for setting up and configuring Papervision object in order to render a 3D mesh with video.

  
          public class @ax-flex-video3d-scene extends Sprite
          {
                  
                  private var scene: Scene3D;
                  private var camera: FreeCamera3D;
                  private var video: DisplayObject;
                  private var bitmapData: BitmapData;
                  
                  
Constructor. Configures and kicks off the rendering of a 3D mesh with video.

  
                  public function @ax-flex-video3d-scene()
                  {
                          this.setupScene();
                          this.setupCollada();
                          this.setupVideo();
                          this.addEventListener(Event.ENTER_FRAME, this.handleEnterFrame);
                  }
                  
                  // Initialization
  
                  
Sets up and configures the 3D scene.

  
                  private function setupScene(): void
                  {
                          this.scene = new Scene3D(this);
                          this.camera = new FreeCamera3D();
                          this.camera.z = -600;
                  }
          
                  
Instructs Papervision to load the Collada file which contains the mesh to which video will be mapped.

  
                  private function setupCollada(): void
                  {
                          //var material: ColorMaterial = new ColorMaterial(0xcecece);
                          
                          this.bitmapData = new BitmapData(466, 350, false, 0xcecece);
                          
                          var material: BitmapMaterial = new BitmapMaterial(this.bitmapData);
                          material.doubleSided = true;
                          
                          var materials: MaterialsList = new MaterialsList();
                          materials.addMaterial(material, "Material");
                          
                          var screen: DisplayObject3D = new DisplayObject3D();
                          screen.addCollada("local/assets/screen.dae", materials);
                          
                          this.scene.addChild(screen, "Screen");
                  }
                  
                  
Loads the video SWF.

  
                  private function setupVideo(): void
                  {
                          //var url: String = "Video.swf";
                          var url: String = "local/assets/imm07214.swf";
                          //var url: String = "http://www.youtube.com/v/imxXfC0Isk0";
                          var request: URLRequest = new URLRequest(url);
                          var loader: Loader = new Loader();
                          
                          var loaderInfo: LoaderInfo = loader.contentLoaderInfo;
                          loaderInfo.addEventListener(ProgressEvent.PROGRESS, this.handleLoadProgress);
                          
                          loader.load(request);
                  }
                  
                  // Event Handlers
                  
                  
Handles the ENTER_FRAME event and updates the 3D scene, mapping the rotation of the mesh to the mouse.

  
                  private function handleEnterFrame(event: Event): void
                  {
                          var screen: DisplayObject3D = this.scene.getChildByName("Screen");
                          
                          var rotationY: Number = -(this.mouseX / this.stage.stageWidth * 720);
                          screen.rotationY += (rotationY - screen.rotationY) / 10;
                          
                          if(this.video != null)
                          {
                                  this.bitmapData.draw(this.video);
                          }
                          
                          this.scene.renderCamera(this.camera);
                  }
                  
                  
Handles the load PROGRESS event from the LoaderInfo object associated with loading the video SWF

  
                  private function handleLoadProgress(event: ProgressEvent): void
                  {
                          var percent: Number = event.bytesLoaded / event.bytesTotal * 100;
                          if(percent >= 25)
                          {
                                  var loaderInfo: LoaderInfo = event.target as LoaderInfo;
                                  this.video = loaderInfo.content;
                                  
                                  loaderInfo.removeEventListener(ProgressEvent.PROGRESS, this.handleLoadProgress);
                          }
                  }
          
          }
          
  }
  


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.