"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get"));

var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));

var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));

var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));

var _webgl = require("@luma.gl/webgl");

var _layersPass = _interopRequireDefault(require("./layers-pass"));

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

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

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; } }

var ShadowPass = function (_LayersPass) {
  (0, _inherits2.default)(ShadowPass, _LayersPass);

  var _super = _createSuper(ShadowPass);

  function ShadowPass(device, props) {
    var _this;

    (0, _classCallCheck2.default)(this, ShadowPass);
    _this = _super.call(this, device, props);
    _this.shadowMap = void 0;
    _this.depthBuffer = void 0;
    _this.fbo = void 0;
    _this.shadowMap = device.createTexture({
      width: 1,
      height: 1,
      sampler: {
        minFilter: 'linear',
        magFilter: 'linear',
        addressModeU: 'clamp-to-edge',
        addressModeV: 'clamp-to-edge'
      }
    });
    _this.depthBuffer = new _webgl.WEBGLRenderbuffer(device, {
      format: 'depth16unorm',
      width: 1,
      height: 1
    });
    _this.fbo = device.createFramebuffer({
      id: 'shadowmap',
      width: 1,
      height: 1,
      colorAttachments: [_this.shadowMap],
      depthStencilAttachment: _this.depthBuffer
    });
    return _this;
  }

  var _proto = ShadowPass.prototype;

  _proto.render = function render(params) {
    var _this2 = this;

    var target = this.fbo;
    (0, _webgl.withGLParameters)(this.device, {
      depthRange: [0, 1],
      depthTest: true,
      blend: false,
      clearColor: [1, 1, 1, 1]
    }, function () {
      var pixelRatio = _this2.device.canvasContext.cssToDeviceRatio();

      var viewport = params.viewports[0];
      var width = viewport.width * pixelRatio;
      var height = viewport.height * pixelRatio;

      if (width !== target.width || height !== target.height) {
        target.resize({
          width: width,
          height: height
        });
      }

      (0, _get2.default)((0, _getPrototypeOf2.default)(ShadowPass.prototype), "render", _this2).call(_this2, _objectSpread(_objectSpread({}, params), {}, {
        target: target,
        pass: 'shadow'
      }));
    });
  };

  _proto.shouldDrawLayer = function shouldDrawLayer(layer) {
    return layer.props.shadowEnabled !== false;
  };

  _proto.getModuleParameters = function getModuleParameters() {
    return {
      drawToShadowMap: true
    };
  };

  _proto.delete = function _delete() {
    if (this.fbo) {
      this.fbo.destroy();
      this.fbo = null;
    }

    if (this.shadowMap) {
      this.shadowMap.destroy();
      this.shadowMap = null;
    }

    if (this.depthBuffer) {
      this.depthBuffer.destroy();
      this.depthBuffer = null;
    }
  };

  return ShadowPass;
}(_layersPass.default);

exports.default = ShadowPass;
//# sourceMappingURL=shadow-pass.js.map