Archive for June, 2011

Away3D and Stage3D (Molehill)

I posted an article earlier this week about Away3D and the VideoMaterial and since I also made tests on Stage3D (Molehill) while doing that research, I thought I would post them here. My point with all this is to show that working with the new version of Away3D is going to be a little different. Here is the code to do mostly what the other post was doing, mostly mapping a texture to a sphere and having the camera in the middle (not a video here, just a bitmap).

public function Main(){
  stage.scaleMode = StageScaleMode.NO_SCALE;
  stage.align = StageAlign.TOP_LEFT;
 
  view = new View3D();
  addChild(view);
 
  view.camera.z=0;
  view.camera.y=0;
  view.camera.x=0;
  view.camera.lookAt(new Vector3D(0,0,1));
 
  light = new PointLight();
  light.radius = 200;
  light.color = 0xffffff;
  light.castsShadows = false;
  light.position=new Vector3D(0,0,0);
  view.scene.addChild(light); 			
 
  spherecontainer = new ObjectContainer3D();
  view.scene.addChild(spherecontainer);
 
  var material : BitmapMaterial = new BitmapMaterial(new Albedo().bitmapData);
  material.bothSides = true;
 
  sphere = new Sphere(material);
  sphere.radius = 500;
  spherecontainer.addChild(sphere);
 
  addEventListener(Event.ENTER_FRAME, _handleEnterFrame);
  stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}

To get starting for Molehill I followed the simple instructions and used the FDT template found on the powerflasher’s site. As you can see if you compare the code, there are some differences. You don’t need to create a scene and a camera, they are already there for you. You need a light and you add that light as any 3D objects (no more using addLight). Lastly, which was my biggest problem, your texture need to be a square otherwise it won’t show. That was really bad for me cause I wasn’t seeing anything and the textures I was using were working fine in Away3D 3.6. But in the end I made it work. Anyway, Adobe needs to release that shiz fast cause it’s awesome!

, , ,

No Comments


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;  
}

, , ,

4 Comments