cesium + threejs 坐标转换问题

795 阅读1分钟

原始坐标数据来源于threejs 在cesium 需要做如下转换

const position = {
    x:1,
    y:1,
    z:1
}
// 坐标系不同需要做转化然后计算笛卡尔坐标系 坐标系具体的不同 自行百度
const newPos: TunnelTypes = {
    x: position.x,
    y: position.z * -1,
    z: position.y
};

转化后使用 geodesy 库实现求具体的偏移量

import LatLonSpherical from 'geodesy/latlon-spherical.js';
const startPoint = new LatLonSpherical(0, 0); // 转换多个可以定义全局
const newX = x;
const newY = y;
const newZ = z;
startPoint.lat = lat; // 原始坐标 lat
startPoint.lon = long; // 原始坐标 long

/**
 *  使用geodesy库的LatLonSpherical类的destinationPoint方法计算给定起点和偏移量的终点。我们首先向北移动OFFSET_Y米,然后向东移动OFFSET_X米。注意,不考虑Z轴上的偏移量。
 *  distance(距离):从起始点沿大圆路径移动的距离,以米为单位。在这个示例中,我们使用OFFSET_X和OFFSET_Y作为距离值。
    initialBearing(初始方位角):从起始点出发的方位角,以角度为单位。方位角是从正北方向顺时针测量的角度,范围为0°至360°。在这个示例中,我们使用0°(正北)和90°(正东)作为初始方位角。
 */
const destinationPoint = startPoint.destinationPoint(newY, 0); // Move north
const destinationPoint2 = destinationPoint.destinationPoint(newX, 90); // Move east
const result = wgs84ToCartesian3({
    long: destinationPoint2.lon,
    lat: destinationPoint2.lat,
    height: new Big(height).plus(newZ).toNumber()
});
const hpr = new HeadingPitchRoll(0, 0, 0);
const modelMatrix = Transforms.headingPitchRollToFixedFrame(result, hpr);

计算需要旋转后对应的坐标方法

// 传入水平旋转角度 需要转换为 水平旋转弧度
const heading = CesiumMath.toRadians(orientationAngle);
const hpr = new HeadingPitchRoll(heading, 0, 0);
// 上面求偏移两点代码不变
const destinationPoint = startPoint.destinationPoint(newY, orientationAngle); // Move north
const destinationPoint2 = destinationPoint.destinationPoint(newX, 90 + orientationAngle); // Move east