Advertisement
modjke

Away3D 4.0 Lens with off-center perspective projection

Oct 3rd, 2012
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  Usage:
  3.  _lens = new PerspectiveOffCenterLens();
  4.  _lens.left = 0.3;  // x-axis projection center (0.3 * width of viewPort)
  5.  _lens.top = 0.5; // y-axis projection center (0.5 * height of viewPort)
  6.  _view = new View3D(null, new Camera3D(_lens))
  7.  
  8.  if you want to set absolute values just calculate them like this
  9.  _lens.left = absoluteX / stage.stageWidth;
  10.  _lens.right = absoluteY / stage.stageHeight;
  11.  
  12.  DON'T FORGET TO SET FAR AND NEAR PROPERTIES CORRECTLY, for example
  13.  _lens.near = 0.1;
  14.  _lens.far = 5000; 
  15.  */
  16. package mod.away3d.cameras.lenses
  17. {
  18. import away3d.cameras.lenses.LensBase;
  19.  
  20. public class PerspectiveOffCenterLens extends LensBase
  21. {
  22.     private var _fieldOfView : Number;
  23.     private var _focalLengthInv : Number;
  24.     private var _yMax : Number;
  25.     private var _xMax : Number;
  26.     private var _left:Number;
  27.     private var _top:Number;
  28.  
  29.  
  30.     public function PerspectiveOffCenterLens(fieldOfView : Number = 60, left:Number = 0.5, top:Number = 0.5)
  31.     {
  32.         this.fieldOfView = fieldOfView;
  33.         this.left = left;
  34.         this.top = top;
  35.     }
  36.  
  37.  
  38.     public function get fieldOfView() : Number { return _fieldOfView; }
  39.     public function get left():Number { return _left };
  40.     public function get top():Number { return _top };
  41.  
  42.     public function set fieldOfView(value : Number) : void
  43.     {
  44.         if (value == _fieldOfView) return;
  45.  
  46.         _fieldOfView = value;
  47.         _focalLengthInv = Math.tan(_fieldOfView*Math.PI/360);
  48.  
  49.         invalidateMatrix();
  50.     }
  51.  
  52.     public function set left(value:Number):void
  53.     {
  54.         if (_left == value) return;
  55.  
  56.         _left = value;
  57.  
  58.         invalidateMatrix();
  59.     }
  60.  
  61.     public function set top(value:Number):void
  62.     {
  63.         if (_top == value) return;
  64.  
  65.         _top = value;
  66.  
  67.         invalidateMatrix();
  68.     }
  69.  
  70.     override protected function updateMatrix() : void
  71.     {
  72.         var yScale:Number = 1.0/Math.tan(_fieldOfView*Math.PI/360);
  73.         var xScale:Number = yScale / _aspectRatio;
  74.  
  75.         var xScaleTotal:Number = 2 * _near / xScale;
  76.         var left:Number = - _left * xScaleTotal;
  77.         var right:Number = (1 - _left) * xScaleTotal;
  78.  
  79.         var yScaleTotal:Number = 2 * _near / yScale;
  80.         var bottom:Number = (_top - 1) * yScaleTotal;
  81.         var top:Number = _top * yScaleTotal;
  82.  
  83.         _matrix.copyRawDataFrom(Vector.<Number>([
  84.             2.0*_near/(right-left), 0.0, 0.0, 0.0,
  85.             0.0, -2.0*_near/(bottom-top), 0.0, 0.0,
  86.             -1.0-2.0*left/(right-left), 1.0+2.0*top/(bottom-top), -_far/(_near-_far), 1.0,
  87.             0.0, 0.0, (_near*_far)/(_near-_far), 0.0
  88.         ]));
  89.  
  90.         _yMax = _near * _focalLengthInv;
  91.         _xMax = _yMax * _aspectRatio;
  92.         var yMaxFar:Number = _far * _focalLengthInv;
  93.         var xMaxFar:Number = yMaxFar * _aspectRatio;
  94.  
  95.         _frustumCorners[0] = _frustumCorners[9] = -_xMax * _left;
  96.         _frustumCorners[3] = _frustumCorners[6] = _xMax * (1 - _left);
  97.         _frustumCorners[1] = _frustumCorners[4] = -_yMax * _top;
  98.         _frustumCorners[7] = _frustumCorners[10] = _yMax * (1 - _top);
  99.  
  100.         _frustumCorners[12] = _frustumCorners[21] = -xMaxFar * _left;
  101.         _frustumCorners[15] = _frustumCorners[18] = xMaxFar * (1 - _left);
  102.         _frustumCorners[13] = _frustumCorners[16] = -yMaxFar * _top;
  103.         _frustumCorners[19] = _frustumCorners[22] = yMaxFar * (1 - _top);
  104.  
  105.         _frustumCorners[2] = _frustumCorners[5] = _frustumCorners[8] = _frustumCorners[11] = _near;
  106.         _frustumCorners[14] = _frustumCorners[17] = _frustumCorners[20] = _frustumCorners[23] = _far;
  107.  
  108.         _matrixInvalid = false;
  109.     }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement