cesium仿真推演
通过脚本形式实现红蓝对抗,实现效果如下:
1.初始化地球
Cesium.Ion.defaultAccessToken = "你的token";
let viewer = new Cesium.Viewer("cesiumContainer", {
geocoder: false, //是否显示搜索框
homeButton: true, //是否显示Home按钮
navigationHelpButton: false, //是否显示操作指南按钮
animation: false, //是否创建动画小器件,左下角仪表
shouldAnimate: true,
timeline: false, //是否显示时间线控件
fullscreenButton: false, //是否显示右下角全屏按钮
baseLayerPicker: false, //是否显示图层选择器
scene3DOnly: false, //如果设置为true,则所有几何图形以3D渲染以节约GPU资源
sceneModePicker: false, //是否显示3D/2D选择器
infoBox: false, //是否显示实现信息框
selectionIndicator: false, //是否显示选中实体标记
requestRenderMode: true, //决胜请求渲染模式
contextOptions: {
webgl: {
preserveDrawingBuffer: true,
alpha: true,
},
},
imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
url: "https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer?f=jsapi",
}),
});
2.定义时间轴
// 起始时间
let start = Cesium.JulianDate.fromDate(new Date(2017, 7, 11));
// 结束时间
let stop = Cesium.JulianDate.addSeconds(start, 800, new Cesium.JulianDate());
// 设置始时钟始时间
viewer.clock.startTime = start.clone();
// 设置时钟当前时间
viewer.clock.currentTime = start.clone();
// 设置始终停止时间
viewer.clock.stopTime = stop.clone();
// 时间速率,数字越大时间过的越快
viewer.clock.multiplier = 40;
// 时间轴
// viewer.timeline.zoomTo(start,stop);
// 循环执行,即为2,到达终止时间,重新从起点时间开始
viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP;
3.数据脚本
// 红方飞机
let dataRed = [
[
{ longitude: 115.90167226526711, dimension: 28.625120981344978, height: 50000, time: 0 },
{ longitude: 116.06694984208235, dimension: 28.066471672734693, height: 50000, time: 40 },
{ longitude: 116.24925479211112, dimension: 27.475234777949026, height: 70000, time: 80 },
{ longitude: 116.64557588315023, dimension: 26.996137747603726, height: 70000, time: 120 },
{ longitude: 116.97220379017409, dimension: 26.66863686778167, height: 70000, time: 160 },
{ longitude: 117.75322832015706, dimension: 25.922013611547982, height: 50000, time: 200 },
{ longitude: 118.71456116448206, dimension: 24.957818869004782, height: 60000, time: 240 },
{ longitude: 119.46174427938871, dimension: 24.210812479910643, height: 80000, time: 280 },
{ longitude: 120.12089834650197, dimension: 23.152518875104718, height: 70000, time: 320 },
{ longitude: 120.12089834650197, dimension: 23.052518875104718, height: 70000, time: 480 }
],
[
{ longitude: 115.81082463521449, dimension: 22.79624472230782, height: 50000, time: 0 },
{ longitude: 115.98677094072605, dimension: 22.47071272291977, height: 20000, time: 40 },
{ longitude: 116.06867412991954, dimension: 22.01236453350201, height: 30000, time: 80 },
{ longitude: 116.19216458394004, dimension: 21.787668223836405, height: 50000, time: 120 },
{ longitude: 116.64786006235653, dimension: 21.68372653628677, height: 40000, time: 160 },
{ longitude: 117.21532723629497, dimension: 21.55501327744841, height: 10000, time: 200 },
{ longitude: 117.74502819587877, dimension: 21.271103462087552, height: 40000, time: 240 },
{ longitude: 118.18638353470584, dimension: 21.0740436622621, height: 40000, time: 280 },
{ longitude: 119.57256305606677, dimension: 20.738900169864934, height: 20000, time: 320 },
{ longitude: 120.28162496305498, dimension: 20.680390511460004, height: 50000, time: 360 },
{ longitude: 121.0598128017038, dimension: 20.66304175586005, height: 50000, time: 400 },
{ longitude: 121.67016447779628, dimension: 20.621869517064766, height: 20000, time: 440 },
{ longitude: 122.2352045926724, dimension: 20.77588453487941, height: 30000, time: 480 },
{ longitude: 122.355738035547, dimension: 21.255686510074923, height: 10000, time: 520 },
{ longitude: 122.27644297970562, dimension: 21.718371556325124, height: 20000, time: 560 },
{ longitude: 121.45042686046793, dimension: 22.418104243855808, height: 10000, time: 600 }
]
];
// 蓝方飞机
let dataBlue = [
[
{ longitude: 121.58182905000967, dimension: 25.022782945162177, height: 40000, time: 0 },
{ longitude: 121.8182559314053, dimension: 24.875146938021118, height: 40000, time: 40 },
{ longitude: 121.94892630210205, dimension: 24.60152650072645, height: 70000, time: 100 },
{ longitude: 121.84264405431378, dimension: 24.059381853500327, height: 70000, time: 280 },
{ longitude: 121.61482766938946, dimension: 23.373206571396267, height: 70000, time: 360 },
{ longitude: 121.41934736848623, dimension: 22.833599805367214, height: 50000, time: 480 }
],
[
{longitude: 120.64238963040782,dimension: 24.16165755368411,height: 30000,time: 0},
{longitude: 120.54482508398326,dimension: 24.227277585456818,height: 20000,time: 40},
{longitude: 120.45427903992982,dimension: 24.293364318851374,height: 30000,time: 80},
{longitude: 120.44707478407804,dimension: 24.412307008786513,height: 30000,time: 120},
{longitude: 120.37816248147197,dimension: 24.514653124919107,height: 10000,time: 160},
{longitude: 120.22219621602466,dimension: 24.554248755142424,height: 50000,time: 200},
{longitude: 120.05172146076706,dimension: 24.620213692294797,height: 30000,time: 240},
{longitude: 119.8014500115608,dimension: 24.613618762754086,height: 50000,time: 280},
{longitude: 119.59833115423396,dimension: 24.53445250129994,height: 30000,time: 320},
{longitude: 119.33355121521748,dimension: 24.26689822167974,height: 30000,time: 360},
{longitude: 119.27551725598164,dimension: 24.12463358054309,height: 40000,time: 400},
{longitude: 119.10504250072404,dimension: 23.8495824814122,height: 40000,time: 440},
{longitude: 118.87653378623185,dimension: 23.87943566695553,height: 50000,time: 480}
]
]
// 红方发射导弹
let launchvehicle = [
[
{ longitude: 117.74502819587877, dimension: 21.271103462087552, height: 40000, time: 240 },
{ longitude: 121.41934736848623, dimension: 22.833599805367214, height: 50000, time: 480 }
]
]
// 爆炸点
let fireList = [
[
{ longitude: 121.41934736848623, dimension: 22.833599805367214, height: 50000, time: 480 },
{ longitude: 121.41934736848623, dimension: 22.833599805367224, height: 50000, time: 500 }
]
]
4.给模型添加时间轴
for (let j = 0; j < dataRed.length; j++) {
let property = computeFlight(dataRed[j]);
//console.log(property)
// 添加模型
let planeModel = viewer.entities.add({
// 和时间轴关联
availability: new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
start: start,
stop: stop
})]),
position: property,
// 根据所提供的速度计算模型的朝向
orientation: new Cesium.VelocityOrientationProperty(property),
// 模型数据
model: {
uri: './f16.glb',
minimumPixelSize: 48,
color: getColor('RED', 1.0),//设置颜色
},
path: {
show: true,
leadTime: 0,
trailTime: 40,
width: 8,
resolution: 1,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.3,
taperPower: 0.3,
color: Cesium.Color.fromCssColorString('#F56C6C'),
}),
}
});
}
for (let j = 0; j < dataBlue.length; j++) {
let property = computeFlight(dataBlue[j]);
// 添加模型
let planeModel = viewer.entities.add({
// 和时间轴关联
availability: new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
start: start,
stop: stop
})]),
position: property,
// 根据所提供的速度计算模型的朝向
orientation: new Cesium.VelocityOrientationProperty(property),
// 模型数据
model: {
uri: './zhanji.glb',
minimumPixelSize: 48,
color: getColor('BLUE', 1.0),//设置颜色
// color: Cesium.Color.fromCssColorString('#409EFF')
},
path: {
show: true,
leadTime: 0,
trailTime: 40,
width: 8,
resolution: 1,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.3,
taperPower: 0.3,
color: Cesium.Color.fromCssColorString('#409EFF'),
}),
}
});
}
for (let j = 0; j < launchvehicle.length; j++) {
let property = computeFlight(launchvehicle[j]);
// 添加模型
let planeModel = viewer.entities.add({
// 和时间轴关联
availability: new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
start: start,
stop: stop
})]),
position: property,
// 根据所提供的速度计算模型的朝向
orientation: new Cesium.VelocityOrientationProperty(property),
// 模型数据
model: {
uri: './launchvehicle.glb',
minimumPixelSize: 48,
color: getColor('RED', 1.0),//设置颜色
// color: Cesium.Color.fromCssColorString('#F56C6C')
},
path: {
show: true,
leadTime: 0,
trailTime: 40,
width: 8,
resolution: 1,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.3,
taperPower: 0.3,
color: Cesium.Color.fromCssColorString('#F56C6C'),
}),
}
});
}
for (let j = 0; j < fireList.length; j++) {
let property = computeFlight(fireList[j]);
// 添加模型
let planeModel = viewer.entities.add({
position: property,
billboard: {
image: './爆炸.png', // 设置图片路径
scale: 0.2, // 设置图片大小
show: true,
}
})
}
function getColor(colorName, alpha) {
const color = Cesium.Color[colorName.toUpperCase()];
return Cesium.Color.fromAlpha(color, parseFloat(alpha));
}
function computeFlight (source) {
// 取样位置 相当于一个集合
let property = new Cesium.SampledPositionProperty();
for (let i = 0; i < source.length; i++) {
let time = Cesium.JulianDate.addSeconds(start, source[i].time, new Cesium.JulianDate);
let position = Cesium.Cartesian3.fromDegrees(source[i].longitude, source[i].dimension, source[i].height);
// 添加位置,和时间对应
property.addSample(time, position);
}
// 设置插值算法为拉格朗日多项式逼近
property.setInterpolationOptions({
interpolationAlgorithm: Cesium.LagrangePolynomialApproximation,
interpolationDegree: 5 // 可以根据需要调整插值次数
});
return property;
}