focal length的計算公式為:
var fl:Numer = (viewport.height / 2) / Math.tan (camera.fov / 2 * (Math.PI / 180));
任何位於 wz = fl 的 plane 後面的物體,看起來都會比實際上小,而位於前面的,就會看起來比較大。
- package
- {
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Sprite;
- import flash.display.StageScaleMode;
- import flash.display.StageAlign;
- import flash.events.Event;
- import sandy.core.scenegraph.Camera3D;
- import sandy.core.scenegraph.Group;
- import sandy.materials.Appearance;
- import sandy.materials.BitmapMaterial;
- import sandy.primitive.Plane3D;
- import sandy.core.Scene3D;
- public class Example003a extends Sprite
- {
- [Embed(source = 'images/Ume.png')]
- private var texture:Class;
- private var scene:Scene3D;
- private var plane:Plane3D;
- public function Example003a()
- {
- var text:Bitmap = new texture;
- var tex:BitmapData = text.bitmapData;
- stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- scene = new Scene3D("myScene", this, new Camera3D(100, 100),
- new Group("myRoot"));
- // 產生一 Plane3D 物件,並設定其參數, 分別為:
- // 識別名稱,寬與高
- plane = new Plane3D("myPlane", tex.height, tex.width);
- // 設定 plane 之外觀
- plane.appearance = new Appearance(new BitmapMaterial(tex));
- scene.root.addChild(plane);
- scene.render();
- stage.addEventListener(Event.RESIZE, onResize);
- onResize(null);
- }
- private function onResize(evt:Event):void
- {
- // 取得舞台的寬及高
- var w:Number = stage.stageWidth;
- var h:Number = stage.stageHeight;
- // 更新攝影機的視點
- scene.camera.viewport.width = w;
- scene.camera.viewport.height = h;
- // 取得 plane 與攝影機的距離
- var d:Number = plane.getPosition("camera").getNorm();
- // 將攝影機的焦點置於 wz = d 並維持1:1大小
- scene.camera.fov = 2 * Math.atan2(h / 2, d) * (180 / Math.PI);
- // 刷新場景
- scene.render();
- }
- }
- }
程式碼出處:http://www.flashsandy.org/tutorials/3.0/sandy_cs3_tut03a