您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ViewerOption.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 skyBoxOption = this._options.skyBox
  70. if (skyBoxOption instanceof Cesium.SkyBox) {
  71. this._viewer.scene.skyBox = skyBoxOption
  72. } else {
  73. let skyBox = this._viewer.scene.skyBox
  74. skyBox.show = skyBoxOption.show ?? true
  75. if (skyBoxOption.offsetAngle) {
  76. skyBox.offsetAngle = skyBoxOption.offsetAngle
  77. }
  78. if (skyBoxOption?.sources) {
  79. skyBox.sources = skyBoxOption.sources
  80. }
  81. }
  82. return this
  83. }
  84. /**
  85. * Sets globe option
  86. * @returns {ViewerOption}
  87. * @private
  88. */
  89. _setGlobeOption() {
  90. if (!this._options.globe) {
  91. return this
  92. }
  93. let globe = this._viewer.scene.globe
  94. let globeOption = this._options.globe
  95. Util.merge(globe, {
  96. show: globeOption?.show ?? true,
  97. showGroundAtmosphere: globeOption?.showGroundAtmosphere ?? true,
  98. enableLighting: globeOption?.enableLighting ?? false,
  99. depthTestAgainstTerrain: globeOption?.depthTestAgainstTerrain ?? false,
  100. tileCacheSize: +globeOption?.tileCacheSize || 100,
  101. preloadSiblings: globeOption?.enableLighting ?? false,
  102. baseColor: globeOption?.baseColor || new Cesium.Color(0, 0, 0.5, 1),
  103. terrainExaggeration: globeOption?.terrainExaggeration || 1,
  104. terrainExaggerationRelativeHeight:
  105. globeOption?.terrainExaggerationRelativeHeight || 0
  106. })
  107. Util.merge(globe.translucency, {
  108. enabled: globeOption?.translucency?.enabled ?? false,
  109. backFaceAlpha: +globeOption?.translucency?.backFaceAlpha || 1,
  110. backFaceAlphaByDistance:
  111. globeOption?.translucency?.backFaceAlphaByDistance,
  112. frontFaceAlpha: +globeOption?.translucency?.frontFaceAlpha || 1,
  113. frontFaceAlphaByDistance:
  114. globeOption?.translucency?.frontFaceAlphaByDistance
  115. })
  116. if (globeOption?.filterColor) {
  117. let filterColor = globeOption?.filterColor
  118. let globeFS = globe._surfaceShaderSet.baseFragmentShaderSource
  119. let oriShder = globeFS.sources[globeFS.sources.length - 1]
  120. globeFS.sources[globeFS.sources.length - 1] = oriShder.replace(
  121. 'gl_FragColor = finalColor;',
  122. `gl_FragColor = finalColor * vec4(${filterColor.red},${filterColor.green},${filterColor.blue},${filterColor.alpha});`
  123. )
  124. }
  125. return this
  126. }
  127. /**
  128. *
  129. * @returns {ViewerOption}
  130. * @private
  131. */
  132. _setCameraController() {
  133. if (!this._options?.cameraController) {
  134. return this
  135. }
  136. let sscc = this._viewer.scene.screenSpaceCameraController
  137. let cameraController = this._options.cameraController
  138. Util.merge(sscc, {
  139. enableInputs: cameraController?.enableInputs ?? true,
  140. enableRotate: cameraController?.enableRotate ?? true,
  141. enableTilt: cameraController?.enableTilt ?? true,
  142. enableTranslate: cameraController?.enableTranslate ?? true,
  143. enableZoom: cameraController?.enableZoom ?? true,
  144. enableCollisionDetection:
  145. cameraController?.enableCollisionDetection ?? true,
  146. minimumZoomDistance: +cameraController?.minimumZoomDistance || 1.0,
  147. maximumZoomDistance: +cameraController?.maximumZoomDistance || 40489014.0
  148. })
  149. return this
  150. }
  151. /**
  152. * Sets options
  153. * @param options
  154. * @returns {ViewerOption}
  155. */
  156. setOptions(options) {
  157. if (Object.keys(options).length === 0) {
  158. return this
  159. }
  160. this._options = {
  161. ...this._options,
  162. ...options
  163. }
  164. this._setViewerOption()
  165. ._setCanvasOption()
  166. ._setSceneOption()
  167. ._setSkyBoxOption()
  168. ._setGlobeOption()
  169. ._setCameraController()
  170. return this
  171. }
  172. }
  173. export default ViewerOption