feat: 提取网格构建内容、简化代码

This commit is contained in:
shihui 2022-12-01 23:17:17 +08:00
parent ed3372e02c
commit 383ba2df7e
3 changed files with 133 additions and 13 deletions

View File

@ -283,6 +283,9 @@ export default class StyleAttributeService implements IStyleAttributeService {
sizePerElement: 0,
elements: [],
};
if (triangulation) {
this.triangulation = triangulation;
}
@ -423,6 +426,7 @@ export default class StyleAttributeService implements IStyleAttributeService {
if (triangulation) {
this.triangulation = triangulation;
}
const descriptors = this.attributes.map((attr) => {
attr.resetDescriptor();
return attr.descriptor;

View File

@ -7,6 +7,6 @@ export default class PointLayer extends BaseLayer<IPointLayerStyleOptions> {
public async buildModels() {
const model = new FillModel(this);
this.models = model.initModels();
this.models = model.buildModels();
}
}

View File

@ -2,7 +2,6 @@ import {
AttributeType,
gl,
IEncodeFeature,
IModelUniform,
ILayer,
TYPES,
IShaderModuleService,
@ -13,10 +12,11 @@ import {
import pointFillFrag from '../shaders/fill_frag.glsl';
import pointFillVert from '../shaders/fill_vert.glsl';
import StyleAttribute from '../../../../core/src/services/layer/StyleAttribute'
export default class FillModel {
protected layer: ILayer;
private attributes: any[] = [];
protected shaderModuleService: IShaderModuleService;
@ -43,15 +43,124 @@ export default class FillModel {
this.registerBuiltinAttributes();
}
public getUninforms(): IModelUniform {
return {
public createAttributesAndIndices(
features: IEncodeFeature[],
triangulation: Triangulation,
segmentNumber: number,
) {
const descriptors = this.attributes.map((attr) => {
attr.resetDescriptor();
return attr.descriptor;
});
let verticesNum = 0;
let vecticesCount = 0; // 在不使用 element 的时候记录顶点、图层所有顶点的总数
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const vertices: number[] = [];
const indices: number[] = [];
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const normals: number[] = [];
let size = 3;
features.forEach((feature, featureIdx) => {
// 逐 feature 进行三角化
const {
indices: indicesForCurrentFeature,
vertices: verticesForCurrentFeature,
normals: normalsForCurrentFeature,
size: vertexSize,
indexes,
count,
} = triangulation(feature, segmentNumber);
if (typeof count === 'number') {
vecticesCount += count;
}
indicesForCurrentFeature.forEach((i) => {
indices.push(i + verticesNum);
});
size = vertexSize;
const verticesNumForCurrentFeature =
verticesForCurrentFeature.length / vertexSize;
verticesNum += verticesNumForCurrentFeature;
// 根据 position 顶点生成其他顶点数据
for (
let vertexIdx = 0;
vertexIdx < verticesNumForCurrentFeature;
vertexIdx++
) {
const normal =
normalsForCurrentFeature?.slice(vertexIdx * 3, vertexIdx * 3 + 3) ||
[];
const vertice = verticesForCurrentFeature.slice(
vertexIdx * vertexSize,
vertexIdx * vertexSize + vertexSize,
);
let vertexIndex = 0;
if (indexes && indexes[vertexIdx] !== undefined) {
vertexIndex = indexes[vertexIdx];
}
descriptors.forEach((descriptor, attributeIdx) => {
if (descriptor && descriptor.update) {
(descriptor.buffer.data as number[]).push(
...descriptor.update(
feature,
featureIdx,
vertice,
vertexIdx, // 当前顶点所在feature索引
normal,
vertexIndex,
// 传入顶点索引 vertexIdx
),
);
} // end if
}); // end for each
} // end for
}); // end features for Each
const {
createAttribute,
createBuffer,
createElements,
} = this.rendererService;
const attributes = {};
descriptors.forEach((descriptor, attributeIdx) => {
if (descriptor) {
// IAttribute 参数透传
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { buffer, update, name, ...rest } = descriptor;
const vertexAttribute = createAttribute({
// IBuffer 参数透传
buffer: createBuffer(buffer),
...rest,
});
attributes[descriptor.name || ''] = vertexAttribute;
}
});
const elements = createElements({
data: indices,
type: gl.UNSIGNED_INT,
count: indices.length,
});
const attributesAndIndices = {
attributes,
elements,
count: vecticesCount,
};
return attributesAndIndices;
}
public initModels() {
return this.buildModels();
}
public buildLayerModel( options: any ) {
const {
@ -69,8 +178,7 @@ export default class FillModel {
});
const { vs, fs, uniforms } = this.shaderModuleService.getModule(moduleName);
const { createModel } = this.rendererService;
const { attributes, elements } =
this.styleAttributeService.createAttributesAndIndices(
const { attributes, elements } =this.createAttributesAndIndices(
[{
color: [1, 0, 0, 1],
coordinates: [120, 30],
@ -81,6 +189,7 @@ export default class FillModel {
triangulation,
segmentNumber,
);
const modelOptions = {
attributes,
uniforms,
@ -90,7 +199,7 @@ export default class FillModel {
...rest,
};
return createModel(modelOptions);
return createModel(modelOptions);
}
@ -115,9 +224,16 @@ export default class FillModel {
return [model];
}
public registerStyleAttribute(
options: any
) {
const attributeToUpdate = new StyleAttribute(options);
this.attributes.push(attributeToUpdate);
}
protected registerBuiltinAttributes() {
this.styleAttributeService.registerStyleAttribute({
this.registerStyleAttribute({
name: 'position',
type: AttributeType.Attribute,
descriptor: {
@ -139,7 +255,7 @@ export default class FillModel {
},
});
this.styleAttributeService.registerStyleAttribute({
this.registerStyleAttribute({
name: 'extrude',
type: AttributeType.Attribute,
descriptor: {