From c6994d0cd31d80f9afb6621a0ef1fe769cb2fc9b Mon Sep 17 00:00:00 2001 From: shihui Date: Sat, 3 Dec 2022 16:58:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=AE=80=E5=8C=96=20shader=20=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../layers/src/point/models/fillShader.ts | 85 ++++--------------- packages/renderer/src/regl/ReglModel.ts | 17 +--- 2 files changed, 18 insertions(+), 84 deletions(-) diff --git a/packages/layers/src/point/models/fillShader.ts b/packages/layers/src/point/models/fillShader.ts index 10d49713fb..e8cd1b0adf 100644 --- a/packages/layers/src/point/models/fillShader.ts +++ b/packages/layers/src/point/models/fillShader.ts @@ -1,23 +1,13 @@ export const vert = ` -attribute vec3 a_Position; -attribute vec3 a_Extrude; - -uniform mat4 u_ModelMatrix; - - #define TILE_SIZE 512.0 #define PI 3.1415926536 #define WORLD_SCALE TILE_SIZE / (PI * 2.0) -#define COORDINATE_SYSTEM_LNGLAT 1.0 // mapbox -#define COORDINATE_SYSTEM_LNGLAT_OFFSET 2.0 // mapbox offset -#define COORDINATE_SYSTEM_VECTOR_TILE 3.0 -#define COORDINATE_SYSTEM_IDENTITY 4.0 #define COORDINATE_SYSTEM_P20 5.0 // amap #define COORDINATE_SYSTEM_P20_OFFSET 6.0 // amap offset -#define COORDINATE_SYSTEM_METER_OFFSET 7.0 -#define COORDINATE_SYSTEM_P20_2 8.0 // amap2.0 +attribute vec3 a_Position; +attribute vec3 a_Extrude; uniform mat4 u_ViewProjectionMatrix; @@ -35,10 +25,6 @@ uniform vec3 u_PixelsPerDegree; uniform vec3 u_PixelsPerDegree2; -uniform vec3 u_PixelsPerMeter; - - - vec2 project_mercator(vec2 lnglat) { float x = lnglat.x; return vec2( @@ -47,87 +33,48 @@ vec2 project_mercator(vec2 lnglat) { ); } -float project_scale(float meters) { - return meters * u_PixelsPerMeter.z; -} - - -// offset coords -> world coords +// offset coords -> world coords 偏移坐标转成世界坐标 vec4 project_offset(vec4 offset) { float dy = offset.y; dy = clamp(dy, -1., 1.); vec3 pixels_per_unit = u_PixelsPerDegree + u_PixelsPerDegree2 * dy; - return vec4(offset.xyz * pixels_per_unit, offset.w); + return vec4(offset.xy * pixels_per_unit.xy, 0.0, 1.0); } +// 投影方法 - 将经纬度转化为平面坐标(xy 平面) vec4 project_position(vec4 position) { - float a = COORDINATE_SYSTEM_LNGLAT_OFFSET; - float b = COORDINATE_SYSTEM_P20_OFFSET; - float c = COORDINATE_SYSTEM_LNGLAT; - if (u_CoordinateSystem == COORDINATE_SYSTEM_LNGLAT_OFFSET - || u_CoordinateSystem == COORDINATE_SYSTEM_P20_OFFSET) { + + if (u_CoordinateSystem == COORDINATE_SYSTEM_P20_OFFSET) { float X = position.x - u_ViewportCenter.x; float Y = position.y - u_ViewportCenter.y; - return project_offset(vec4(X, Y, position.z, position.w)); - } - if (u_CoordinateSystem < COORDINATE_SYSTEM_LNGLAT + 0.01 && u_CoordinateSystem >COORDINATE_SYSTEM_LNGLAT - 0.01) { - return vec4( - project_mercator(position.xy) * WORLD_SCALE * u_ZoomScale, - project_scale(position.z), - position.w - ); + return project_offset(vec4(X, Y, 0.0, 1.0)); } if (u_CoordinateSystem == COORDINATE_SYSTEM_P20) { return vec4( (project_mercator(position.xy) * WORLD_SCALE * u_ZoomScale - vec2(215440491., 106744817.)) * vec2(1., -1.), - project_scale(position.z), - position.w + 0.0, + 1.0 ); } return position; } vec2 project_pixel(vec2 pixel) { - if (u_CoordinateSystem == COORDINATE_SYSTEM_P20 || u_CoordinateSystem == COORDINATE_SYSTEM_P20_OFFSET) { - // P20 坐标系下,为了和 Web 墨卡托坐标系统一,zoom 默认减1 - return pixel * pow(2.0, (19.0 - u_Zoom)); - } - if(u_CoordinateSystem == COORDINATE_SYSTEM_P20_2) { - // P20_2 坐标系下,为了和 Web 墨卡托坐标系统一,zoom 默认减3 - return pixel * pow(2.0, (19.0 - 3.0 - u_Zoom)); - } - return pixel * -1.; + // P20 坐标系下,为了和 Web 墨卡托坐标系统一,zoom 默认减1 + return pixel * pow(2.0, (19.0 - u_Zoom)); } -vec4 project_common_position_to_clipspace(vec4 position, mat4 viewProjectionMatrix, vec4 center) { - if (u_CoordinateSystem == COORDINATE_SYSTEM_METER_OFFSET || - u_CoordinateSystem == COORDINATE_SYSTEM_LNGLAT_OFFSET) { - // Needs to be divided with project_uCommonUnitsPerMeter - position.w *= u_PixelsPerMeter.z; - } - - return viewProjectionMatrix * position + center; -} - -// Projects from common space coordinates to clip space -vec4 project_common_position_to_clipspace(vec4 position) { - return project_common_position_to_clipspace( - position, - u_ViewProjectionMatrix, - u_ViewportCenterProjection - ); -} - void main() { vec2 offset = a_Extrude.xy * 10.0; offset = project_pixel(offset); vec4 project_pos = project_position(vec4(a_Position.xy, 0.0, 1.0)); - - gl_Position = project_common_position_to_clipspace(vec4(project_pos.xy + offset, 0.0, 1.0)); + + vec4 worldPos = u_ViewProjectionMatrix * vec4(project_pos.xy + offset, 0.0, 1.0) + u_ViewportCenterProjection; + gl_Position = worldPos; } `; @@ -139,8 +86,6 @@ export const frag = ` #endif void main() { - gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); - } `; \ No newline at end of file diff --git a/packages/renderer/src/regl/ReglModel.ts b/packages/renderer/src/regl/ReglModel.ts index 9a3a9011ab..1f74ea85c4 100644 --- a/packages/renderer/src/regl/ReglModel.ts +++ b/packages/renderer/src/regl/ReglModel.ts @@ -1,7 +1,4 @@ import { - gl, - IAttribute, - IElements, IModel, IModelDrawOptions, IModelInitializationOptions, @@ -9,9 +6,6 @@ import { } from '@antv/l7-core'; import regl from 'l7regl'; import { isPlainObject, isTypedArray } from 'lodash'; -import { - primitiveMap, -} from './constants'; import ReglAttribute from './ReglAttribute'; import ReglElements from './ReglElements'; @@ -20,9 +14,7 @@ import ReglElements from './ReglElements'; */ export default class ReglModel implements IModel { private reGl: regl.Regl; - private drawCommand: regl.DrawCommand; - private drawParams: regl.DrawConfig; - private options: IModelInitializationOptions; + private drawCommand: regl.DrawCommand; private uniforms: { [key: string]: IUniform; } = {}; @@ -34,11 +26,10 @@ export default class ReglModel implements IModel { fs, attributes, uniforms, - primitive, elements, } = options; const reglUniforms: { [key: string]: IUniform } = {}; - this.options = options; + if (uniforms) { this.uniforms = this.extractUniforms(uniforms); @@ -59,9 +50,7 @@ export default class ReglModel implements IModel { frag: fs, uniforms: reglUniforms, vert: vs, - blend: {}, - primitive: - primitiveMap[primitive === undefined ? gl.TRIANGLES : primitive], + primitive: 'triangles' }; drawParams.elements = (elements as ReglElements).get();