移动实体
const handlerGE = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
let isMove = fasle;
handlerGE.setInputAction(event => {
if (isMove) return;
const entity = viewer.scene.drillPick(event.position, 1)[0];
if (!entity) return;
isMove = true;
let diff = [];
let type = null;
let positions = null;
let newPosition = null;
const position = changeToThree(event.position);
if (entity.id.polygon) {
if (!entity.id.polygon.hierarchy?._value) return;
type = "polygon";
positions = entity.id.polygon.hierarchy._value.positions;
} else if (entity.id.polyline) {
if (!entity.id.polyline.positions?._value) return;
type = "polyline"
positions = entity.id.polyline.positions._value;
};
positions.forEach(item => {
diff.push({
x: item.x - position.x,
y: item.y - position.y,
z: item.z - position.z,
});
});
handlerGE.setInputAction(event => {
const movePosition = changeToThree(event.endPosition);
newPosition = diff.map(item => ({
x: item.x + movePosition.x,
y: item.y + movePosition.y,
z: item.z + movePosition.z,
}));
if (type === "polygon") {
entity.id.polygon.hierarchy = new Cesium.CallbackProperty(function() {
return new Cesium.PolygonHierarchy(newPosition);
}, false);
entity.id.polygon.positions = new Cesium.CallbackProperty(function() {
return newPosition.concat([newPosition[0]]);
}, false);
} else if (type === "polyline") {
entity.id.polygon.positions = new Cesium.CallbackProperty(function() {
return newPosition;
}, false);
};
}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
handlerGE.setInputAction(() => {
if (type === "polygon") {
entity.id.polygon.hierarchy = newPosition;
entity.id.polyline.positions = newPosition.concat([newPosition[0]]);
} else if (type === "polyline") {
entity.id.polyline.positions = newPosition;
};
diff = [];
type = null;
positions = null;
isMove = false;
newPosition = null;
handlerGE.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
handlerGE.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
}, Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
function changeToThree(position) {
if (!position) return [];
return viewer.scene.globe.pick(viewer.camera.getPickRay(position), viewer.scene);
};