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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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._viewer.scene.screenSpaceCameraController.maximumZoomDistance = 40489014.0
  11. this._options = {}
  12. }
  13. /**
  14. * Sets viewer option
  15. * @returns {ViewerOption}
  16. * @private
  17. */
  18. _setViewerOption() {
  19. this._viewer.delegate.shadows = this._options?.shadows ?? false
  20. this._viewer.delegate.resolutionScale =
  21. this._options?.resolutionScale || 1.0
  22. return this
  23. }
  24. /**
  25. * sets canvas option
  26. * @returns {ViewerOption}
  27. * @private
  28. */
  29. _setCanvasOption() {
  30. this._options.tabIndex &&
  31. this._viewer.scene.canvas.setAttribute('tabIndex', this._options.tabIndex)
  32. return this
  33. }
  34. /**
  35. * Sets scene option
  36. * @returns {ViewerOption}
  37. * @private
  38. */
  39. _setSceneOption() {
  40. let scene = this._viewer.scene
  41. scene.skyAtmosphere.show = this._options.showAtmosphere ?? true
  42. scene.sun.show = this._options.showSun ?? true
  43. scene.moon.show = this._options.showMoon ?? true
  44. scene.postProcessStages.fxaa.enabled = this._options.enableFxaa ?? false
  45. if (scene.msaaSupported) {
  46. scene.msaaSamples = +this._options.msaaSamples || 1
  47. }
  48. return this
  49. }
  50. /**
  51. *
  52. * @returns {ViewerOption}
  53. * @private
  54. */
  55. _setSkyBoxOption() {
  56. if (!this._options.skyBox) {
  57. return this
  58. }
  59. let skyBoxOption = this._options.skyBox
  60. if (skyBoxOption instanceof Cesium.SkyBox) {
  61. this._viewer.scene.skyBox = skyBoxOption
  62. } else {
  63. let skyBox = this._viewer.scene.skyBox
  64. skyBox.show = skyBoxOption.show ?? true
  65. if (skyBoxOption.offsetAngle) {
  66. skyBox.offsetAngle = skyBoxOption.offsetAngle
  67. }
  68. if (skyBoxOption?.sources) {
  69. skyBox.sources = skyBoxOption.sources
  70. }
  71. }
  72. return this
  73. }
  74. /**
  75. * Sets globe option
  76. * @returns {ViewerOption}
  77. * @private
  78. */
  79. _setGlobeOption() {
  80. if (!this._options.globe) {
  81. return this
  82. }
  83. let globe = this._viewer.scene.globe
  84. let globeOption = this._options.globe
  85. Util.merge(globe, {
  86. show: globeOption?.show ?? true,
  87. showGroundAtmosphere: globeOption?.showGroundAtmosphere ?? true,
  88. enableLighting: globeOption?.enableLighting ?? false,
  89. depthTestAgainstTerrain: globeOption?.depthTestAgainstTerrain ?? false,
  90. tileCacheSize: +globeOption?.tileCacheSize || 100,
  91. preloadSiblings: globeOption?.enableLighting ?? false,
  92. baseColor: globeOption?.baseColor || new Cesium.Color(0, 0, 0.5, 1),
  93. terrainExaggeration: globeOption?.terrainExaggeration || 1,
  94. terrainExaggerationRelativeHeight:
  95. globeOption?.terrainExaggerationRelativeHeight || 0
  96. })
  97. Util.merge(globe.translucency, {
  98. enabled: globeOption?.translucency?.enabled ?? false,
  99. backFaceAlpha: +globeOption?.translucency?.backFaceAlpha || 1,
  100. backFaceAlphaByDistance:
  101. globeOption?.translucency?.backFaceAlphaByDistance,
  102. frontFaceAlpha: +globeOption?.translucency?.frontFaceAlpha || 1,
  103. frontFaceAlphaByDistance:
  104. globeOption?.translucency?.frontFaceAlphaByDistance
  105. })
  106. if (globeOption?.filterColor) {
  107. let filterColor = globeOption?.filterColor
  108. let globeFS = globe._surfaceShaderSet.baseFragmentShaderSource
  109. let oriShder = globeFS.sources[globeFS.sources.length - 1]
  110. globeFS.sources[globeFS.sources.length - 1] = oriShder.replace(
  111. 'gl_FragColor = finalColor;',
  112. `gl_FragColor = finalColor * vec4(${filterColor.red.toFixed(
  113. 2
  114. )},${filterColor.green.toFixed(2)},${filterColor.blue.toFixed(
  115. 2
  116. )},${filterColor.alpha.toFixed(2)});`
  117. )
  118. }
  119. return this
  120. }
  121. /**
  122. *
  123. * @returns {ViewerOption}
  124. * @private
  125. */
  126. _setCameraController() {
  127. if (!this._options?.cameraController) {
  128. return this
  129. }
  130. let sscc = this._viewer.scene.screenSpaceCameraController
  131. let cameraController = this._options.cameraController
  132. Util.merge(sscc, {
  133. enableInputs: cameraController?.enableInputs ?? true,
  134. enableRotate: cameraController?.enableRotate ?? true,
  135. enableTilt: cameraController?.enableTilt ?? true,
  136. enableTranslate: cameraController?.enableTranslate ?? true,
  137. enableZoom: cameraController?.enableZoom ?? true,
  138. enableCollisionDetection:
  139. cameraController?.enableCollisionDetection ?? true,
  140. minimumZoomDistance: +cameraController?.minimumZoomDistance || 1.0,
  141. maximumZoomDistance: +cameraController?.maximumZoomDistance || 40489014.0
  142. })
  143. return this
  144. }
  145. /**
  146. * Sets options
  147. * @param options
  148. * @returns {ViewerOption}
  149. */
  150. setOptions(options) {
  151. if (Object.keys(options).length === 0) {
  152. return this
  153. }
  154. this._options = {
  155. ...this._options,
  156. ...options
  157. }
  158. this._setViewerOption()
  159. ._setCanvasOption()
  160. ._setSceneOption()
  161. ._setSkyBoxOption()
  162. ._setGlobeOption()
  163. ._setCameraController()
  164. return this
  165. }
  166. }
  167. export default ViewerOption