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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * @Author: Caven
  3. * @Date: 2020-01-07 20:51:56
  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 Overlay from '../Overlay'
  9. class Tileset extends Overlay {
  10. constructor(url, options = {}) {
  11. super()
  12. this._delegate = new Cesium.Cesium3DTileset({
  13. ...options,
  14. url: url
  15. })
  16. this._tileVisibleCallback = undefined
  17. this._properties = undefined
  18. this._customShader = undefined
  19. this.type = Overlay.getOverlayType('tileset')
  20. this._state = State.INITIALIZED
  21. }
  22. get readyPromise() {
  23. return this._delegate.readyPromise
  24. }
  25. /**
  26. *
  27. * @private
  28. */
  29. _bindVisibleEvent() {
  30. this._tileVisibleCallback && this._tileVisibleCallback()
  31. this._tileVisibleCallback = this._delegate.tileVisible.addEventListener(
  32. this._updateTile,
  33. this
  34. )
  35. }
  36. /**
  37. * Updates tile
  38. * @param tile
  39. * @private
  40. */
  41. _updateTile(tile) {
  42. let content = tile.content
  43. for (let i = 0; i < content.featuresLength; i++) {
  44. let feature = content.getFeature(i)
  45. let model = feature.content._model
  46. // sets properties
  47. if (this._properties && this._properties.length) {
  48. this._properties.forEach(property => {
  49. if (
  50. feature.hasProperty(property['key']) &&
  51. feature.getProperty(property['key']) === property['keyValue']
  52. ) {
  53. feature.setProperty(
  54. property['propertyName'],
  55. property['propertyValue']
  56. )
  57. }
  58. })
  59. }
  60. // sets customShader
  61. if (
  62. this._customShader &&
  63. model &&
  64. model._sourcePrograms &&
  65. model._rendererResources
  66. ) {
  67. Object.keys(model._sourcePrograms).forEach(key => {
  68. let program = model._sourcePrograms[key]
  69. model._rendererResources.sourceShaders[
  70. program.fragmentShader
  71. ] = this._customShader
  72. })
  73. model._shouldRegenerateShaders = true
  74. }
  75. }
  76. }
  77. /**
  78. * Sets position
  79. * @param position
  80. * @returns {Tileset}
  81. */
  82. setPosition(position) {
  83. position = Parse.parsePosition(position)
  84. this.readyPromise.then(tileset => {
  85. let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(
  86. Cesium.Cartesian3.fromDegrees(position.lng, position.lat, position.alt)
  87. )
  88. let rotationX = Cesium.Matrix4.fromRotationTranslation(
  89. Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(position.heading))
  90. )
  91. Cesium.Matrix4.multiply(modelMatrix, rotationX, modelMatrix)
  92. tileset.root.transform = modelMatrix
  93. })
  94. return this
  95. }
  96. /**
  97. *
  98. * @param {*} text
  99. * @param {*} textStyle
  100. */
  101. setLabel(text, textStyle) {
  102. return this
  103. }
  104. /**
  105. * Clamps To Ground
  106. * @returns {Tileset}
  107. */
  108. clampToGround() {
  109. this.readyPromise.then(tileset => {
  110. let center = Cesium.Cartographic.fromCartesian(
  111. tileset.boundingSphere.center
  112. )
  113. let surface = Cesium.Cartesian3.fromRadians(
  114. center.longitude,
  115. center.latitude,
  116. center.height
  117. )
  118. let offset = Cesium.Cartesian3.fromRadians(
  119. center.longitude,
  120. center.latitude,
  121. 0
  122. )
  123. let translation = Cesium.Cartesian3.subtract(
  124. offset,
  125. surface,
  126. new Cesium.Cartesian3()
  127. )
  128. tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation)
  129. })
  130. return this
  131. }
  132. /**
  133. * Sets height
  134. * @param height
  135. * @param isAbsolute
  136. * @returns {Tileset}
  137. */
  138. setHeight(height, isAbsolute = false) {
  139. this.readyPromise.then(tileset => {
  140. let center = Cesium.Cartographic.fromCartesian(
  141. tileset.boundingSphere.center
  142. )
  143. let surface = Cesium.Cartesian3.fromRadians(
  144. center.longitude,
  145. center.latitude,
  146. center.height
  147. )
  148. let offset = Cesium.Cartesian3.fromRadians(
  149. center.longitude,
  150. center.latitude,
  151. isAbsolute ? height : center.height + height
  152. )
  153. let translation = Cesium.Cartesian3.subtract(
  154. offset,
  155. surface,
  156. new Cesium.Cartesian3()
  157. )
  158. tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation)
  159. })
  160. return this
  161. }
  162. /**
  163. * Sets scale
  164. * @param scale
  165. * @returns {Tileset}
  166. */
  167. setScale(scale) {
  168. this.readyPromise.then(tileset => {
  169. let modelMatrix = tileset.root.transform
  170. if (scale > 0 && scale !== 1) {
  171. Cesium.Matrix4.multiplyByUniformScale(modelMatrix, scale, modelMatrix)
  172. }
  173. tileset.root.transform = modelMatrix
  174. })
  175. return this
  176. }
  177. /**
  178. * Sets feature property
  179. * @param properties
  180. * @returns {Tileset}
  181. */
  182. setProperties(properties) {
  183. this._properties = properties
  184. this._bindVisibleEvent()
  185. return this
  186. }
  187. /**
  188. * Sets feature FS
  189. * @param customShader
  190. * @returns {Tileset}
  191. */
  192. setCustomShader(customShader) {
  193. this._customShader = customShader
  194. this._bindVisibleEvent()
  195. return this
  196. }
  197. /**
  198. * Sets style
  199. * @param style
  200. * @returns {Tileset}
  201. */
  202. setStyle(style) {
  203. if (style && style instanceof Cesium.Cesium3DTileStyle) {
  204. this._style = style
  205. this._delegate.style = this._style
  206. }
  207. return this
  208. }
  209. }
  210. Overlay.registerType('tileset')
  211. export default Tileset