UNPKG

convex-pixel

Version:

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

87 lines 3.77 kB
import * as PIXI from "pixi.js"; import { BaseConvexObject } from "../display/base"; import { CameraControllerEventTypes } from "../camera/controller"; export class P3DCameraProcessor { constructor(_room) { this._room = _room; this._handlerCameraChangePosition = (e) => { this.updatePositions(); }; this._handlerCameraChangePOV = (e) => { this.updatePOV(this._room); }; this._handlerCameraChangeZoom = (e) => { this._room.scale.x = this._room.scale.y = this._room.camera.maxZoom; // this.updatePositions(this._scene); }; this._handlerCameraChangeRotate = (e) => { // this.updatePositions(this._scene); }; if (!this._room) { throw Error(`Property "room" is not defined.`); } this.addCameraListeners(); } get room() { return this._room; } updatePOV(displayObject) { const t = PIXI.Ticker.shared.lastTime; // check for debounce if (this._lastTime === t) return; const cam = this._room.camera; const object = displayObject instanceof BaseConvexObject ? displayObject : undefined; if (cam && object && object.setPOV) { const sceneBounds = this._room.roomBound; const abs = object.getGlobalPosition ? object.getGlobalPosition() : undefined; const cx = (sceneBounds.width * 0.5 - (abs ? abs.x : 0)) * this._room.scale.x; const cy = (sceneBounds.height * 0.5 - (abs ? abs.y : 0)) * this._room.scale.y; const sx = -(cam.pov.x - cx) * cam.xOffset; const sy = -(cam.pov.y - cy) * cam.yOffset; const povX = sx * 0.01 * cam.povFactor * object.scale.x; const 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 (const child of displayObject.children) { this.updatePOV(child); } } } destroy() { this.removeCameraListeners(); } updatePositions() { this._room.x = -this._room.camera.x; this._room.y = -this._room.camera.y; } addCameraListeners() { const 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); } removeCameraListeners() { const 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); } } //# sourceMappingURL=camera-processor.js.map