Hello everyone,
I begin with Away 3D 4.1 alpha for a flex project but I’m already stucked .
I need to draw 3d shapes over a simple bitmap (wich is a HD render of a building but it doesn’t really matter). This bitmap needs to be flat and fit to the camera size regardless the camera angle and position.
I first thought that I could render my view over a bitmap (a mx:Image component) but it seems that the view must have a visible background so I followed that tutorial : http://jansensan.net/how-to-fully-fit-away3d-plane-in-viewport which I try to adapt to Away 3D 4.1 but I failed, I can see the plane with its texture but the rotation isn’t good.
I created a simple class to include my background :
package
{
import flash.display.Bitmap;
import away3d.containers.View3D;
import away3d.entities.Mesh;
import away3d.materials.TextureMaterial;
import away3d.primitives.PlaneGeometry;
import away3d.textures.BitmapTexture;
import net.jansensan.utils.degreesToRadians;
public class FacingPlane
{
private var _view3D:View3D;
private var _plane:Mesh;
private var _material:TextureMaterial;
public function FacingPlane(view:View3D, pWidth:Number, pHeight:Number, bitmap:Bitmap)
{
_view3D = view;
var texture:BitmapTexture = new BitmapTexture(bitmap.bitmapData);
_material = new TextureMaterial(texture);
_material.mipmap = (pWidth == pHeight);
// camera values
// see away3d.camerasCamera3D's default lens' field of view
// in away3d.cameras.lenses.PerspectiveLens
// it turns out that the field of view is vertical
var angleY:Number = 60;
var cameraZ:int = _view3D.camera.z;
// this position is arbitrary as we will see further
// useful to calculate the distance between the camera and the plane
var planeZ:int = 500;
var distFromCamToPlane:Number = Math.abs(cameraZ) + planeZ;
// since the field of view is a vertical angle, calculate the height of the plane first
// use pythagorean theorem to calculate
// tip for trigonometry: soh-cah-toa
// toa: tan(angle) = oppositeSide / adjacentSide
var planeHeight:int = Math.tan(degreesToRadians(angleY * 0.5)) * distFromCamToPlane;
// and since it was for a rectangle triangle, thus half the size, double the length
planeHeight *= 2;
// useful for resizing the image
var aspectRatio:Number = pWidth / pHeight;
// use the aspect ratio to calulcate the width of the plane
var planeWidth:int = planeHeight * aspectRatio;
var geo:PlaneGeometry = new PlaneGeometry(planeWidth, planeHeight);
geo.yUp = false;
// create the plane
_plane = new Mesh(geo, _material);
_plane.z = planeZ;
// add 3D objects to the scene
_view3D.scene.addChild(_plane);
// add the away3d view to the display list
//addChild(_view3D);
}
}
}
If someone could explain me my mistake(s), or explain me a better way to proceed.
Thanks.