core/Terrain.js

/*
 * 地形主类
 * @Author: jianlei wang
 * @Date: 2024-02-21 13:43:22
 * @Last Modified by: jianlei wang
 * @Last Modified time: 2024-03-28 11:22:52
 */
import { Cesium } from '../../namespace'
import { boolTerrain } from '../utils/Terrain'
import TerrainExcavation from '../utils/TerrainExcavation'

/**
 * 地形主类
 * @class
 */
class Terrain {
  /**
   * 构造函数
   * @param {Object} viewer 地图场景对象
   * @see {@link TerrainExcavation} - 地形开挖类
   */
  constructor(viewer) {
    this._viewer = viewer
    /**
     * 地形开挖类
     * @type {TerrainExcavation}
     */
    this.TerrainExcavation = new TerrainExcavation(this._viewer)
    this._alpha = 1.0
    this._updateTranslucency(false)
    this._provider = this._viewer.terrainProvider
  }

  /**
   * 地形对象,参考Cesium的TerrainProvider类
   * @readonly
   * @type {TerrainProvider}
   */
  get provider() {
    return this._viewer.terrainProvider
  }
  /**
   * 是否显示地形
   * @param {boolean} bool
   */
  set show(bool) {
    const terrain = boolTerrain(this._viewer)
    if (Boolean(terrain) === bool) return
    if (bool) {
      this._viewer.terrainProvider = this._provider
    } else {
      this._provider = this._viewer.terrainProvider
      this._viewer.terrainProvider = new Cesium.EllipsoidTerrainProvider({})
    }
  }

  /**
   * 深度检测
   * @type {Boolean}
   */
  get depthTest() {
    return this._viewer.scene.globe.depthTestAgainstTerrain
  }
  set depthTest(val) {
    this._viewer.scene.globe.depthTestAgainstTerrain = val
  }

  /**
   * 地形夸张系数
   * @type {Number}
   */
  get exaggeration() {
    return this._viewer.scene.verticalExaggeration
  }
  set exaggeration(scale) {
    this._viewer.scene.verticalExaggeration = scale
  }

  /**
   * 地表透明度,只有在开启碰撞检测的时候才能生效
   * @type {Number}
   */
  get alpha() {
    return this._alpha
  }
  set alpha(val) {
    this._updateAlpha(val)
    this._alpha = val
  }

  /**
   * 地表碰撞检测
   * @type {Boolean}
   */
  get translucency() {
    return this._viewer.scene.globe.translucency.enabled
  }
  set translucency(bool) {
    this._updateTranslucency(bool)
  }

  /**
   * 是否允许进入地下
   * @type {Boolean}
   */
  get enableUnderground() {
    return !this._viewer.scene.screenSpaceCameraController
      .enableCollisionDetection
  }
  set enableUnderground(bool) {
    this._viewer.scene.screenSpaceCameraController.enableCollisionDetection =
      !bool
  }

  _updateAlpha(val) {
    const frontFaceAlphaByDistance =
      this._viewer.scene.globe.translucency.frontFaceAlphaByDistance
    frontFaceAlphaByDistance.nearValue = val
    frontFaceAlphaByDistance.farValue = val
  }

  _updateTranslucency(bool) {
    this._viewer.scene.globe.translucency.frontFaceAlphaByDistance =
      new Cesium.NearFarScalar(1.5e2, 0.5, 8.0e6, 1.0)
    this._viewer.scene.globe.translucency.enabled = bool //是否开启透明
    this._updateAlpha(this._alpha)
  }
}

export default Terrain