"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;
var _cesium = require("cesium");
var _parabola = _interopRequireDefault(require("../_util/parabola"));
var _transformRgb = _interopRequireDefault(require("../_util/transformRgb"));
var _CircleRippleMaterialProperty = _interopRequireDefault(require("../map/material/CircleRippleMaterialProperty"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
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); }
var FlowingLineMaterialGLSL = 'float SPEED_STEP = 0.01; \n' + 'vec4 drawLight(float xPos, vec2 st, float headOffset, float tailOffset, float widthOffset){ \n' + 'float lineLength = smoothstep(xPos + headOffset, xPos, st.x) - smoothstep(xPos, xPos - tailOffset, st.x); \n' + 'float lineWidth = smoothstep(widthOffset, 0.5, st.y) - smoothstep(0.5, 1.0 - widthOffset, st.y); \n' + 'return vec4(lineLength * lineWidth); \n' + '}\n' + 'czm_material czm_getMaterial(czm_materialInput materialInput) \n' + '{ \n' + 'czm_material m = czm_getDefaultMaterial(materialInput);\n' + 'float sinTime = sin(czm_frameNumber * SPEED_STEP * speed); \n' + 'vec4 v4_core;\n' + 'vec4 v4_color;\n' + 'float xPos = 0.0; \n' + 'if (sinTime < 0.0){ \n' + 'xPos = cos(czm_frameNumber * SPEED_STEP * speed)+ 1.0 - tailSize; \n' + '}else{ \n' + 'xPos = -cos(czm_frameNumber * SPEED_STEP * speed)+ 1.0 - tailSize; \n' + '} \n' + 'v4_color = drawLight(xPos, materialInput.st, headSize, tailSize, widthOffset);\n' + 'v4_core = drawLight(xPos, materialInput.st, coreSize, coreSize*2.0, widthOffset*2.0);\n' + 'm.diffuse = color.xyz + v4_core.xyz*v4_core.w*0.8; \n' + 'm.alpha = pow(v4_color.w, 3.0); \n' + 'return m; \n' + '} \n';
var FlightLineLayer = /*#__PURE__*/function () {
  function FlightLineLayer(options) {
    var _this$data;
    _classCallCheck(this, FlightLineLayer);
    var _options$show = options.show,
      show = _options$show === void 0 ? true : _options$show;
    this.map = options.map;
    this.data = options.data;
    this.color = options.color;
    this.endEffect = options.endEffect;
    this.entities = [];
    this.primitives = [];
    this.show = show;
    this.flyWidth = options.flyWidth;
    this.heightProportion = options.heightProportion;
    this.lineWidth = options.lineWidth;
    this.uniforms = options.uniforms || {};
    if ((_this$data = this.data) !== null && _this$data !== void 0 && _this$data.length) {
      this.loadData(this.data);
    }
  }
  return _createClass(FlightLineLayer, [{
    key: "show",
    set: function set(value) {
      var _this$primitives, _this$entities;
      if ((_this$primitives = this.primitives) !== null && _this$primitives !== void 0 && _this$primitives.length) {
        this.primitives.map(function (primitive) {
          primitive.show = value;
        });
      }
      if ((_this$entities = this.entities) !== null && _this$entities !== void 0 && _this$entities.length) {
        this.entities.map(function (item) {
          item.show = value;
        });
      }
    }
  }, {
    key: "loadData",
    value: function loadData(data) {
      var _this = this;
      this.data = data;
      var map = this.map;
      var show = this.show;
      this.destroy();
      var colors = {};
      var line_instances = [];
      this.entities = [];
      this.primitives = [];
      data.map(function (item) {
        var _item$hasOwnProperty, _this$endEffect;
        // 飞行线颜色
        var flyColor = item.color || _this.color || '#05FFD5';

        // 轨迹颜色
        var lineColor = item.lineColor || _this.lineColor || flyColor;

        // 轨迹颜色转换
        var line_color = _cesium.Color.fromCssColorString((0, _transformRgb["default"])(lineColor, item.lineCpacity || _this.lineCpacity || 0.4));

        // 飞行线颜色转换
        var color = _cesium.Color.fromCssColorString((0, _transformRgb["default"])(flyColor, item.opacity || _this.opacity || 1));

        // 缓存颜色
        var color_str = color.toString();
        colors[color_str] = colors[color_str] || {
          color: color,
          point_instances: []
        };
        var distance = _cesium.Cartesian3.distance(_cesium.Cartesian3.fromDegrees.apply(_cesium.Cartesian3, _toConsumableArray(item.start)), _cesium.Cartesian3.fromDegrees.apply(_cesium.Cartesian3, _toConsumableArray(item.end)));
        var heightProportion = item.heightProportion || _this.heightProportion || 0.23; // 自定义最高点和总距离的比值(即图中H比上AB的值)，值越大则抛物线越弯曲
        var height = item !== null && item !== void 0 && (_item$hasOwnProperty = item.hasOwnProperty) !== null && _item$hasOwnProperty !== void 0 && _item$hasOwnProperty.call(item, 'height') ? item.height : distance * heightProportion;
        var positions = (0, _parabola["default"])({
          startPosition: item.start,
          endPosition: item.end,
          height: height
        });
        line_instances.push(new _cesium.GeometryInstance({
          geometry: new _cesium.PolylineGeometry({
            positions: positions,
            width: item.lineWidth || _this.lineWidth || 2.0,
            vertexFormat: _cesium.PolylineColorAppearance.VERTEX_FORMAT
          }),
          attributes: {
            color: new _cesium.ColorGeometryInstanceAttribute.fromColor(line_color)
          }
        }));
        colors[color_str].point_instances.push(new _cesium.GeometryInstance({
          geometry: new _cesium.PolylineGeometry({
            positions: positions,
            width: item.flyWidth || _this.flyWidth || 20.0,
            vertexFormat: _cesium.VertexFormat.ALL
          })
        }));
        if ((_this$endEffect = _this.endEffect) !== null && _this$endEffect !== void 0 && _this$endEffect.show) {
          _this.entities.push(map.entities.add({
            position: _cesium.Cartesian3.fromDegrees.apply(_cesium.Cartesian3, _toConsumableArray(item.end)),
            ellipse: {
              semiMinorAxis: _this.endEffect.radius || 500.0,
              semiMajorAxis: _this.endEffect.radius || 500.0,
              material: new _CircleRippleMaterialProperty["default"]({
                color: _cesium.Color.fromCssColorString((0, _transformRgb["default"])(flyColor || '#f7ff01', 0.7)),
                speed: 12.0,
                count: 3,
                gradient: 0.2
              })
            },
            show: show
          }));
        }
      });
      for (var color_str in colors) {
        var _colors$color_str = colors[color_str],
          color = _colors$color_str.color,
          point_instances = _colors$color_str.point_instances;
        var point = new _cesium.Primitive({
          geometryInstances: point_instances,
          show: show,
          appearance: new _cesium.PolylineMaterialAppearance({
            material: new _cesium.Material({
              fabric: {
                type: 'FlowingLineMaterial',
                uniforms: _objectSpread({
                  color: color,
                  // light color
                  speed: 1.5,
                  // flowing speed, speed > 0.0
                  headSize: 0.05,
                  // 0.0 < headSize < 1.0
                  tailSize: 0.5,
                  // 0.0 < tailSize < 1.0
                  widthOffset: 0.1,
                  // 0.0 < widthOffset < 1.0
                  coreSize: 0.05
                }, this.uniforms),
                source: FlowingLineMaterialGLSL
              }
            })
          })
        });
        map.scene.primitives.add(point);
        this.primitives.push(point);
      }
      var line = new _cesium.Primitive({
        geometryInstances: line_instances,
        show: show,
        appearance: new _cesium.PolylineColorAppearance()
      });
      map.scene.primitives.add(line);
      this.primitives.push(line);
    }
  }, {
    key: "destroy",
    value: function destroy() {
      var _this$primitives2, _this$entities2;
      var map = this.map;
      if ((_this$primitives2 = this.primitives) !== null && _this$primitives2 !== void 0 && _this$primitives2.length) {
        this.primitives.map(function (primitive) {
          map.scene.primitives.remove(primitive);
        });
        this.primitives = null;
      }
      if ((_this$entities2 = this.entities) !== null && _this$entities2 !== void 0 && _this$entities2.length) {
        this.entities.map(function (item) {
          map.entities.remove(item);
        });
        this.entities = null;
      }
    }
  }]);
}();
var _default = exports["default"] = FlightLineLayer;
//# sourceMappingURL=FlightLineLayer.js.map
