Cesium渲染模型按点飞行问题,有没有好人给解答一下
代码如下:
说明:这是个react项目里面使用的cesium,所以下面使用的方法没有带this,方法之间调用是OK的,这些都是网上找的
let lnglatArr = [
[121.527589, 38.957547],
[121.527825, 38.960166],
[121.536472, 38.959098],
[121.540442, 38.958464],
[121.543489, 38.958131],
[121.542888, 38.955861],
[121.542266, 38.953325],
]
addTrackLine(lnglatArr)
// 模型飞动
function addTrackLine(positions) {
let timeObj = getSiteTimes(positions, 50);
// console.log(timeObj.siteTimes,timeObj.timeSum)
let startTime = Cesium.JulianDate.fromDate(new Date());
let stopTime = Cesium.JulianDate.addSeconds(startTime, timeObj.timeSum, new Cesium.JulianDate());
viewer.clock.startTime = startTime.clone();
viewer.clock.stopTime = stopTime.clone();
viewer.clock.currentTime = startTime.clone();
viewer.clock.clockRange = Cesium.ClockRange.CLAMPED;
let property = computeCirclularFlight(positions, startTime, timeObj.siteTimes);
console.log(property)
viewer.entities.add({
availability: new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
start: startTime,
stop: stopTime
})]),
position: property,
orientation: new Cesium.VelocityOrientationProperty(property),
model: {
uri: "static/Bee.glb",
scale: 0.5
},
path: {
resolution: 1,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.1,
color: Cesium.Color.YELLOW
}),
width: 10
}
});
}
//获取飞行的动画点位
function computeCirclularFlight(pArr, startTime, siteTimes) {
var property = new Cesium.SampledPositionProperty();
for (var i = 0; i < pArr.length; i++) {
// console.log('秒数',startTime,'天数',siteTimes[i])
const time = Cesium.JulianDate.addSeconds(startTime, siteTimes[i], new Cesium.JulianDate());
console.log('Time',time)
property.addSample(time, pArr[i]);
}
console.log('property',property)
return property;
}
function spaceDistance(positions) {
let distance = 0;
for (let i = 0; i < positions.length-1; i++) {
// console.log(positions[i])
if(positions[i] !== undefined){
// WGS84坐标转弧度
let point1cartographic = Cesium.Cartographic.fromDegrees(...positions[i]);
let point2cartographic = Cesium.Cartographic.fromDegrees(...positions[i + 1]);
// console.log('值来',point1cartographic,point2cartographic)
/**根据经纬度计算出距离**/
let geodesic = new Cesium.EllipsoidGeodesic();
geodesic.setEndPoints(point1cartographic, point2cartographic);
let s = geodesic.surfaceDistance;
// //返回两点之间的距离
s = Math.sqrt(Math.pow(s, 2) + Math.pow(point2cartographic.height - point1cartographic.height, 2));
distance = distance + s;
}
}
return distance.toFixed(2);
}
// 计算每个点位时间和总时间
function getSiteTimes(positions, speed) {
let timeSum = 0;
let times = [];
for (let i = 0; i < positions.length; i++) {
if (i === 0) {
times.push(0);
}
timeSum += spaceDistance([positions[i - 1], positions[i]]) / speed;
console.log('timeSum',timeSum)
times.push(timeSum);
}
return {
timeSum,
siteTimes: times,
};
}
其中如果把里面的position: property,就不报错了,但是没有模型。
viewer.entities.add({
availability: new Cesium.TimeIntervalCollection([new Cesium.TimeInterval({
start: startTime,
stop: stopTime
})]),
// position: property,
orientation: new Cesium.VelocityOrientationProperty(property),
model: {
uri: "static/Bee.glb",
scale: 0.5
},
path: {
resolution: 1,
material: new Cesium.PolylineGlowMaterialProperty({
glowPower: 0.1,
color: Cesium.Color.YELLOW
}),
width: 10
}
});
发现是computeCirclularFlight这个方法最后反的值好像有问题,但是我不确定这方法里面的cesium函数用的是否正常,有没有好人给看一下,或者有没有成熟的案例给分享下呗