@orca-fe/x-map
Version:
37 lines (36 loc) • 2.25 kB
JavaScript
import { mat4, vec3, glMatrix } from 'gl-matrix';
import WebMercatorViewport from '@math.gl/web-mercator';
glMatrix.setMatrixArrayType(Array);
const DEGREES_TO_RADIANS = Math.PI / 180;
const centerOffset = [12125858.694656704, 4101446.337724882];
export default class AMapViewport extends WebMercatorViewport {
// projectionMatrix = mat4.create();
// viewMatrix = mat4.create();
// viewProjectionMatrix = mat4.create();
// ViewProjectionMatrixUncentered = mat4.create();
// viewUncenteredMatrix = mat4.create();
syncWithMapCamera(mapCamera) {
const { pitch = 0, bearing = 0, offsetOrigin = [0, 0], cameraHeight = 1, aspect = 1, near = 0.1, far = 1000, fov = 0, } = mapCamera;
const pitchInRadians = pitch * DEGREES_TO_RADIANS;
const rotationInRadians = (360 - bearing) * DEGREES_TO_RADIANS;
// 计算透视投影矩阵 projectionMatrix
mat4.perspective(this.projectionMatrix, fov, aspect, near, far);
// 计算相机矩阵 viewMatrix
const eye = vec3.fromValues(cameraHeight * Math.sin(pitchInRadians) * Math.sin(rotationInRadians), -cameraHeight * Math.sin(pitchInRadians) * Math.cos(rotationInRadians), cameraHeight * Math.cos(pitchInRadians));
const up = vec3.fromValues(-Math.cos(pitchInRadians) * Math.sin(rotationInRadians), Math.cos(pitchInRadians) * Math.cos(rotationInRadians), Math.sin(pitchInRadians));
mat4.lookAt(this.viewMatrix, eye, vec3.fromValues(0, 0, 0), up);
// this.viewUncenteredMatrix = mat4.clone(this.viewMatrix);
// 移动相机位置
const scale = 6.698434986953029;
mat4.translate(this.viewMatrix, this.viewMatrix, vec3.fromValues(-offsetOrigin[0] - scale * centerOffset[0], offsetOrigin[1] - scale * centerOffset[1], 0));
const scaleMat = mat4.create();
mat4.scale(scaleMat, scaleMat, vec3.fromValues(scale, scale, scale));
mat4.multiply(this.viewMatrix, this.viewMatrix, scaleMat);
mat4.multiply(this.viewProjectionMatrix, this.projectionMatrix, this.viewMatrix);
// mat4.multiply(
// this.ViewProjectionMatrixUncentered,
// this.projectionMatrix,
// this.viewMatrix,
// );
}
}