@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.
@@ -4,11 +4,14 @@ import {Threebox} from '../../../lib/threebox-plugin';
4
4
  import { lnglatsToWorld ,normalizeVertices,flattenVectors,projectToWorld} from '../../utils/mapUtil';
5
5
  import CameraSync from '../../utils/CameraSync';
6
6
  const WORLD_SIZE = 1024000;
7
- var tb = {};
8
- let scene;
7
+ // var this.
8
+ // let this.scene;
9
9
  let cameraTransform;
10
- var group = new THREE.Group();
10
+ // var this.group = new THREE.Group();
11
11
  export default class ThreeWallLayer{
12
+ tb = {};
13
+ group = new THREE.Group();
14
+ scene;
12
15
  opacity = 0.8
13
16
  scale = 2
14
17
  limit = 6
@@ -27,301 +30,269 @@ export default class ThreeWallLayer{
27
30
  this.addedFun = config.addedFun
28
31
  }
29
32
 
30
-
31
- drawWall(dataList){
32
- let geometry = new THREE.BufferGeometry();
33
+
34
+ drawWall(dataList,offx = 0,offy = 0,r = false){
35
+ this.removeObj(this.group);
36
+ let geometry = new THREE.BufferGeometry();
37
+ // let geometryList = []
33
38
  let indices =[]
34
- let vertices = [
35
- // 82.71083,
36
- // 2,
37
- // 40.68735,
38
-
39
- // 82.71083,
40
- // 4,
41
- // 40.68735,
42
-
43
- // 82.71083,
44
- // 6,
45
- // 40.68735,
46
-
47
- // 82.42999,
48
- // 2,
49
- // 40.58893,
50
-
51
- // 82.42999,
52
- // 4,
53
- // 40.58893,
54
-
55
- // 82.42999,
56
- // 6,
57
- // 40.58893,
58
-
59
- // 82.3689,
60
- // 2,
61
- // 40.79668,
62
-
63
- // 82.3689,
64
- // 4,
65
- // 40.79668,
66
-
67
- // 82.3689,
68
- // 6,
69
- // 40.79668,
70
-
71
- // 82.30361,
72
- // 2,
73
- // 40.76822,
74
-
75
- // 82.30361,
76
- // 4,
77
- // 40.76822,
78
-
79
- // 82.30361,
80
- // 6,
81
- // 40.76822,
82
-
83
- // 82.15195,
84
- // 2,
85
- // 40.261634,
86
-
87
- // 82.15195,
88
- // 4,
89
- // 40.261634,
90
-
91
- // 82.15195,
92
- // 6,
93
- // 40.261634
94
- ]
39
+ let vertices = []
95
40
  let normals = [];
96
41
  let colors = []
42
+ let uvs = [];
97
43
  let distance = dataList[0].distance;
98
44
  let noDataLenth = distance[0] / distance[1]
99
- let limitIndex = this.limit / distance[1]
45
+ let limitIndex = Math.floor(this.limit / distance[1])
100
46
  limitIndex = limitIndex >= distance[2] -1 ? distance[2] -1 : limitIndex
101
- dataList = dataList
47
+ // dataList = dataList.slice(0,5)
102
48
  let len = limitIndex + noDataLenth;
103
49
  let index = 0;
104
50
  const _color = new THREE.Color();
51
+ let positionList = [];
52
+
105
53
  for(let i in dataList){
106
- i = Number(i)
107
- let item = dataList[i]
54
+ let _i = Number(i);
55
+ let bo = i < dataList.length -1;
56
+ let nextIndex = _i +1;
57
+ let item = dataList[_i]
108
58
  let lng = item.lng;
109
59
  let lat = item.lat;
110
- let position = projectToWorld([lng,lat])
111
- if(i < dataList.length -1){
112
- let next = dataList[i+1]
60
+ let position = projectToWorld([lng,lat])
61
+ // let position = mapboxgl.MercatorCoordinate.fromLngLat({lng:item.lng,lat:item.lat})
62
+ if(bo){
63
+ let next = dataList[nextIndex]
113
64
  if(lng == next.lng && lat == next.lat){
114
65
  continue;
115
66
  }
116
- }
117
-
118
- for(let j in item.data){
119
- j = Number(j)
120
- let indexJ = j+noDataLenth-1;
121
- if(j > -noDataLenth && j <= limitIndex){
122
- //let position = mapboxgl.MercatorCoordinate.fromLngLat({lng: lng, lat: lat}, (j+noDataLenth) * this.scale);
123
-
124
- //(Math.floor(position.x * 1000000 )/10000,(j+noDataLenth) * this.scale ,Math.floor(position.y * 1000000)/10000)
125
- vertices.push(position.x ,position.y,position.z+(j+noDataLenth) * this.scale )
126
- // debugger
127
- normals.push( 0, 1, 0);
128
- let color = this.colorFun(item.data[j])
129
- _color.setRGB( color[0]/255, color[1]/255, color[2]/255, THREE.SRGBColorSpace );
130
- colors.push( _color.r, _color.g, _color.b ,color[3]);
131
- if(i < (dataList.length -2) && j< (limitIndex)){
132
- let a = index +1
133
- let b = index
134
- let c = index +len
135
- let d = index +len +1
136
- indices.push( a, b, d ); // face one
137
- indices.push( b, c, d ); // face two
138
- index = index +1;
139
- }
140
- }
141
- }
142
- index = vertices.length / 3
67
+ let nposition = projectToWorld([next.lng,next.lat])
68
+ let x = position.x,y = position.y,z= position.z;
69
+
70
+ let nx = nposition.x ,ny =nposition.y,nz = position.z;
71
+ let c,_n;
72
+ positionList.push(position)
73
+
74
+ let p1 = new THREE.Vector3(x,y,z+this.scale )
75
+ let p2 = new THREE.Vector3(x,y,z+2*this.scale)
76
+ let p3 = new THREE.Vector3(nx,ny,nz+2*this.scale)
77
+ const a = p2.clone().sub(p1);
78
+ const b = p2.clone().sub(p3);
79
+ c = a.clone().cross(b) ;
80
+ _n= c.normalize();
81
+ // normals.push(_n.x,_n.y,_n.z)
82
+ // let arrowHelper = new THREE.ArrowHelper( new THREE.Vector3(_n.x, _n.y, _n.z), new THREE.Vector3( 0, 0, 0 ), 100, 0xffff00 );
83
+ // this.scene.add(arrowHelper)
84
+ // console.log(_n)
85
+ for(let j in item.data){
86
+ let h = (j * 1 -1 +noDataLenth) * this.scale
87
+ j = Number(j)
88
+ if(j > -noDataLenth && j <= limitIndex){
89
+ //let position = mapboxgl.MercatorCoordinate.fromLngLat({lng: lng, lat: lat}, (j+noDataLenth) * this.scale);
90
+ //(Math.floor(position.x * 1000000 )/10000,(j+noDataLenth) * this.scale ,Math.floor(position.y * 1000000)/10000)
91
+ let point = [position.x + offx, position.y +offy ,position.z+h]
92
+ this.point = point
93
+ //正面
94
+ vertices.push(...point)
95
+
96
+ //设置法线
97
+ normals.push(_n.x,_n.z,_n.y)
98
+ uvs.push(j/limitIndex);
99
+ uvs.push(1-_i/dataList.length-1);
100
+
101
+ // uvs.push(j)
102
+ // uvs.push(j-0.5)
103
+ // // debugger
104
+ // normals.push( 1, 1, 1);
105
+ let color = this.colorFun(item.data[j])
106
+ _color.setRGB( color[0]/255, color[1]/255, color[2]/255, THREE.SRGBColorSpace );
107
+ colors.push( _color.r, _color.g, _color.b);
108
+ if(i < (dataList.length -2) && j< (limitIndex -1)){
109
+ let a = index
110
+ let b = index +1
111
+ let c = index +len
112
+ let d = index +len +1
113
+ // if(_n.x < 0 && _n.y > 0){
114
+
115
+ // // indices.push(a, b, c);
116
+ // // indices.push(c, b,d);
117
+ // }
118
+ // else if(_n.x > 0 && _n.y > 0){
119
+ // // indices.push( b, c,a);
120
+ // // indices.push( b,d,c);
121
+ // indices.push(c, a, b);
122
+ // indices.push( d,c,b);
123
+ // }
124
+ // else{
125
+ // // indices.push(a, c, b);
126
+ // // indices.push(c, d,b);
127
+
128
+ // }
129
+ //正
130
+ if(r){
131
+ indices.push( b,d,a); // face one
132
+ indices.push( a, d, c );
133
+ }else{
134
+ indices.push( b,a,d); // face one
135
+ indices.push( a, c, d );
136
+ }
137
+ // indices.push( b,d,a); // face one
138
+ // indices.push( a, d, c );
139
+
140
+ // geometry.addGroup(b,6,0)
141
+
142
+ }
143
+ index = index +1;
144
+ }
145
+ }
146
+ }
147
+ index = vertices.length / 3
143
148
  }
149
+
150
+
144
151
  geometry.setIndex( indices );
145
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
146
- geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
147
- geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 4) );
152
+ geometry.setAttribute( 'position', new THREE.BufferAttribute(new Float32Array(vertices), 3));
153
+ // geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
154
+ geometry.setAttribute( 'color', new THREE.BufferAttribute(new Float32Array(colors), 3) );
155
+
156
+ geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
157
+ geometry.computeBoundingSphere();
158
+ geometry.computeVertexNormals();
148
159
  let material = new THREE.MeshBasicMaterial( {
160
+ vertexColors: true,
149
161
  side: THREE.DoubleSide,
150
- transparent:true,
151
- vertexColors: true
162
+ transparent: true,
163
+ opacity: this.opacity,
164
+ depthWrite: false, // 关闭深度写入,避免透明物体干扰
165
+ depthTest: true,
166
+ // polygonOffset: true, // 启用深度偏移
167
+ // polygonOffsetFactor: 1, // 偏移系数,正值将物体"推近"相机
168
+ // polygonOffsetUnits: 1
152
169
  } );
153
- //material.color.convertSRGBToLinear();
170
+
171
+ // const material = new THREE.MeshPhongMaterial({
172
+ // vertexColors: true,
173
+ // side: THREE.DoubleSide,
174
+ // transparent: true,
175
+ // opacity: this.opacity,
176
+ // depthWrite: true,
177
+ // depthTest: true,
178
+ // polygonOffset: true, // 启用深度偏移
179
+ // polygonOffsetFactor: 1, // 偏移系数,正值将物体"推近"相机
180
+ // polygonOffsetUnits: 1
181
+ // });
182
+
183
+ // const shader = {
184
+ // vertexShader: `
185
+ // ${THREE.ShaderChunk.common}
186
+ // ${THREE.ShaderChunk.logdepthbuf_pars_vertex}
187
+ // attribute vec3 color;
188
+ // varying vec3 v_color;
189
+ // void main() {
190
+ // gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
191
+ // v_color = color;
192
+ // ${THREE.ShaderChunk.logdepthbuf_vertex}
193
+ // }
194
+ // `,
195
+ // fragmentShader: `
196
+ // ${THREE.ShaderChunk.logdepthbuf_pars_fragment}
197
+ // varying vec3 v_color;
198
+ // void main() {
199
+ // float depth = gl_FragCoord.z;
200
+ // gl_FragColor = vec4(vec3(depth), 1.0);
201
+
202
+
203
+ // }
204
+ // `,
205
+ // };
206
+ // gl_FragDepth = 1.0 /gl_FragCoord.y;
207
+ // gl_FragColor = vec4(v_color,1);
208
+ // #gl_FragDepth = gl_FragCoord.x * -1.0;
209
+ // const material = new THREE.ShaderMaterial({
210
+ // ...shader,
211
+
212
+ // depthTest: true,
213
+ // depthWrite:true,
214
+ // transparent: true,
215
+ // side:THREE.DoubleSide,
216
+ // });
217
+
218
+ // material.needsUpdate = true;
219
+ // material.color.convertSRGBToLinear();
154
220
  let mesh = new THREE.Mesh( geometry, material );
155
- // mesh = tb.Object3D({ obj: mesh, units: 'meters',anchor:'center'});
156
- // tb.add(mesh);
157
- group.add( mesh );
221
+ this.group.add( mesh );
222
+ // 定义路径点和墙面参数
158
223
  this.map.fitBounds(getBBox(dataList.map(a=>[a.lng,a.lat])), {
159
224
  padding: {top: 45, bottom:45, left: 35, right: 35},
160
225
  maxZoom:11,
161
226
  pitch:30
162
- });
163
-
164
- }
165
-
166
- drawWallTest(dataList){
167
- let geometry = new THREE.BufferGeometry();
168
- let indices =[]
169
- let vertices = []
170
- let normals = [];
171
- let colors = []
172
- let size = 20;
173
- let segments = 10;
174
-
175
- let halfSize = size / 2;
176
- let segmentSize = size / segments;
177
-
178
- let _color = new THREE.Color();
179
-
180
- // generate vertices, normals and color data for a simple grid geometry
181
-
182
- for ( let i = 0; i <= segments; i ++ ) {
183
-
184
- let y = ( i * segmentSize ) - halfSize;
185
-
186
- for ( let j = 0; j <= segments; j ++ ) {
187
-
188
- let x = ( j * segmentSize ) - halfSize;
189
-
190
- vertices.push( x, - y, 0 );
191
- normals.push( 0, 0, 1 );
192
-
193
- let r = ( x / size ) + 0.5;
194
- let g = ( y / size ) + 0.5;
195
-
196
- _color.setRGB( r, g, 1, THREE.SRGBColorSpace );
197
-
198
- colors.push( _color.r, _color.g, _color.b );
199
-
200
- }
201
-
202
- }
203
-
204
- // generate indices (data for element array buffer)
205
-
206
- for ( let i = 0; i < segments; i ++ ) {
207
-
208
- for ( let j = 0; j < segments; j ++ ) {
209
-
210
- let a = i * ( segments + 1 ) + ( j + 1 );
211
- let b = i * ( segments + 1 ) + j;
212
- let c = ( i + 1 ) * ( segments + 1 ) + j;
213
- let d = ( i + 1 ) * ( segments + 1 ) + ( j + 1 );
214
-
215
- // generate two faces (triangles) per iteration
216
-
217
- indices.push( a, b, d ); // face one
218
- indices.push( b, c, d ); // face two
219
-
220
- }
221
-
222
- }
223
-
224
- //
225
-
226
- geometry.setIndex( indices );
227
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
228
- geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
229
- geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
230
-
231
- let material = new THREE.MeshPhongMaterial( {
232
- side: THREE.DoubleSide,
233
- vertexColors: true
234
- } );
227
+ });
228
+ }
235
229
 
