VideoMaterial with Away3D (version 3.6)

I was trying to map a video to a sphere today, so I wanted to use the VideoMaterial in Away3D, but I wasn’t finding any example on it. Also seems like it is not that straight forward so let me put an example here.

Here is the base code for it (I guess SphereView could be compiled):

public function SphereView() {
  scene = new Scene3D();
 
  camera = new Camera3D();
  camera.fov = 35;
  camera.z = 0;
 
  view = new View3D();
  view.scene= scene;
  view.camera = camera;
  addChild(view);
 
  material2 = new VideoMaterial({lockW:2000, lockH:1000});
  material2.loop = true;
  material2.file = "../flv/test2.flv";
  material2.smooth = true;
 
  sphere = new Sphere();
  sphere.material = material2;
  sphere.radius = 2000;
  sphere.segmentsW = 40;
  sphere.segmentsH = 20;
  sphere.invertFaces();
 
  spherecontainer = new ObjectContainer3D(sphere);
  scene.addChild(spherecontainer);
 
  addEventListener(Event.ENTER_FRAME, _onEnterFrame);
  addEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);
}

I had to fiddle a bit with the Away3D VideoMaterial.as to make it accept my very wide video. Find the function playStream and replace it with this one:

private function playStream():void {
  _netStream = new NetStream(nc);
  _netStream.checkPolicyFile = true;
  _netStream.client = CustomClient;
  _netStream.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler,false,0,true);
  _netStream.addEventListener(AsyncErrorEvent.ASYNC_ERROR, ayncErrorHandler,false,0,true);
  _netStream.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler,false,0,true);
  play();
 
  if(video == null){
    // Setup video object
    video = new Video(_lockH, _lockW);
    video.smoothing = true;
    sprite.addChild(video);
  }
 
  video.attachNetStream(_netStream);
}

All I needed to do was add _lockW and _lockH to the Video constructor, but it improved the quality of the played video (it was 320×240 before, the default value of Video). Now with this you should be able to start mapping your own videos inside of a sphere. Oh yeah if you want it, here are the functions that are missing in the first snippets, they basically enable you to move the camera around.

private function _onAddedToStage(event : Event) : void {
  view.x = stage.stageWidth / 2;
  view.y = stage.stageHeight / 2;
 
  stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);			
}
 
private function _onEnterFrame(event:Event):void{					
  if (move) {
    spherecontainer.rotationX = (mouseY - lastMouseY)/10 + lastRotationX;
    if (spherecontainer.rotationX > 90)
      spherecontainer.rotationX = 90;
    if (spherecontainer.rotationX < -90)
      spherecontainer.rotationX = -90;
    sphere.rotationY = (lastMouseX - mouseX)/10 + lastRotationY;
  }			
  view.render();
}
 
private function onMouseDown(event:MouseEvent):void{
  lastRotationX = spherecontainer.rotationX;
  lastRotationY = sphere.rotationY;
  lastMouseX = mouseX;
  lastMouseY = mouseY;
  move = true;
}
 
private function onMouseUp(event:MouseEvent):void{
  move = false;  
}

, , ,

  1. #1 by Andros - June 7th, 2011 at 12:08

    Thanks very much! :)
    I’ve been all afternoon searching this!

  2. #2 by joan - March 14th, 2012 at 06:39

    Thank you for this insight and solution.

    What I don’t understand is how everyone else is doing, and why this is not built-in. Out of the box the VideoMaterial is unusable due to pixelation. Is really everyone else patching the source to get around ?

  3. #3 by Bjorn - April 19th, 2012 at 11:02

    A solution is to use the set video(newvideo:Video) function to override the default Video instance.

(will not be published)
Subscribe to comments feed