"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(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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, _toPropertyKey(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 _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(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() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
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
