Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Usage:
- _lens = new PerspectiveOffCenterLens();
- _lens.left = 0.3; // x-axis projection center (0.3 * width of viewPort)
- _lens.top = 0.5; // y-axis projection center (0.5 * height of viewPort)
- _view = new View3D(null, new Camera3D(_lens))
- if you want to set absolute values just calculate them like this
- _lens.left = absoluteX / stage.stageWidth;
- _lens.right = absoluteY / stage.stageHeight;
- DON'T FORGET TO SET FAR AND NEAR PROPERTIES CORRECTLY, for example
- _lens.near = 0.1;
- _lens.far = 5000;
- */
- package mod.away3d.cameras.lenses
- {
- import away3d.cameras.lenses.LensBase;
- public class PerspectiveOffCenterLens extends LensBase
- {
- private var _fieldOfView : Number;
- private var _focalLengthInv : Number;
- private var _yMax : Number;
- private var _xMax : Number;
- private var _left:Number;
- private var _top:Number;
- public function PerspectiveOffCenterLens(fieldOfView : Number = 60, left:Number = 0.5, top:Number = 0.5)
- {
- this.fieldOfView = fieldOfView;
- this.left = left;
- this.top = top;
- }
- public function get fieldOfView() : Number { return _fieldOfView; }
- public function get left():Number { return _left };
- public function get top():Number { return _top };
- public function set fieldOfView(value : Number) : void
- {
- if (value == _fieldOfView) return;
- _fieldOfView = value;
- _focalLengthInv = Math.tan(_fieldOfView*Math.PI/360);
- invalidateMatrix();
- }
- public function set left(value:Number):void
- {
- if (_left == value) return;
- _left = value;
- invalidateMatrix();
- }
- public function set top(value:Number):void
- {
- if (_top == value) return;
- _top = value;
- invalidateMatrix();
- }
- override protected function updateMatrix() : void
- {
- var yScale:Number = 1.0/Math.tan(_fieldOfView*Math.PI/360);
- var xScale:Number = yScale / _aspectRatio;
- var xScaleTotal:Number = 2 * _near / xScale;
- var left:Number = - _left * xScaleTotal;
- var right:Number = (1 - _left) * xScaleTotal;
- var yScaleTotal:Number = 2 * _near / yScale;
- var bottom:Number = (_top - 1) * yScaleTotal;
- var top:Number = _top * yScaleTotal;
- _matrix.copyRawDataFrom(Vector.<Number>([
- 2.0*_near/(right-left), 0.0, 0.0, 0.0,
- 0.0, -2.0*_near/(bottom-top), 0.0, 0.0,
- -1.0-2.0*left/(right-left), 1.0+2.0*top/(bottom-top), -_far/(_near-_far), 1.0,
- 0.0, 0.0, (_near*_far)/(_near-_far), 0.0
- ]));
- _yMax = _near * _focalLengthInv;
- _xMax = _yMax * _aspectRatio;
- var yMaxFar:Number = _far * _focalLengthInv;
- var xMaxFar:Number = yMaxFar * _aspectRatio;
- _frustumCorners[0] = _frustumCorners[9] = -_xMax * _left;
- _frustumCorners[3] = _frustumCorners[6] = _xMax * (1 - _left);
- _frustumCorners[1] = _frustumCorners[4] = -_yMax * _top;
- _frustumCorners[7] = _frustumCorners[10] = _yMax * (1 - _top);
- _frustumCorners[12] = _frustumCorners[21] = -xMaxFar * _left;
- _frustumCorners[15] = _frustumCorners[18] = xMaxFar * (1 - _left);
- _frustumCorners[13] = _frustumCorners[16] = -yMaxFar * _top;
- _frustumCorners[19] = _frustumCorners[22] = yMaxFar * (1 - _top);
- _frustumCorners[2] = _frustumCorners[5] = _frustumCorners[8] = _frustumCorners[11] = _near;
- _frustumCorners[14] = _frustumCorners[17] = _frustumCorners[20] = _frustumCorners[23] = _far;
- _matrixInvalid = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement