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 5.9KB

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