UNPKG

convex-pixel

Version:

The library for creating pseudo 3d interactive scenes based on webgl

95 lines 4.32 kB
import * as PIXI from "pixi.js"; import { BaseConvexObject } from "../display/base"; import { CameraControllerEventTypes } from "../camera/controller"; var P3DCameraProcessor = /** @class */ (function () { function P3DCameraProcessor(_room) { var _this = this; this._room = _room; this._handlerCameraChangePosition = function (e) { _this.updatePositions(); }; this._handlerCameraChangePOV = function (e) { _this.updatePOV(_this._room); }; this._handlerCameraChangeZoom = function (e) { _this._room.scale.x = _this._room.scale.y = _this._room.camera.maxZoom; // this.updatePositions(this._scene); }; this._handlerCameraChangeRotate = function (e) { // this.updatePositions(this._scene); }; if (!this._room) { throw Error("Property \"room\" is not defined."); } this.addCameraListeners(); } Object.defineProperty(P3DCameraProcessor.prototype, "room", { get: function () { return this._room; }, enumerable: true, configurable: true }); P3DCameraProcessor.prototype.updatePOV = function (displayObject) { var t = PIXI.Ticker.shared.lastTime; // check for debounce if (this._lastTime === t) return; var cam = this._room.camera; var object = displayObject instanceof BaseConvexObject ? displayObject : undefined; if (cam && object && object.setPOV) { var sceneBounds = this._room.roomBound; var abs = object.getGlobalPosition ? object.getGlobalPosition() : undefined; var cx = (sceneBounds.width * 0.5 - (abs ? abs.x : 0)) * this._room.scale.x; var cy = (sceneBounds.height * 0.5 - (abs ? abs.y : 0)) * this._room.scale.y; var sx = -(cam.pov.x - cx) * cam.xOffset; var sy = -(cam.pov.y - cy) * cam.yOffset; var povX = sx * 0.01 * cam.povFactor * object.scale.x; var povY = sy * 0.01 * cam.povFactor * object.scale.y; object.setPOV(povX * object.scale.x, povY * object.scale.y); if (object.config.isBackground) { // static. etc... } else { if (object.container) { object.container.x = object.config.depth * povX; object.container.y = object.config.depth * povY; } } } if (displayObject.children) { for (var _i = 0, _a = displayObject.children; _i < _a.length; _i++) { var child = _a[_i]; this.updatePOV(child); } } }; P3DCameraProcessor.prototype.destroy = function () { this.removeCameraListeners(); }; P3DCameraProcessor.prototype.updatePositions = function () { this._room.x = -this._room.camera.x; this._room.y = -this._room.camera.y; }; P3DCameraProcessor.prototype.addCameraListeners = function () { var cam = this._room.camera; if (!cam) throw new Error("The camera is not defined"); cam.addListener(CameraControllerEventTypes.MOVE, this._handlerCameraChangePosition); cam.addListener(CameraControllerEventTypes.POV, this._handlerCameraChangePOV); cam.addListener(CameraControllerEventTypes.ZOOM, this._handlerCameraChangeZoom); cam.addListener(CameraControllerEventTypes.ROTATE, this._handlerCameraChangeRotate); }; P3DCameraProcessor.prototype.removeCameraListeners = function () { var cam = this._room.camera; if (!cam) throw new Error("The camera is not defined"); cam.removeListener(CameraControllerEventTypes.POV, this._handlerCameraChangePosition); cam.removeListener(CameraControllerEventTypes.MOVE, this._handlerCameraChangePOV); cam.removeListener(CameraControllerEventTypes.ZOOM, this._handlerCameraChangeZoom); cam.removeListener(CameraControllerEventTypes.ROTATE, this._handlerCameraChangeRotate); }; return P3DCameraProcessor; }()); export { P3DCameraProcessor }; //# sourceMappingURL=camera-processor.js.map