fix: 采用非偏移坐标系坐标系解决高德地图中国区域抖动的问题

This commit is contained in:
thinkinggis 2020-04-10 15:17:16 +08:00
parent b8f58992d1
commit 124a1d27aa
10 changed files with 41 additions and 11 deletions

View File

@ -67,6 +67,10 @@ export default class CameraService implements ICameraService {
return this.viewport.getViewMatrixUncentered(); return this.viewport.getViewMatrixUncentered();
} }
public getViewProjectionMatrixUncentered(): number[] {
return this.viewport.getViewProjectionMatrixUncentered();
}
public getViewProjectionMatrix(): number[] { public getViewProjectionMatrix(): number[] {
return ( return (
this.overridedViewProjectionMatrix || this.overridedViewProjectionMatrix ||

View File

@ -16,6 +16,7 @@ export interface IViewport {
getProjectionMatrix(): number[]; getProjectionMatrix(): number[];
getViewMatrix(): number[]; getViewMatrix(): number[];
getViewMatrixUncentered(): number[]; getViewMatrixUncentered(): number[];
getViewProjectionMatrixUncentered(): number[];
getViewProjectionMatrix(): number[]; getViewProjectionMatrix(): number[];
getZoom(): number; getZoom(): number;
getZoomScale(): number; getZoomScale(): number;

View File

@ -260,14 +260,17 @@ export default class HeatMapModel extends BaseModel {
} = this.layer.getLayerConfig() as IHeatMapLayerStyleOptions; } = this.layer.getLayerConfig() as IHeatMapLayerStyleOptions;
const invert = mat4.invert( const invert = mat4.invert(
mat4.create(), mat4.create(),
// @ts-ignore mat4.fromValues(
mat4.fromValues(...this.cameraService.getViewProjectionMatrix()), // @ts-ignore
...this.cameraService.getViewProjectionMatrixUncentered(),
),
) as mat4; ) as mat4;
this.colorModel.draw({ this.colorModel.draw({
uniforms: { uniforms: {
u_opacity: opacity || 1.0, u_opacity: opacity || 1.0,
u_colorTexture: this.colorTexture, u_colorTexture: this.colorTexture,
u_texture: this.heatmapFramerBuffer, u_texture: this.heatmapFramerBuffer,
u_ViewProjectionMatrixUncentered: this.cameraService.getViewProjectionMatrixUncentered(),
u_InverseViewProjectionMatrix: [...invert], u_InverseViewProjectionMatrix: [...invert],
}, },
}); });

View File

@ -5,6 +5,7 @@ uniform sampler2D u_texture;
varying vec2 v_texCoord; varying vec2 v_texCoord;
uniform mat4 u_ModelMatrix; uniform mat4 u_ModelMatrix;
uniform mat4 u_InverseViewProjectionMatrix; uniform mat4 u_InverseViewProjectionMatrix;
uniform mat4 u_ViewProjectionMatrixUncentered;
varying float v_intensity; varying float v_intensity;
vec2 toBezier(float t, vec2 P0, vec2 P1, vec2 P2, vec2 P3) { vec2 toBezier(float t, vec2 P0, vec2 P1, vec2 P2, vec2 P3) {
@ -26,8 +27,8 @@ void main() {
vec4 p1 = vec4(pos, 0.0, 1.0); vec4 p1 = vec4(pos, 0.0, 1.0);
vec4 p2 = vec4(pos, 1.0, 1.0); vec4 p2 = vec4(pos, 1.0, 1.0);
vec4 inverseP1 = unproject_clipspace_to_position(p1, u_InverseViewProjectionMatrix); vec4 inverseP1 = u_InverseViewProjectionMatrix * p1;
vec4 inverseP2 = unproject_clipspace_to_position(p2, u_InverseViewProjectionMatrix) ; vec4 inverseP2 = u_InverseViewProjectionMatrix * p2;
inverseP1 = inverseP1 / inverseP1.w; inverseP1 = inverseP1 / inverseP1.w;
inverseP2 = inverseP2 / inverseP2.w; inverseP2 = inverseP2 / inverseP2.w;
@ -40,6 +41,6 @@ void main() {
v_intensity = texture2D(u_texture, v_texCoord).r; v_intensity = texture2D(u_texture, v_texCoord).r;
fh = toBezier(v_intensity, b).y; fh = toBezier(v_intensity, b).y;
gl_Position = project_common_position_to_clipspace(vec4(position.xy, fh * project_pixel(50.), 1.0)); gl_Position = u_ViewProjectionMatrixUncentered * vec4(position.xy, fh * project_pixel(50.), 1.0);
} }

View File

@ -7,6 +7,7 @@ export default class Viewport implements IViewport {
private projectionMatrix: mat4 = mat4.create(); private projectionMatrix: mat4 = mat4.create();
private viewMatrix: mat4 = mat4.create(); private viewMatrix: mat4 = mat4.create();
private viewProjectionMatrix: mat4 = mat4.create(); private viewProjectionMatrix: mat4 = mat4.create();
private ViewProjectionMatrixUncentered: mat4 = mat4.create();
private viewUncenteredMatrix: mat4 = mat4.create(); private viewUncenteredMatrix: mat4 = mat4.create();
private zoom: number; private zoom: number;
private center: number[]; private center: number[];
@ -60,6 +61,11 @@ export default class Viewport implements IViewport {
this.projectionMatrix, this.projectionMatrix,
this.viewMatrix, this.viewMatrix,
); );
mat4.multiply(
this.ViewProjectionMatrixUncentered,
this.projectionMatrix,
this.viewMatrix,
);
} }
public getZoom(): number { public getZoom(): number {
@ -95,6 +101,11 @@ export default class Viewport implements IViewport {
return this.viewProjectionMatrix; return this.viewProjectionMatrix;
} }
public getViewProjectionMatrixUncentered(): number[] {
// @ts-ignore
return this.ViewProjectionMatrixUncentered;
}
public getFocalDistance() { public getFocalDistance() {
return 1; return 1;
} }

View File

@ -58,6 +58,10 @@ export default class Viewport implements IViewport {
return this.viewport.viewProjectionMatrix; return this.viewport.viewProjectionMatrix;
} }
public getViewProjectionMatrixUncentered(): number[] {
// @ts-ignore
return this.viewport.viewProjectionMatrix;
}
public getFocalDistance() { public getFocalDistance() {
return 1; return 1;
} }

View File

@ -9,6 +9,7 @@ import DashLineDemo from './components/dash';
import DataUpdate from './components/data_update'; import DataUpdate from './components/data_update';
import HeatMapDemo from './components/HeatMap'; import HeatMapDemo from './components/HeatMap';
import HeatMapDemo3D from './components/heatmap3d'; import HeatMapDemo3D from './components/heatmap3d';
import HeatMap3D_2 from './components/heatmap2';
import HexagonLayerDemo from './components/hexagon'; import HexagonLayerDemo from './components/hexagon';
import LightDemo from './components/light'; import LightDemo from './components/light';
import LineLayer from './components/Line'; import LineLayer from './components/Line';
@ -42,6 +43,7 @@ storiesOf('图层', module)
.add('2D弧线', () => <Arc2DLineDemo />) .add('2D弧线', () => <Arc2DLineDemo />)
.add('热力图', () => <HeatMapDemo />) .add('热力图', () => <HeatMapDemo />)
.add('热力图3D', () => <HeatMapDemo3D />) .add('热力图3D', () => <HeatMapDemo3D />)
.add('热力图2', () => <HeatMap3D_2 />)
.add('网格热力图', () => <HexagonLayerDemo />) .add('网格热力图', () => <HexagonLayerDemo />)
.add('栅格', () => <RasterLayerDemo />) .add('栅格', () => <RasterLayerDemo />)
.add('图片', () => <ImageLayerDemo />) .add('图片', () => <ImageLayerDemo />)

View File

@ -1,5 +1,5 @@
import { HeatmapLayer, Scene } from '@antv/l7'; import { HeatmapLayer, Scene } from '@antv/l7';
import { Mapbox } from '@antv/l7-maps'; import { GaodeMap, Mapbox } from '@antv/l7-maps';
// @ts-ignore // @ts-ignore
import * as React from 'react'; import * as React from 'react';
@ -21,7 +21,7 @@ export default class HeatMapLayerDemo extends React.Component {
center: [121.268, 30.3628], center: [121.268, 30.3628],
pitch: 0, pitch: 0,
style: 'dark', style: 'dark',
zoom: 2, zoom: 12,
}), }),
}); });
const data = await response.json(); const data = await response.json();
@ -48,8 +48,8 @@ export default class HeatMapLayerDemo extends React.Component {
}, },
}); });
scene.addLayer(layer); scene.addLayer(layer);
scene.on('loaded', () => { scene.on('zoom', () => {
console.log('scene loaded'); console.log(scene.getZoom());
}); });
this.scene = scene; this.scene = scene;
} }

View File

@ -14,9 +14,9 @@ export default class HeatMapLayerDemo extends React.Component {
public async componentDidMount() { public async componentDidMount() {
const scene = new Scene({ const scene = new Scene({
id: 'map', id: 'map',
map: new Mapbox({ map: new GaodeMap({
style: 'dark', style: 'dark',
pitch: 58.5, pitch: 8.5,
center: [116.49434030056, 39.868073421167621], center: [116.49434030056, 39.868073421167621],
zoom: 13, zoom: 13,
}), }),

View File

@ -104,6 +104,9 @@ export default React.memo(function Map() {
feature: args.feature, feature: args.feature,
}); });
} }
function hidePopup(args: any): void {
setPopupInfo(undefined);
}
return ( return (
<> <>
@ -235,6 +238,7 @@ export default React.memo(function Map() {
}} }}
> >
<LayerEvent type="mousemove" handler={showPopup} /> <LayerEvent type="mousemove" handler={showPopup} />
<LayerEvent type="mouseout" handler={hidePopup} />
</PointLayer>, </PointLayer>,
<PointLayer <PointLayer
key={'5'} key={'5'}