UNPKG

@awayjs/scene

Version:
229 lines (228 loc) 8.7 kB
import { __extends } from "tslib"; import { MathConsts } from '@awayjs/core'; import { ControllerBase } from '../controllers/ControllerBase'; /** * Extended camera used to hover round a specified target object. * * @see away3d.containers.View3D */ var FirstPersonController = /** @class */ (function (_super) { __extends(FirstPersonController, _super); /** * Creates a new <code>HoverController</code> object. */ function FirstPersonController(targetObject, panAngle, tiltAngle, minTiltAngle, maxTiltAngle, steps, wrapPanAngle) { if (targetObject === void 0) { targetObject = null; } if (panAngle === void 0) { panAngle = 0; } if (tiltAngle === void 0) { tiltAngle = 90; } if (minTiltAngle === void 0) { minTiltAngle = -90; } if (maxTiltAngle === void 0) { maxTiltAngle = 90; } if (steps === void 0) { steps = 8; } if (wrapPanAngle === void 0) { wrapPanAngle = false; } var _this = _super.call(this, targetObject) || this; _this._iCurrentPanAngle = 0; _this._iCurrentTiltAngle = 90; _this._panAngle = 0; _this._tiltAngle = 90; _this._minTiltAngle = -90; _this._maxTiltAngle = 90; _this._steps = 8; _this._walkIncrement = 0; _this._strafeIncrement = 0; _this._wrapPanAngle = false; _this.fly = false; _this.panAngle = panAngle; _this.tiltAngle = tiltAngle; _this.minTiltAngle = minTiltAngle; _this.maxTiltAngle = maxTiltAngle; _this.steps = steps; _this.wrapPanAngle = wrapPanAngle; //values passed in contrustor are applied immediately _this._iCurrentPanAngle = _this._panAngle; _this._iCurrentTiltAngle = _this._tiltAngle; return _this; } Object.defineProperty(FirstPersonController.prototype, "steps", { /** * Fractional step taken each time the <code>hover()</code> method is called. Defaults to 8. * * Affects the speed at which the <code>tiltAngle</code> and <code>panAngle</code> resolve to their targets. * * @see #tiltAngle * @see #panAngle */ get: function () { return this._steps; }, set: function (val) { val = (val < 1) ? 1 : val; if (this._steps == val) return; this._steps = val; this.pNotifyUpdate(); }, enumerable: false, configurable: true }); Object.defineProperty(FirstPersonController.prototype, "panAngle", { /** * Rotation of the camera in degrees around the y axis. Defaults to 0. */ get: function () { return this._panAngle; }, set: function (val) { if (this._panAngle == val) return; this._panAngle = val; this.pNotifyUpdate(); }, enumerable: false, configurable: true }); Object.defineProperty(FirstPersonController.prototype, "tiltAngle", { /** * Elevation angle of the camera in degrees. Defaults to 90. */ get: function () { return this._tiltAngle; }, set: function (val) { val = Math.max(this._minTiltAngle, Math.min(this._maxTiltAngle, val)); if (this._tiltAngle == val) return; this._tiltAngle = val; this.pNotifyUpdate(); }, enumerable: false, configurable: true }); Object.defineProperty(FirstPersonController.prototype, "minTiltAngle", { /** * Minimum bounds for the <code>tiltAngle</code>. Defaults to -90. * * @see #tiltAngle */ get: function () { return this._minTiltAngle; }, set: function (val) { if (this._minTiltAngle == val) return; this._minTiltAngle = val; this.tiltAngle = Math.max(this._minTiltAngle, Math.min(this._maxTiltAngle, this._tiltAngle)); }, enumerable: false, configurable: true }); Object.defineProperty(FirstPersonController.prototype, "maxTiltAngle", { /** * Maximum bounds for the <code>tiltAngle</code>. Defaults to 90. * * @see #tiltAngle */ get: function () { return this._maxTiltAngle; }, set: function (val) { if (this._maxTiltAngle == val) return; this._maxTiltAngle = val; this.tiltAngle = Math.max(this._minTiltAngle, Math.min(this._maxTiltAngle, this._tiltAngle)); }, enumerable: false, configurable: true }); Object.defineProperty(FirstPersonController.prototype, "wrapPanAngle", { /** * Defines whether the value of the pan angle wraps when over 360 degrees or under 0 degrees. Defaults to false. */ get: function () { return this._wrapPanAngle; }, set: function (val) { if (this._wrapPanAngle == val) return; this._wrapPanAngle = val; this.pNotifyUpdate(); }, enumerable: false, configurable: true }); /** * Updates the current tilt angle and pan angle values. * * Values are calculated using the defined <code>tiltAngle</code>, * <code>panAngle</code> and <code>steps</code> variables. * * @param interpolate If the update to a target pan- or tiltAngle is interpolated. Default is true. * * @see #tiltAngle * @see #panAngle * @see #steps */ FirstPersonController.prototype.update = function (interpolate) { if (interpolate === void 0) { interpolate = true; } if (this._tiltAngle != this._iCurrentTiltAngle || this._panAngle != this._iCurrentPanAngle) { this._pControllerDirty = true; if (this._wrapPanAngle) { if (this._panAngle < 0) { this._iCurrentPanAngle += this._panAngle % 360 + 360 - this._panAngle; this._panAngle = this._panAngle % 360 + 360; } else { this._iCurrentPanAngle += this._panAngle % 360 - this._panAngle; this._panAngle = this._panAngle % 360; } while (this._panAngle - this._iCurrentPanAngle < -180) this._iCurrentPanAngle -= 360; while (this._panAngle - this._iCurrentPanAngle > 180) this._iCurrentPanAngle += 360; } if (interpolate) { this._iCurrentTiltAngle += (this._tiltAngle - this._iCurrentTiltAngle) / (this.steps + 1); this._iCurrentPanAngle += (this._panAngle - this._iCurrentPanAngle) / (this.steps + 1); } else { this._iCurrentTiltAngle = this._tiltAngle; this._iCurrentPanAngle = this._panAngle; } //snap coords if angle differences are close if ((Math.abs(this.tiltAngle - this._iCurrentTiltAngle) < 0.01) && (Math.abs(this._panAngle - this._iCurrentPanAngle) < 0.01)) { this._iCurrentTiltAngle = this._tiltAngle; this._iCurrentPanAngle = this._panAngle; } } this.targetObject.rotationX = this._iCurrentTiltAngle; this.targetObject.rotationY = this._iCurrentPanAngle; if (this._walkIncrement) { if (this.fly) { this.targetObject.transform.moveForward(this._walkIncrement); } else { this.targetObject.x += this._walkIncrement * Math.sin(this._panAngle * MathConsts.DEGREES_TO_RADIANS); this.targetObject.z += this._walkIncrement * Math.cos(this._panAngle * MathConsts.DEGREES_TO_RADIANS); } this._walkIncrement = 0; } if (this._strafeIncrement) { this.targetObject.transform.moveRight(this._strafeIncrement); this._strafeIncrement = 0; } }; FirstPersonController.prototype.incrementWalk = function (val) { if (val == 0) return; this._walkIncrement += val; this.pNotifyUpdate(); }; FirstPersonController.prototype.incrementStrafe = function (val) { if (val == 0) return; this._strafeIncrement += val; this.pNotifyUpdate(); }; return FirstPersonController; }(ControllerBase)); export { FirstPersonController };