@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,253 @@
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
+ drawPlane(){
34
+ let width = 5,height = 0,depth = 10,widthSegments = 1,heightSegments = 1,depthSegments = 1;
35
+ widthSegments = Math.floor( widthSegments );
36
+ heightSegments = Math.floor( heightSegments );
37
+ depthSegments = Math.floor( depthSegments );
38
+ const indices = [];
39
+ const vertices = [];
40
+ const normals = [];
41
+ const colors = []
42
+ const uvs = [];
43
+ let numberOfVertices = 0;
44
+ let groupStart = 0;
45
+ function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) {
46
+ const segmentWidth = width / gridX;
47
+ const segmentHeight = height / gridY;
48
+ const widthHalf = width / 2;
49
+ const heightHalf = height / 2;
50
+ const depthHalf = depth / 2;
51
+ const gridX1 = gridX + 1;
52
+ const gridY1 = gridY + 1;
53
+ let vertexCounter = 0;
54
+ let groupCount = 0;
55
+ const vector = new THREE.Vector3();
56
+ const color= new THREE.Color();
57
+ // generate vertices, normals and uvs
58
+ for ( let iy = 0; iy < gridY1; iy ++ ) {
59
+ const y = iy * segmentHeight - heightHalf;
60
+ for ( let ix = 0; ix < gridX1; ix ++ ) {
61
+ const x = ix * segmentWidth - widthHalf;
62
+ // set values to correct vector component
63
+ vector[ u ] = x * udir;
64
+ vector[ v ] = y * vdir ;
65
+ vector[ w ] = depthHalf;
66
+ // now apply vector to vertex buffer
67
+
68
+ vertices.push( vector.x, vector.y, vector.z );
69
+ // set values to correct vector component
70
+ vector[ u ] = 0;
71
+ vector[ v ] = 0;
72
+ vector[ w ] = depth > 0 ? 1 : - 1;
73
+ // now apply vector to normal buffer
74
+ normals.push( vector.x, vector.y, vector.z );
75
+ // uvs
76
+ uvs.push( ix / gridX );
77
+ uvs.push( 1 - ( iy / gridY ) );
78
+ // counters
79
+ vertexCounter += 1;
80
+ }
81
+ }
82
+
83
+ // indices
84
+ // 1. you need three indices to draw a single face
85
+ // 2. a single segment consists of two faces
86
+ // 3. so we need to generate six (2*3) indices per segment
87
+ for ( let iy = 0; iy < gridY; iy ++ ) {
88
+
89
+ for ( let ix = 0; ix < gridX; ix ++ ) {
90
+
91
+ const a = numberOfVertices + ix + gridX1 * iy;
92
+ const b = numberOfVertices + ix + gridX1 * ( iy + 1 );
93
+ const c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 );
94
+ const d = numberOfVertices + ( ix + 1 ) + gridX1 * iy;
95
+ // faces
96
+ indices.push( a, b, d );
97
+ indices.push( b, c, d );
98
+ // let _color = color.setRGB(Math.random(),Math.random(),Math.random(),THREE.SRGBColorSpace )
99
+ colors.push(Math.random(),Math.random(),Math.random())
100
+ colors.push(Math.random(),Math.random(),Math.random())
101
+ colors.push(Math.random(),Math.random(),Math.random())
102
+ colors.push(Math.random(),Math.random(),Math.random())
103
+ // increase counter
104
+ groupCount += 6;
105
+ }
106
+ }
107
+ // add a group to the geometry. this will ensure multi material support
108
+ // scope.addGroup( groupStart, groupCount, materialIndex );
109
+ // calculate new start value for groups
110
+ groupStart += groupCount;
111
+ // update total number of vertices
112
+ numberOfVertices += vertexCounter;
113
+ }
114
+ // buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); // px
115
+ // buildPlane( 'z', 'y', 'x', 1, - 1, depth , height, - width, depthSegments, heightSegments, 1 ); // nx
116
+ buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); // py
117
+ buildPlane( 'x', 'z', 'y', 1, -1, width, depth, - height , widthSegments, depthSegments, 3 ); // ny
118
+ buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height+10, widthSegments, depthSegments, 2 ); // py
119
+ buildPlane( 'x', 'z', 'y', 1, -1, width, depth, - height+10 , widthSegments, depthSegments, 3 ); // ny
120
+ return {
121
+ indices,vertices,normals,colors,uvs
122
+ }
123
+ }
124
+ drawWallPoints(dataList){
125
+ let center = [dataList[0].lng,dataList[0].lat]
126
+ let {indices,vertices,normals,colors,uvs} = this.drawPlane()
127
+ // indices = [
128
+ // 0,
129
+ // 2,
130
+ // 1,
131
+ // 2,
132
+ // 3,
133
+ // 1,
134
+ // 4,
135
+ // 5,
136
+ // 6,
137
+ // 6,
138
+ // 5,
139
+ // 7
140
+ // ]
141
+ // vertices = [
142
+ // -2.5,
143
+ // 0,
144
+ // -5,
145
+ // 2.5,
146
+ // 3,
147
+ // -5,
148
+ // -2.5,
149
+ // 0,
150
+ // 5,
151
+ // 2.5,
152
+ // 3,
153
+ // 5,
154
+ // -2.5,
155
+ // 0,
156
+ // -5,
157
+ // 2.5,
158
+ // 3,
159
+ // -5,
160
+ // -2.5,
161
+ // 0,
162
+ // 5,
163
+ // 2.5,
164
+ // 3,
165
+ // 5,
166
+ // // -2.5,
167
+ // // 0,
168
+ // // 5,
169
+ // // 2.5,
170
+ // // 3,
171
+ // // 5,
172
+ // // -2.5,
173
+ // // 0,
174
+ // // -5,
175
+ // // 2.5,
176
+ // // 3,
177
+ // // -5
178
+ // ]
179
+ let geometry = new THREE.BufferGeometry();
180
+ geometry.setIndex( indices );
181
+ geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
182
+ // geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
183
+ geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
184
+ // geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
185
+ let material = new THREE.MeshBasicMaterial( {
186
+ vertexColors: true,
187
+ } );
188
+
189
+ // material.needsUpdate = true;
190
+ material.color.convertSRGBToLinear();
191
+ let mesh = new THREE.Mesh( geometry, material );
192
+ mesh = this.tb.Object3D({ obj: mesh, fixedZoom :15 ,anchor:'center'});
193
+ mesh.setCoords(center)
194
+ this.tb.renderer.outputEncoding = THREE.sRGBEncoding;
195
+ this.tb.add(mesh);
196
+ this.map.jumpTo({center:center})
197
+ }
198
+
199
+
200
+ onAdd(map, gl) {
201
+ this.map = map;
202
+ this.tb = new Threebox(
203
+ map,
204
+ gl, //get the context from Mapbox
205
+ { defaultLights: true ,passiveRendering:false}
206
+ );
207
+ if(this.addedFun)
208
+ this.addedFun()
209
+ }
210
+
211
+ render(gl, matrix) {
212
+ this.tb.update();
213
+ // // const rotationX = new THREE.Matrix4().makeRotationAxis(
214
+ // // new THREE.Vector3(1, 0, 0),
215
+ // // modelTransform.rotateX
216
+ // // );
217
+ // // const rotationY = new THREE.Matrix4().makeRotationAxis(
218
+ // // new THREE.Vector3(0, 1, 0),
219
+ // // modelTransform.rotateY
220
+ // // );
221
+ // // const rotationZ = new THREE.Matrix4().makeRotationAxis(
222
+ // // new THREE.Vector3(0, 0, 1),
223
+ // // modelTransform.rotateZ
224
+ // // );
225
+
226
+ // // const m = new THREE.Matrix4().fromArray(matrix);
227
+ // // const l = new THREE.Matrix4()
228
+ // // .makeTranslation(
229
+ // // modelTransform.translateX,
230
+ // // modelTransform.translateY,
231
+ // // modelTransform.translateZ
232
+ // // )
233
+ // // .scale(
234
+ // // new THREE.Vector3(
235
+ // // modelTransform.scale,
236
+ // // -modelTransform.scale,
237
+ // // modelTransform.scale
238
+ // // )
239
+ // // )
240
+ // // .multiply(rotationX)
241
+ // // .multiply(rotationY)
242
+ // // .multiply(rotationZ);
243
+
244
+ // //this.camera.projectionMatrix = m.multiply(l);
245
+ // // var m = new THREE.Matrix4().fromArray(matrix);
246
+ // // this.camera.projectionMatrix = m.multiply(cameraTransform);;
247
+ // this.renderer.resetState();
248
+
249
+ // this.renderer.render(this.scene, this.camera);
250
+ this.map.triggerRepaint();
251
+ }
252
+
253
+ }
@@ -7,8 +7,9 @@ import { Line2 } from 'three/addons/lines/Line2.js';
7
7
  import {LineMaterial} from 'three/examples/jsm/lines/LineMaterial.js'
