@ohuoy/easymap 1.0.21 → 1.0.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,467 @@
1
+
2
+ import {hasProperty,transform} from '../../utils/util.js'
3
+ import mapboxgl from 'mapbox-gl';
4
+ import { getBBox } from '../../utils/util';
5
+ import { lnglatsToWorld ,normalizeVertices,flattenVectors,projectToWorld} from '../../utils/mapUtil';
6
+ export default class ScanWallLayer{
7
+ opacity = 0.8
8
+ scale = 2
9
+ limit = 6
10
+ colorFun = ()=>{
11
+ return [0,0,0,0]
12
+ }
13
+ addedFun = null
14
+ aPosition;
15
+ aColor;
16
+ vertices = [];
17
+ indices = [];
18
+ program;
19
+ constructor(id,config) {
20
+ this.id = id;
21
+ this.type = 'custom';
22
+ this.renderingMode = '2d';
23
+ this.opacity = Number(config.opacity)
24
+ if(config.colorFun){
25
+ this.colorFun = config.colorFun
26
+ }
27
+ this.addedFun = config.addedFun
28
+ }
29
+
30
+ createProgram(gl, vsSource, fsSource) {
31
+ this.program = gl.createProgram();
32
+ let program = this.program
33
+ const vertexShader = this.compileShader(gl, gl.VERTEX_SHADER, vsSource);
34
+ const fragmentShader = this.compileShader(gl, gl.FRAGMENT_SHADER, fsSource);
35
+
36
+ gl.attachShader(program, vertexShader);
37
+ gl.attachShader(program, fragmentShader);
38
+ gl.linkProgram(program);
39
+
40
+ if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
41
+ alert('程序链接失败:' + gl.getProgramInfoLog(program));
42
+ return null;
43
+ }
44
+ return program;
45
+ }
46
+
47
+ compileShader(gl, type, source) {
48
+ const shader = gl.createShader(type);
49
+ gl.shaderSource(shader, source);
50
+ gl.compileShader(shader);
51
+
52
+ if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
53
+ alert('着色器编译失败:' + gl.getShaderInfoLog(shader));
54
+ return null;
55
+ }
56
+ return shader;
57
+ }
58
+ onAdd(map, gl) {
59
+ // create GLSL source for vertex shader
60
+ const vsSource = `
61
+ uniform mat4 u_matrix;
62
+ attribute vec4 aPosition;
63
+ attribute vec4 aColor;
64
+ varying vec4 vColor;
65
+ void main() {
66
+ gl_Position = u_matrix * vec4(aPosition);
67
+ vColor = aColor;
68
+ }`;
69
+
70
+ // create GLSL source for fragment shader
71
+ const fsSource = `
72
+ precision mediump float;
73
+ varying vec4 vColor;
74
+ void main() {
75
+
76
+ gl_FragColor = vColor;
77
+ }`;
78
+
79
+ // create a vertex shader
80
+ // const vertexShader = gl.createShader(gl.VERTEX_SHADER);
81
+ // gl.shaderSource(vertexShader, vertexSource);
82
+ // gl.compileShader(vertexShader);
83
+
84
+ // // create a fragment shader
85
+ // const fragmentShader = gl.createShader(gl.FRAGMENT_SHADER);
86
+ // gl.shaderSource(fragmentShader, fragmentSource);
87
+ // gl.compileShader(fragmentShader);
88
+
89
+ // link the two shaders into a WebGL program
90
+ this.gl = gl;
91
+ this.program = this.createProgram(gl, vsSource, fsSource);
92
+ let program = this.program;
93
+ gl.useProgram(this.program );
94
+
95
+ this.map = map;
96
+ if(this.addedFun)
97
+ this.addedFun()
98
+ }
99
+
100
+
101
+ // createPoints(_pathPoints){
102
+ // // const pathPoints = [
103
+ // // { x: -2, z: -1, color: [1, 0, 0, 1] }, // 红色
104
+ // // { x: 0, z: 1, color: [0, 1, 0, 1] }, // 绿色
105
+ // // { x: 2, z: -1, color: [0, 0, 1, 1] }, // 蓝色
106
+ // // ];
107
+ // let vertices = [];
108
+ // let indices = [];
109
+ // for (const point of _pathPoints) {
110
+ // // 底部顶点 (x, 0, z)
111
+ // vertices.push(point.x,point.y, point.z, ...point.color);
112
+ // // 顶部顶点 (x, height, z)
113
+ // vertices.push(point.x, point.y, point.z , ...point.color);
114
+ // }
115
+
116
+
117
+ // for (let i = 0; i < _pathPoints.length - 1; i++) {
118
+ // const base = i * 2;
119
+ // indices.push(
120
+ // base, base + 1, base + 3, // 第一个三角形
121
+ // base, base + 3, base + 2 // 第二个三角形
122
+ // );
123
+ // }
124
+ // }
125
+
126
+ draw(){
127
+ let gl = this.gl;
128
+ this.createBuffer()
129
+ gl.enable(gl.DEPTH_TEST);
130
+ gl.enable(gl.BLEND);
131
+ gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
132
+ gl.clear(gl.DEPTH_BUFFER_BIT);
133
+ // gl.clearColor(0.2, 0.2, 0.2, 1.0);
134
+ // gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
135
+ gl.drawElements(gl.TRIANGLES, this.indices.length, gl.UNSIGNED_SHORT, 0);
136
+ }
137
+
138
+ createBuffer(){
139
+ let gl = this.gl;
140
+ let program = this.program;
141
+ // 创建缓冲区
142
+ const vertexBuffer = gl.createBuffer();
143
+ gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
144
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(this.vertices), gl.STATIC_DRAW);
145
+
146
+ const indexBuffer = gl.createBuffer();
147
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, indexBuffer);
148
+ gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(this.indices), gl.STATIC_DRAW);
149
+ const aPosition = gl.getAttribLocation(program, 'aPosition');
150
+ const aColor = gl.getAttribLocation(program, 'aColor');
151
+ this.aPosition = aPosition;
152
+ this.aColor = aColor;
153
+ const stride = 7 * 4; // 3(position) + 4(color)
154
+ gl.vertexAttribPointer(aPosition, 3, gl.FLOAT, false, stride, 0);
155
+ gl.enableVertexAttribArray(aPosition);
156
+ gl.vertexAttribPointer(aColor, 4, gl.FLOAT, false, stride, 12);
157
+ gl.enableVertexAttribArray(aColor);
158
+ }
159
+
160
+
161
+ drawWallPoints(dataList){
162
+ dataList = dataList.slice(0,373)
163
+
164
+ let indices =[
165
+ ]
166
+ let vertices = [
167
+ ]
168
+ let distance = dataList[0].distance;
169
+ let noDataLenth = distance[0] / distance[1]
170
+ let limitIndex = Math.floor(this.limit / distance[1])
171
+ limitIndex = limitIndex >= distance[2] -1 ? distance[2] -1 : limitIndex
172
+ let len = limitIndex + noDataLenth;
173
+ let index = 0;
174
+ let pointList = []
175
+ for(let i in dataList){
176
+ let bo = i < dataList.length -1;
177
+ if(bo){
178
+ let nextIndex = i *1 +1;
179
+ let item = dataList[i]
180
+ let lng = item.lng;
181
+ let lat = item.lat;
182
+ // let position2 = projectToWorld([lng,lat])
183
+ let position = mapboxgl.MercatorCoordinate.fromLngLat({lng:item.lng,lat:item.lat})
184
+ // debugger
185
+ let next = dataList[nextIndex]
186
+ if(lng == next.lng && lat == next.lat){
187
+ continue;
188
+ }else{
189
+ // let point = [position.x ,position.y,position.z+h ]
190
+ // let color = this.colorFun(item.data[j])
191
+ // pointList.push({
192
+ // point:point,
193
+ // color:[color[0]/255,color[1]/255,color[2]/255 ,color[3]]
194
+ // })
195
+ index = vertices.length / 7
196
+ for(let j in item.data){
197
+ let h = (j * 1 -1 +noDataLenth) * this.scale * 0.0000001
198
+ if(j > -noDataLenth && j <= limitIndex){
199
+ let point = [position.x ,position.y,position.z+h ]
200
+ let color = this.colorFun(item.data[j])
201
+ vertices.push(...point,color[0]/255,color[1]/255,color[2]/255 ,color[3])
202
+ // debugger
203
+ // debugger
204
+ // vertices.push(position.x ,position.y,position.z+2*h ,...color )
205
+ if(i < (dataList.length -2) && j<= (limitIndex -1)){
206
+ let a = index +1
207
+ let b = index
208
+ let c = index +len
209
+ let d = index +len +1
210
+ indices.push( a,b,d); // face one
211
+ indices.push( b, c, d );
212
+ index = index +1;
213
+ }
214
+ }
215
+ }
216
+ }
217
+ }
218
+ console.log('面:'+indices.length / 6)
219
+ console.log('点:'+vertices.length / 7)
220
+ console.log('长度:'+len)
221
+ // index = vertices.length / 7
222
+ }
223
+ this.indices = indices;
224
+ this.vertices = vertices;
225
+ // this.createWallBuffer(pointList,len)
226
+ this.createBuffer();
227
+ this.map.fitBounds(getBBox(dataList.map(a=>[a.lng,a.lat])), {
228
+ padding: {top: 45, bottom:45, left: 35, right: 35},
229
+ maxZoom:11,
230
+ pitch:30
231
+ });
232
+ }
233
+
234
+ createWallBuffer(_pathPoints,height){
235
+ let vertices = [],indices = [];
236
+ for (const point of _pathPoints) {
237
+ for(let i=0;i < height;i++){
238
+ vertices.push(...point.point, ...point.color);
239
+ }
240
+ }
241
+
242
+
243
+ for (let i = 0; i < _pathPoints.length - 1; i++) {
244
+ let base = height * i
245
+ for(let j=0;j < height -1;j++){
246
+ let a = base;
247
+ let b = base +1 ;
248
+ let c = base + height;
249
+ let d = base + height +1
250
+ indices.push(
251
+ b,d,a,
252
+ a,d,c
253
+ );
254
+ base = base +=1;
255
+ }
256
+ }
257
+ this.vertices = vertices;
258
+ this.indices = indices;
259
+ }
260
+
261
+ // method fired on each animation frame
262
+ // https://docs.mapbox.com/mapbox-gl-js/api/#map.event:render
263
+ render(gl, matrix) {
264
+ gl.useProgram(this.program);
265
+ gl.uniformMatrix4fv(
266
+ gl.getUniformLocation(this.program, 'u_matrix'),
267
+ false,
268
+ matrix
269
+ );
270
+ this.draw()
271
+ //gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
272
+ // gl.enableVertexAttribArray(this.aPos);
273
+ // gl.vertexAttribPointer(this.aPos, 3, gl.FLOAT, false, 0, 0);
274
+ // gl.enable(gl.BLEND);
275
+ // gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
276
+ //gl.drawArrays(gl.TRIANGLE_STRIP, 0, 12);
277
+
278
+ }
279
+
280
+ // //
281
+ // createBufferData(item){
282
+ // let height = 0.0000001
283
+ // let arrs = []
284
+ // //let colors = []
285
+ // for(let cr in item.coords){
286
+ // let _next = 1
287
+ // if(cr !=0 ){
288
+ // let p1 = mapboxgl.MercatorCoordinate.fromLngLat({
289
+ // lng: item.coords[cr-_next][0],
290
+ // lat: item.coords[cr-_next][1]
291
+ // }, 10)
292
+ // let p2 = mapboxgl.MercatorCoordinate.fromLngLat({
293
+ // lng: item.coords[cr][0],
294
+ // lat: item.coords[cr][1]
295
+ // }, 10)
296
+ // if(p1.x == p2.x && p1.y == p2.y && p1.z== p2.z){
297
+ // _next +=1
298
+ // }else{
299
+ // //创建矩形区域
300
+ // for(let _c in item.colors[cr]){
301
+ // let _height = height + height *Number(_c)
302
+ // //创建垂直矩形区域
303
+ // // let rect = [
304
+ // // p1.x,p1.y,p1.z,
305
+ // // p2.x,p2.y,p2.z,
306
+ // // p1.x,p1.y,_height,
307
+ // // p2.x,p2.y,_height,
308
+ // // ]
309
+ // let _colorinfo = item.colors[cr][_c]
310
+ // let _color = [_colorinfo[0]/255,_colorinfo[1]/255,_colorinfo[2]/255,_colorinfo[3]]
311
+ // arrs.push(p1.x)
312
+ // arrs.push(p1.y)
313
+ // arrs.push(p1.z)
314
+ // arrs.push(_color[0])
315
+ // arrs.push(_color[1])
316
+ // arrs.push(_color[2])
317
+ // arrs.push(_color[3])
318
+ // arrs.push(p2.x)
319
+ // arrs.push(p2.y)
320
+ // arrs.push(p2.z)
321
+ // arrs.push(_color[0])
322
+ // arrs.push(_color[1])
323
+ // arrs.push(_color[2])
324
+ // arrs.push(_color[3])
325
+ // arrs.push(p1.x)
326
+ // arrs.push(p1.y)
327
+ // arrs.push(_height)
328
+ // arrs.push(_color[0])
329
+ // arrs.push(_color[1])
330
+ // arrs.push(_color[2])
331
+ // arrs.push(_color[3])
332
+ // arrs.push(p2.x)
333
+ // arrs.push(p2.y)
334
+ // arrs.push(_height)
335
+ // arrs.push(_color[0])
336
+ // arrs.push(_color[1])
337
+ // arrs.push(_color[2])
338
+ // arrs.push(_color[3])
339
+ // }
340
+ // _next=1;
341
+ // }
342
+ // }
343
+ // }
344
+ // return arrs;
345
+ // }
346
+
347
+ // drawArrayItem(item){
348
+ // if(!item?.coords || !item.colors){
349
+ // return;
350
+ // }
351
+ // // if(!item?.text){
352
+ // // this.removeText(item)
353
+ // // }
354
+ // // if(item?.text?.type == 'layer'){
355
+ // // this.drawTextByLayer(item)
356
+ // // }
357
+ // // if(item?.text){
358
+ // // this.drawText(item)
359
+ // // }
360
+ // let gl = this.gl;
361
+ // if(!item?.buffer) {
362
+ // item.buffer = gl.createBuffer();
363
+ // let _data = this.createBufferData(item);
364
+ // gl.bindBuffer(gl.ARRAY_BUFFER, item.buffer);
365
+ // let bufferDataArray = new Float32Array(_data);
366
+ // item.bufferDataLength = _data.length;
367
+ // gl.bufferData(
368
+ // gl.ARRAY_BUFFER,
369
+ // bufferDataArray,
370
+ // gl.STATIC_DRAW
371
+ // );
372
+
373
+ // item.bufferColor = gl.createBuffer();
374
+ // gl.bindBuffer(gl.ARRAY_BUFFER, item.bufferColor);
375
+ // gl.bufferData(
376
+ // gl.ARRAY_BUFFER,
377
+ // bufferDataArray,
378
+ // gl.STATIC_DRAW
379
+ // );
380
+ // item.byteSize = bufferDataArray.BYTES_PER_ELEMENT
381
+ // item.aColor = gl.getAttribLocation(this.program, 'a_color');
382
+ // // 每帧都要绑定 VBO,并启用 vertexAttributes、设置 vertexAttributes 的参数
383
+ // //gl.bindBuffer(gl.ARRAY_BUFFER, item.buffer)
384
+ // // gl.enableVertexAttribArray(this.aPos)
385
+ // // gl.enableVertexAttribArray(this.aUv)
386
+ // // gl.vertexAttribPointer(this.aPos, 3, gl.FLOAT, false, 20, 0)
387
+ // // gl.vertexAttribPointer(this.aUv, 2, gl.FLOAT, false, 20, 12)
388
+ // }
389
+ // // item.buffer = gl.createBuffer()
390
+ // // let bufferData = this.createBufferData(item)
391
+ // // item.dataLength = bufferData.length
392
+ // // gl.bufferData(
393
+ // // gl.ARRAY_BUFFER,
394
+ // // new Float32Array([0.8250514363610296,
395
+ // // 0.41078121183273425,
396
+ // // 2.99947200815501e-7,
397
+ // // 0.8250521974715219,
398
+ // // 0.40078065150822717,
399
+ // // 2.999477853115788e-7,
400
+ // // 0.8250514363610296,
401
+ // // 0.41078121183273425,
402
+ // // 0.02,
403
+ // // 0.8250521974715219,
404
+ // // 0.40078065150822717,
405
+ // // 0.02]),
406
+ // // gl.STATIC_DRAW
407
+ // // )
408
+
409
+ // gl.bindBuffer(gl.ARRAY_BUFFER, item.buffer);
410
+ // gl.enableVertexAttribArray(this.aPos);
411
+ // gl.vertexAttribPointer(this.aPos, 3, gl.FLOAT, false, item.byteSize *7, 0);
412
+
413
+ // gl.bindBuffer(gl.ARRAY_BUFFER, item.bufferColor);
414
+ // gl.enableVertexAttribArray(item.aColor);
415
+ // gl.vertexAttribPointer(item.aColor, 4, gl.FLOAT, false, item.byteSize * 7, item.byteSize*3);
416
+
417
+ // gl.enable(gl.BLEND);
418
+ // gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
419
+ // gl.drawArrays(gl.TRIANGLE_STRIP, 0, item.bufferDataLength);
420
+ // }
421
+
422
+ // render(gl, matrix) {
423
+ // if(this.drawArray.length == 0 || !this.program) return;
424
+ // gl.useProgram(this.program)
425
+ // // 每帧都要传递 uniform
426
+ // gl.uniformMatrix4fv(
427
+ // gl.getUniformLocation(this.program, 'u_matrix'),
428
+ // false,
429
+ // matrix
430
+ // )
431
+ // //gl.uniform1f(gl.getUniformLocation(this.program, 'opa'),this.opacity)
432
+ // this.draw()
433
+
434
+ // // gl.useProgram(this.program)
435
+ // // // 每帧都要传递 uniform
436
+ // // gl.uniformMatrix4fv(
437
+ // // gl.getUniformLocation(this.program, 'u_matrix'),
438
+ // // false,
439
+ // // matrix
440
+ // // )
441
+ // // // 每帧都要绑定纹理参数
442
+ // // gl.bindTexture(gl.TEXTURE_2D, this.texture)
443
+ // // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
444
+ // // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
445
+ // // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
446
+ // // gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
447
+
448
+ // // // 每帧都要绑定 VBO,并启用 vertexAttributes、设置 vertexAttributes 的参数
449
+ // // gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer)
450
+
451
+
452
+ // // 如果你用不着透明度,可以不执行这两行
453
+ // // gl.enable(gl.BLEND)
454
+ // // gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
455
+
456
+ // // 触发绘制
457
+ // //gl.drawArrays(gl.TRIANGLES, 0, 6)
458
+ // // 每帧都要指定用哪个着色器程序
459
+ // if(this.renderBack!=null) {
460
+ // if(this.renderBack) this.renderBack()
461
+ // }
462
+ // //this.map.triggerRepaint();
463
+ // }
464
+ }
465
+
466
+
467
+