Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ViewerOption.js 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * @Author: Caven
  3. * @Date: 2019-12-30 09:24:37
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import { Util } from '@dc-modules/utils'
  7. class ViewerOption {
  8. constructor(viewer) {
  9. this._viewer = viewer
  10. this._options = {}
  11. this._init()
  12. }
  13. /**
  14. * Init viewer
  15. * @private
  16. */
  17. _init() {
  18. this._viewer.delegate.cesiumWidget.creditContainer.style.display = 'none'
  19. this._viewer.delegate.cesiumWidget.screenSpaceEventHandler.removeInputAction(
  20. Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK
  21. )
  22. this._viewer.scene.screenSpaceCameraController.maximumZoomDistance = 40489014.0
  23. this._viewer.scene.backgroundColor = Cesium.Color.TRANSPARENT
  24. this._viewer.delegate.imageryLayers.removeAll()
  25. }
  26. /**
  27. * Sets viewer option
  28. * @returns {ViewerOption}
  29. * @private
  30. */
  31. _setViewerOption() {
  32. this._viewer.delegate.shadows = this._options?.shadows ?? false
  33. this._viewer.delegate.resolutionScale =
  34. this._options?.resolutionScale || 1.0
  35. return this
  36. }
  37. /**
  38. * sets canvas option
  39. * @returns {ViewerOption}
  40. * @private
  41. */
  42. _setCanvasOption() {
  43. this._options.tabIndex &&
  44. this._viewer.scene.canvas.setAttribute('tabIndex', this._options.tabIndex)
  45. return this
  46. }
  47. /**
  48. * Sets scene option
  49. * @returns {ViewerOption}
  50. * @private
  51. */
  52. _setSceneOption() {
  53. let scene = this._viewer.scene
  54. scene.skyAtmosphere.show = this._options.showAtmosphere ?? true
  55. scene.sun.show = this._options.showSun ?? true
  56. scene.moon.show = this._options.showMoon ?? true
  57. scene.postProcessStages.fxaa.enabled = this._options.enableFxaa ?? false
  58. return this
  59. }
  60. /**
  61. *
  62. * @returns {ViewerOption}
  63. * @private
  64. */
  65. _setSkyBoxOption() {
  66. if (!this._options.skyBox) {
  67. return this
  68. }
  69. let skyBox = this._viewer.scene.skyBox
  70. let skyBoxOption = this._options.skyBox
  71. skyBox.show = skyBoxOption.show ?? true
  72. skyBox.offsetAngle = skyBoxOption.offsetAngle || 0
  73. if (skyBoxOption.sources) {
  74. skyBox.sources = skyBoxOption?.sources
  75. }
  76. return this
  77. }
  78. /**
  79. * Sets globe option
  80. * @returns {ViewerOption}
  81. * @private
  82. */
  83. _setGlobeOption() {
  84. if (!this._options.globe) {
  85. return this
  86. }
  87. let globe = this._viewer.scene.globe
  88. let globeOption = this._options.globe
  89. Util.merge(globe, {
  90. show: globeOption?.show ?? true,
  91. showGroundAtmosphere: globeOption?.showGroundAtmosphere ?? true,
  92. enableLighting: globeOption?.enableLighting ?? false,
  93. depthTestAgainstTerrain: globeOption?.depthTestAgainstTerrain ?? false,
  94. tileCacheSize: +globeOption?.tileCacheSize || 100,
  95. preloadSiblings: globeOption?.enableLighting ?? false,
  96. baseColor: globeOption?.baseColor || new Cesium.Color(0, 0, 0.5, 1)
  97. })
  98. Util.merge(globe.translucency, {
  99. enabled: globeOption?.translucency?.enabled ?? false,
  100. backFaceAlpha: +globeOption?.translucency?.backFaceAlpha || 1,
  101. backFaceAlphaByDistance:
  102. globeOption?.translucency?.backFaceAlphaByDistance,
  103. frontFaceAlpha: +globeOption?.translucency?.frontFaceAlpha || 1,
  104. frontFaceAlphaByDistance:
  105. globeOption?.translucency?.frontFaceAlphaByDistance
  106. })
  107. return this
  108. }
  109. /**
  110. *
  111. * @returns {ViewerOption}
  112. * @private
  113. */
  114. _setCameraController() {
  115. if (!this._options?.cameraController) {
  116. return this
  117. }
  118. let sscc = this._viewer.scene.screenSpaceCameraController
  119. let cameraController = this._options.cameraController
  120. Util.merge(sscc, {
  121. enableInputs: cameraController?.enableInputs ?? true,
  122. enableRotate: cameraController?.enableRotate ?? true,
  123. enableTilt: cameraController?.enableTilt ?? true,
  124. enableTranslate: cameraController?.enableTranslate ?? true,
  125. enableZoom: cameraController?.enableZoom ?? true,
  126. enableCollisionDetection:
  127. cameraController?.enableCollisionDetection ?? true,
  128. minimumZoomDistance: +cameraController?.minimumZoomDistance || 1.0,
  129. maximumZoomDistance: +cameraController?.maximumZoomDistance || 40489014.0
  130. })
  131. return this
  132. }
  133. /**
  134. * Sets options
  135. * @param options
  136. * @returns {ViewerOption}
  137. */
  138. setOptions(options) {
  139. if (Object.keys(options).length === 0) {
  140. return this
  141. }
  142. this._options = {
  143. ...this._options,
  144. ...options
  145. }
  146. this._setViewerOption()
  147. ._setCanvasOption()
  148. ._setSceneOption()
  149. ._setSkyBoxOption()
  150. ._setGlobeOption()
  151. ._setCameraController()
  152. return this
  153. }
  154. }
  155. export default ViewerOption