"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;
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
var _Cesium = Cesium,
  Cesium3DTileset = _Cesium.Cesium3DTileset,
  Matrix4 = _Cesium.Matrix4,
  Cesium3DTileStyle = _Cesium.Cesium3DTileStyle;
;
var TilesBuilding = /*#__PURE__*/function () {
  function TilesBuilding(options) {
    _classCallCheck(this, TilesBuilding);
    var url = options.url || "http://183.129.170.220:10301/geoserver/www/laiyang/jianzhu/tileset.json";
    this.viewer = options.viewer;
    this.tiles = this.load(url);
    this.show = options.show || true;
    if (options.visibleHeight) {
      this.setVisibleByHeight(options.visibleHeight);
    }
    if (options.style) {
      this.render(options.style);
    }
  }
  _createClass(TilesBuilding, [{
    key: "show",
    set: function set(val) {
      this.tiles.show = val;
    }
  }, {
    key: "load",
    value: function load(url) {
      var viewer = this.viewer;
      return viewer.scene.primitives.add(new Cesium3DTileset({
        url: url,
        modelMatrix: Matrix4.fromArray([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]),
        maximumMemoryUsage: 100,
        //不可设置太高，目标机子空闲内存值以内，防止浏览器过于卡
        maximumScreenSpaceError: 50,
        //用于驱动细节细化级别的最大屏幕空间错误;较高的值可提供更好的性能，但视觉质量较低。
        preferLeaves: true,
        shadows: false,
        //是否显示阴影
        skipLevelOfDetail: true,
        baseScreenSpaceError: 1024,
        skipScreenSpaceErrorFactor: 16,
        skipLevels: 1,
        immediatelyLoadDesiredLevelOfDetail: false,
        loadSiblings: false,
        cullWithChildrenBounds: true,
        dynamicScreenSpaceError: true,
        dynamicScreenSpaceErrorDensity: 0.00278,
        dynamicScreenSpaceErrorFactor: 4.0,
        dynamicScreenSpaceErrorHeightFalloff: 0.25
      }));
    }
  }, {
    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(type, color) {
      this.tiles.style = null;
      if (type === 'blue') {
        this.renderBlueBuild();
      } else if (type === 'type') {
        // this.clearBlueBuildStyle();
        this.renderType();
      } else if (type === 'height') {
        this.clearBlueBuildStyle();
        this.renderHeight();
      } else {
        this.clearBlueBuildStyle();
        this.renderCustom(color);
      }
    }
  }, {
    key: "clearBlueBuildStyle",
    value: function clearBlueBuildStyle() {
      this.tiles.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 / 120.0), 3);\n                  }");
              });
              model._shouldRegenerateShaders = true;
            }
          };
          for (var i = 0; i < featuresLength; i += 2) {
            _loop();
          }
        });
      });
    }
  }, {
    key: "renderBlueBuild",
    value: function renderBlueBuild() {
      this.tiles.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 / 140.0), 3);\n                  }");
              });
              model._shouldRegenerateShaders = true;
            }
          };
          for (var i = 0; i < featuresLength; i += 2) {
            _loop2();
          }
        });
      });
    }
  }, {
    key: "renderType",
    value: function renderType() {
      this.tiles.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 / 150.0), 3);\n                  }");
              });
              model._shouldRegenerateShaders = true;
            }
          };
          for (var i = 0; i < featuresLength; i += 2) {
            _loop3();
          }
        });
      });
    }
  }, {
    key: "renderHeight",
    value: function renderHeight() {
      this.tiles.style = new Cesium3DTileStyle({
        color: {
          conditions: [['${height} < 20', 'rgb(255, 237, 160)'], ['${height} >= 25 && ${height} < 30', 'rgb(254, 217, 118)'], ['${height} >= 30 && ${height} < 35', 'rgb(254, 178, 76)'], ['${height} >= 35 && ${height} < 40', 'rgb(253, 141, 60)'], ['${height} >= 40', 'rgb(252, 78, 42)']]
        }
      });
    }
  }, {
    key: "renderCustom",
    value: function renderCustom(color) {
      if (color) {
        this.tiles.style = new Cesium3DTileStyle({
          color: {
            conditions: [['${height} > 0', "color(\"".concat(color, "\")")]]
          }
        });
      }
    }
  }]);
  return TilesBuilding;
}();
exports["default"] = TilesBuilding;
//# sourceMappingURL=TilesBuilding.js.map
