convex-pixel
Version:
The library for creating pseudo 3d interactive scenes based on webgl
114 lines • 4.14 kB
JavaScript
import * as PIXI from "pixi.js";
import { MapTypes } from "../material";
import { BaseContainer } from "./base-container";
export class BaseConvexObject extends BaseContainer {
constructor(appContext, _config) {
super(appContext);
this._config = _config;
this._textures = { diffuse: undefined, depth: undefined };
this._filters = [];
this._originalWidth = 0;
this._originalHeight = 0;
this.initialize();
}
/**
* Setupt the POV-effect weights
*/
setPOV(x, y) {
if (this._displacementFilter) {
this._displacementFilter.scale.x = x;
this._displacementFilter.scale.y = y;
}
}
destroy() {
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.destroy();
}
initialize() {
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((loader, resources) => {
if (resources.diffuse) {
this._textures.diffuse = resources.diffuse.texture;
}
if (resources.depth) {
this._textures.depth = resources.depth.texture;
}
this.create();
});
}
create() {
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
*/
loadingComplete() {
if (this._diffuseMapSprite) {
this._diffuseMapSprite.filters = this._filters;
}
this.emit("loaded");
}
get container() {
return this._container;
}
get config() {
return this._config;
}
}
//# sourceMappingURL=base-sprite.js.map