'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var Matrix = require('../../maths/Matrix.js');
var Point = require('../../maths/Point.js');
var Filter = require('../../rendering/filters/shared/Filter.js');
var GlProgram = require('../../rendering/renderers/gl/shader/GlProgram.js');
var GpuProgram = require('../../rendering/renderers/gpu/shader/GpuProgram.js');
var UniformGroup = require('../../rendering/renderers/shared/shader/UniformGroup.js');
var Sprite = require('../../rendering/sprite/shared/Sprite.js');
var deprecation = require('../../utils/logging/deprecation.js');
var displacement$1 = require('./displacement.js');
var displacement = require('./displacement2.js');
var displacement$2 = require('./displacement3.js');

class DisplacementFilter extends Filter.Filter {
  constructor(...args) {
    let options = args[0];
    if (options instanceof Sprite.Sprite) {
      if (args[1]) {
        deprecation.deprecation(deprecation.v8_0_0, "DisplacementFilter now uses options object instead of params. {sprite, scale}");
      }
      options = { sprite: options, scale: args[1] };
    }
    let scale = options.scale ?? 20;
    if (typeof scale === "number") {
      scale = new Point.Point(scale, scale);
    }
    const filterUniforms = new UniformGroup.UniformGroup({
      filterMatrix: { value: new Matrix.Matrix(), type: "mat3x3<f32>" },
      scale: { value: scale, type: "vec2<f32>" },
      rotation: { value: new Float32Array([0, 0, 0, 0]), type: "vec4<f32>" }
    });
    const glProgram = GlProgram.GlProgram.from({
      vertex: displacement["default"],
      fragment: displacement$1["default"],
      name: "displacement-filter"
    });
    const gpuProgram = GpuProgram.GpuProgram.from({
      vertex: {
        source: displacement$2["default"],
        entryPoint: "mainVertex"
      },
      fragment: {
        source: displacement$2["default"],
        entryPoint: "mainFragment"
      }
    });
    const textureSource = options.sprite.texture.source;
    super({
      gpuProgram,
      glProgram,
      resources: {
        filterUniforms,
        mapTexture: textureSource,
        mapSampler: textureSource.style
      }
    });
    this._sprite = options.sprite;
    this._sprite.renderable = false;
  }
  apply(filterManager, input, output, clearMode) {
    const uniforms = this.resources.filterUniforms.uniforms;
    filterManager.calculateSpriteMatrix(
      uniforms.filterMatrix,
      this._sprite
    );
    const wt = this._sprite.worldTransform;
    const lenX = Math.sqrt(wt.a * wt.a + wt.b * wt.b);
    const lenY = Math.sqrt(wt.c * wt.c + wt.d * wt.d);
    if (lenX !== 0 && lenY !== 0) {
      uniforms.rotation[0] = wt.a / lenX;
      uniforms.rotation[1] = wt.b / lenX;
      uniforms.rotation[2] = wt.c / lenY;
      uniforms.rotation[3] = wt.d / lenY;
    }
    this.resources.mapTexture = this._sprite.texture.source;
    filterManager.applyFilter(this, input, output, clearMode);
  }
  get scale() {
    return this.resources.filterUniforms.uniforms.scale;
  }
}

exports.DisplacementFilter = DisplacementFilter;
//# sourceMappingURL=DisplacementFilter.js.map
