UNPKG

@vtx/cs-map

Version:

React components for Vortex

203 lines (195 loc) 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _cesium = require("cesium"); var _BottomCircle = _interopRequireDefault(require("./BottomCircle.js")); var _ButtomRotateCircle = _interopRequireDefault(require("./ButtomRotateCircle.js")); var _transformRgb = _interopRequireDefault(require("../_util/transformRgb")); 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 _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 ConeGlowLayer = exports["default"] = /*#__PURE__*/function () { function ConeGlowLayer(options) { var _options$data; _classCallCheck(this, ConeGlowLayer); var _options$show = options.show, show = _options$show === void 0 ? true : _options$show; this.viewer = options.map; this.map = options.map; this.options = options; // color 颜色 height 光柱高度 bottomRadius 底部圆的半径 this.height = options.height || 300; this.topRadius = options.topRadius || 0; this.bottomRadius = options.bottomRadius || 30; this.color = options.color || '#FFFFFF'; //给个亮亮的颜色 this.primitives = []; this.layer = null; this.show = show; if ((_options$data = options.data) !== null && _options$data !== void 0 && _options$data.length) { this.loadData(options.data); } } return _createClass(ConeGlowLayer, [{ key: "show", set: function set(value) { var _this$primitives; if ((_this$primitives = this.primitives) !== null && _this$primitives !== void 0 && _this$primitives.length) { this.primitives.map(function (primitive) { primitive.show = value; }); } if (this.layer) { this.layer.show = value; } } }, { key: "loadData", value: function loadData(data) { var _this = this; this.data = data; var map = this.map; var show = this.show; var height = this.height; var topRadius = this.topRadius; var bottomRadius = this.bottomRadius; this.destroy(); this.primitives = []; var layer = new _cesium.CustomDataSource({ show: show }); layer.cancelPopup = true; map.dataSources.add(layer); this.layer = layer; var colors = {}; data.map(function (item) { var position = _cesium.Cartesian3.fromDegrees.apply(_cesium.Cartesian3, _toConsumableArray(item.data)); var color_str = item.color || _this.color || '#0080ff'; var rgba = _cesium.Color.fromCssColorString((0, _transformRgb["default"])(color_str, 1)); // 添加底部圆 layer.entities.add({ position: position, ellipse: { semiMinorAxis: (item.bottomRadius || bottomRadius) * 2, semiMajorAxis: (item.bottomRadius || bottomRadius) * 2, height: item.data[2] || 0.0, material: new _BottomCircle["default"](rgba) } }); // 添加底部旋转圆 var stRotation = 360; layer.entities.add({ position: position, ellipse: { semiMinorAxis: (item.bottomRadius || bottomRadius) * 1.45, semiMajorAxis: (item.bottomRadius || bottomRadius) * 1.45, height: item.data[2] || 0.0, material: new _ButtomRotateCircle["default"](rgba), stRotation: new _cesium.CallbackProperty(function () { stRotation--; stRotation < 0 && (stRotation = 360); return _cesium.Math.toRadians(stRotation); }, false) } }); // 缓存颜色 colors[color_str] = colors[color_str] || { item: item, _instances: [], color: color_str }; var modelMatrix = _cesium.Matrix4.multiplyByTranslation(_cesium.Transforms.eastNorthUpToFixedFrame(position), new _cesium.Cartesian3(0.0, 0.0, (item.height || height) * 0.5), new _cesium.Matrix4()); var cylinderGeometry = new _cesium.CylinderGeometry({ length: item.height || height, topRadius: item.topRadius || topRadius || 0.0, bottomRadius: (item.bottomRadius || bottomRadius) * 0.7, vertexFormat: _cesium.MaterialAppearance.MaterialSupport.TEXTURED.vertexFormat }); // 缓存颜色对应的数据 colors[color_str]._instances.push(new _cesium.GeometryInstance({ geometry: cylinderGeometry, modelMatrix: modelMatrix })); }); for (var color_str in colors) { var _colors$color_str = colors[color_str], color = _colors$color_str.color, _instances = _colors$color_str._instances; var cylinder = new _cesium.Primitive({ geometryInstances: _instances, show: show, appearance: new _cesium.MaterialAppearance({ material: new _cesium.Material({ fabric: { type: "VtxfShader1", uniforms: { color: _cesium.Color.fromCssColorString((0, _transformRgb["default"])(color, 1)), alpha: 2 }, source: "\n uniform vec4 color;\n czm_material czm_getMaterial(czm_materialInput materialInput)\n {\n czm_material material = czm_getDefaultMaterial(materialInput);\n vec2 st = materialInput.st;\n float dis = distance(st, vec2(0.5));\n material.diffuse =2.9 * color.rgb;\n material.alpha = color.a * dis * alpha ;\n return material;\n }\n " }, translucent: false }), faceForward: false, // 当绘制的三角面片法向不能朝向视点时,自动翻转法向,从而避免法向计算后发黑等问题 closed: true // 是否为封闭体,实际上执行的是是否进行背面裁剪 }) }); map.scene.primitives.add(cylinder); this.primitives.push(cylinder); } this.viewer.scene.preUpdate.addEventListener(this.preUpdateHandle, this); } }, { key: "preUpdateHandle", value: function preUpdateHandle() { var _this$primitives2, _this2 = this; if ((_this$primitives2 = this.primitives) !== null && _this$primitives2 !== void 0 && _this$primitives2.length) { this.primitives.forEach(function (primitive) { //圆锥闪烁动画 var alpha = primitive.appearance.material.uniforms.alpha; if (alpha >= 2) { _this2.symbol = '-'; } else if (alpha <= 0.5) { _this2.symbol = '+'; } if (_this2.symbol === "-") { primitive.appearance.material.uniforms.alpha -= 0.1; } else { primitive.appearance.material.uniforms.alpha += 0.1; } }); } } // 销毁 }, { key: "destroy", value: function destroy() { var _this$primitives3; var map = this.map; if ((_this$primitives3 = this.primitives) !== null && _this$primitives3 !== void 0 && _this$primitives3.length) { this.primitives.map(function (primitive) { map.scene.primitives.remove(primitive); }); this.primitives = null; } if (this.layer) { map.dataSources.remove(this.layer); this.layer = null; } map.scene.preUpdate.removeEventListener(this.preUpdateHandle, this); } }]); }(); //# sourceMappingURL=ConeGlowLayer.js.map