UNPKG

convex-pixel

Version:

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

128 lines 4.93 kB
import { __extends } from "tslib"; import * as PIXI from "pixi.js"; import { MapTypes } from "../material"; import { BaseContainer } from "./base-container"; var BaseConvexObject = /** @class */ (function (_super) { __extends(BaseConvexObject, _super); function BaseConvexObject(appContext, _config) { var _this = _super.call(this, appContext) || this; _this._config = _config; _this._textures = { diffuse: undefined, depth: undefined }; _this._filters = []; _this._originalWidth = 0; _this._originalHeight = 0; _this.initialize(); return _this; } /** * Setupt the POV-effect weights */ BaseConvexObject.prototype.setPOV = function (x, y) { if (this._displacementFilter) { this._displacementFilter.scale.x = x; this._displacementFilter.scale.y = y; } }; BaseConvexObject.prototype.destroy = function () { if (this._loader) { this._loader.destroy(); } if (this._diffuseMapSprite) { this.removeChild(this._diffuseMapSprite); this._diffuseMapSprite.destroy(); this._diffuseMapSprite = undefined; } if (this._depthMapSprite) { this.removeChild(this._depthMapSprite); this._depthMapSprite.destroy(); this._depthMapSprite = undefined; } if (this._container) { this.removeChild(this._container); this._container.destroy(); this._container = undefined; } this._displacementFilter = undefined; _super.prototype.destroy.call(this); }; BaseConvexObject.prototype.initialize = function () { var _this = this; this._container = new PIXI.Container(); this.addChild(this._container); if (this._config.diffuseMapTexture) { this._textures.diffuse = this._config.diffuseMapTexture; if (this._config.depthMapTexture) { this._textures.depth = this._config.depthMapTexture; } this.create(); return; } this._loader = new PIXI.Loader(); if (this._config.diffuseMap) { if (!this._loader.resources[MapTypes.DIFFUSE]) { this._loader.add(MapTypes.DIFFUSE, this._config.diffuseMap); } } if (this._config.depthMap) { if (!this._loader.resources[MapTypes.DEPTH]) { this._loader.add(MapTypes.DEPTH, this._config.depthMap); } } this._loader.load(function (loader, resources) { if (resources.diffuse) { _this._textures.diffuse = resources.diffuse.texture; } if (resources.depth) { _this._textures.depth = resources.depth.texture; } _this.create(); }); }; BaseConvexObject.prototype.create = function () { if (!this._container) { throw new Error("Property \"container\" is not defined."); } if (this._textures.diffuse) { this._diffuseMapSprite = new PIXI.Sprite(this._textures.diffuse); this._container.addChild(this._diffuseMapSprite); if (this._textures.depth) { this._depthMapSprite = new PIXI.Sprite(this._textures.depth); this._depthMapSprite.width = this._diffuseMapSprite.width; this._depthMapSprite.height = this._diffuseMapSprite.height; this._container.addChild(this._depthMapSprite); this._displacementFilter = new PIXI.filters.DisplacementFilter(this._depthMapSprite, 0); this._displacementFilter.autoFit = true; this._filters.push(this._displacementFilter); } this._originalWidth = this._diffuseMapSprite.width; this._originalHeight = this._diffuseMapSprite.height; this.loadingComplete(); } }; /** * Calling when loading is fineshed */ BaseConvexObject.prototype.loadingComplete = function () { if (this._diffuseMapSprite) { this._diffuseMapSprite.filters = this._filters; } this.emit("loaded"); }; Object.defineProperty(BaseConvexObject.prototype, "container", { get: function () { return this._container; }, enumerable: true, configurable: true }); Object.defineProperty(BaseConvexObject.prototype, "config", { get: function () { return this._config; }, enumerable: true, configurable: true }); return BaseConvexObject; }(BaseContainer)); export { BaseConvexObject }; //# sourceMappingURL=base-sprite.js.map