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.

Tileset.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 rotation = Cesium.Matrix4.fromRotationTranslation(
  89. Cesium.Matrix3.fromHeadingPitchRoll(
  90. new Cesium.HeadingPitchRoll(
  91. Cesium.Math.toRadians(position.heading),
  92. Cesium.Math.toRadians(position.pitch),
  93. Cesium.Math.toRadians(position.roll)
  94. )
  95. )
  96. )
  97. Cesium.Matrix4.multiply(modelMatrix, rotation, modelMatrix)
  98. tileset.root.transform = modelMatrix
  99. })
  100. return this
  101. }
  102. /**
  103. *
  104. * @param heading
  105. * @param pitch
  106. * @param roll
  107. * @returns {Tileset}
  108. */
  109. setHeadingPitchRoll(heading, pitch, roll) {
  110. this.readyPromise.then(tileset => {
  111. let modelMatrix = tileset.root.transform
  112. let rotation = Cesium.Matrix4.fromRotationTranslation(
  113. Cesium.Matrix3.fromHeadingPitchRoll(
  114. new Cesium.HeadingPitchRoll(
  115. Cesium.Math.toRadians(heading || 0),
  116. Cesium.Math.toRadians(pitch || 0),
  117. Cesium.Math.toRadians(roll || 0)
  118. )
  119. )
  120. )
  121. Cesium.Matrix4.multiply(modelMatrix, rotation, modelMatrix)
  122. tileset.root.transform = modelMatrix
  123. })
  124. return this
  125. }
  126. /**
  127. *
  128. * @param {*} text
  129. * @param {*} textStyle
  130. */
  131. setLabel(text, textStyle) {
  132. return this
  133. }
  134. /**
  135. * Clamps To Ground
  136. * @returns {Tileset}
  137. */
  138. clampToGround() {
  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. 0
  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 height
  164. * @param height
  165. * @param isAbsolute
  166. * @returns {Tileset}
  167. */
  168. setHeight(height, isAbsolute = false) {
  169. this.readyPromise.then(tileset => {
  170. let center = Cesium.Cartographic.fromCartesian(
  171. tileset.boundingSphere.center
  172. )
  173. let surface = Cesium.Cartesian3.fromRadians(
  174. center.longitude,
  175. center.latitude,
  176. center.height
  177. )
  178. let offset = Cesium.Cartesian3.fromRadians(
  179. center.longitude,
  180. center.latitude,
  181. isAbsolute ? height : center.height + height
  182. )
  183. let translation = Cesium.Cartesian3.subtract(
  184. offset,
  185. surface,
  186. new Cesium.Cartesian3()
  187. )
  188. tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation)
  189. })
  190. return this
  191. }
  192. /**
  193. * Sets scale
  194. * @param scale
  195. * @returns {Tileset}
  196. */
  197. setScale(scale) {
  198. this.readyPromise.then(tileset => {
  199. let modelMatrix = tileset.root.transform
  200. if (scale > 0 && scale !== 1) {
  201. Cesium.Matrix4.multiplyByUniformScale(modelMatrix, scale, modelMatrix)
  202. }
  203. tileset.root.transform = modelMatrix
  204. })
  205. return this
  206. }
  207. /**
  208. * Sets feature property
  209. * @param properties
  210. * @returns {Tileset}
  211. */
  212. setProperties(properties) {
  213. this._properties = properties
  214. this._bindVisibleEvent()
  215. return this
  216. }
  217. /**
  218. * Sets feature FS
  219. * @param customShader
  220. * @returns {Tileset}
  221. */
  222. setCustomShader(customShader) {
  223. this._customShader = customShader
  224. this._bindVisibleEvent()
  225. return this
  226. }
  227. /**
  228. * Sets style
  229. * @param style
  230. * @returns {Tileset}
  231. */
  232. setStyle(style) {
  233. if (style && style instanceof Cesium.Cesium3DTileStyle) {
  234. this._style = style
  235. this._delegate.style = this._style
  236. }
  237. return this
  238. }
  239. }
  240. Overlay.registerType('tileset')
  241. export default Tileset