Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: mapbox 下部分面数据图层绘制异常 #2453

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-seas-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@antv/l7-layers': patch
---

fix: mapbox 下部分面数据图层绘制异常
1 change: 1 addition & 0 deletions examples/demos/bugfix/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export { MapRender as color } from './color';
export { MapRender as data_shake } from './data-shake';
export { MapRender as event_legend } from './event_legend';
export { MapRender as mutiPolygon } from './muti-polygon';
export { MapRender as polygon } from './polygon';
export { MapRender as remove_muti_layer } from './remove-muti-layer';
export { MapRender as size } from './size';
Expand Down
42 changes: 42 additions & 0 deletions examples/demos/bugfix/muti-polygon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { PolygonLayer, Scene } from '@antv/l7';
import * as allMap from '@antv/l7-maps';
import type { RenderDemoOptions } from '../../types';

export function MapRender(options: RenderDemoOptions) {
const scene = new Scene({
id: 'map',
renderer: options.renderer,
map: new allMap[options.map]({
style: 'dark',
center: [-96, 37.8],
zoom: 3,
}),
});

fetch(
'https://npm.elemecdn.com/static-geo-atlas/geo-data/choropleth-data/country/100000_country_province.json',
)
.then((res) => res.json())
.then((data) => data.features.slice(4, 5))
.then((geoData) => {
const layer = new PolygonLayer({
autoFit: true,
})
.source(geoData, {
parser: {
type: 'json',
geometry: 'geometry',
},
})
.color('rgb(22,199,255)')
.shape('fill')
// .shape('extrude')
// .size(1200 * 100)
.active(true)
.style({
opacity: 0.5,
});

scene.addLayer(layer);
});
}
12 changes: 9 additions & 3 deletions packages/layers/src/core/shape/extrude.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { lngLatToMeters } from '@antv/l7-utils';
import earcut from 'earcut';
import { vec3 } from 'gl-matrix';
import { getPolygonSurfaceIndices } from '../utils';
import type { IPath } from './Path';

export interface IExtrudeGeomety {
positions: number[];
index: number[];
Expand Down Expand Up @@ -62,6 +64,7 @@ export default function extrudePolygon(path: IPath[]): IExtrudeGeomety {
index: indexArray,
};
}

export function fillPolygon(points: IPath[]) {
const flattengeo = earcut.flatten(points);
const triangles = earcut(flattengeo.vertices, flattengeo.holes, flattengeo.dimensions);
Expand All @@ -82,7 +85,7 @@ export function extrude_PolygonNormal(
}
const n = path[0].length;
const flattengeo = earcut.flatten(path);
const { vertices, dimensions } = flattengeo;
const { vertices, dimensions, holes } = flattengeo;
const positions = [];
const indexArray = [];
const normals = [];
Expand All @@ -97,8 +100,10 @@ export function extrude_PolygonNormal(
);
normals.push(0, 0, 1);
}
const triangles = earcut(flattengeo.vertices, flattengeo.holes, flattengeo.dimensions);
indexArray.push(...triangles);

const indices = getPolygonSurfaceIndices(vertices, holes, dimensions, needFlat);
indexArray.push(...indices);

// 设置侧面
for (let i = 0; i < n; i++) {
const prePoint = flattengeo.vertices.slice(i * dimensions, (i + 1) * dimensions);
Expand Down Expand Up @@ -145,6 +150,7 @@ export function extrude_PolygonNormal(
normals,
};
}

function computeVertexNormals(
p1: [number, number, number],
p2: [number, number, number],
Expand Down
16 changes: 9 additions & 7 deletions packages/layers/src/core/triangulation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import {
} from '../earth/utils';
import ExtrudePolyline from '../utils/extrude_polyline';
import type { IPosition, ShapeType2D, ShapeType3D } from './shape/Path';
import { geometryShape } from './shape/Path';
import type { IExtrudeGeomety } from './shape/extrude';
import extrudePolygon, { extrude_PolygonNormal, fillPolygon } from './shape/extrude';
import { getPolygonSurfaceIndices } from './utils';

import { geometryShape } from './shape/Path';
type IShape = ShapeType2D & ShapeType3D;
interface IGeometryCache {
[key: string]: IExtrudeGeomety;
Expand Down Expand Up @@ -334,23 +335,24 @@ export function polygonTriangulation(feature: IEncodeFeature) {
const { coordinates } = feature;
const flattengeo = earcut.flatten(coordinates as number[][][]);
const { vertices, dimensions, holes } = flattengeo;

const indices = getPolygonSurfaceIndices(vertices, holes, dimensions);

return {
indices: earcut(vertices, holes, dimensions),
indices,
vertices,
size: dimensions,
};
}

// 构建几何图形(带有中心点和大小)
export function polygonTriangulationWithCenter(feature: IEncodeFeature) {
const { coordinates } = feature;
const flattengeo = earcut.flatten(coordinates as number[][][]);
const { vertices, dimensions, holes } = flattengeo;
const { indices, vertices, size } = polygonTriangulation(feature);

return {
indices: earcut(vertices, holes, dimensions),
indices: indices,
vertices: getVerticesWithCenter(vertices),
size: dimensions + 4,
size: size + 4,
};
}

Expand Down
42 changes: 42 additions & 0 deletions packages/layers/src/core/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
import { lngLatToMeters } from '@antv/l7-utils';
import earcut from 'earcut';

export function MultipleOfFourNumber(num: number) {
return Math.max(Math.ceil(num / 4) * 4, 4);
}

/**
* Get vertex indices for drawing polygon mesh (triangulation)
*/
export function getPolygonSurfaceIndices(
positions: number[],
holeIndices: number[],
positionSize: number,
preproject = true,
) {
const is3d = positionSize === 3;

if (preproject) {
positions = positions.slice();
const p: number[] = [];
for (let i = 0; i < positions.length; i += positionSize) {
p[0] = positions[i];
p[1] = positions[i + 1];

if (is3d) {
p[2] = positions[i + 2];
}

// earcut is a 2D triangulation algorithm, and handles 3D data as if it was projected onto the XY plane
const xy = lngLatToMeters(p, true, { enable: false, decimal: 1 });

positions[i] = xy[0];
positions[i + 1] = xy[1];

if (is3d) {
positions[i + 2] = xy[2];
}
}
}

const indices = earcut(positions, holeIndices, positionSize);

return indices;
}
5 changes: 5 additions & 0 deletions packages/utils/src/geo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function transform(item: any[], cb: (item: any[]) => any): any {
return cb(item);
}
export function lngLatToMeters(lnglat: Point): Point;
export function lngLatToMeters(
lnglat: Point,
validate?: boolean,
accuracy?: { enable: boolean; decimal: number },
): Point;
export function lngLatToMeters(
lnglat: Point,
validate: boolean = true,
Expand Down
Loading