@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,517 @@
1
+ import * as THREE from 'three'
2
+ import { getBBox } from '../../utils/util';
3
+ import {Threebox} from '../../../lib/threebox-plugin';
4
+ import { lnglatsToWorld ,normalizeVertices,flattenVectors,projectToWorld} from '../../utils/mapUtil';
5
+ import CameraSync from '../../utils/CameraSync';
6
+ const WORLD_SIZE = 1024000;
7
+ // var this.
8
+ // let this.scene;
9
+ let cameraTransform;
10
+ // var this.group = new THREE.Group();
11
+ export default class ThreeWallLayer{
12
+ tb = {};
13
+ group = new THREE.Group();
14
+ scene;
15
+ opacity = 0.8
16
+ scale = 2
17
+ limit = 6
18
+ colorFun = ()=>{
19
+ return [0,0,0,0]
20
+ }
21
+ addedFun = null
22
+ constructor(id,config) {
23
+ this.id = id;
24
+ this.opacity = Number(config.opacity)
25
+ if(config.colorFun){
26
+ this.colorFun = config.colorFun
27
+ }
28
+ this.type = 'custom';
29
+ this.renderingMode = '2d';
30
+ this.addedFun = config.addedFun
31
+ }
32
+
33
+ drawWallPoints(){
34
+ // this.removeObj(this.group);
35
+ debugger
36
+ // let geometry = new THREE.BufferGeometry();
37
+ // let indices =[[
38
+ // 1,
39
+ // 0,
40
+ // 3,
41
+ // 0,
42
+ // 2,
43
+ // 3
44
+ // ]]
45
+ // let vertices = [
46
+ // -332702,
47
+ // -1016287,
48
+ // 2,
49
+ // -332702,
50
+ // -1016287,
51
+ // 4000,
52
+ // -542702,
53
+ // -1016287,
54
+ // 2,
55
+ // -542702,
56
+ // -1016287,
57
+ // 4000,
58
+ // ]
59
+ // let normals = [1,0,0];
60
+ // let colors = []
61
+ // const _color = new THREE.Color();
62
+ // _color.setRGB( 0/255, 133/255, 205/255, THREE.SRGBColorSpace );
63
+ // colors.push( _color.r, _color.g, _color.b );
64
+ // colors.push( _color.r, _color.g, _color.b );
65
+ // geometry.setIndex( indices );
66
+ // geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
67
+ // // geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
68
+ // geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3) );
69
+ // // geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
70
+ // geometry.computeBoundingSphere();
71
+ // geometry.computeVertexNormals();
72
+ // let material = new THREE.MeshBasicMaterial( {
73
+ // side: THREE.DoubleSide,
74
+ // // wireframe:true,
75
+ // transparent:true,
76
+ // // opacity:this.opacity,
77
+ // opacity:1,
78
+ // vertexColors: true,
79
+ // } );
80
+
81
+ // material.color.convertSRGBToLinear();
82
+ // let mesh = new THREE.Mesh( geometry, material );
83
+ // mesh.renderOrder = 1;
84
+ // this.group.add( mesh );
85
+ const triangles = 12000;
86
+ const geometry = new THREE.BufferGeometry();
87
+ const normals = [];
88
+ const positions = [];
89
+ const colors = [];
90
+ const color = new THREE.Color();
91
+
92
+ const pA = new THREE.Vector3();
93
+ const pB = new THREE.Vector3();
94
+ const pC = new THREE.Vector3();
95
+
96
+ const cb = new THREE.Vector3();
97
+ const ab = new THREE.Vector3();
98
+
99
+ const n = 800, n2 = n / 2;
100
+ const d = 120, d2 = d / 2;
101
+
102
+ for (let i = 0; i < triangles; i++) {
103
+
104
+ const x = -332702 + Math.random() * n - n2;
105
+ const y = -1016287 + Math.random() * n - n2;
106
+ const z = Math.random() * n - n2;
107
+
108
+ const ax = x + Math.random() * d - d2;
109
+ const ay = y + Math.random() * d - d2;
110
+ const az = z + Math.random() * d - d2;
111
+
112
+ const bx = x + Math.random() * d - d2;
113
+ const by = y + Math.random() * d - d2;
114
+ const bz = z + Math.random() * d - d2;
115
+
116
+ const cx = x + Math.random() * d - d2;
117
+ const cy = y + Math.random() * d - d2;
118
+ const cz = z + Math.random() * d - d2;
119
+
120
+ positions.push(ax, ay, az);
121
+ positions.push(bx, by, bz);
122
+ positions.push(cx, cy, cz);
123
+
124
+ pA.set(ax, ay, az);
125
+ pB.set(bx, by, bz);
126
+ pC.set(cx, cy, cz);
127
+
128
+ cb.subVectors(pC, pB);
129
+ ab.subVectors(pA, pB);
130
+ cb.cross(ab);
131
+
132
+ cb.normalize();
133
+
134
+
135
+ const nx = cb.x;
136
+ const ny = cb.y;
137
+ const nz = cb.z;
138
+
139
+ normals.push(nx, ny, nz);
140
+ normals.push(nx, ny, nz);
141
+ normals.push(nx, ny, nz);
142
+
143
+ color.setRGB(Math.random(), Math.random(), Math.random());
144
+
145
+ const alpha = Math.random();
146
+
147
+ colors.push(color.r, color.g, color.b, alpha);
148
+ colors.push(color.r, color.g, color.b, alpha);
149
+ colors.push(color.r, color.g, color.b, alpha);
150
+
151
+ }
152
+
153
+ geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
154
+ geometry.setAttribute('normal', new THREE.Float32BufferAttribute(normals, 3));
155
+ geometry.setAttribute('color', new THREE.Float32BufferAttribute(colors, 3));
156
+ geometry.computeBoundingSphere();
157
+
158
+ const material = new THREE.MeshPhongMaterial({
159
+ color: 0xFFFFFF,
160
+ vertexColors: true,
161
+ side: THREE.DoubleSide,
162
+ });
163
+
164
+ let mesh = new THREE.Mesh(geometry, material);
165
+ this.scene.add(mesh);
166
+ // for(let i in dataList){
167
+ // i = Number(i)
168
+ // let item = dataList[i]
169
+ // let lng = item.lng;
170
+ // let lat = item.lat;
171
+ // let position = projectToWorld([lng,lat])
172
+ // if(i < dataList.length -1){
173
+ // let next = dataList[i+1]
174
+ // if(lng == next.lng && lat == next.lat){
175
+ // continue;
176
+ // }
177
+ // }
178
+ // for(let j in item.data){
179
+ // j = Number(j)
180
+ // let indexJ = j+noDataLenth-1;
181
+ // if(j > -noDataLenth && j <= limitIndex){
182
+
183
+ // }
184
+ // }
185
+ // }
186
+ }
187
+
188
+ removeObj = (obj) => {
189
+ const clearCache = (item) => {
190
+ item.geometry.dispose();
191
+ item.material.dispose();
192
+ };
193
+ let arr = obj.children.filter((x) =>!! x);
194
+ arr.forEach((item) => {
195
+ if (item.children.length) {
196
+ removeObj(item);
197
+ } else {
198
+ clearCache(item);
199
+ item.clear();
200
+ }
201
+ });
202
+ obj.clear();
203
+ arr = null;
204
+ };
205
+
206
+ drawWall(dataList){
207
+ this.removeObj(this.group);
208
+ let geometry = new THREE.BufferGeometry();
209
+ let indices =[]
210
+ let vertices = []
211
+ let normals = [];
212
+ let colors = []
213
+ let uvs = [];
214
+ let distance = dataList[0].distance;
215
+ let noDataLenth = distance[0] / distance[1]
216
+ let limitIndex = this.limit / distance[1]
217
+ limitIndex = limitIndex >= distance[2] -1 ? distance[2] -1 : limitIndex
218
+ // dataList = dataList.slice(0,5)
219
+ let len = limitIndex + noDataLenth;
220
+ let index = 0;
221
+ const _color = new THREE.Color();
222
+ for(let i in dataList){
223
+ i = Number(i)
224
+ let item = dataList[i]
225
+ let lng = item.lng;
226
+ let lat = item.lat;
227
+ let position = projectToWorld([lng,lat])
228
+ if(i < dataList.length -1){
229
+ let next = dataList[i+1]
230
+ if(lng == next.lng && lat == next.lat){
231
+ continue;
232
+ }
233
+ }
234
+
235
+ for(let j in item.data){
236
+ j = Number(j)
237
+ let indexJ = j+noDataLenth-1;
238
+ if(j > -noDataLenth && j <= limitIndex){
239
+ //let position = mapboxgl.MercatorCoordinate.fromLngLat({lng: lng, lat: lat}, (j+noDataLenth) * this.scale);
240
+ //(Math.floor(position.x * 1000000 )/10000,(j+noDataLenth) * this.scale ,Math.floor(position.y * 1000000)/10000)
241
+ vertices.push(position.x ,position.y,position.z+(j+noDataLenth) * this.scale )
242
+ // uvs.push(j)
243
+ // uvs.push(j-0.5)
244
+ // // debugger
245
+ // normals.push( 1, 1, 1);
246
+ let color = this.colorFun(item.data[j])
247
+ _color.setRGB( color[0]/255, color[1]/255, color[2]/255, THREE.SRGBColorSpace );
248
+ colors.push( _color.r, _color.g, _color.b );
249
+ if(i < (dataList.length -2) && j< (limitIndex -1)){
250
+ let cb = new THREE.Vector3();//方向向量
251
+ let ab = new THREE.Vector3();
252
+ let a = index +1
253
+ let b = index
254
+ let c = index +len
255
+ let d = index +len +1
256
+ let pA = new THREE.Vector3(vertices[a],vertices[a+1],vertices[a+2]);
257
+ let pB = new THREE.Vector3(vertices[b],vertices[b+1],vertices[b+2]);
258
+ let pC = new THREE.Vector3(vertices[c],vertices[c+1],vertices[c+2]);
259
+ let pD = new THREE.Vector3(vertices[d],vertices[d+1],vertices[d+2]);
260
+ cb.subVectors(pC,pB)
261
+ ab.subVectors(pA,pB)
262
+ cb.cross(ab)
263
+ cb.normalize();
264
+ normals.push(cb.x,cb.y,cb.z)
265
+ normals.push(cb.x,cb.y,cb.z)
266
+ indices.push( a,b,d); // face one
267
+ indices.push( b, c, d ); // face two
268
+ index = index +1;
269
+ }
270
+ }
271
+ }
272
+ index = vertices.length / 3
273
+ }
274
+ geometry.setIndex( indices );
275
+ geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
276
+ geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
277
+ geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3) );
278
+ // geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
279
+ geometry.computeBoundingSphere();
280
+ geometry.computeVertexNormals();
281
+ let material = new THREE.MeshBasicMaterial( {
282
+ side: THREE.DoubleSide,
283
+ // wireframe:true,
284
+ color: 0xFFFFFF,
285
+ // forceSinglePass:false,
286
+ // depthWrite:false,
287
+ // depthTest:false,
288
+ //transparent:true,
289
+ // opacity:this.opacity,
290
+ // opacity:1,
291
+ // shadowSide: THREE.DoubleSide,
292
+ // depthFunc:THREE.LessDepth,
293
+ // depthTest:false,
294
+ // depthWrite:false,
295
+ vertexColors: true,
296
+ // forceSinglePass:true
297
+ } );
298
+
299
+ // material.needsUpdate = true;
300
+ material.color.convertSRGBToLinear();
301
+ let mesh = new THREE.Mesh( geometry, material );
302
+ mesh.renderOrder = 1;
303
+ // mesh = this.tb.Object3D({ obj: mesh, units: 'meters',anchor:'center'});
304
+ // this.tb.add(mesh);
305
+ this.group.add( mesh );
306
+ this.map.fitBounds(getBBox(dataList.map(a=>[a.lng,a.lat])), {
307
+ padding: {top: 45, bottom:45, left: 35, right: 35},
308
+ maxZoom:11,
309
+ pitch:30
310
+ });
311
+
312
+ }
313
+
314
+ drawWallTest(dataList){
315
+ let geometry = new THREE.BufferGeometry();
316
+ let indices =[]
317
+ let vertices = []
318
+ let normals = [];
319
+ let colors = []
320
+ let size = 20;
321
+ let segments = 10;
322
+
323
+ let halfSize = size / 2;
324
+ let segmentSize = size / segments;
325
+
326
+ let _color = new THREE.Color();
327
+
328
+ // generate vertices, normals and color data for a simple grid geometry
329
+
330
+ for ( let i = 0; i <= segments; i ++ ) {
331
+
332
+ let y = ( i * segmentSize ) - halfSize;
333
+
334
+ for ( let j = 0; j <= segments; j ++ ) {
335
+
336
+ let x = ( j * segmentSize ) - halfSize;
337
+
338
+ vertices.push( x, - y, 0 );
339
+ normals.push( 0, 0, 1 );
340
+
341
+ let r = ( x / size ) + 0.5;
342
+ let g = ( y / size ) + 0.5;
343
+
344
+ _color.setRGB( r, g, 1, THREE.SRGBColorSpace );
345
+
346
+ colors.push( _color.r, _color.g, _color.b );
347
+
348
+ }
349
+
350
+ }
351
+
352
+ // generate indices (data for element array buffer)
353
+
354
+ for ( let i = 0; i < segments; i ++ ) {
355
+
356
+ for ( let j = 0; j < segments; j ++ ) {
357
+
358
+ let a = i * ( segments + 1 ) + ( j + 1 );
359
+ let b = i * ( segments + 1 ) + j;
360
+ let c = ( i + 1 ) * ( segments + 1 ) + j;
361
+ let d = ( i + 1 ) * ( segments + 1 ) + ( j + 1 );
362
+
363
+ // generate two faces (triangles) per iteration
364
+
365
+ indices.push( a, b, d ); // face one
366
+ indices.push( b, c, d ); // face two
367
+
368
+ }
369
+
370
+ }
371
+
372
+ //
373
+
374
+ geometry.setIndex( indices );
375
+ geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
376
+ geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
377
+ geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
378
+
379
+ let material = new THREE.MeshBasicMaterial( {
380
+ // side: THREE.DoubleSide,
381
+ vertexColors: true
382
+ } );
383
+
384
+ let mesh = new THREE.Mesh( geometry, material );
385
+ this.scene.add( mesh );
386
+ // mesh = this.tb.Object3D({ obj: mesh, units: 'meters',anchor:'center'});
387
+ // this.tb.add(mesh);
388
+ // for(let line of lines){
389
+ // this.createLine(line.coords,`rgba(${line.color.join(',')})`)
390
+ // }
391
+ }
392
+
393
+ createLine(path,color){
394
+ let line = this.tb.line({
395
+ width:this.width,
396
+ opacity:this.opacity,
397
+ color:color,
398
+ geometry:path
399
+ })
400
+ this.tb.add(line)
401
+ }
402
+
403
+ onAdd(map, gl) {
404
+ this.map = map;
405
+ let container = map.getCanvas();
406
+ const w = container.clientWidth;
407
+ const h = container.clientHeight;
408
+ this.camera = new THREE.PerspectiveCamera(map.transform.fov, w / h, 0.1, 1);
409
+ this.scene = new THREE.Scene();
410
+ this.scene.add(this.group)
411
+ new CameraSync(this.map, this.camera, this.group);
412
+ // let centerlngLat = map.getCenter()
413
+ // let center = mapboxgl.MercatorCoordinate.fromLngLat(centerlngLat)
414
+ // const scale = center.meterInMercatorCoordinateUnits();
415
+ // //this.scale = scale;
416
+ // let {x,y,z} = center;
417
+ // const modelRotate = [Math.PI / 2, 0, 0];
418
+
419
+
420
+ // cameraTransform = new THREE.Matrix4()
421
+ // .makeTranslation(
422
+ // x,
423
+ // y,
424
+ // z
425
+ // )
426
+ // .scale(
427
+ // new THREE.Vector3(
428
+ // scale,
429
+ // -scale,
430
+ // scale
431
+ // )
432
+ // )
433
+ // .multiply( new THREE.Matrix4().makeRotationAxis(
434
+ // new THREE.Vector3(1, 0, 0),
435
+ // modelRotate[0]
436
+ // ))
437
+ // .multiply(new THREE.Matrix4().makeRotationAxis(
438
+ // new THREE.Vector3(0, 1, 0),
439
+ // modelRotate[1]
440
+ // ))
441
+ // .multiply(new THREE.Matrix4().makeRotationAxis(
442
+ // new THREE.Vector3(0, 0, 1),
443
+ // modelRotate[2]
444
+ // ));
445
+ // create two three.js lights to illuminate the model
446
+ // let directionalLight = new THREE.DirectionalLight(0xffffff);
447
+ // directionalLight.position.set(0, -70, 100).normalize();
448
+ // this.scene.add(directionalLight);
449
+
450
+ // let directionalLight2 = new THREE.DirectionalLight(0xffffff);
451
+ // directionalLight2.position.set(0, 70, 100).normalize();
452
+ // this.scene.add(directionalLight2);
453
+ // this.tb = new Threebox(
454
+ // map,
455
+ // gl, //get the context from Mapbox
456
+ // { defaultLights: true ,passiveRendering:false}
457
+ // );
458
+ this.scene.add(new THREE.AxesHelper(1000));
459
+ // this.scene.add(new THREE.CameraHelper( this.camera ));
460
+ // var ambient = new THREE.AmbientLight(0x444444);
461
+ // this.scene.add(ambient);
462
+ this.renderer = new THREE.WebGLRenderer({
463
+ canvas: map.getCanvas(),
464
+ context: gl,
465
+ logarithmicDepthBuffer: true,
466
+ antialias: true
467
+ });
468
+ this.renderer.outputEncoding = THREE.sRGBEncoding;
469
+
470
+ this.renderer.autoClear = false;
471
+ if(this.addedFun)
472
+ this.addedFun()
473
+ }
474
+
475
+ render(gl, matrix) {
476
+ //this.tb.update();
477
+ // const rotationX = new THREE.Matrix4().makeRotationAxis(
478
+ // new THREE.Vector3(1, 0, 0),
479
+ // modelTransform.rotateX
480
+ // );
481
+ // const rotationY = new THREE.Matrix4().makeRotationAxis(
482
+ // new THREE.Vector3(0, 1, 0),
483
+ // modelTransform.rotateY
484
+ // );
485
+ // const rotationZ = new THREE.Matrix4().makeRotationAxis(
486
+ // new THREE.Vector3(0, 0, 1),
487
+ // modelTransform.rotateZ
488
+ // );
489
+
490
+ // const m = new THREE.Matrix4().fromArray(matrix);
491
+ // const l = new THREE.Matrix4()
492
+ // .makeTranslation(
493
+ // modelTransform.translateX,
494
+ // modelTransform.translateY,
495
+ // modelTransform.translateZ
496
+ // )
497
+ // .scale(
498
+ // new THREE.Vector3(
499
+ // modelTransform.scale,
500
+ // -modelTransform.scale,
501
+ // modelTransform.scale
502
+ // )
503
+ // )
504
+ // .multiply(rotationX)
505
+ // .multiply(rotationY)
506
+ // .multiply(rotationZ);
507
+
508
+ //this.camera.projectionMatrix = m.multiply(l);
509
+ // var m = new THREE.Matrix4().fromArray(matrix);
510
+ // this.camera.projectionMatrix = m.multiply(cameraTransform);;
511
+ // this.renderer.resetState();
512
+
513
+ this.renderer.render(this.scene, this.camera);
514
+ this.map.triggerRepaint();
515
+ }
516
+
517
+ }