import * as PIXI from "pixi.js";

export default class RTTShader {
  readonly texture: PIXI.RenderTexture;
  readonly container: PIXI.Container;
  readonly renderer: PIXI.Renderer;

  constructor(renderer: PIXI.Renderer, filter: PIXI.Filter, scale: number = 2) {
    this.renderer = renderer;
    this.texture = PIXI.RenderTexture.create({
      width: this.renderer.width / scale,
      height: this.renderer.height / scale,
    });
    this.container = new PIXI.Container();
    this.container.filterArea = this.texture.frame;
    this.container.filters = [filter];
  }

  rescale(scale: number) {
    this.texture.resize(
      this.renderer.width / scale,
      this.renderer.height / scale
    );
    this.container.filterArea = this.texture.frame;
  }

  render() {
    this.renderer.render(this.container, this.texture);
  }
}
