Map.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. import * as THREE from "../../../../libs/three.js/build/three.module.js";
  2. import math from '../../utils/math.js'
  3. import browser from '../../utils/browser.js'
  4. let texLoader = new THREE.TextureLoader()
  5. texLoader.crossOrigin = "anonymous"
  6. let createErrorMaterial = function() {
  7. var t = new THREE.MeshBasicMaterial({
  8. transparent: !0,
  9. depthWrite: !1,
  10. depthTest: !0,
  11. opacity: 1,
  12. side: THREE.DoubleSide
  13. });
  14. return t.color = new THREE.Color(3355443),
  15. t
  16. }
  17. let tempVector = new THREE.Vector3, //sharedata
  18. face1 = new THREE.Face3(0,1,2),
  19. face2 = new THREE.Face3(2,3,0),
  20. errorMaterial = createErrorMaterial(),
  21. uv00 = new THREE.Vector2(0,0),
  22. uv01 = new THREE.Vector2(0,1),
  23. uv10 = new THREE.Vector2(1,0),
  24. uv11 = new THREE.Vector2(1,1),
  25. face1UV = [uv00, uv10, uv11],
  26. face2UV = [uv11, uv01, uv00]
  27. const HALF_WORLD_SIZE = 21e6 //略大于半个周长(mapSizeM/2)
  28. const MAX_VERTICAL_DIST = 2
  29. const MAX_VERTICAL_DIST_TO_BEST = 1
  30. function defineLocalProj(locationLonLat){
  31. proj4.defs("LOCAL_MAP", "+proj=tmerc +ellps=WGS84 +lon_0=" + locationLonLat[0].toPrecision(15) + " +lat_0=" + locationLonLat[1].toPrecision(15));
  32. }
  33. //高德坐标拾取工具 : https://lbs.amap.com/tools/picker
  34. export class MapLayer extends THREE.EventDispatcher{ // 包括了 MapLayerBase SceneLayer
  35. constructor(viewer_, viewport){
  36. super()
  37. this.sceneGroup = new THREE.Object3D;
  38. this.sceneGroup.name = "MapLayer"
  39. this.loadingInProgress = 0
  40. this.maps = []
  41. this.frustum = new THREE.Frustum
  42. this.frustumMatrix = new THREE.Matrix4
  43. this.tileColor = new THREE.Color(16777215)
  44. this.viewport = viewport
  45. this.changeViewer(viewer_)
  46. //添加地图
  47. var map = new TiledMapOpenStreetMap(this, this.tileColor )
  48. this.addMap(map)
  49. //map.setEnable(false)
  50. }
  51. addMapEntity(data, datasetId){
  52. if(!data || !data[0]){
  53. Potree.Log('平面图无数据',{font:'red'})
  54. return
  55. }
  56. var floorplan = new TiledMapFromEntity(this, this.tileColor, data[0] )//[0]?
  57. if(floorplan){
  58. floorplan.name += "_"+ datasetId
  59. this.addMap(floorplan)
  60. floorplan.updateProjection()
  61. floorplan.updateObjectGroup()
  62. let visible = false
  63. if(datasetId in Potree.settings.floorplanEnables){
  64. visible = Potree.settings.floorplanEnables[datasetId]
  65. }else{
  66. visible = Potree.settings.floorplanEnable
  67. }
  68. if(visible){
  69. this.needUpdate = true
  70. }else{
  71. floorplan.setEnable(false)
  72. }
  73. this.dispatchEvent({type:'floorplanLoaded', floorplan})
  74. }
  75. return floorplan
  76. }
  77. getFloorplan(datasetId){
  78. return this.maps.find(e=>e.name == 'floorplan'+"_"+ datasetId )
  79. }
  80. addMap(t){
  81. this.maps.push(t)
  82. //this.view.invalidateScene()
  83. this.needUpdate = true
  84. this.viewer.mapChanged = true
  85. }
  86. removeMap(t){
  87. var e = this.maps.indexOf(t);
  88. if(e >= 0){
  89. t.removeFromSceneGroup(this.sceneGroup)
  90. this.maps.splice(e, 1)
  91. }
  92. /* this.view.invalidateScene() */
  93. this.needUpdate = true
  94. this.viewer.mapChanged = true
  95. }
  96. changeViewer(viewer_){//add
  97. this.viewer = viewer_
  98. }
  99. initProjection(){
  100. this.maps.forEach(map=>{
  101. map.updateProjection()
  102. map.updateObjectGroup()
  103. })
  104. }
  105. visibilityChanged(){
  106. if (!this.visible)
  107. for (var t = 0, e = this.maps; t < e.length; t++){
  108. e[t].removeFromSceneGroup(this.sceneGroup)
  109. }
  110. }
  111. update(){
  112. this.needUpdate = false
  113. if(this.disabled || !this.maps.find(e=>!e.disabled) || !this.maps.find(e=>e.objectGroup.visible) )return //add
  114. this.viewer.mapChanged = true
  115. var e, n, i, r, o;
  116. this.updateTimer = void 0,
  117. e = this.viewport.camera,
  118. n = e.projectionMatrix.clone()
  119. let expandRatio = 1.3
  120. n.elements[0] /= expandRatio
  121. n.elements[5] /= expandRatio // 为了缓存吗,使边界处也提前加载,扩大显示区域
  122. this.frustumMatrix.multiplyMatrices(n, e.matrixWorldInverse),
  123. this.frustum.setFromProjectionMatrix(this.frustumMatrix),
  124. this.frustum.planes[4].setComponents(0, 0, 0, 0),
  125. this.frustum.planes[5].setComponents(0, 0, 0, 0),
  126. i = !0
  127. for (r = 0; r < this.maps.length; r++){
  128. var map = this.maps[r]
  129. i = map.update(this.frustum, this.sceneGroup) && i;
  130. }
  131. return [2, i]
  132. }
  133. updateProjection(){
  134. for (var t = 0, e = this.maps; t < e.length; t++){
  135. var n = e[t];
  136. n.clearProjection(),
  137. n.updateObjectGroup()
  138. }
  139. }
  140. }
  141. export class TiledMapBase extends THREE.EventDispatcher{
  142. constructor(/* t, */name, mapLayer, tileColor, projection){
  143. super();
  144. this.name = name
  145. //this.TransformService = t,
  146. this.mapLayer = mapLayer,
  147. this.tileColor = tileColor,
  148. this.bias = 0
  149. this.zIndex = -1
  150. this.objectGroup = new THREE.Object3D;
  151. this.objectGroup.name = name
  152. this.objectGroupAdded = !1,
  153. this.baseTile = new MapTile(this, this.objectGroup, this.tileColor, null, '0'),
  154. this.isTileVisibleBox = new THREE.Box3,
  155. this.isTileVisibleVec = new THREE.Vector3
  156. this.projection = projection
  157. this._zoomLevel = 0;//1-20
  158. this.objectGroup.addEventListener('isVisible',()=>{
  159. this.mapLayer.viewer.mapChanged = true
  160. })
  161. this.computeCount = 0
  162. }
  163. get zoomLevel(){
  164. return this._zoomLevel
  165. }
  166. set zoomLevel(zoomLevel){
  167. if(this._zoomLevel != zoomLevel){
  168. this._zoomLevel = zoomLevel
  169. //this.dispatchEvent('zoomLevelChange',zoomLevel)
  170. //if(this.name == 'map')console.log(zoomLevel,viewer.mapViewer.camera.zoom)
  171. }
  172. }
  173. updateObjectGroup(){
  174. this.position && this.objectGroup.position.copy(this.position),
  175. this.quaternion && this.objectGroup.quaternion.copy(this.quaternion),
  176. this.objectGroup.updateMatrixWorld(!0)
  177. }
  178. updateProjection(){
  179. if(!this.transformMapToLocal){
  180. if(proj4.defs("LOCAL_MAP")){
  181. /* if(this.projection == "EPSG:4550"){
  182. this.transformMapToLocal = {
  183. forward:(e)=>{
  184. var a = viewer.transform.lonlatTo4550.inverse(e)
  185. return viewer.transform.lonlatToLocal.forward(a)
  186. },
  187. }
  188. }else{ */
  189. this.transformMapToLocal = proj4(this.projection, "LOCAL_MAP")
  190. //}
  191. }
  192. }
  193. }
  194. clearProjection(){
  195. this.transformMapToLocal = void 0
  196. this.projection !== 'LOCAL_MAP' && this.baseTile.remove()
  197. }
  198. setEnable(enable){//add
  199. if(!this.disabled == enable)return
  200. if(enable){
  201. console.log('setEnable',true)
  202. }
  203. this.disabled = !enable
  204. Potree.Utils.updateVisible(this.objectGroup, 'setEnable', enable)
  205. if(!enable){
  206. this.baseTile.remove()
  207. }else{
  208. this.mapLayer.needUpdate = true
  209. }
  210. }
  211. update(e, n){
  212. this.computeCount = 0
  213. var unavailable = (this.disabled || !this.objectGroup.visible)//地图即使不显示也要获得zoomlevel
  214. if(this.name != 'map' && unavailable)return
  215. this.updateProjection()
  216. if(!this.transformMapToLocal)return
  217. if (!this.isTileVisible(new THREE.Vector3(0,0,0), this.mapSizeM, e))
  218. return this.removeFromSceneGroup(n), !0;
  219. let viewport = this.mapLayer.viewport
  220. var i = new THREE.Vector3(-.5 * this.mapSizeM,0,0);
  221. i.applyMatrix4(this.objectGroup.matrixWorld),
  222. i.project(viewport.camera);
  223. var o = new THREE.Vector3(.5 * this.mapSizeM,0,0);
  224. o.applyMatrix4(this.objectGroup.matrixWorld),
  225. o.project(viewport.camera);
  226. var a = viewport.resolution.x
  227. , s = viewport.resolution.y
  228. if (a <= 0 || s <= 0 || isNaN(i.x) || isNaN(o.x)) return !1;
  229. i.sub(o),
  230. i.x *= a / 2,
  231. i.y *= s / 2;
  232. //add 高纬度的因倾斜而造成tile较小,所以放大些,否则会造成显示的tile过多而卡
  233. let lonlat = viewer.transform.lonlatToLocal.inverse(viewport.camera.position.clone())
  234. let cos = Math.cos(THREE.Math.degToRad(lonlat.y)); //越小就在纬度上越高,tile表现越小
  235. //为什么lonlat.y会超出90?
  236. if(lonlat.y>90){
  237. console.log('lonlat.y>90',lonlat.y)
  238. }
  239. cos = THREE.Math.clamp(cos, 0,1)
  240. let lonShift = Math.abs(viewer.mapViewer.camera.position.x / this.mapSizeM * 16 ) //越大就在经度离中心越远,tile表现越大 。
  241. lonShift = THREE.Math.clamp(lonShift, 0, Math.PI)
  242. lonShift = (1 - Math.sin( 1/2 * lonShift + Math.PI/2 )) * Math.PI // 0-Math.PI sin增速向上
  243. let scale = 0.5 * cos * (1+lonShift) + 0.5 * Math.pow(cos, lonShift)
  244. var c = this.tileSizePx / i.length() / scale //多除以一个scale缩放因子,scale越大level越小
  245. , level = Math.ceil(-Math.log(c) / Math.log(2) - this.bias);
  246. level = Math.max(level, 0)
  247. level = Math.min(level, void 0 === this.maxDepth ? 1 / 0 : this.maxDepth)
  248. this.zoomLevel = level//add
  249. /* if(isNaN(this.zoomLevel )){
  250. console.log(level, cos , scale , lonlat )
  251. } */
  252. if(!unavailable){
  253. this.addToSceneGroup(n)
  254. return this.baseTile.update(this, e, level, this.mapSizeM, 0, 0, "")
  255. }
  256. }
  257. isTileVisible(e, n, i){
  258. if (n > HALF_WORLD_SIZE) return !0;
  259. var r = .5 * n;
  260. //简单版:
  261. this.transformMapToLocal.forward(e) //e转化为local
  262. this.isTileVisibleBox.makeEmpty()
  263. this.isTileVisibleVec.set(e.x - r, e.y - r, e.z).applyMatrix4(this.objectGroup.matrixWorld)
  264. this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec)
  265. this.isTileVisibleVec.set(e.x - r, e.y + r, e.z).applyMatrix4(this.objectGroup.matrixWorld)
  266. this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec)
  267. this.isTileVisibleVec.set(e.x + r, e.y - r, e.z).applyMatrix4(this.objectGroup.matrixWorld)
  268. this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec)
  269. this.isTileVisibleVec.set(e.x + r, e.y + r, e.z).applyMatrix4(this.objectGroup.matrixWorld)
  270. this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec)
  271. //仿造createMesh写的准确版,但会因为大的tile非矩形,而视口是矩形,若视口刚好在tile的曲线边缘外却识别为可见,就会创建冗余tile。 但上面那个简单版在zoomlevel低的时候地球边缘容易有识别不到的tile,造成黑色三角形。
  272. //容易出现奇怪的mesh
  273. /* this.isTileVisibleBox.makeEmpty()
  274. this.isTileVisibleVec.set(e.x - r, e.y - r, e.z)
  275. this.transformMapToLocal.forward(this.isTileVisibleVec)
  276. this.isTileVisibleVec.applyMatrix4(this.objectGroup.matrixWorld)
  277. this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec)
  278. this.isTileVisibleVec.set(e.x - r, e.y + r, e.z)
  279. this.transformMapToLocal.forward(this.isTileVisibleVec)
  280. this.isTileVisibleVec.applyMatrix4(this.objectGroup.matrixWorld)
  281. this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec)
  282. this.isTileVisibleVec.set(e.x + r, e.y - r, e.z)
  283. this.transformMapToLocal.forward(this.isTileVisibleVec)
  284. this.isTileVisibleVec.applyMatrix4(this.objectGroup.matrixWorld)
  285. this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec)
  286. this.isTileVisibleVec.set(e.x + r, e.y + r, e.z)
  287. this.transformMapToLocal.forward(this.isTileVisibleVec)
  288. this.isTileVisibleVec.applyMatrix4(this.objectGroup.matrixWorld)
  289. this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec) */
  290. return i.intersectsBox(this.isTileVisibleBox)
  291. }
  292. addToSceneGroup(t){
  293. this.objectGroupAdded || (t.add(this.objectGroup),
  294. this.objectGroupAdded = !0)
  295. }
  296. removeFromSceneGroup(t){
  297. this.baseTile.remove(),
  298. this.objectGroupAdded && (t.remove(this.objectGroup),
  299. this.objectGroupAdded = !1)
  300. }
  301. }
  302. export class MapTile{
  303. constructor(map, e, n, parent, name){
  304. this.map = map;
  305. this.name = name;
  306. this.parent = parent;
  307. this.objectGroup = e,
  308. this.tileColor = n,
  309. this.meshAdded = !1,
  310. this.textureLoaded = !1,
  311. this.children = []
  312. }
  313. update(e, n, i, r, o, a, s){
  314. return !!this.doesNotContainTilesToBeDisplayed(e) || (0 === i ? this.updateTile(e, r, o, a) : this.updateSubTiles(e, n, i, r, o, a, s))
  315. }
  316. doesNotContainTilesToBeDisplayed(t){
  317. return t.tilePresenceMap && t.tilePresenceMap.empty
  318. }
  319. updateTile(t, e, n, i){
  320. if(!this.mesh){
  321. this.createTileObject(t, e, n, i)
  322. }
  323. if(!this.meshAdded){
  324. this.objectGroup.add(this.mesh)
  325. this.meshAdded = !0
  326. }
  327. if(this.textureLoaded){
  328. this.removeChildren()
  329. }
  330. return this.textureLoaded
  331. }
  332. updateSubTiles(entity, n, level, o, a, s, c){
  333. for (var l = !0, u = [-.25 * o, .25 * o, -.25 * o, .25 * o], d = [.25 * o, .25 * o, -.25 * o, -.25 * o], p = 0; p < 4; ++p){
  334. var h = c + p.toString(10);
  335. //一级(512):0 1 2 3分别为左上、右上、左下、右下。二级(1024)就是把一级的每一块分裂,如00 01 02 03分别是0的左上、右上、左下、右下……
  336. /* if(entity.name == 'floorplan'){
  337. console.log(1)
  338. } */
  339. if (!entity.tilePresenceMap || entity.tilePresenceMap[h]){
  340. //去掉判断,直接显示
  341. var f = a + u[p]
  342. , m = s + d[p];
  343. tempVector.set(f, m, 0);
  344. this.map.computeCount ++
  345. //console.log(this.map.computeCount, this.name, 'level:',level)
  346. if (entity.isTileVisible(tempVector, .5 * o, n)){
  347. this.children[p] || (this.children[p] = new MapTile(this.map, this.objectGroup,this.tileColor, this, this.name+p ))
  348. l = this.children[p].update(entity, n, level - 1, .5 * o, f, m, h) && l
  349. } else {
  350. if (this.children[p]){
  351. this.children[p].remove()
  352. delete this.children[p]
  353. }
  354. }
  355. }
  356. }
  357. return l && this.removeObject3D(),
  358. l
  359. }
  360. createTileObject(t, e, n, a){
  361. var s = this;
  362. this.mesh = this.createMesh(t.transformMapToLocal, e, n, a),
  363. this.textureLoaded = !1;
  364. var c = t.mapSizeM / e
  365. , l = Math.log(c) / Math.log(2)
  366. , u = n / e + .5 * (c - 1)
  367. , d = -a / e + .5 * (c - 1)
  368. , p = t.getTileUrl(Math.round(l), Math.round(u), Math.round(d));
  369. Potree.Utils.setObjectLayers(this.mesh, 'map' )
  370. this.mesh.renderOrder = -(1e6 - l - 100 * (t.zIndex || 0));
  371. this.mesh.name = this.name //add
  372. var h = this.mesh.material;
  373. /* let area = math.getArea(this.mesh.geometry.vertices.slice(0,3));
  374. if(area >0){
  375. this.mesh.visible = false
  376. console.log('area>0',this.mesh.name)
  377. } */
  378. var loadDone = ()=>{
  379. this.map.mapLayer.loadingInProgress--
  380. if(this.map.mapLayer.loadingInProgress == 0){
  381. this.map.mapLayer.dispatchEvent('loadDone')
  382. }
  383. }
  384. h.map = texLoader.load(p, (tex)=>{ //如果一直加载不了会影响其他的加载,如google地图没有vpn会使全景图一直加载不了
  385. if(this.mesh){//如果还要显示的话
  386. this.textureLoaded = true
  387. this.mesh.material.opacity = 1
  388. //this.mapLayer.view.invalidateScene()
  389. this.map.mapLayer.viewer.mapChanged = true
  390. this.map.mapLayer.needUpdate = true //表示还要继续update(以removeChildren)
  391. }else{
  392. tex.dispose()
  393. }
  394. loadDone()
  395. } , void 0, (()=>{//error
  396. this.textureLoaded = !0
  397. if(this.mesh){
  398. this.mesh.material.dispose() //o.disposeMeshMaterial(this.mesh)
  399. this.mesh.material = errorMaterial
  400. //this.map.mapLayer.view.invalidateScene())
  401. this.map.mapLayer.viewer.mapChanged = true
  402. }
  403. loadDone()
  404. }))
  405. h.map.anisotropy = 0,
  406. h.map.generateMipmaps = !1,
  407. h.map.minFilter = THREE.LinearFilter,
  408. h.map.magFilter = THREE.LinearFilter,
  409. this.map.mapLayer.loadingInProgress++
  410. }
  411. createMesh(t, e, n, o){
  412. var a = new THREE.Geometry;
  413. return tempVector.set(n - e / 2, o - e / 2, 0),
  414. a.vertices.push(new THREE.Vector3().copy(t.forward(tempVector))),
  415. tempVector.set(n + e / 2, o - e / 2, 0),
  416. a.vertices.push(new THREE.Vector3().copy(t.forward(tempVector))),
  417. tempVector.set(n + e / 2, o + e / 2, 0),
  418. a.vertices.push(new THREE.Vector3().copy(t.forward(tempVector))),
  419. tempVector.set(n - e / 2, o + e / 2, 0),
  420. a.vertices.push(new THREE.Vector3().copy(t.forward(tempVector))),
  421. a.faces.push(face1),
  422. a.faces.push(face2),
  423. a.faceVertexUvs[0].push(face1UV),
  424. a.faceVertexUvs[0].push(face2UV),
  425. new THREE.Mesh(a,this.createMaterial())
  426. }
  427. createMaterial(){
  428. var t = new THREE.MeshBasicMaterial({
  429. transparent: !0,
  430. depthWrite: !1,
  431. depthTest: !0,
  432. opacity: 0,
  433. side: THREE.DoubleSide,
  434. });
  435. if(Potree.settings.isTest) {
  436. var colorHue = Math.random()
  437. t.color = new THREE.Color().setHSL(colorHue, 0.6, 0.92)
  438. }else{
  439. t.color = this.tileColor ? this.tileColor : new THREE.Color(16777215)
  440. }
  441. return t
  442. }
  443. traverse(f){//add
  444. return THREE.Mesh.prototype.traverse.call(this,f)
  445. }
  446. remove(){
  447. this.removeObject3D(),
  448. this.removeChildren()
  449. }
  450. removeObject3D(){
  451. if (this.mesh){
  452. if (this.objectGroup.remove(this.mesh),
  453. this.textureLoaded){
  454. var t = this.mesh.material.map;
  455. t && t.dispose()
  456. }
  457. this.mesh.material.dispose() //o.disposeMeshMaterial(this.mesh),
  458. this.mesh.geometry.dispose()
  459. this.mesh = void 0
  460. }
  461. this.meshAdded = !1,
  462. this.textureLoaded = !1
  463. }
  464. removeChildren(){
  465. for (var t = 0, e = this.children; t < e.length; t++){
  466. var n = e[t];
  467. n && (n.removeObject3D(),
  468. n.removeChildren())
  469. }
  470. this.children.length = 0
  471. }
  472. }
  473. proj4.defs("EPSG:3857", "+title=WGS 84 / Pseudo-Mercator +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs")
  474. //这里地图世界的中心是不是lon:0,lat:0
  475. export class TiledMapOpenStreetMap extends TiledMapBase{
  476. constructor(mapLayer, tileColor){
  477. let baseUrl, attribution, projection, maxDepth;
  478. if(Potree.settings.mapCompany == 'google'){
  479. projection = "EPSG:900913"//"EPSG:4326"//4550
  480. baseUrl = "https://mt2.google.com/vt/lyrs=m@159000000&hl=zh-CN&gl=cn&x=${x}&y=${y}&z=${z}&s=mt1" /* "http://mt2.google.cn/vt/lyrs=m@177000000&hl=zh-CN&gl=cn&src=app&x=${x}&y=${y}&z=${z}" */ //最高只到19
  481. attribution = "© PopSmart, © 谷歌地图"
  482. maxDepth = 22
  483. }else{
  484. projection = "EPSG:3857"
  485. baseUrl = "https://wprd04.is.autonavi.com/appmaptile?lang=zh_cn&style=7&x=${x}&y=${y}&z=${z}" // https://blog.csdn.net/fredricen/article/details/77189453
  486. attribution = "© PopSmart, © 高德地图"
  487. maxDepth = 19
  488. }
  489. super('map', mapLayer, tileColor, projection ) //EPSG projection
  490. //this.baseUrl = "https://wprd03.is.autonavi.com/appmaptile?style=7&x=${x}&y=${y}&z=${z}",
  491. //this.baseUrl = "https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x=${x}&y=${y}&z=${z}" //最高只到18 level
  492. this.maxDepth = maxDepth
  493. this.baseUrl = baseUrl;
  494. this.attribution = attribution
  495. this.tileSizePx = 256
  496. this.mapSizeM = 40075017 //总占据多少米(地球赤道周长) 和三维空间的不一样 - -, 空间上的是直径,地图上的是半个圆周
  497. this.bias = 0.5
  498. }
  499. getTileUrl(t, e, n){
  500. return this.baseUrl.replace(/\${z}/, t.toString(10)).replace(/\${x}/, e.toString(10)).replace(/\${y}/, n.toString(10))
  501. }
  502. }
  503. //yrs=y为混合地图,s为卫星地图,m为普通地图。我们使用谷歌地图的瓦片图层的时候默认采用的是lyrs=s,也就是普通的卫星图层,现在我们希望包含路网信息,只需要设置lyrs=y就OK了。
  504. export class TiledMapFromEntity extends TiledMapBase{
  505. constructor(mapLayer, tileColor, data){
  506. super('floorplan', mapLayer, tileColor, "LOCAL" /* "EPSG:3857" *//* "WGS84" */) //直接就是本地坐标,没有projec
  507. let entity = this.tiledMapEntity = this.fillFromData(data)
  508. let time = entity.updateTime || entity.createTime
  509. this.tileSizePx = entity.tileSizePx,
  510. this.mapSizeM = entity.mapSizeM,
  511. this.maxDepth = entity.maxDepth;
  512. this.postStamp = time ? time.replace(/[^0-9]/ig,'') : (new Date).getTime()
  513. //this.projection = n.crsLocal,
  514. this.zIndex = 0,
  515. this.tilePresenceMap = this.decodeBitStream(this.tiledMapEntity.quadtree) //包含tile分裂信息,如果写错了会造成tile显示不全
  516. }
  517. fillFromData(e){
  518. let data = {}
  519. data.id = e.id
  520. data.globalLocation = Potree.Utils.VectorFactory.fromArray3(e.location)
  521. data.orientation = Potree.Utils.QuaternionFactory.fromArray(e.orientation)
  522. //if(Potree.fileServer){
  523. data.filePath = `${Potree.settings.urls.prefix1}${e.file_path}`
  524. //}else{
  525. // data.filePath = `${Potree.settings.urls.prefix}/data/${Potree.settings.number}/${e.file_path}`
  526. //}
  527. //if(!data.filePath.includes('building_1'))data.filePath = data.filePath.replace('building','building_1')//暂时
  528. data.fileName = '$DEPTH/$X/$Y.png' //e.file_name,
  529. data.type = e.type,
  530. data.mapSizeM = e.map_size_m,
  531. data.tileSizePx = e.tile_size_px,
  532. data.maxDepth = e.max_depth,
  533. data.quadtree = e.quadtree,
  534. data.floorId = e.floor_id,
  535. data.bundleId = e.bundle_id
  536. //this.computeLocalCoordinates()
  537. return data
  538. }
  539. computeLocalCoordinates(){
  540. if(proj4.defs("LOCAL_MAP")){
  541. let lonlat = this.tiledMapEntity.globalLocation
  542. /* if(window.AMapWith84){//需要转换
  543. lonlat = AMapWith84.wgs84ToAMap(lonlat)
  544. } */
  545. lonlat = viewer.transform.lonlatToLocal.forward(lonlat)
  546. this.tiledMapEntity.location = new THREE.Vector3().copy(lonlat)
  547. }
  548. }
  549. updateProjection() {
  550. super.updateProjection()
  551. if(!this.position){
  552. this.computeLocalCoordinates()
  553. }
  554. /* this.projection = this.TransformService.crsLocal,
  555. t.prototype.updateProjection.call(this) */
  556. }
  557. get position(){
  558. return this.tiledMapEntity.location
  559. /* enumerable: !0,
  560. configurable: !0 */
  561. }
  562. get quaternion(){
  563. return this.tiledMapEntity.orientation
  564. /* enumerable: !0,
  565. configurable: !0 */
  566. }
  567. getTileUrl(t, e, n) {
  568. var i = (this.tiledMapEntity.filePath + "/" + this.tiledMapEntity.fileName).replace(/\$DEPTH/g, t.toString(10)).replace(/\$X/g, e.toString(10)).replace(/\$Y/g, n.toString(10));
  569. return i += "?t=" + this.postStamp
  570. //this.RestService.addAuthorizationQueryParameter(i) //????
  571. }
  572. decodeBitStream(t) {
  573. if (!t)
  574. return {
  575. empty: !0
  576. };
  577. for (var e = {}, n = [e], i = 0; i < t.length; i++) {
  578. var r = n.shift()
  579. , o = parseInt(t.substr(i, 1), 16);
  580. if (1 & o) {
  581. var a = {};
  582. r[0] = a,
  583. n.push(a)
  584. }
  585. 2 & o && (a = {},
  586. r[1] = a,
  587. n.push(a)),
  588. 4 & o && (a = {},
  589. r[2] = a,
  590. n.push(a)),
  591. 8 & o && (a = {},
  592. r[3] = a,
  593. n.push(a))
  594. }
  595. var s = {
  596. empty: !0
  597. };
  598. return this.computeHashes(s, e, ""),
  599. s
  600. }
  601. computeHashes(t, e, n) {
  602. for (var i = 0; i < 4; i++)
  603. e[i] && (t[n + i.toString(10)] = !0,
  604. t.empty = !1,
  605. this.computeHashes(t, e[i], n + i.toString(10)))
  606. }
  607. }
  608. /*
  609. note:
  610. 目前缩小了能看出形态是一个地球。相机在高空朝下观测,地球平放着。
  611. 所以越靠近赤道和地球朝上的那面所在的中央经度(也就是local 0,0,0所对应的初始经度),tile越接近正方形。
  612. 所以在两极地区要怎么显示?
  613. 注册地理坐标时需要滚动地球吗?(修改初始经度、重定义NAVVIS:TMERC, 就需要更新所有三维世界中的物体位置)
  614. 切换中心点:
  615. var locationLonLat = viewer.transform.lonlatToLocal.inverse(viewer.mapViewer.camera.position.clone())
  616. proj4.defs("LOCAL_MAP", "+proj=tmerc +ellps=WGS84 +lon_0=" + locationLonLat.x.toPrecision(15) + " +lat_0=" + locationLonLat.y.toPrecision(15));
  617. viewer.mapViewer.mapLayer.maps[0].transformMapToLocal = null
  618. 地理注册部分地图上的1和2标记有两层意思。当地图全屏展示时,标记的是当前右侧经纬度的位置;当地图为小窗时,标记的是对应场景里三维位置。(所以感觉最好换一个ui?)且在2023.2.1之前才改好,之前都是后者。
  619. 为什么边缘总是有奇怪的mesh,是因为有顶点到背面了吗
  620. https://lbs.amap.com/tools/picker 高德坐标拾取器,但和这里展示的不一样, 要用AMapWith84.aMapToWgs84({x: ,y: })转成84。 (qq or 手机登录)
  621. https://www.google.com/maps/@77.7730021,-34.4952712,4z google取点
  622. 打印所有mapTile的名字,字符串最长的代表有显示的mesh。
  623. viewer.mapViewer.mapLayer.maps[0].baseTile.traverse(function(e){console.log(e.name)})
  624. 能查看有几个显示的mesh
  625. viewer.mapViewer.mapLayer.maps[0].objectGroup.children
  626. */