Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

Tileset.js 6.1KB

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