8
8
  import * as THREE from 'three'
9
9
  import {Threebox} from '../../../lib/threebox-plugin';
10
- var tb = {};
10
+ import { esMapWatchEffect } from '../../utils/mapWatch.js';
11
11
  export default class ThreeScanLayer{
12
+ tb = {};
12
13
  drawArray = []
13
14
  opacity = 0.8
14
15
  textClass = ''
@@ -23,11 +24,11 @@ export default class ThreeScanLayer{
23
24
  }
24
25
 
25
26
  //同时渲染多个地点图片文本
26
- async drawImageList(list,delay = 300){
27
+ async drawImageList(list,delay = 300){
27
28
  for(let i in list){
28
29
  await this.update(list[i])
29
30
  //this.drawArray.push(list[i])
30
- }
31
+ }
31
32
  for(let i in this.drawArray){
32
33
  let _point = list.find(a=>a.center[0] == this.drawArray[i].center[0] && a.center[1] == this.drawArray[i].center[1])
33
34
  if(!_point){
@@ -36,8 +37,8 @@ export default class ThreeScanLayer{
36
37
  }
37
38
  }
38
39
  this.drawArray = this.drawArray.filter(a=>a)
39
- tb.update();
40
- this.map.triggerRepaint();
40
+ // this.tb.update();
41
+ // this.map.triggerRepaint();
41
42
  await sleep(delay)
42
43
  let canvas = this.map.getCanvas()
43
44
  return canvas.toDataURL();
@@ -50,7 +51,7 @@ export default class ThreeScanLayer{
50
51
 
51
52
  remove(point){
52
53
  point.mesh.label.element.remove()
53
- tb.remove(point.mesh)
54
+ this.tb.remove(point.mesh)
54
55
  }
55
56
 
56
57
  removeAll(){
@@ -59,7 +60,7 @@ export default class ThreeScanLayer{
59
60
  this.drawArray[i] = null
60
61
  }
61
62
  this.drawArray = this.drawArray.filter(a=>a)
62
- tb.update();
63
+ this.tb.update();
63
64
  this.map.triggerRepaint();
64
65
  }
65
66
 
@@ -82,7 +83,7 @@ export default class ThreeScanLayer{
82
83
  let w = point.rect.width ? point.rect.width: this.rectMesh.w
83
84
  if(!this.rectMesh.mesh || w!= this.rectMesh.w || point.rect.position[0]!=this.rectMesh.lng || point.rect.position[1] !=this.rectMesh.lat){
84
85
  if(this.rectMesh.mesh){
85
- tb.remove(this.rectMesh.mesh)
86
+ this.tb.remove(this.rectMesh.mesh)
86
87
  }
87
88
  let bounds = getLayerMapPosition(w,transform(point.rect.position))
88
89
  bounds.push(bounds[0])
@@ -92,11 +93,11 @@ export default class ThreeScanLayer{
92
93
  width: 20 // random width between 1 and 2
93
94
  }
94
95
 
95
- let lineMesh = tb.line(lineOptions);
96
+ let lineMesh = this.tb.line(lineOptions);
96
97
  this.rectMesh.mesh = lineMesh;
97
- tb.add(lineMesh)
98
+ this.tb.add(lineMesh)
98
99
  // if(this.rectMesh.mesh){
99
- // tb.remove(this.rectMesh.mesh)
100
+ // this.tb.remove(this.rectMesh.mesh)
100
101
  // }
101
102
  // let plane = new THREE.PlaneGeometry( w*20,w*20);
102
103
  // let material = new THREE.MeshLambertMaterial({
@@ -113,20 +114,33 @@ export default class ThreeScanLayer{
113
114
 
114
115
  // let line = new THREE.LineSegments(edges, edgesMaterial);
115
116
  // mesh.add(line);
116
- // mesh = tb.Object3D({ obj: mesh, units: 'meters',fixedZoom :15 ,anchor:'center'});
117
+ // mesh = this.tb.Object3D({ obj: mesh, units: 'meters',fixedZoom :15 ,anchor:'center'});
117
118
  // this.rectMesh.mesh = mesh;
118
119
  // this.rectMesh.w = w;
119
120
 
120
121
  }
121
122
  // if(this.rectMesh.mesh){
122
123
  // this.rectMesh.mesh.setCoords(transform(point.rect.position))
123
- // tb.add(this.rectMesh.mesh);
124
+ // this.tb.add(this.rectMesh.mesh);
124
125
  // }
125
126
  }
126
127
 
128
+ async updateStyleByTheme(){
129
+ let elList = this.map._container.getElementsByClassName('esm-font-time')
130
+ this.map.theme = this.map.theme ? this.map.theme :this.map.style.stylesheet.theme
131
+ let color = this.map.theme.value == "dark"?"#fff" : "#000"
132
+ if(elList.length == 0){
133
+ await sleep(200)
134
+ }
135
+ for(let el of elList){
136
+ el.style.color = color
137
+ }
138
+ }
139
+
127
140
  async update(point){
128
141
  let texture
129
142
  point.text = point.text ? point.text:''
143
+ point.height = point.height ? point.height:point.width;
130
144
  if(this.forceClear){
131
145
  THREE.Cache.clear();
132
146
  texture?.dispose();
@@ -153,11 +167,18 @@ export default class ThreeScanLayer{
153
167
  point.url += `?tempid=${Math.random()}`
154
168
  }
155
169
  let p = ()=>new Promise(function(resolve,reject){
156
- new THREE.TextureLoader().load(point.url,(_texture)=>{
157
- resolve(_texture)
158
- })
170
+ try{
171
+ new THREE.TextureLoader().load(point.url,(_texture)=>{
172
+ resolve(_texture)
173
+ },()=>{},()=>{
174
+ resolve(null)
175
+ })
176
+ }catch{
177
+ reject(null)
178
+ }
159
179
  })
160
- texture = await p()
180
+ texture = await p()
181
+ if(!texture) return;
161
182
  texture.center.set(0.5,0.5);
162
183
  texture.rotation = -Math.PI;
163
184
  }
@@ -168,11 +189,12 @@ export default class ThreeScanLayer{
168
189
  let index = this.drawArray.findIndex(a=>a.center[0] == point.center[0] && a.center[1] == point.center[1])
169
190
  if(index >= 0){
170
191
  let _point = this.drawArray[index]
171
- if(_point && point.width != _point.width){
192
+ _point.height = _point.height?_point.height:_point.width
193
+ if(_point && (point.width != _point.width || point.height != _point.height)){
172
194
  this.remove(this.drawArray[index])
173
195
  this.drawArray[index] = null
174
196
  this.drawArray = this.drawArray.filter(a=>a)
175
- let geometry = new THREE.PlaneGeometry(point.width *20,point.width*20);
197
+ let geometry = new THREE.PlaneGeometry(point.width *20,point.height*20);
176
198
  //geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
177
199
  let material = new THREE.MeshBasicMaterial({
178
200
  map:texture,
@@ -182,13 +204,13 @@ export default class ThreeScanLayer{
182
204
  material.color.convertSRGBToLinear();
183
205
  let mesh = new THREE.Mesh(geometry, material);
184
206
  mesh.name = 'scanimg'
185
- mesh = tb.Object3D({ obj: mesh, units: 'meters',fixedZoom :15 ,anchor:'center'});
207
+ mesh = this.tb.Object3D({ obj: mesh, units: 'meters',fixedZoom :15 ,anchor:'center'});
186
208
  mesh.setCoords( transform(point.center));
187
- tb.renderer.outputEncoding = THREE.sRGBEncoding;
188
- tb.add(mesh);
209
+ this.tb.renderer.outputEncoding = THREE.sRGBEncoding;
210
+ this.tb.add(mesh);
189
211
  let dom =document.createElement('div')
190
212
  mesh.addLabel(dom,true,{x:mesh.anchor.x,y:mesh.anchor.y+point.distance *1200,z:mesh.anchor.z},0)
191
- mesh.label.element.className += ('' +this.textClass)
213
+ mesh.label.element.className += (' esm-font-time ' +this.textClass)
192
214
  mesh.label.element.style.fontSize = '24px'
193
215
  mesh.label.element.innerHTML = point.text
194
216
  point.mesh = mesh;
@@ -207,7 +229,7 @@ export default class ThreeScanLayer{
207
229
  }
208
230
  }
209
231
  }else{
210
- let geometry = new THREE.PlaneGeometry(point.width *20,point.width*20);
232
+ let geometry = new THREE.PlaneGeometry(point.width *20,point.height*20);
211
233
  //geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
212
234
  let material = new THREE.MeshBasicMaterial({
213
235
  map:texture,
@@ -217,13 +239,13 @@ export default class ThreeScanLayer{
217
239
  material.color.convertSRGBToLinear();
218
240
  let mesh = new THREE.Mesh(geometry, material);
219
241
  mesh.name = 'scanimg'
220
- mesh = tb.Object3D({ obj: mesh, units: 'meters',fixedZoom :15 ,anchor:'center'});
242
+ mesh = this.tb.Object3D({ obj: mesh, units: 'meters',fixedZoom :15 ,anchor:'center'});
221
243
  mesh.setCoords( transform(point.center));
222
- tb.renderer.outputEncoding = THREE.sRGBEncoding;
223
- tb.add(mesh);
244
+ this.tb.renderer.outputEncoding = THREE.sRGBEncoding;
245
+ this.tb.add(mesh);
224
246
  let dom =document.createElement('div')
225
247
  mesh.addLabel(dom,true,{x:mesh.anchor.x,y:mesh.anchor.y+point.distance *1200,z:mesh.anchor.z},0)
226
- mesh.label.element.className += ('' +this.textClass)
248
+ mesh.label.element.className += (' esm-font-time ' +this.textClass)
227
249
  mesh.label.element.style.fontSize = '24px'
228
250
  mesh.label.element.innerHTML = point.text
229
251
  point.mesh = mesh;
@@ -231,7 +253,8 @@ export default class ThreeScanLayer{
231
253
  if(point.rect){
232
254
  this.updateRect(point)
233
255
  }
234
- }
256
+ }
257
+ this.updateStyleByTheme()
235
258
  }
236
259
 
237
260
 
@@ -254,10 +277,10 @@ export default class ThreeScanLayer{
254
277
  material.color.convertSRGBToLinear();
255
278
  let mesh = new THREE.Mesh(geometry, material);
256
279
  mesh.name = 'scanimg'
257
- mesh = tb.Object3D({ obj: mesh, units: 'meters',fixedZoom :15 ,anchor:'center'});
280
+ mesh = this.tb.Object3D({ obj: mesh, units: 'meters',fixedZoom :15 ,anchor:'center'});
258
281
  mesh.setCoords( transform(point.center));
259
- tb.renderer.outputEncoding = THREE.sRGBEncoding;
260
- tb.add(mesh);
282
+ this.tb.renderer.outputEncoding = THREE.sRGBEncoding;
283
+ this.tb.add(mesh);
261
284
  let dom =document.createElement('div')
262
285
  mesh.addLabel(dom,true,{x:mesh.anchor.x,y:mesh.anchor.y+point.distance *1200,z:mesh.anchor.z},0)
263
286
  mesh.label.element.className += ('' +this.textClass)
@@ -266,15 +289,18 @@ export default class ThreeScanLayer{
266
289
  }
267
290
 
268
291
  onAdd(map, gl) {
269
- this.map = map;
270
- tb = new Threebox(
292
+ this.map = map;
293
+ this.tb = new Threebox(
271
294
  map,
272
295
  gl, //get the context from Mapbox
273
296
  { defaultLights: true ,passiveRendering:false}
274
- );
297
+ );
298
+ esMapWatchEffect(()=>{
299
+ this.updateStyleByTheme()
300
+ })
275
301
  }
276
- render(gl, matrix) {
277
- tb.update();
302
+ render(gl, matrix) {
303
+ this.tb.update();
278
304
  this.map.triggerRepaint();
279
305
  }
280
306