UNPKG

@vtx/cs-map

Version:

React components for Vortex

201 lines (195 loc) 12.4 kB
"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(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } 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(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } 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 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); } } _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; } } }]); return FlightLineLayer; }(); var _default = exports["default"] = FlightLineLayer; //# sourceMappingURL=FlightLineLayer.js.map