"use strict";

function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _cesium = require("cesium");
var _CesiumTilesLayer2 = _interopRequireDefault(require("../cesium-tiles-layer/CesiumTilesLayer"));
var _excluded = ["map", "render", "heightField"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], t.indexOf(o) >= 0 || {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.indexOf(n) >= 0) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
var BuildingTilesLayer = exports["default"] = /*#__PURE__*/function (_CesiumTilesLayer) {
  function BuildingTilesLayer(opt_options) {
    var _this2;
    _classCallCheck(this, BuildingTilesLayer);
    var _ref = opt_options || {},
      map = _ref.map,
      render = _ref.render,
      heightField = _ref.heightField,
      defaultOpt = _objectWithoutProperties(_ref, _excluded);
    _this2 = _callSuper(this, BuildingTilesLayer, [defaultOpt]);
    _this2.map = map;
    _this2.heightField = heightField || 'high';
    if (render) {
      _this2.render(render);
    }
    return _this2;
  }
  _inherits(BuildingTilesLayer, _CesiumTilesLayer);
  return _createClass(BuildingTilesLayer, [{
    key: "setVisibleByHeight",
    value: function setVisibleByHeight(height) {
      var _this = this;
      var viewer = this.viewer;
      viewer.build3DTilesVisible = function () {
        // 当前高度
        var currentHeight = viewer.camera.positionCartographic.height;
        _this.show = currentHeight <= height;
      };
    }
  }, {
    key: "render",
    value: function render(_ref2) {
      var type = _ref2.type,
        color = _ref2.color;
      this.style = null;
      if (type === 'blue') {
        this.renderBlueBuild();
      } else if (type === 'type') {
        this.renderType();
      } else if (type === 'height') {
        this.renderHeight();
      } else {
        this.renderCustom(color);
      }
    }
  }, {
    key: "clearBlueBuildStyle",
    value: function clearBlueBuildStyle() {
      this.readyPromise.then(function (tiles) {
        tiles.tileVisible.addEventListener(function (tile) {
          var content = tile.content;
          var featuresLength = content.featuresLength;
          var _loop = function _loop() {
            var feature = content.getFeature(i);
            var model = feature.content._model;
            if (model && model._sourcePrograms && model._rendererResources) {
              Object.keys(model._sourcePrograms).forEach(function (key) {
                var program = model._sourcePrograms[key];
                var fragmentShader = model._rendererResources.sourceShaders[program.fragmentShader];
                var v_position = '';
                if (fragmentShader.indexOf(' v_positionEC;') !== -1) {
                  v_position = 'v_positionEC';
                } else if (fragmentShader.indexOf(' v_pos;') !== -1) {
                  v_position = 'v_pos';
                }
                var color = "vec4(".concat(feature.color.toString(), ")");
                model._rendererResources.sourceShaders[program.fragmentShader] = "varying vec3 ".concat(v_position, ";\n                                       void main(void) {\n                                        vec4 position = czm_inverseModelView * vec4(").concat(v_position, ", 1);\n                                        gl_FragColor = ").concat(color, ";\n                                        gl_FragColor *= vec4(vec3(position.y / 100.0), 3);\n                                      }");
              });
              model._shouldRegenerateShaders = true;
            }
          };
          for (var i = 0; i < featuresLength; i += 2) {
            _loop();
          }
        });
      });
    }
  }, {
    key: "renderBlueBuild",
    value: function renderBlueBuild() {
      this.readyPromise.then(function (tiles) {
        tiles.tileVisible.addEventListener(function (tile) {
          var content = tile.content;
          var featuresLength = content.featuresLength;
          var _loop2 = function _loop2() {
            var feature = content.getFeature(i);
            var model = feature.content._model;
            if (model && model._sourcePrograms && model._rendererResources) {
              Object.keys(model._sourcePrograms).forEach(function (key) {
                var program = model._sourcePrograms[key];
                var fragmentShader = model._rendererResources.sourceShaders[program.fragmentShader];
                var v_position = '';
                if (fragmentShader.indexOf(' v_positionEC;') !== -1) {
                  v_position = 'v_positionEC';
                } else if (fragmentShader.indexOf(' v_pos;') !== -1) {
                  v_position = 'v_pos';
                }
                var color = "vec4(".concat(feature.color.toString(), ")");
                model._rendererResources.sourceShaders[program.fragmentShader] = "varying vec3 ".concat(v_position, ";\n                                       void main(void) {\n                                        vec4 position = czm_inverseModelView * vec4(").concat(v_position, ", 1);\n                                        float glowRange = 360.0;\n                                        gl_FragColor = ").concat(color, ";\n                                        gl_FragColor = vec4(0.2, 0.8, 1.0, 0.5);\n                                        gl_FragColor *= vec4(vec3(position.y / 100.0), 3);\n                                      }");
              });
              model._shouldRegenerateShaders = true;
            }
          };
          for (var i = 0; i < featuresLength; i += 2) {
            _loop2();
          }
        });
      });
    }
  }, {
    key: "renderType",
    value: function renderType() {
      this.readyPromise.then(function (tiles) {
        tiles.tileVisible.addEventListener(function (tile) {
          var content = tile.content;
          var featuresLength = content.featuresLength;
          var _loop3 = function _loop3() {
            var feature = content.getFeature(i);
            var model = feature.content._model;
            if (model && model._sourcePrograms && model._rendererResources) {
              Object.keys(model._sourcePrograms).forEach(function (key) {
                var program = model._sourcePrograms[key];
                var fragmentShader = model._rendererResources.sourceShaders[program.fragmentShader];
                var v_position = '';
                if (fragmentShader.indexOf(' v_positionEC;') !== -1) {
                  v_position = 'v_positionEC';
                } else if (fragmentShader.indexOf(' v_pos;') !== -1) {
                  v_position = 'v_pos';
                }
                var color = "vec4(".concat(feature.color.toString(), ")");
                model._rendererResources.sourceShaders[program.fragmentShader] = "varying vec3 ".concat(v_position, ";\n                                       void main(void) {\n                                        vec4 position = czm_inverseModelView * vec4(").concat(v_position, ", 1);\n                                        gl_FragColor = ").concat(color, ";\n                                        gl_FragColor = vec4(0.25, 0.69, 0.56, 1);\n                                        gl_FragColor *= vec4(vec3(position.y / 100.0), 3);\n                                      }");
              });
              model._shouldRegenerateShaders = true;
            }
          };
          for (var i = 0; i < featuresLength; i += 2) {
            _loop3();
          }
        });
      });
    }
  }, {
    key: "renderHeight",
    value: function renderHeight() {
      this.style = new _cesium.Cesium3DTileStyle({
        color: {
          conditions: [['${' + this.heightField + '} < 20', 'rgb(255, 237, 160)'], ['${' + this.heightField + '} >= 25 && ${' + this.heightField + '} < 30', 'rgb(254, 217, 118)'], ['${' + this.heightField + '} >= 30 && ${' + this.heightField + '} < 35', 'rgb(254, 178, 76)'], ['${' + this.heightField + '} >= 35 && ${' + this.heightField + '} < 40', 'rgb(253, 141, 60)'], ['${' + this.heightField + '} >= 40', 'rgb(252, 78, 42)']]
        }
      });
    }
  }, {
    key: "renderCustom",
    value: function renderCustom(color) {
      if (color) {
        this.style = new _cesium.Cesium3DTileStyle({
          color: {
            conditions: [['${' + this.heightField + '} > 0', "color(\"".concat(color, "\")")]]
          }
        });
      }
    }
  }]);
}(_CesiumTilesLayer2["default"]);
//# sourceMappingURL=BuildingTilesLayer.js.map
