@vtx/cs-map
Version:
React components for Vortex
205 lines (196 loc) • 10.6 kB
JavaScript
"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(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 _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 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);
}
}
_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);
}
}]);
return ConeGlowLayer;
}();
//# sourceMappingURL=ConeGlowLayer.js.map