236
- let mesh = new THREE.Mesh( geometry, material );
237
- scene.add( mesh );
238
- // mesh = tb.Object3D({ obj: mesh, units: 'meters',anchor:'center'});
239
- // tb.add(mesh);
240
- // for(let line of lines){
241
- // this.createLine(line.coords,`rgba(${line.color.join(',')})`)
242
- // }
230
+ drawWallPoints(dataList){
231
+ // this.drawWall(dataList)
232
+ this.drawWall(dataList)
233
+ // this.drawWall(dataList.reverse(),-100,100,true)
243
234
  }
244
235
 
245
- createLine(path,color){
246
- let line = tb.line({
247
- width:this.width,
248
- opacity:this.opacity,
249
- color:color,
250
- geometry:path
251
- })
252
- tb.add(line)
253
- }
236
+
237
+
238
+
239
+ removeObj = (obj) => {
240
+ const clearCache = (item) => {
241
+ item.geometry.dispose();
242
+ item.material.dispose();
243
+ };
244
+ let arr = obj.children.filter((x) =>!! x);
245
+ arr.forEach((item) => {
246
+ if (item.children.length) {
247
+ removeObj(item);
248
+ } else {
249
+ clearCache(item);
250
+ item.clear();
251
+ }
252
+ });
253
+ obj.clear();
254
+ arr = null;
255
+ };
254
256
 
