fix: 修复文本更新bug fixes #232

This commit is contained in:
thinkinggis 2020-03-25 23:00:34 +08:00
parent 6ef8e87ef0
commit 7a71b92b17
3 changed files with 79 additions and 75 deletions

View File

@ -67,8 +67,9 @@ export default class DataMappingPlugin implements ILayerPlugin {
), ),
); );
} }
this.logger.debug('remapping finished'); this.logger.debug('remapping finished');
// 处理文本更新
layer.emit('remapping', null);
} }
}); });
} }
@ -88,11 +89,6 @@ export default class DataMappingPlugin implements ILayerPlugin {
return this.applyAttributeMapping(filter, record)[0]; return this.applyAttributeMapping(filter, record)[0];
}); });
} }
// TODO: FIXME
// if (!filterData) {
// return;
// }
// mapping with source data
layer.setEncodedData(this.mapping(attributes, filterData)); layer.setEncodedData(this.mapping(attributes, filterData));
} }

View File

@ -9,7 +9,6 @@ export default class UpdateModelPlugin implements ILayerPlugin {
layer.hooks.beforeRender.tap('UpdateModelPlugin', () => { layer.hooks.beforeRender.tap('UpdateModelPlugin', () => {
// 处理文本更新 // 处理文本更新
if (layer.layerModel) { if (layer.layerModel) {
// console.log(layer.layerModelNeedUpdate);
layer.layerModel.needUpdate(); layer.layerModel.needUpdate();
} }
}); });

View File

@ -36,44 +36,56 @@ interface IPointTextLayerStyleOptions {
textAllowOverlap: boolean; textAllowOverlap: boolean;
} }
export function TextTriangulation(feature: IEncodeFeature) { export function TextTriangulation(feature: IEncodeFeature) {
const centroid = feature.centroid as number[]; // 计算中心点 // @ts-ignore
const { glyphQuads } = feature; const that = this as TextModel;
const id = feature.id as number;
const vertices: number[] = []; const vertices: number[] = [];
const indices: number[] = []; const indices: number[] = [];
if (!that.glyphInfoMap || !that.glyphInfoMap[id]) {
return {
vertices: [], // [ x, y, z, tex.x,tex.y, offset.x. offset.y]
indices: [],
size: 7,
};
}
const centroid = that.glyphInfoMap[id].centroid as number[]; // 计算中心点
const coord = const coord =
centroid.length === 2 ? [centroid[0], centroid[1], 0] : centroid; centroid.length === 2 ? [centroid[0], centroid[1], 0] : centroid;
glyphQuads.forEach((quad: IGlyphQuad, index: number) => { that.glyphInfoMap[id].glyphQuads.forEach(
vertices.push( (quad: IGlyphQuad, index: number) => {
...coord, vertices.push(
quad.tex.x, ...coord,
quad.tex.y + quad.tex.height, quad.tex.x,
quad.tl.x, quad.tex.y + quad.tex.height,
quad.tl.y, quad.tl.x,
...coord, quad.tl.y,
quad.tex.x + quad.tex.width, ...coord,
quad.tex.y + quad.tex.height, quad.tex.x + quad.tex.width,
quad.tr.x, quad.tex.y + quad.tex.height,
quad.tr.y, quad.tr.x,
...coord, quad.tr.y,
quad.tex.x + quad.tex.width, ...coord,
quad.tex.y, quad.tex.x + quad.tex.width,
quad.br.x, quad.tex.y,
quad.br.y, quad.br.x,
...coord, quad.br.y,
quad.tex.x, ...coord,
quad.tex.y, quad.tex.x,
quad.bl.x, quad.tex.y,
quad.bl.y, quad.bl.x,
); quad.bl.y,
indices.push( );
0 + index * 4, indices.push(
1 + index * 4, 0 + index * 4,
2 + index * 4, 1 + index * 4,
2 + index * 4, 2 + index * 4,
3 + index * 4, 2 + index * 4,
0 + index * 4, 3 + index * 4,
); 0 + index * 4,
}); );
},
);
return { return {
vertices, // [ x, y, z, tex.x,tex.y, offset.x. offset.y] vertices, // [ x, y, z, tex.x,tex.y, offset.x. offset.y]
indices, indices,
@ -82,21 +94,20 @@ export function TextTriangulation(feature: IEncodeFeature) {
} }
export default class TextModel extends BaseModel { export default class TextModel extends BaseModel {
private texture: ITexture2D; public glyphInfo: IEncodeFeature[];
private glyphInfo: IEncodeFeature[]; public glyphInfoMap: {
private currentZoom: number = -1;
private extent: [[number, number], [number, number]];
private textureHeight: number = 0;
private textCount: number = 0;
private preTextStyle: Partial<IPointTextLayerStyleOptions> = {};
private glyphInfoMap: {
[key: string]: { [key: string]: {
shaping: any; shaping: any;
glyphQuads: IGlyphQuad[]; glyphQuads: IGlyphQuad[];
centroid: number[]; centroid: number[];
}; };
} = {}; } = {};
private texture: ITexture2D;
private currentZoom: number = -1;
private extent: [[number, number], [number, number]];
private textureHeight: number = 0;
private textCount: number = 0;
private preTextStyle: Partial<IPointTextLayerStyleOptions> = {};
public getUninforms(): IModelUniform { public getUninforms(): IModelUniform {
const { const {
opacity = 1.0, opacity = 1.0,
@ -130,6 +141,11 @@ export default class TextModel extends BaseModel {
} }
public buildModels(): IModel[] { public buildModels(): IModel[] {
this.layer.on('remapping', () => {
this.initGlyph();
this.updateTexture();
this.reBuildModel();
});
this.extent = this.textExtent(); this.extent = this.textExtent();
const { const {
textAnchor = 'center', textAnchor = 'center',
@ -147,7 +163,7 @@ export default class TextModel extends BaseModel {
moduleName: 'pointText', moduleName: 'pointText',
vertexShader: textVert, vertexShader: textVert,
fragmentShader: textFrag, fragmentShader: textFrag,
triangulation: TextTriangulation, triangulation: TextTriangulation.bind(this),
depth: { enable: false }, depth: { enable: false },
blend: this.getBlend(), blend: this.getBlend(),
}), }),
@ -166,17 +182,7 @@ export default class TextModel extends BaseModel {
(!textAllowOverlap && (Math.abs(this.currentZoom - zoom) > 1 || !flag)) || (!textAllowOverlap && (Math.abs(this.currentZoom - zoom) > 1 || !flag)) ||
textAllowOverlap !== this.preTextStyle.textAllowOverlap textAllowOverlap !== this.preTextStyle.textAllowOverlap
) { ) {
this.filterGlyphs(); this.reBuildModel();
this.layer.models = [
this.layer.buildLayerModel({
moduleName: 'pointText',
vertexShader: textVert,
fragmentShader: textFrag,
triangulation: TextTriangulation,
depth: { enable: false },
blend: this.getBlend(),
}),
];
return true; return true;
} }
return false; return false;
@ -314,13 +320,11 @@ export default class TextModel extends BaseModel {
feature.shaping = shaping; feature.shaping = shaping;
feature.glyphQuads = glyphQuads; feature.glyphQuads = glyphQuads;
feature.centroid = calculteCentroid(coordinates); feature.centroid = calculteCentroid(coordinates);
if (id) { this.glyphInfoMap[id as number] = {
this.glyphInfoMap[id] = { shaping,
shaping, glyphQuads,
glyphQuads, centroid: calculteCentroid(coordinates),
centroid: calculteCentroid(coordinates), };
};
}
return feature; return feature;
}); });
} }
@ -333,9 +337,11 @@ export default class TextModel extends BaseModel {
textAllowOverlap = false, textAllowOverlap = false,
} = this.layer.getLayerConfig() as IPointTextLayerStyleOptions; } = this.layer.getLayerConfig() as IPointTextLayerStyleOptions;
if (textAllowOverlap) { if (textAllowOverlap) {
this.layer.setEncodedData(this.glyphInfo); // 如果允许文本覆盖
// this.layer.setEncodedData(this.glyphInfo);
return; return;
} }
this.glyphInfoMap = {};
this.currentZoom = this.mapService.getZoom(); this.currentZoom = this.mapService.getZoom();
this.extent = this.textExtent(); this.extent = this.textExtent();
const { width, height } = this.rendererService.getViewportSize(); const { width, height } = this.rendererService.getViewportSize();
@ -362,7 +368,11 @@ export default class TextModel extends BaseModel {
return false; return false;
} }
}); });
this.layer.setEncodedData(filterData); filterData.forEach((item) => {
// @ts-ignore
this.glyphInfoMap[item.id as number] = item;
});
// this.layer.setEncodedData(filterData);
} }
/** /**
* *
@ -389,15 +399,14 @@ export default class TextModel extends BaseModel {
}); });
} }
private rebuildModel() { private reBuildModel() {
// 避让 anchor,等属性变化时需要重新构建model
this.filterGlyphs(); this.filterGlyphs();
return [ this.layer.models = [
this.layer.buildLayerModel({ this.layer.buildLayerModel({
moduleName: 'pointText', moduleName: 'pointText',
vertexShader: textVert, vertexShader: textVert,
fragmentShader: textFrag, fragmentShader: textFrag,
triangulation: TextTriangulation, triangulation: TextTriangulation.bind(this),
depth: { enable: false }, depth: { enable: false },
blend: this.getBlend(), blend: this.getBlend(),
}), }),