From b0f6265cd3b16c6ff39d0a6693788a25fca7bda2 Mon Sep 17 00:00:00 2001 From: thinkinggis Date: Mon, 4 Nov 2019 11:19:45 +0800 Subject: [PATCH] fix(raster layer): update raster triangle --- .../services/layer/IStyleAttributeService.ts | 2 - .../services/layer/StyleAttributeService.ts | 3 +- packages/layers/src/core/BaseLayer.ts | 1 - .../src/raster/buffers/triangulation.ts | 5 +- packages/layers/src/raster/raster.ts | 64 +++++++++++++------ 5 files changed, 46 insertions(+), 29 deletions(-) diff --git a/packages/core/src/services/layer/IStyleAttributeService.ts b/packages/core/src/services/layer/IStyleAttributeService.ts index c5ba434ee2..03f8b69533 100644 --- a/packages/core/src/services/layer/IStyleAttributeService.ts +++ b/packages/core/src/services/layer/IStyleAttributeService.ts @@ -131,7 +131,6 @@ export interface IStyleAttribute extends IStyleAttributeInitializationOptions { export type Triangulation = ( feature: IEncodeFeature, - parserData: IParseDataItem, ) => { vertices: number[]; indices: number[]; @@ -161,7 +160,6 @@ export interface IStyleAttributeService { createAttributesAndIndices( encodedFeatures: IEncodeFeature[], triangulation: Triangulation, - parserData: IParseDataItem[], ): { attributes: { [attributeName: string]: IAttribute; diff --git a/packages/core/src/services/layer/StyleAttributeService.ts b/packages/core/src/services/layer/StyleAttributeService.ts index a40647dc69..42cdfaac59 100644 --- a/packages/core/src/services/layer/StyleAttributeService.ts +++ b/packages/core/src/services/layer/StyleAttributeService.ts @@ -167,7 +167,6 @@ export default class StyleAttributeService implements IStyleAttributeService { public createAttributesAndIndices( features: IEncodeFeature[], triangulation: Triangulation, - parserData: IParseDataItem[], ): { attributes: { [attributeName: string]: IAttribute; @@ -188,7 +187,7 @@ export default class StyleAttributeService implements IStyleAttributeService { vertices: verticesForCurrentFeature, normals: normalsForCurrentFeature, size: vertexSize, - } = triangulation(feature, parserData[featureIdx]); + } = triangulation(feature); indices.push(...indicesForCurrentFeature.map((i) => i + verticesNum)); vertices.push(...verticesForCurrentFeature); if (normalsForCurrentFeature) { diff --git a/packages/layers/src/core/BaseLayer.ts b/packages/layers/src/core/BaseLayer.ts index 750e43d302..13d60d574d 100644 --- a/packages/layers/src/core/BaseLayer.ts +++ b/packages/layers/src/core/BaseLayer.ts @@ -351,7 +351,6 @@ export default class BaseLayer implements ILayer { } = this.styleAttributeService.createAttributesAndIndices( this.encodedData, triangulation, - parserData, ); return createModel({ attributes, diff --git a/packages/layers/src/raster/buffers/triangulation.ts b/packages/layers/src/raster/buffers/triangulation.ts index 5fe8f48f60..2d15ce9c0f 100644 --- a/packages/layers/src/raster/buffers/triangulation.ts +++ b/packages/layers/src/raster/buffers/triangulation.ts @@ -1,10 +1,7 @@ import { IEncodeFeature, IParseDataItem } from '@l7/core'; // @ts-ignore import Martini from '@mapbox/martini'; -export function RasterTriangulation( - feature: IEncodeFeature, - parserData: IParseDataItem, -) { +export function RasterTriangulation(parserData: IParseDataItem) { const { coordinates, data, min, max, width, height } = parserData; const maxlength = Math.max(width, height); const gridSize = Math.pow(2, Math.ceil(Math.log2(maxlength))) + 1; diff --git a/packages/layers/src/raster/raster.ts b/packages/layers/src/raster/raster.ts index 941497947c..011eaef5bd 100644 --- a/packages/layers/src/raster/raster.ts +++ b/packages/layers/src/raster/raster.ts @@ -24,8 +24,8 @@ interface IRasterLayerStyleOptions { rampColors: IColorRamp; } -export default class ImageLayer extends BaseLayer { - public name: string = 'RasterLayer'; +export default class RasterLayer extends BaseLayer { + public name: string = 'e'; protected texture: ITexture2D; protected colorTexture: ITexture2D; @@ -81,24 +81,48 @@ export default class ImageLayer extends BaseLayer { height: imageData.height, flipY: true, }); - this.models = [ - this.buildLayerModel({ - moduleName: 'Raster', - vertexShader: rasterVert, - fragmentShader: rasterFrag, - triangulation: RasterTriangulation, - primitive: gl.TRIANGLES, - depth: { enable: false }, - blend: { - enable: true, - func: { - srcRGB: gl.SRC_ALPHA, - srcAlpha: 1, - dstRGB: gl.ONE_MINUS_SRC_ALPHA, - dstAlpha: 1, - }, - }, + this.models = [this.buildRasterModel()]; + } + private buildRasterModel() { + const source = this.getSource(); + const sourceFeature = source.data.dataArray[0]; + const triangulation = RasterTriangulation(sourceFeature); + this.shaderModuleService.registerModule('raster', { + vs: rasterVert, + fs: rasterFrag, + }); + + const { vs, fs, uniforms } = this.shaderModuleService.getModule('raster'); + const { + createAttribute, + createElements, + createBuffer, + createModel, + } = this.rendererService; + return createModel({ + vs, + fs, + attributes: { + a_Position: createAttribute({ + buffer: createBuffer({ + data: triangulation.vertices, + type: gl.FLOAT, + }), + size: 2, + }), + }, + primitive: gl.TRIANGLES, + uniforms: { + ...uniforms, + }, + depth: { + enable: true, + }, + elements: createElements({ + data: triangulation.indices, + type: gl.UNSIGNED_INT, + count: triangulation.indices.length, }), - ]; + }); } }