255
257
  onAdd(map, gl) {
256
258
  this.map = map;
257
259
  let container = map.getCanvas();
258
260
  const w = container.clientWidth;
259
261
  const h = container.clientHeight;
260
- this.camera = new THREE.PerspectiveCamera(map.transform.fov, w / h, 0.1, 1e21);
261
- scene = new THREE.Scene();
262
- scene.add(group)
263
- new CameraSync(this.map, this.camera, group);
264
- // let centerlngLat = map.getCenter()
265
- // let center = mapboxgl.MercatorCoordinate.fromLngLat(centerlngLat)
266
- // const scale = center.meterInMercatorCoordinateUnits();
267
- // //this.scale = scale;
268
- // let {x,y,z} = center;
269
- // const modelRotate = [Math.PI / 2, 0, 0];
270
-
262
+ this.camera = new THREE.PerspectiveCamera(map.transform.fov, w / h, 0.1, 1000);
263
+ this.scene = new THREE.Scene();
264
+ this.scene.add(this.group)
265
+ new CameraSync(this.map, this.camera, this.group);
271
266
 
272
- // cameraTransform = new THREE.Matrix4()
273
- // .makeTranslation(
274
- // x,
275
- // y,
276
- // z
277
- // )
278
- // .scale(
279
- // new THREE.Vector3(
280
- // scale,
281
- // -scale,
282
- // scale
283
- // )
284
- // )
285
- // .multiply( new THREE.Matrix4().makeRotationAxis(
286
- // new THREE.Vector3(1, 0, 0),
287
- // modelRotate[0]
288
- // ))
289
- // .multiply(new THREE.Matrix4().makeRotationAxis(
290
- // new THREE.Vector3(0, 1, 0),
291
- // modelRotate[1]
292
- // ))
293
- // .multiply(new THREE.Matrix4().makeRotationAxis(
294
- // new THREE.Vector3(0, 0, 1),
295
- // modelRotate[2]
296
- // ));
297
- // create two three.js lights to illuminate the model
298
- // let directionalLight = new THREE.DirectionalLight(0xffffff);
299
- // directionalLight.position.set(0, -70, 100).normalize();
300
- // scene.add(directionalLight);
267
+ // this.scene.add(new THREE.AxesHelper(1000));
268
+ // this.scene.add(new THREE.CameraHelper( this.camera ));
301
269
 
302
- // let directionalLight2 = new THREE.DirectionalLight(0xffffff);
303
- // directionalLight2.position.set(0, 70, 100).normalize();
304
- // scene.add(directionalLight2);
305
- // tb = new Threebox(
306
- // map,
307
- // gl, //get the context from Mapbox
308
- // { defaultLights: true ,passiveRendering:false}
309
- // );
310
- //scene.add(new THREE.AxesHelper(1000));
311
270
  this.renderer = new THREE.WebGLRenderer({
312
271
  canvas: map.getCanvas(),
313
- context: gl,
314
- antialias: true
272
+ context: gl,
273
+ logarithmicDepthBuffer: true // 启用对数深度缓冲
274
+ // logarithmicDepthBuffer: true,
275
+ // antialias: true
315
276
  });
277
+ // gl.disable(gl.DEPTH_TEST);
278
+ // gl.clear(gl.DEPTH_BUFFER_BIT);
316
279
  this.renderer.outputEncoding = THREE.sRGBEncoding;
317
-
318
- this.renderer.autoClear = false;
280
+ // this.map
281
+ // .on('move',()=> {
282
+ // //console.log(111)
283
+ // this.update();
284
+ // })
285
+ // .on('resize', ()=> {
286
+ // this.setup();
287
+ // })
288
+
289
+ this.renderer.autoClear = false;
319
290
  if(this.addedFun)
320
291
  this.addedFun()
292
+
321
293
  }
