You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViewerOption.js 5.4KB

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