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.

WaterPrimitive.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-10-11 18:24:37
  4. */
  5. import { Cesium } from '@dc-modules/namespace'
  6. import State from '@dc-modules/state/State'
  7. import Parse from '@dc-modules/parse/Parse'
  8. import { Util } from '@dc-modules/utils'
  9. import { Transform } from '@dc-modules/transform'
  10. import Overlay from '../Overlay'
  11. class WaterPrimitive extends Overlay {
  12. constructor(positions, holes = []) {
  13. super()
  14. this._positions = Parse.parsePositions(positions)
  15. this._holes = holes.map(item => Parse.parsePositions(item))
  16. this._delegate = new Cesium.GroundPrimitive({
  17. geometryInstances: new Cesium.GeometryInstance({
  18. geometry: {}
  19. }),
  20. asynchronous: true
  21. })
  22. this._state = State.INITIALIZED
  23. }
  24. get type() {
  25. return Overlay.getOverlayType('water_primitive')
  26. }
  27. set positions(positions) {
  28. this._positions = Parse.parsePositions(positions)
  29. this._delegate.geometryInstances.geometry = new Cesium.PolygonGeometry({
  30. polygonHierarchy: new Cesium.PolygonHierarchy(
  31. Transform.transformWGS84ArrayToCartesianArray(this._positions),
  32. this._holes.map(
  33. item =>
  34. new Cesium.PolygonHierarchy(
  35. Transform.transformWGS84ArrayToCartesianArray(item)
  36. )
  37. )
  38. ),
  39. height: this._style?.height,
  40. extrudedHeight: this._style?.extrudedHeight,
  41. closeTop: this._style?.closeTop,
  42. closeBottom: this._style?.closeBottom,
  43. vertexFormat: Cesium.EllipsoidSurfaceAppearance.VERTEX_FORMAT
  44. })
  45. return this
  46. }
  47. get positions() {
  48. return this._positions
  49. }
  50. /**
  51. *
  52. * @private
  53. */
  54. _setAppearance() {
  55. if (!this._style) {
  56. return
  57. }
  58. this._delegate.appearance = new Cesium.EllipsoidSurfaceAppearance({
  59. material: Cesium.Material.fromType('Water', {
  60. baseWaterColor:
  61. this._style?.baseWaterColor || new Cesium.Color(0.2, 0.3, 0.6, 1.0),
  62. blendColor:
  63. this._style?.blendColor || new Cesium.Color(0.0, 1.0, 0.699, 1.0),
  64. specularMap: this._style?.specularMap || Cesium.Material.DefaultImageId,
  65. normalMap: this._style?.normalMap || Cesium.Material.DefaultImageId,
  66. frequency: this._style?.frequency || 1000.0,
  67. animationSpeed: this._style?.animationSpeed || 0.01,
  68. amplitude: this._style?.amplitude || 10,
  69. specularIntensity: this._style?.specularIntensity || 0.5
  70. })
  71. })
  72. }
  73. _mountedHook() {
  74. /**
  75. * set the positions
  76. */
  77. this.positions = this._positions
  78. /**
  79. * set the appearance
  80. */
  81. !this._delegate.appearance && this._setAppearance()
  82. }
  83. /**
  84. * Sets Style
  85. * @param style
  86. * @returns {WaterPrimitive}
  87. */
  88. setStyle(style) {
  89. if (Object.keys(style).length === 0) {
  90. return this
  91. }
  92. Util.merge(this._style, style)
  93. if (this._style?.classificationType) {
  94. this._delegate.classificationType = this._style.classificationType
  95. }
  96. this._setAppearance()
  97. return this
  98. }
  99. }
  100. Overlay.registerType('water_primitive')
  101. export default WaterPrimitive