@vtx/cs-map
Version:
React components for Vortex
136 lines (132 loc) • 8.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _cesium = require("cesium");
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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
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 _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); }
/**
* @class
* @description 3dtiles 模型按照(经纬度二维)范围裁剪
*/
var Clipping = /*#__PURE__*/function () {
function Clipping(tileset, opt) {
_classCallCheck(this, Clipping);
if (!tileset) {
throw new Error("参数错误!");
}
console.log("polygons:", opt.polygons);
if (opt.polygons && opt.polygons instanceof Array && opt.polygons.length > 0) {
var _iterator = _createForOfIteratorHelper(opt.polygons),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var polygon = _step.value;
if (!(polygon instanceof Array && polygon.length >= 3)) {
throw new Error("参数错误!");
}
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
} else {
throw new Error("参数错误!");
}
this.tileset = tileset;
if (typeof opt.excavate === "boolean") {
this.excavate = opt.excavate;
} else {
this.excavate = false;
}
this.polygons = opt.polygons;
this.center = tileset.boundingSphere.center.clone();
this.matrix = _cesium.Transforms.eastNorthUpToFixedFrame(this.center.clone());
this.localMatrix = _cesium.Matrix4.inverse(this.matrix, new _cesium.Matrix4());
this.localPositionsArr = [];
for (var pp = 0; pp < this.polygons.length; pp++) {
var positions = this.polygons[pp];
var localCoor = this.cartesiansToLocal(positions);
this.localPositionsArr.push(localCoor);
}
this.initialize();
}
_createClass(Clipping, [{
key: "initialize",
value: function initialize() {
var out_v_str = "";
var in_v_str = "";
var temp_str = "";
for (var m = 0; m < this.localPositionsArr.length; m++) {
var polygon = this.localPositionsArr[m];
out_v_str += "vec2 points_".concat(m, "[").concat(polygon.length, "];\n bool isPointInPolygon_").concat(m, "(vec2 point){\n int nCross = 0; // \u4EA4\u70B9\u6570\n const int n = ").concat(polygon.length, ";\n for(int i = 0; i < n; i++){\n vec2 p1 = points_").concat(m, "[i];\n vec2 p2 = points_").concat(m, "[int(mod(float(i+1),float(n)))];\n if(p1[1] == p2[1]){\n continue;\n }\n if(point[1] < min(p1[1], p2[1])){\n continue;\n }\n if(point[1] >= max(p1[1], p2[1])){\n continue;\n }\n float x = p1[0] + ((point[1] - p1[1]) * (p2[0] - p1[0])) / (p2[1] - p1[1]);\n if(x > point[0]){\n nCross++;\n }\n }\n return int(mod(float(nCross), float(2))) == 1;\n }\n\n ");
for (var n = 0; n < polygon.length; n++) {
in_v_str += "points_".concat(m, "[").concat(n, "] = vec2(").concat(polygon[n][0], ", ").concat(polygon[n][1], ");\n");
}
temp_str += "isPointInPolygon_".concat(m, "(position2D)||");
}
temp_str = temp_str.slice(0, -2);
if (this.excavate == false) {
in_v_str += "\n if(".concat(temp_str, "){\n }else{\n vsOutput.positionMC *= 0.0;\n }\n ");
} else if (this.excavate == true) {
in_v_str += "\n if(".concat(temp_str, "){\n vsOutput.positionMC *= 0.0;\n }\n ");
}
this.updateShader(out_v_str, in_v_str);
}
/**
* 销毁
*/
}, {
key: "destroy",
value: function destroy() {
this.tileset.customShader = undefined;
}
}, {
key: "updateShader",
value: function updateShader(vtx1, vtx2) {
var v_text = "\n // \u6240\u6709isPointInPolygon\u51FD\u6570\n ".concat(vtx1, "\n void vertexMain(VertexInput vsInput, inout czm_modelVertexOutput vsOutput){\n vec3 modelMC = vsInput.attributes.positionMC;\n vec4 model_local_position = vec4(modelMC.x, modelMC.y, modelMC.z, 1.0);\n vec4 tileset_local_position = u_tileset_worldToLocalMatrix * czm_model * model_local_position;\n vec2 position2D = vec2(tileset_local_position.x,tileset_local_position.y);\n // \u591A\u4E2A\u591A\u8FB9\u5F62\u533A\u57DF\n ").concat(vtx2, "\n }");
var flatCustomShader = new _cesium.CustomShader({
uniforms: {
u_tileset_localToWorldMatrix: {
type: _cesium.UniformType.MAT4,
value: this.matrix
},
u_tileset_worldToLocalMatrix: {
type: _cesium.UniformType.MAT4,
value: this.localMatrix
},
u_flatHeight: {
type: _cesium.UniformType.FLOAT,
value: this.flatHeight
}
},
vertexShaderText: v_text
});
this.tileset.customShader = flatCustomShader;
}
// 世界坐标转数组局部坐标
}, {
key: "cartesiansToLocal",
value: function cartesiansToLocal(positions) {
var arr = [];
for (var i = 0; i < positions.length; i++) {
var position = positions[i];
var localp = _cesium.Matrix4.multiplyByPoint(this.localMatrix, position.clone(), new _cesium.Cartesian3());
arr.push([localp.x, localp.y]);
}
return arr;
}
}]);
return Clipping;
}();
var _default = exports["default"] = Clipping;
//# sourceMappingURL=TilesetCut.js.map