322
294
 
323
- render(gl, matrix) {
324
- //tb.update();
295
+ render(gl, matrix) {
325
296
  // const rotationX = new THREE.Matrix4().makeRotationAxis(
326
297
  // new THREE.Vector3(1, 0, 0),
327
298
  // modelTransform.rotateX
@@ -356,8 +327,12 @@ export default class ThreeWallLayer{
356
327
  //this.camera.projectionMatrix = m.multiply(l);
357
328
  // var m = new THREE.Matrix4().fromArray(matrix);
358
329
  // this.camera.projectionMatrix = m.multiply(cameraTransform);;
359
- // this.renderer.resetState();
360
- this.renderer.render(scene, this.camera);
330
+ // this.camera.lookAt(0, 0, 0);
331
+ // console.log(this.group.position)
332
+ // this.renderer.clearDepth();
333
+ this.renderer.resetState();
334
+
335
+ this.renderer.render(this.scene, this.camera);
361
336
  this.map.triggerRepaint();
362
337
  }
363
338
 
@@ -1,6 +1,6 @@
1
1
 
2
- import {hasProperty,transform} from '../../utils/util'
3
- import mapboxgl from '../../../lib/mapbox-gl/dist/mapbox-gl.js';
2
+ import {hasProperty,transform} from '../../utils/util.js'
3
+ import mapboxgl from 'mapbox-gl';
4
4
  export default class ScanLayer{
5
5
  drawArray = []
6
6
  defaultDrawInfo = {
@@ -84,6 +84,7 @@ export default class ScanLayer{
84
84
  this.draw()
85
85
  }
86
86
 
87
+
87
88
  //添加object 支持多路径添加
88
89
  //data 二维数组 croods 路径列表 colors 二维数组
89
90
  drawList(_scanInfo){