選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Tileset.js 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /**
  2. * @Author : Caven Chen
  3. */
  4. import { Cesium } from '../../../namespace'
  5. import Overlay from '../Overlay'
  6. import Parse from '../../parse/Parse'
  7. import State from '../../state/State'
  8. import { TilesetEvent } from '../../event'
  9. class Tileset extends Overlay {
  10. constructor(url, options = {}) {
  11. super()
  12. this._delegate = Cesium.Cesium3DTileset.fromUrl(url, options)
  13. this._tilesetEvent = new TilesetEvent(this._delegate)
  14. this._tileVisibleCallback = undefined
  15. this._properties = undefined
  16. this._state = State.INITIALIZED
  17. }
  18. get type() {
  19. return Overlay.getOverlayType('tileset')
  20. }
  21. set show(show) {
  22. this._show = show
  23. this._delegate.then((tileset) => {
  24. tileset.show = show
  25. })
  26. }
  27. /**
  28. *
  29. * @private
  30. */
  31. _bindVisibleEvent() {
  32. this._tileVisibleCallback && this._tileVisibleCallback()
  33. this._tileVisibleCallback = this._delegate.then((tileset) => {
  34. tileset.tileVisible.addEventListener(this._updateTile, this)
  35. })
  36. }
  37. /**
  38. * Updates tile
  39. * @param tile
  40. * @private
  41. */
  42. _updateTile(tile) {
  43. let content = tile.content
  44. // sets properties
  45. for (let i = 0; i < content.featuresLength; i++) {
  46. let feature = content.getFeature(i)
  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. }
  61. }
  62. /**
  63. * Sets position
  64. * @param position
  65. * @returns {Tileset}
  66. */
  67. setPosition(position) {
  68. position = Parse.parsePosition(position)
  69. this._delegate.then((tileset) => {
  70. let modelMatrix = Cesium.Transforms.eastNorthUpToFixedFrame(
  71. Cesium.Cartesian3.fromDegrees(position.lng, position.lat, position.alt)
  72. )
  73. let rotation = Cesium.Matrix4.fromRotationTranslation(
  74. Cesium.Matrix3.fromHeadingPitchRoll(
  75. new Cesium.HeadingPitchRoll(
  76. Cesium.Math.toRadians(position.heading),
  77. Cesium.Math.toRadians(position.pitch),
  78. Cesium.Math.toRadians(position.roll)
  79. )
  80. )
  81. )
  82. Cesium.Matrix4.multiply(modelMatrix, rotation, modelMatrix)
  83. tileset.root.transform = modelMatrix
  84. })
  85. return this
  86. }
  87. /**
  88. *
  89. * @param heading
  90. * @param pitch
  91. * @param roll
  92. * @returns {Tileset}
  93. */
  94. setHeadingPitchRoll(heading, pitch, roll) {
  95. this._delegate.then((tileset) => {
  96. let modelMatrix = tileset.root.transform
  97. let rotation = Cesium.Matrix4.fromRotationTranslation(
  98. Cesium.Matrix3.fromHeadingPitchRoll(
  99. new Cesium.HeadingPitchRoll(
  100. Cesium.Math.toRadians(heading || 0),
  101. Cesium.Math.toRadians(pitch || 0),
  102. Cesium.Math.toRadians(roll || 0)
  103. )
  104. )
  105. )
  106. Cesium.Matrix4.multiply(modelMatrix, rotation, modelMatrix)
  107. tileset.root.transform = modelMatrix
  108. })
  109. return this
  110. }
  111. /**
  112. *
  113. * @param {*} text
  114. * @param {*} textStyle
  115. */
  116. setLabel(text, textStyle) {
  117. return this
  118. }
  119. /**
  120. * Clamps To Ground
  121. * @returns {Tileset}
  122. */
  123. clampToGround() {
  124. this._delegate.then((tileset) => {
  125. let center = Cesium.Cartographic.fromCartesian(
  126. tileset.boundingSphere.center
  127. )
  128. let surface = Cesium.Cartesian3.fromRadians(
  129. center.longitude,
  130. center.latitude,
  131. center.height
  132. )
  133. let offset = Cesium.Cartesian3.fromRadians(
  134. center.longitude,
  135. center.latitude,
  136. 0
  137. )
  138. let translation = Cesium.Cartesian3.subtract(
  139. offset,
  140. surface,
  141. new Cesium.Cartesian3()
  142. )
  143. tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation)
  144. })
  145. return this
  146. }
  147. /**
  148. * Sets height
  149. * @param height
  150. * @param isAbsolute
  151. * @returns {Tileset}
  152. */
  153. setHeight(height, isAbsolute = false) {
  154. this._delegate.then((tileset) => {
  155. let center = Cesium.Cartographic.fromCartesian(
  156. tileset.boundingSphere.center
  157. )
  158. let surface = Cesium.Cartesian3.fromRadians(
  159. center.longitude,
  160. center.latitude,
  161. center.height
  162. )
  163. let offset = Cesium.Cartesian3.fromRadians(
  164. center.longitude,
  165. center.latitude,
  166. isAbsolute ? height : center.height + height
  167. )
  168. let translation = Cesium.Cartesian3.subtract(
  169. offset,
  170. surface,
  171. new Cesium.Cartesian3()
  172. )
  173. tileset.modelMatrix = Cesium.Matrix4.fromTranslation(translation)
  174. })
  175. return this
  176. }
  177. /**
  178. * Sets scale
  179. * @param scale
  180. * @returns {Tileset}
  181. */
  182. setScale(scale) {
  183. this._delegate.then((tileset) => {
  184. let modelMatrix = tileset.root.transform
  185. if (scale > 0 && scale !== 1) {
  186. Cesium.Matrix4.multiplyByUniformScale(modelMatrix, scale, modelMatrix)
  187. }
  188. tileset.root.transform = modelMatrix
  189. })
  190. return this
  191. }
  192. /**
  193. * Sets feature property
  194. * @param properties
  195. * @returns {Tileset}
  196. */
  197. setProperties(properties) {
  198. this._properties = properties
  199. this._bindVisibleEvent()
  200. return this
  201. }
  202. /**
  203. *
  204. * @param splitDirection
  205. * @return {Tileset}
  206. */
  207. setSplitDirection(splitDirection) {
  208. this._delegate.then((tileset) => {
  209. tileset.splitDirection = splitDirection
  210. })
  211. return this
  212. }
  213. /**
  214. *
  215. * @param customShader
  216. * @return {Tileset}
  217. */
  218. setCustomShader(customShader) {
  219. this._delegate.then((tileset) => {
  220. tileset.customShader = customShader
  221. })
  222. return this
  223. }
  224. /**
  225. * Sets style
  226. * @param style
  227. * @returns {Tileset}
  228. */
  229. setStyle(style) {
  230. if (style && style instanceof Cesium.Cesium3DTileStyle) {
  231. this._style = style
  232. this._delegate.then((tileset) => {
  233. tileset.style = style
  234. })
  235. }
  236. return this
  237. }
  238. /**
  239. * Subscribe event
  240. * @param type
  241. * @param callback
  242. * @param context
  243. * @returns {Overlay}
  244. */
  245. on(type, callback, context) {
  246. this._overlayEvent.on(type, callback, context || this)
  247. this._tilesetEvent.on(type, callback, context || this)
  248. return this
  249. }
  250. /**
  251. * Unsubscribe event
  252. * @param type
  253. * @param callback
  254. * @param context
  255. * @returns {Overlay}
  256. */
  257. off(type, callback, context) {
  258. this._overlayEvent.off(type, callback, context || this)
  259. this._tilesetEvent.off(type, callback, context || this)
  260. return this
  261. }
  262. }
  263. Overlay.registerType('tileset')
  264. export default Tileset