Babylon.js 构建 地球,支持切片地图 (六)

针对切片管理


针对EarthControl类 主要是管理主类 负责事件监听后重新刷新切片

```javascript

const BABYLON = require('babylonjs');

import {EarthTool} from './EarthTool';

import {BuildTileTool} from './BuildTileTool';

export class EarthControl {

constructor(scene) {

this.scene = scene,

this.rangeAera = 6,

this.maxTilesCount = 512,

this.tilesCount = 0,

this.tilesRemoveCount = 0,

this.level = 0,

this.tiles = [],

this.checkForTile = true,

this.stoprendering = false,

this.nVisible = 0,

this.nHidden = 0,

EarthTool.SetLevel(),

this.addObserver(),

this.camera = this.scene.getCameraByName("EarthCamera"),

this.drawMinimalEarth(),

this._tick = ()=>this.tick(),

this.scene.registerBeforeRender(this._tick),

this.keyHelper()

}

drawMinimalEarth() {

this.pixelXY = EarthTool.LatLongToPixelXY(90, -180, 3),

this.tileXY = EarthTool.PixelXYToTileXY(this.pixelXY.x, this.pixelXY.y),

this.tileInfo = EarthTool.ComputeVisibleTiles(this.tileXY.x, this.tileXY.y, 3, 8, false);

for (let t = 0; t < this.tileInfo.length; t++) { if (void 0 !== EarthControl.TilesbyQuadKey[this.tileInfo[t].quadKey]) continue; this._tileTexturCB = ()=>this.updateGeoInfo();

const i = new BuildTileTool(this.scene,this.tileInfo[t].quadKey,360 / this.tileInfo[t].nFaces,this.tileInfo[t].offsetX,this.tileInfo[t].offsetY,16,this.tileInfo[t].level,this.tileInfo[t].k,this.tileInfo[t].j,this._tileTexturCB);

console.log(360 / this.tileInfo[t].nFaces),

this.tiles.push(i),

EarthControl.TilesbyQuadKey[this.tileInfo[t].quadKey] = i,

this.tilesCount++

}

}

tick() {

this.camera.isDirty ? (this.level = this.getBestLevelResolution(),

this.checkForTile = true) : true === this.checkForTile && (this.level = this.getBestLevelResolution(),

this.getGeoInfo(this.level),

this.checkForTile = false)

}

getBestLevelResolution() {

const e = EarthTool.MapNumberToInterval(this.camera.radius, 50, 150, 0, 50);

return EarthTool.GetBestLevelResolution(e, this.scene.getEngine().getRenderHeight())

}

pointerup(e) {

const t = e.pickInfo.pickedPoint;

t && "earth" === e.pickInfo.pickedMesh.name && EarthTool.Vec3ToLatLong(t, false)

}

getGeoInfo(t) {

const i = EarthTool.CameraToLatlong(this.camera.beta, this.camera.alpha);

this.pixelXY = EarthTool.LatLongToPixelXY(i.x, i.y, t),

this.tileXY = EarthTool.PixelXYToTileXY(this.pixelXY.x, this.pixelXY.y),

this.tileInfo = EarthTool.ComputeVisibleTiles(this.tileXY.x, this.tileXY.y, t, this.rangeAera, true);

for (let n = 0; n < this.tileInfo.length; n++) { if (void 0 !== EarthControl.TilesbyQuadKey[this.tileInfo[n].quadKey]) continue; const t = new BuildTileTool(this.scene,this.tileInfo[n].quadKey,360 / this.tileInfo[n].nFaces,this.tileInfo[n].offsetX,this.tileInfo[n].offsetY,8,this.tileInfo[n].level,this.tileInfo[n].k,this.tileInfo[n].j,this._tileTexturCB); this.tiles.push(t), EarthControl.TilesbyQuadKey[this.tileInfo[n].quadKey] = t, this.tilesCount++ } this.topleftLatLon = void 0, this.showHide(this.level), this.removeTile() } updateGeoInfo() { this.time = Date.now(), this.checkInterval || (this.checkInterval = setInterval(()=>{

Date.now() - this.time >= 150 && (this.level = this.getBestLevelResolution(),

this.getGeoInfo(this.level),

clearInterval(this.checkInterval),

this.checkInterval = null)

}

, 150))

 更多参考 https://xiaozhuanlan.com/topic/2753480169

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容