(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "three", "./Pass.js", "../shaders/BokehShader.js"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("three"), require("./Pass.js"), require("../shaders/BokehShader.js"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.three, global.Pass, global.BokehShader);
    global.BokehPass = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _three, _Pass2, _BokehShader) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.BokehPass = void 0;

  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

  function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } Object.defineProperty(subClass, "prototype", { value: Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }), writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }

  function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

  function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

  function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

  function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

  function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

  function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

  /**
   * Depth-of-field post-process with bokeh shader
   */
  var BokehPass = /*#__PURE__*/function (_Pass) {
    _inherits(BokehPass, _Pass);

    var _super = _createSuper(BokehPass);

    function BokehPass(scene, camera, params) {
      var _this;

      _classCallCheck(this, BokehPass);

      _this = _super.call(this);
      _this.scene = scene;
      _this.camera = camera;
      var focus = params.focus !== undefined ? params.focus : 1.0;
      var aspect = params.aspect !== undefined ? params.aspect : camera.aspect;
      var aperture = params.aperture !== undefined ? params.aperture : 0.025;
      var maxblur = params.maxblur !== undefined ? params.maxblur : 1.0; // render targets

      var width = params.width || window.innerWidth || 1;
      var height = params.height || window.innerHeight || 1;
      _this.renderTargetDepth = new _three.WebGLRenderTarget(width, height, {
        minFilter: _three.NearestFilter,
        magFilter: _three.NearestFilter
      });
      _this.renderTargetDepth.texture.name = 'BokehPass.depth'; // depth material

      _this.materialDepth = new _three.MeshDepthMaterial();
      _this.materialDepth.depthPacking = _three.RGBADepthPacking;
      _this.materialDepth.blending = _three.NoBlending; // bokeh material

      if (_BokehShader.BokehShader === undefined) {
        console.error('THREE.BokehPass relies on BokehShader');
      }

      var bokehShader = _BokehShader.BokehShader;

      var bokehUniforms = _three.UniformsUtils.clone(bokehShader.uniforms);

      bokehUniforms['tDepth'].value = _this.renderTargetDepth.texture;
      bokehUniforms['focus'].value = focus;
      bokehUniforms['aspect'].value = aspect;
      bokehUniforms['aperture'].value = aperture;
      bokehUniforms['maxblur'].value = maxblur;
      bokehUniforms['nearClip'].value = camera.near;
      bokehUniforms['farClip'].value = camera.far;
      _this.materialBokeh = new _three.ShaderMaterial({
        defines: Object.assign({}, bokehShader.defines),
        uniforms: bokehUniforms,
        vertexShader: bokehShader.vertexShader,
        fragmentShader: bokehShader.fragmentShader
      });
      _this.uniforms = bokehUniforms;
      _this.needsSwap = false;
      _this.fsQuad = new _Pass2.FullScreenQuad(_this.materialBokeh);
      _this._oldClearColor = new _three.Color();
      return _this;
    }

    _createClass(BokehPass, [{
      key: "render",
      value: function render(renderer, writeBuffer, readBuffer
      /*, deltaTime, maskActive*/
      ) {
        // Render depth into texture
        this.scene.overrideMaterial = this.materialDepth;
        renderer.getClearColor(this._oldClearColor);
        var oldClearAlpha = renderer.getClearAlpha();
        var oldAutoClear = renderer.autoClear;
        renderer.autoClear = false;
        renderer.setClearColor(0xffffff);
        renderer.setClearAlpha(1.0);
        renderer.setRenderTarget(this.renderTargetDepth);
        renderer.clear();
        renderer.render(this.scene, this.camera); // Render bokeh composite

        this.uniforms['tColor'].value = readBuffer.texture;
        this.uniforms['nearClip'].value = this.camera.near;
        this.uniforms['farClip'].value = this.camera.far;

        if (this.renderToScreen) {
          renderer.setRenderTarget(null);
          this.fsQuad.render(renderer);
        } else {
          renderer.setRenderTarget(writeBuffer);
          renderer.clear();
          this.fsQuad.render(renderer);
        }

        this.scene.overrideMaterial = null;
        renderer.setClearColor(this._oldClearColor);
        renderer.setClearAlpha(oldClearAlpha);
        renderer.autoClear = oldAutoClear;
      }
    }]);

    return BokehPass;
  }(_Pass2.Pass);

  _exports.BokehPass = BokehPass;
});