@orca-fe/x-map
Version:
41 lines (40 loc) • 2.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const gl_matrix_1 = require("gl-matrix");
const web_mercator_1 = tslib_1.__importDefault(require("@math.gl/web-mercator"));
gl_matrix_1.glMatrix.setMatrixArrayType(Array);
const DEGREES_TO_RADIANS = Math.PI / 180;
const centerOffset = [12125858.694656704, 4101446.337724882];
class AMapViewport extends web_mercator_1.default {
// 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
gl_matrix_1.mat4.perspective(this.projectionMatrix, fov, aspect, near, far);
// 计算相机矩阵 viewMatrix
const eye = gl_matrix_1.vec3.fromValues(cameraHeight * Math.sin(pitchInRadians) * Math.sin(rotationInRadians), -cameraHeight * Math.sin(pitchInRadians) * Math.cos(rotationInRadians), cameraHeight * Math.cos(pitchInRadians));
const up = gl_matrix_1.vec3.fromValues(-Math.cos(pitchInRadians) * Math.sin(rotationInRadians), Math.cos(pitchInRadians) * Math.cos(rotationInRadians), Math.sin(pitchInRadians));
gl_matrix_1.mat4.lookAt(this.viewMatrix, eye, gl_matrix_1.vec3.fromValues(0, 0, 0), up);
// this.viewUncenteredMatrix = mat4.clone(this.viewMatrix);
// 移动相机位置
const scale = 6.698434986953029;
gl_matrix_1.mat4.translate(this.viewMatrix, this.viewMatrix, gl_matrix_1.vec3.fromValues(-offsetOrigin[0] - scale * centerOffset[0], offsetOrigin[1] - scale * centerOffset[1], 0));
const scaleMat = gl_matrix_1.mat4.create();
gl_matrix_1.mat4.scale(scaleMat, scaleMat, gl_matrix_1.vec3.fromValues(scale, scale, scale));
gl_matrix_1.mat4.multiply(this.viewMatrix, this.viewMatrix, scaleMat);
gl_matrix_1.mat4.multiply(this.viewProjectionMatrix, this.projectionMatrix, this.viewMatrix);
// mat4.multiply(
// this.ViewProjectionMatrixUncentered,
// this.projectionMatrix,
// this.viewMatrix,
// );
}
}
exports.default = AMapViewport;