@pirireis/webglobeplugins 0.17.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (274) hide show
  1. package/Math/{angle-calculation.js → angle-calculation.ts} +18 -14
  2. package/Math/{arc-cdf-points.js → arc-cdf-points.ts} +329 -272
  3. package/Math/{arc-generate-points-exponantial.js → arc-generate-points-exponantial.ts} +299 -254
  4. package/Math/{arc.js → arc.ts} +421 -292
  5. package/Math/bounds/line-bbox.js +225 -186
  6. package/Math/{circle-cdf-points.js → circle-cdf-points.ts} +143 -78
  7. package/Math/{circle.js → circle.ts} +49 -33
  8. package/Math/{constants.js → constants.ts} +12 -4
  9. package/Math/contour/{quadtreecontours.js → quadtreecontours.ts} +371 -300
  10. package/Math/contour/quadtreecontours1.js +336 -298
  11. package/Math/{finite-line-2d.js → finite-line-2d.ts} +68 -58
  12. package/Math/haversine.ts +33 -0
  13. package/Math/index.js +0 -1
  14. package/Math/juction/{arc-plane.js → arc-plane.ts} +203 -143
  15. package/Math/juction/{line-sphere.js → line-sphere.ts} +32 -22
  16. package/Math/juction/{plane-plane.js → plane-plane.ts} +62 -53
  17. package/Math/{line.js → line.ts} +84 -52
  18. package/Math/matrix4.ts +0 -0
  19. package/Math/mesh/mapbox-delaunay.d.ts +74 -0
  20. package/Math/{methods.js → methods.ts} +182 -107
  21. package/Math/{plane.js → plane.ts} +92 -56
  22. package/Math/{quaternion.js → quaternion.ts} +128 -106
  23. package/Math/roadmap.md +10 -0
  24. package/Math/templete-shapes/{grid-visually-equal.js → grid-visually-equal.ts} +118 -65
  25. package/Math/tessellation/constants.ts +1 -0
  26. package/Math/tessellation/methods.ts +79 -0
  27. package/Math/tessellation/nearest-value-padding.ts +147 -0
  28. package/Math/tessellation/roadmap.md +48 -0
  29. package/Math/tessellation/spherical-triangle-area.ts +127 -0
  30. package/Math/tessellation/tile-merger.ts +578 -0
  31. package/Math/tessellation/triangle-tessellation.ts +533 -0
  32. package/Math/tessellation/types.ts +1 -0
  33. package/Math/types.ts +68 -0
  34. package/Math/utils.js +3 -2
  35. package/Math/{vec3.js → vec3.ts} +227 -151
  36. package/Math/xyz-tile.ts +26 -0
  37. package/algorithms/search-binary.js +14 -16
  38. package/altitude-locator/adaptors.js +0 -1
  39. package/altitude-locator/keymethod.js +0 -1
  40. package/altitude-locator/plugin.js +445 -345
  41. package/altitude-locator/types.js +26 -21
  42. package/compass-rose/compass-rose-padding-flat.js +274 -230
  43. package/compass-rose/{compass-text-writer.js → compass-text-writer.ts} +210 -155
  44. package/compass-rose/index.js +3 -3
  45. package/{constants.js → constants.ts} +8 -6
  46. package/heatwave/datamanager.js +168 -149
  47. package/heatwave/heatwave.js +261 -206
  48. package/heatwave/index.js +5 -5
  49. package/heatwave/isobar.js +340 -303
  50. package/heatwave/{texture-point-sampler.js → texture-point-sampler.ts} +220 -187
  51. package/investigation-tools/draw/tiles/adapters.ts +133 -0
  52. package/investigation-tools/draw/tiles/tiles.ts +162 -0
  53. package/jest.config.js +6 -7
  54. package/package.json +1 -1
  55. package/pin/pin-object-array1.js +381 -300
  56. package/pin/pin-point-totem1.js +77 -60
  57. package/programs/arrowfield/arrow-field.js +89 -60
  58. package/programs/arrowfield/logic.js +173 -141
  59. package/programs/data2legend/density-to-legend.js +86 -68
  60. package/programs/data2legend/point-to-density-texture.js +84 -67
  61. package/programs/float2legendwithratio/index.js +3 -2
  62. package/programs/float2legendwithratio/logic.js +144 -118
  63. package/programs/float2legendwithratio/object.js +141 -104
  64. package/programs/helpers/blender.js +73 -58
  65. package/programs/helpers/{fadeaway.js → fadeaway.ts} +73 -55
  66. package/programs/index.js +19 -20
  67. package/programs/line-on-globe/circle-accurate-3d.js +112 -85
  68. package/programs/line-on-globe/circle-accurate-flat.js +200 -148
  69. package/programs/line-on-globe/degree-padding-around-circle-3d.js +134 -102
  70. package/programs/line-on-globe/index.js +0 -1
  71. package/programs/line-on-globe/lines-color-instanced-flat.js +99 -80
  72. package/programs/line-on-globe/linestrip/data.ts +29 -0
  73. package/programs/line-on-globe/linestrip/{linestrip.js → linestrip.ts} +152 -93
  74. package/programs/line-on-globe/{naive-accurate-flexible.js → naive-accurate-flexible.ts} +175 -126
  75. package/programs/line-on-globe/util.js +8 -5
  76. package/programs/picking/pickable-polygon-renderer.js +129 -98
  77. package/programs/picking/pickable-renderer.js +130 -98
  78. package/programs/point-on-globe/element-globe-surface-glow.js +122 -93
  79. package/programs/point-on-globe/element-point-glow.js +114 -80
  80. package/programs/point-on-globe/square-pixel-point.js +139 -120
  81. package/programs/polygon-on-globe/roadmap.md +8 -0
  82. package/programs/polygon-on-globe/texture-dem-triangles.ts +290 -0
  83. package/programs/{programcache.js → programcache.ts} +134 -126
  84. package/programs/rings/index.js +1 -1
  85. package/programs/rings/partial-ring/{piece-of-pie.js → piece-of-pie.ts} +222 -152
  86. package/programs/totems/camera-totem-attactment-interface.ts +4 -0
  87. package/programs/totems/{camerauniformblock.js → camerauniformblock.ts} +326 -230
  88. package/programs/totems/{canvas-webglobe-info.js → canvas-webglobe-info.ts} +147 -132
  89. package/programs/totems/dem-textures-manager.ts +368 -0
  90. package/programs/totems/{globe-changes.js → globe-changes.ts} +79 -59
  91. package/programs/totems/gpu-selection-uniform-block.js +127 -99
  92. package/programs/totems/{index.js → index.ts} +2 -2
  93. package/programs/two-d/pixel-padding-for-compass.js +101 -87
  94. package/programs/util.js +19 -14
  95. package/programs/vectorfields/logics/{constants.js → constants.ts} +5 -4
  96. package/programs/vectorfields/logics/drawrectangleparticles.ts +182 -0
  97. package/programs/vectorfields/logics/index.js +4 -2
  98. package/programs/vectorfields/logics/particle-ubo.ts +23 -0
  99. package/programs/vectorfields/logics/{pixelbased.js → pixelbased.ts} +119 -84
  100. package/programs/vectorfields/logics/ubo.js +57 -51
  101. package/programs/vectorfields/{pingpongbuffermanager.js → pingpongbuffermanager.ts} +113 -73
  102. package/publish.bat +62 -0
  103. package/range-tools-on-terrain/bearing-line/{adapters.js → adapters.ts} +154 -114
  104. package/range-tools-on-terrain/bearing-line/{plugin.js → plugin.ts} +569 -457
  105. package/range-tools-on-terrain/bearing-line/types.ts +65 -0
  106. package/range-tools-on-terrain/circle-line-chain/{adapters.js → adapters.ts} +104 -85
  107. package/range-tools-on-terrain/circle-line-chain/{chain-list-map.js → chain-list-map.ts} +446 -382
  108. package/range-tools-on-terrain/circle-line-chain/{plugin.js → plugin.ts} +607 -464
  109. package/range-tools-on-terrain/circle-line-chain/types.ts +43 -0
  110. package/range-tools-on-terrain/range-ring/{adapters.js → adapters.ts} +114 -93
  111. package/range-tools-on-terrain/range-ring/{enum.js → enum.ts} +2 -2
  112. package/range-tools-on-terrain/range-ring/{plugin.js → plugin.ts} +444 -377
  113. package/range-tools-on-terrain/range-ring/rangeringangletext.ts +396 -0
  114. package/range-tools-on-terrain/range-ring/types.ts +30 -0
  115. package/semiplugins/interface.ts +14 -0
  116. package/semiplugins/lightweight/{line-plugin.js → line-plugin.ts} +342 -221
  117. package/semiplugins/lightweight/{piece-of-pie-plugin.js → piece-of-pie-plugin.ts} +275 -200
  118. package/semiplugins/shape-on-terrain/{arc-plugin.js → arc-plugin.ts} +616 -472
  119. package/semiplugins/shape-on-terrain/{circle-plugin.js → circle-plugin.ts} +588 -444
  120. package/semiplugins/shape-on-terrain/goal.md +12 -0
  121. package/semiplugins/shape-on-terrain/{padding-1-degree.js → padding-1-degree.ts} +713 -539
  122. package/semiplugins/shape-on-terrain/terrain-polygon/adapters.ts +69 -0
  123. package/semiplugins/shape-on-terrain/terrain-polygon/data/cache-shortcuts.md +20 -0
  124. package/semiplugins/shape-on-terrain/terrain-polygon/data/cache.ts +149 -0
  125. package/semiplugins/shape-on-terrain/terrain-polygon/data/index-polygon-map.ts +58 -0
  126. package/semiplugins/shape-on-terrain/terrain-polygon/data/manager.ts +4 -0
  127. package/semiplugins/shape-on-terrain/terrain-polygon/data/master-worker.js +196 -0
  128. package/semiplugins/shape-on-terrain/terrain-polygon/data/master-worker.ts +209 -0
  129. package/semiplugins/shape-on-terrain/terrain-polygon/data/polygon-to-triangles.ts +144 -0
  130. package/semiplugins/shape-on-terrain/terrain-polygon/data/random.ts +165 -0
  131. package/semiplugins/shape-on-terrain/terrain-polygon/data/readme.md +5 -0
  132. package/semiplugins/shape-on-terrain/terrain-polygon/data/types.ts +37 -0
  133. package/semiplugins/shape-on-terrain/terrain-polygon/data/worker-contact.ts +81 -0
  134. package/semiplugins/shape-on-terrain/terrain-polygon/data/worker.js +146 -0
  135. package/semiplugins/shape-on-terrain/terrain-polygon/notes.md +90 -0
  136. package/semiplugins/shape-on-terrain/terrain-polygon/terrain-polygon.ts +265 -0
  137. package/semiplugins/shape-on-terrain/terrain-polygon/types.ts +69 -0
  138. package/semiplugins/shell/bbox-renderer/index.ts +2 -0
  139. package/semiplugins/shell/bbox-renderer/{logic.js → logic.ts} +273 -249
  140. package/semiplugins/shell/bbox-renderer/object.ts +129 -0
  141. package/semiplugins/type.ts +8 -0
  142. package/semiplugins/utility/{container-plugin.js → container-plugin.ts} +126 -94
  143. package/semiplugins/utility/{object-pass-container-plugin.js → object-pass-container-plugin.ts} +101 -80
  144. package/shaders/fragment-toy/firework.js +1 -1
  145. package/shaders/fragment-toy/singularity.js +5 -2
  146. package/terrain-plugin.mmd +83 -0
  147. package/tests/Math/arc-sampling-test.js +367 -0
  148. package/tests/Math/arc-sampling-test.ts +429 -0
  149. package/tests/Math/arc.test.ts +77 -0
  150. package/tests/Math/junction/arc-limit.test.ts +7 -0
  151. package/tests/Math/junction/arc-plane-points.test.ts +196 -0
  152. package/tests/Math/junction/arc-plane.test.ts +172 -0
  153. package/tests/Math/junction/line-sphere.test.ts +127 -0
  154. package/tests/Math/junction/plane-plane.test.ts +91 -0
  155. package/tests/Math/plane-test.ts +17 -0
  156. package/tests/Math/plane.test.ts +43 -0
  157. package/tests/Math/vec3.test.ts +33 -0
  158. package/tracks/point-heat-map/adaptors/timetracksplugin-format-to-this.js +78 -63
  159. package/tracks/point-heat-map/index.js +0 -1
  160. package/tracks/point-heat-map/plugin-webworker.js +148 -121
  161. package/tracks/point-heat-map/point-to-heat-map-flow.js +150 -121
  162. package/tracks/point-heat-map/readme.md +15 -0
  163. package/tracks/point-tracks/key-methods.js +3 -2
  164. package/tracks/point-tracks/plugin.js +487 -393
  165. package/tracks/timetracks/adaptors-line-strip.js +79 -65
  166. package/tracks/timetracks/plugin-line-strip.js +295 -240
  167. package/tracks/timetracks/program-line-strip.js +495 -411
  168. package/tracks/timetracks/programpoint-line-strip.js +137 -109
  169. package/tracks/timetracks/readme.md +1 -0
  170. package/tsconfig.json +22 -0
  171. package/types/@pirireis/webglobe.d.ts +102 -0
  172. package/types/delaunator.d.ts +40 -0
  173. package/types/earcut.d.ts +11 -0
  174. package/types/rbush.d.ts +57 -0
  175. package/types.ts +319 -0
  176. package/util/account/bufferoffsetmanager.js +209 -176
  177. package/util/account/create-buffermap-orchastration.ts +85 -0
  178. package/util/account/index.js +6 -3
  179. package/util/account/single-attribute-buffer-management/{buffer-manager.js → buffer-manager.ts} +151 -117
  180. package/util/account/single-attribute-buffer-management/{buffer-orchestrator.js → buffer-orchestrator.ts} +238 -212
  181. package/util/account/single-attribute-buffer-management/{buffer-orchestrator1.js → buffer-orchestrator1.ts} +184 -159
  182. package/util/account/single-attribute-buffer-management/{index.js → index.ts} +11 -4
  183. package/util/account/single-attribute-buffer-management/{object-store.js → object-store.ts} +76 -55
  184. package/util/account/single-attribute-buffer-management/types.ts +43 -0
  185. package/util/account/util.js +22 -18
  186. package/util/algorithms/index.js +0 -1
  187. package/util/algorithms/search-binary.js +26 -25
  188. package/util/build-strategy/{static-dynamic.js → static-dynamic.ts} +50 -41
  189. package/util/check/index.js +0 -1
  190. package/util/check/typecheck.ts +74 -0
  191. package/util/{frame-counter-trigger.js → frame-counter-trigger.ts} +99 -84
  192. package/util/geometry/{index.js → index.ts} +155 -121
  193. package/util/gl-util/buffer/{attribute-loader.js → attribute-loader.ts} +84 -62
  194. package/util/gl-util/buffer/{index.js → index.ts} +6 -3
  195. package/util/gl-util/draw-options/{methods.js → methods.ts} +47 -32
  196. package/util/gl-util/uniform-block/{manager.js → manager.ts} +232 -190
  197. package/util/{webglobe/gldefaultstates.js → globe-default-gl-states.ts} +5 -4
  198. package/util/helper-methods.ts +9 -0
  199. package/util/index.js +9 -10
  200. package/util/interpolation/index.js +0 -1
  201. package/util/interpolation/timetrack/index.js +9 -2
  202. package/util/interpolation/timetrack/timetrack-interpolator.js +94 -79
  203. package/util/interpolation/timetrack/web-worker.js +51 -46
  204. package/util/picking/{fence.js → fence.ts} +47 -41
  205. package/util/picking/picker-displayer.ts +226 -0
  206. package/util/programs/draw-from-pixel-coords.js +201 -164
  207. package/util/programs/{draw-texture-on-canvas.js → draw-texture-on-canvas.ts} +92 -67
  208. package/util/programs/supersampletotextures.js +130 -97
  209. package/util/programs/texturetoglobe.js +153 -128
  210. package/util/shaderfunctions/{geometrytransformations.js → geometrytransformations.ts} +169 -41
  211. package/util/shaderfunctions/index.js +2 -2
  212. package/util/shaderfunctions/nodata.js +4 -2
  213. package/util/shaderfunctions/noisefunctions.js +10 -7
  214. package/util/{webglobjectbuilders.js → webglobjectbuilders.ts} +446 -358
  215. package/vectorfield/arrowfield/adaptor.js +11 -11
  216. package/vectorfield/arrowfield/index.js +3 -3
  217. package/vectorfield/arrowfield/plugin.js +128 -83
  218. package/vectorfield/waveparticles/adaptor.js +16 -15
  219. package/vectorfield/waveparticles/index.js +3 -3
  220. package/vectorfield/waveparticles/plugin.ts +506 -0
  221. package/vectorfield/wind/adapters/image-to-fields.ts +74 -0
  222. package/vectorfield/wind/adapters/types.ts +12 -0
  223. package/vectorfield/wind/{imagetovectorfieldandmagnitude.js → imagetovectorfieldandmagnitude.ts} +78 -56
  224. package/vectorfield/wind/index.js +5 -5
  225. package/vectorfield/wind/plugin-persistant copy.ts +461 -0
  226. package/vectorfield/wind/plugin-persistant.ts +483 -0
  227. package/vectorfield/wind/plugin.js +883 -671
  228. package/vectorfield/wind/vectorfieldimage.js +27 -23
  229. package/write-text/{context-text-bulk.js → context-text-bulk.ts} +285 -200
  230. package/write-text/context-text3.ts +252 -0
  231. package/write-text/{context-text4.js → context-text4.ts} +231 -145
  232. package/write-text/context-textDELETE.js +125 -94
  233. package/write-text/objectarraylabels/{index.js → index.ts} +2 -2
  234. package/write-text/objectarraylabels/objectarraylabels.d.ts +72 -0
  235. package/write-text/objectarraylabels/objectarraylabels.js +247 -200
  236. package/Math/matrix4.js +0 -1
  237. package/Math/tessellation/earcut/adapters.js +0 -37
  238. package/Math/tessellation/hybrid-triangle-tessellation-meta.js +0 -123
  239. package/Math/tessellation/methods.js +0 -46
  240. package/Math/tessellation/shred-input.js +0 -18
  241. package/Math/tessellation/tile-merger.js +0 -56
  242. package/Math/tessellation/tiler.js +0 -50
  243. package/Math/tessellation/triangle-tessellation-meta.js +0 -516
  244. package/Math/tessellation/triangle-tessellation.js +0 -14
  245. package/Math/tessellation/types.js +0 -1
  246. package/Math/tessellation/zoom-catch.js +0 -1
  247. package/Math/types.js +0 -1
  248. package/programs/line-on-globe/linestrip/data.js +0 -4
  249. package/programs/polygon-on-globe/partial-tesselation.js +0 -1
  250. package/programs/polygon-on-globe/texture-dem-triangle-test-plugin-triangle.js +0 -204
  251. package/programs/polygon-on-globe/texture-dem-triangle-test-plugin.js +0 -118
  252. package/programs/polygon-on-globe/texture-dem-triangles.js +0 -236
  253. package/programs/vectorfields/logics/drawrectangleparticles.js +0 -112
  254. package/programs/vectorfields/logics/ubo-new.js +0 -25
  255. package/range-tools-on-terrain/bearing-line/types.js +0 -1
  256. package/range-tools-on-terrain/circle-line-chain/types.js +0 -1
  257. package/range-tools-on-terrain/range-ring/rangeringangletext.js +0 -331
  258. package/range-tools-on-terrain/range-ring/types.js +0 -9
  259. package/semiplugins/interface.js +0 -1
  260. package/semiplugins/shape-on-terrain/terrain-cover/texture-dem-cover.js +0 -1
  261. package/semiplugins/shell/bbox-renderer/index.js +0 -2
  262. package/semiplugins/shell/bbox-renderer/object.js +0 -65
  263. package/semiplugins/type.js +0 -1
  264. package/types.js +0 -19
  265. package/util/account/create-buffermap-orchastration.js +0 -39
  266. package/util/account/single-attribute-buffer-management/types.js +0 -1
  267. package/util/check/typecheck.js +0 -66
  268. package/util/gl-util/uniform-block/types.js +0 -1
  269. package/util/picking/picker-displayer.js +0 -134
  270. package/util/webglobe/index.js +0 -2
  271. package/util/webglobe/rasteroverlay.js +0 -76
  272. package/vectorfield/waveparticles/plugin.js +0 -290
  273. package/write-text/attached-text-writer.js +0 -95
  274. package/write-text/context-text3.js +0 -167
@@ -1,38 +1,60 @@
1
- /**
2
- * Author: Toprak Nihat Deniz Ozturk
3
- */
4
- import { createImageFromBase64, createProgramWrapper, createTexture, createBuffer, bindAttribute, bindFramebuffer, bindTexture, getColorRamp } from "../../util/webglobjectbuilders";
5
- import imageToMagnitude, { imageToRadianAngle } from "./imagetovectorfieldandmagnitude";
6
- import { TexturePointSampler } from "../../heatwave/texture-point-sampler";
7
- /**
8
- * Shader Dökümanı:
9
- * * Vektör alan haritasında kaçış değeri
10
- * texture2D(u_wind, pos).rg == vec2(0.0, 0.0) değerini kaçış için kullandım.
11
- * Deniz rüzgarları gibi haritalarda kara için rg değerleri 0.0 oluyor.
12
- * Gerçek bir partikülün rg == vec2(0.0, 0.0) değerinde olma olasılığı düşük.
13
- * Hem yatay ve hem dikey boyutta en düşük değere sahip olmalı.
14
- *
15
-
16
- *
17
- */
18
- /**
19
- * @typedef {Object} WindData for setWindDataWithImageBase64
20
- * @property {string} image - base64 encoded image
21
- * @property {number} width - image width
22
- * @property {number} height - image height
23
- * @property {number} uMin - minimum u value
24
- * @property {number} vMin - minimum v value
25
- * @property {number} uMax - maximum u value
26
- * @property {number} vMax - maximum v value
27
- * @property {Array} bbox - bounding box [minLon, minLat, maxLon, maxLat]
28
- * @property {LegendData} legendData - legend data
29
-
30
- */
31
- /**
32
- * @typedef {Object} LegendData
33
- * @property {Array} thresholds - list of integers
34
- * @property {Array} values - list of colors
35
- */
1
+ /**
2
+ * Author: Toprak Nihat Deniz Ozturk
3
+ */
4
+
5
+
6
+
7
+ import {
8
+ createImageFromBase64,
9
+ createProgramWrapper,
10
+ createTexture,
11
+ createBuffer,
12
+ bindAttribute,
13
+ bindFramebuffer,
14
+ bindTexture,
15
+ getColorRamp
16
+ } from "../../util/webglobjectbuilders";
17
+ import { defaultblendfunction } from "../../util/globe-default-gl-states";
18
+
19
+ import imageToMagnitude, { imageToRadianAngle } from "./imagetovectorfieldandmagnitude";
20
+ import { TexturePointSampler } from "../../heatwave/texture-point-sampler";
21
+ /**
22
+ * Shader Dökümanı:
23
+ * * Vektör alan haritasında kaçış değeri
24
+ * texture2D(u_wind, pos).rg == vec2(0.0, 0.0) değerini kaçış için kullandım.
25
+ * Deniz rüzgarları gibi haritalarda kara için rg değerleri 0.0 oluyor.
26
+ * Gerçek bir partikülün rg == vec2(0.0, 0.0) değerinde olma olasılığı düşük.
27
+ * Hem yatay ve hem dikey boyutta en düşük değere sahip olmalı.
28
+ *
29
+
30
+ *
31
+ */
32
+
33
+
34
+ /**
35
+ * @typedef {Object} WindData for setWindDataWithImageBase64
36
+ * @property {string} image - base64 encoded image
37
+ * @property {number} width - image width
38
+ * @property {number} height - image height
39
+ * @property {number} uMin - minimum u value
40
+ * @property {number} vMin - minimum v value
41
+ * @property {number} uMax - maximum u value
42
+ * @property {number} vMax - maximum v value
43
+ * @property {Array} bbox - bounding box [minLon, minLat, maxLon, maxLat]
44
+ * @property {LegendData} legendData - legend data
45
+
46
+ */
47
+
48
+
49
+ /**
50
+ * @typedef {Object} LegendData
51
+ * @property {Array} thresholds - list of integers
52
+ * @property {Array} values - list of colors
53
+ */
54
+
55
+
56
+
57
+
36
58
  const quadVert = `
37
59
  precision mediump float;
38
60
 
@@ -43,7 +65,8 @@ varying vec2 v_tex_pos;
43
65
  void main() {
44
66
  v_tex_pos = a_pos;
45
67
  gl_Position = vec4(1.0 - 2.0 * a_pos, 0, 1);
46
- }`;
68
+ }`;
69
+
47
70
  const screenFrag = `precision mediump float;
48
71
 
49
72
  uniform sampler2D u_screen;
@@ -60,7 +83,8 @@ void main() {
60
83
  gl_FragColor = vec4( floor(255.0 * color * u_opacity) / 255.0);
61
84
  }
62
85
 
63
- `;
86
+ `;
87
+
64
88
  const updateFrag = `
65
89
  precision highp float;
66
90
 
@@ -111,7 +135,6 @@ void main() {
111
135
 
112
136
  // take EPSG:4236 distortion into account for calculating where the particle moved
113
137
  // float distortion = 1.0; //cos(radians(pos.y * 180.0 - 90.0));
114
- // vec2 offset = vec2(velocity.x / distortion, -velocity.y) * 0.0001 * u_speed_factor;
115
138
  vec2 offset = vec2(velocity.x, velocity.y) * 0.0001 * u_speed_factor;
116
139
 
117
140
  // update particle position, wrapping around the date line
@@ -134,7 +157,9 @@ void main() {
134
157
  fract(pos * 255.0),
135
158
  floor(pos * 255.0) / 255.0);
136
159
  }
137
- `;
160
+ `;
161
+
162
+
138
163
  const drawFrag = `precision mediump float;
139
164
 
140
165
  uniform sampler2D u_wind;
@@ -177,7 +202,9 @@ void main() {
177
202
 
178
203
  gl_FragColor = texture2D(u_color_ramp, ramp_pos) * u_base_opacity;
179
204
  }
180
- `;
205
+ `;
206
+
207
+
181
208
  const drawSphereVert = `
182
209
  precision mediump float;
183
210
 
@@ -239,7 +266,8 @@ void main() {
239
266
  gl_Position = uProjectionMatrix * uModelViewMatrix * vec4(pos - uTranslate, 1.0);
240
267
  gl_PointSize = u_point_size;
241
268
 
242
- }`;
269
+ }`;
270
+
243
271
  const drawFlatVert = `
244
272
  precision mediump float;
245
273
 
@@ -289,633 +317,817 @@ void main() {
289
317
  gl_Position = uProjectionMatrix * vec4(normalizedX * u_scrWH.x, (1.0 - normalizedY) * u_scrWH.y, 0.0, 1.0);
290
318
 
291
319
  gl_PointSize = u_point_size;
292
- }`;
293
- const defaultRampColors = [
294
- [0.0, '#5e4fa2'],
295
- [0.08, '#3288bd'],
296
- [0.2, '#66c2a5'],
297
- [0.3, '#abdda4'],
298
- [0.4, '#e6f598'],
299
- [0.5, '#fee08b'],
300
- [0.6, '#fdae61'],
301
- [0.7, '#f46d43'],
302
- [1.0, '#d53e4f']
303
- ];
304
- const windyLegendData = {
305
- "thresholds": [0, 3, 3, 5, 5, 7, 10, 10, 13, 15, 15, 17, 20, 20, 25, 25, 30],
306
- // 0 5 10 20 30 40 60
307
- // "thresholds": [0, 5, 5, 10, 10, 15, 15, 20, 20, 25, 25, 30, 30, 35, 40, 60],
308
- "values": [
309
- "#6271B8",
310
- "#6271B8",
311
- "#6271B8",
312
- "#6271B8",
313
- "#3D6EA3",
314
- "#4A94AA",
315
- "#4A9294",
316
- "#4D8E7C",
317
- "#4CA44C",
318
- "#67A436",
319
- "#A28740",
320
- "#A26D5C",
321
- "#8D3F5C",
322
- "#974B91",
323
- "#5F64A0",
324
- "#5B88A1",
325
- "#5B88A1"
326
- ]
327
- };
328
- export default class WindPlugin {
329
- /**
330
- * @param {String} id
331
- * @param {Object} windDataMeta
332
- * @param {number} windDataMeta.width - image width
333
- * @param {number} windDataMeta.height - image height
334
- * @param {Array} windDataMeta.bbox - bounding box [minLon, minLat, maxLon, maxLat]
335
- * @param {Object} options
336
- * @param {number} options.fadeOpacity - how fast the particle trails fade on each frame | between 0 - 1 | default 0.746
337
- * @param {number} options.speedFactor - how fast the particles move | between 0 - 1 | default 0.6
338
- * @param {number} options.dropRate - how often the particles move to a random place | between 0 - 1 | default 0.007
339
- * @param {number} options.dropRateBump - drop rate increase relative to individual particle speed
340
- * @param {number} options.baseOpacity - opacity of drawn particle trails | between 0 - 1 | default 1.0
341
- * @param {number} options.pointSize - draw size of each particle | positive number | default 2.0
342
- * @param {number} options.minSpeed - minimum speed value | positive number | default 0.0
343
- * @param {number} options.maxSpeed - maximum speed value | positive number | default 1000.0
344
- * @param {number} options.height - height of the particles | number | default 0.0
345
- * @param {number} numParticles - number of particles | positive integer
346
- * @param {LegendData} options.legendData - legend data
347
- */
348
- constructor(id, windDataMeta, { fadeOpacity = 0.746, speedFactor = 0.6, dropRate = 0.007, dropRateBump = 0.001, baseOpacity = 1.0, pointSize = 2.0, minSpeed = 0.0, maxSpeed = 1000.0, height = 0.0, numParticles = 40000, legendData = windyLegendData } = {}) {
349
- this.id = id;
350
- this._windDataMeta = windDataMeta;
351
- this.globe = null;
352
- this.gl = null;
353
- this._screenMoved = false;
354
- this.projMatrix = null;
355
- this.modelviewMatrix = null;
356
- this.transPos = new Float32Array([0, 0, 0]);
357
- this.windData = null;
358
- this._fadeOpacity = fadeOpacity;
359
- this._speedFactor = speedFactor;
360
- this._dropRate = dropRate;
361
- this._dropRateBump = dropRateBump;
362
- this._baseOpacity = baseOpacity;
363
- this._pointSize = pointSize;
364
- this._minSpeed = minSpeed;
365
- this._maxSpeed = maxSpeed;
366
- this._height = height;
367
- this._numParticles = numParticles;
368
- this._legendData = legendData;
369
- this.lastdatas = [[], [], { x: 0, y: 0, z: 0 }];
370
- this._drawParticles = this._drawParticlesSphere;
371
- this._lastLOD = 0;
372
- }
373
- // Uniforms are loaded once, on initiation and when they are changed.
374
- set height(value) {
375
- this._height = value;
376
- this._loadHeight();
377
- }
378
- set minSpeed(value) {
379
- this._minSpeed = value;
380
- this._loadMinMaxSpeed();
381
- }
382
- set maxSpeed(value) {
383
- this._maxSpeed = value;
384
- this._loadMinMaxSpeed();
385
- }
386
- set fadeOpacity(value) {
387
- this._fadeOpacity = value;
388
- this._loadFadeOpacity();
389
- }
390
- set speedFactor(value) {
391
- this._speedFactor = value;
392
- this._loadSpeedFactor();
393
- }
394
- set dropRate(value) {
395
- this._dropRate = value;
396
- this._loadDropRate();
397
- }
398
- set dropRateBump(value) {
399
- this._dropRateBump = value;
400
- this._loadDropRateBump();
401
- }
402
- set baseOpacity(value) {
403
- this._baseOpacity = value;
404
- this._loadBaseOpacity();
405
- }
406
- set pointSize(value) {
407
- this._pointSize = value;
408
- this._loadPointSize();
409
- }
410
- // Color ramp methods.
411
- // Color ramp is used to colorize the particles according to their speed.
412
- // Legend max value is used to normalize the speed values.
413
- setColorRamp(colors) {
414
- // lookup texture for colorizing the particles according to their speed
415
- if (this.gl == null) {
416
- return;
417
- }
418
- this.colorRampTexture = createTexture(this.gl, this.gl.LINEAR, getColorRamp(colors), 16, 16);
419
- }
420
- setColorRampDefault() {
421
- const { uMax, uMin, vMax, vMin } = this.windData;
422
- const maxSpeed = Math.sqrt(uMax * uMax + vMax * vMax);
423
- const minSpeed = Math.sqrt(uMin * uMin + vMin * vMin);
424
- // console.log("maxSpeed", maxSpeed, "minSpeed", minSpeed);
425
- this._rampMax = maxSpeed > minSpeed ? maxSpeed : minSpeed;
426
- this._loadRampMax();
427
- this.setColorRamp(defaultRampColors);
428
- }
429
- /**
430
- *
431
- * @param {*} legendData
432
- * @param {Array} legendData.thresholds - list of integers
433
- * @param {Array} legendData.values - list of colors in hex format like #ff0000 (alpha is not included)
434
- *
435
- */
436
- setLegend(legendData) {
437
- const { gl } = this;
438
- if (gl === null) {
439
- throw new Error("wind plugin. setColorRampFromService is called before plugin is registered.");
440
- }
441
- const { thresholds, values } = legendData;
442
- if (thresholds.length === 0 || values.length === 0) {
443
- return;
444
- }
445
- thresholds.push(thresholds[thresholds.length - 1]);
446
- values.push(values[values.length - 1]);
447
- const maxVelocity = thresholds[thresholds.length - 1];
448
- const colors = [];
449
- for (let i = 0; i < thresholds.length; i++) {
450
- colors.push([
451
- thresholds[i] / maxVelocity,
452
- values[i]
453
- ]);
454
- }
455
- const colorRamp = getColorRamp(colors);
456
- this.colorRampTexture = createTexture(gl, gl.LINEAR, colorRamp, 16, 16);
457
- this._rampMax = maxVelocity;
458
- this._loadRampMax();
459
- }
460
- // particle coordinates are encoded into RGBA texture
461
- set numParticles(numParticles) {
462
- const gl = this.gl;
463
- // we create a square texture where each pixel will hold a particle position encoded as RGBA
464
- const particleRes = this.particleStateResolution = Math.ceil(Math.sqrt(numParticles));
465
- this._numParticles = particleRes * particleRes;
466
- const particleState = new Uint8Array(this._numParticles * 4);
467
- for (let i = 0; i < particleState.length; i++) {
468
- particleState[i] = Math.floor(Math.random() * 256); // randomize the initial particle positions
469
- }
470
- // textures to hold the particle state for the current and the next frame
471
- this.particleStateTexture0 = createTexture(gl, gl.NEAREST, particleState, particleRes, particleRes);
472
- this.particleStateTexture1 = createTexture(gl, gl.NEAREST, particleState, particleRes, particleRes);
473
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
474
- gl.useProgram(this.updateProgram.program);
475
- gl.uniform1f(this.updateProgram.u_wind_res, this.particleStateResolution);
476
- gl.useProgram(this.drawSphereProgram.program);
477
- gl.uniform1f(this.drawSphereProgram.u_particles_res, this.particleStateResolution);
478
- gl.useProgram(this.drawFlatProgram.program);
479
- gl.uniform1f(this.drawFlatProgram.u_particles_res, this.particleStateResolution);
480
- gl.useProgram(currentProgram);
481
- // console.log("numParticles", this._numParticles);
482
- const particleIndices = new Float32Array(this._numParticles);
483
- for (let i = 0; i < this._numParticles; i++)
484
- particleIndices[i] = i;
485
- this.particleIndexBuffer = createBuffer(gl, particleIndices);
486
- }
487
- get numParticles() {
488
- return this._numParticles;
489
- }
490
- // Vector field texture is used to calculate the particle movement.
491
- /**
492
- * @param {Object} windData
493
- * @param {HTMLImageElement} windData.image - image element
494
-
495
- * @param {number} windData.uMin - minimum u value
496
- * @param {number} windData.vMin - minimum v value
497
- * @param {number} windData.uMax - maximum u value
498
- * @param {number} windData.vMax - maximum v value
499
- */
500
- setWind(windData) {
501
- if (windData === null) {
502
- return;
503
- }
504
- const windDataMeta = this._windDataMeta;
505
- const gl = this.gl;
506
- this.windData = windData;
507
- this.windTexture = createTexture(gl, gl.LINEAR, windData.image);
508
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
509
- gl.useProgram(this.updateProgram.program);
510
- gl.uniform2f(this.updateProgram.u_wind_res, windDataMeta.width, windDataMeta.height);
511
- gl.uniform2f(this.updateProgram.u_wind_min, this.windData.uMin, this.windData.vMin);
512
- gl.uniform2f(this.updateProgram.u_wind_max, this.windData.uMax, this.windData.vMax);
513
- this.setGeometry();
514
- console.log("windData bbox", windData.bbox);
515
- const minXY = this._latLongToPixelXY(windDataMeta.bbox[1], windDataMeta.bbox[0]);
516
- const maxXY = this._latLongToPixelXY(windDataMeta.bbox[3], windDataMeta.bbox[2]);
517
- this._loadBoundingBoxData(minXY.x, minXY.y, maxXY.x, maxXY.y);
518
- gl.useProgram(currentProgram);
519
- this._setCoorcinatesDataCalculatorData();
520
- this.resize();
521
- }
522
- // Vector field texture is used to calculate the particle movement.
523
- /**
524
- * @param {Object} windData
525
- * @param {BaseInt64} windData.image - image element
526
- * @param {number} windData.width - image width
527
- * @param {number} windData.height - image height
528
- * @param {number} windData.uMin - minimum u value
529
- * @param {number} windData.vMin - minimum v value
530
- * @param {number} windData.uMax - maximum u value
531
- * @param {number} windData.vMax - maximum v value
532
- * @param {Array} windData.bbox - bounding box [minLon, minLat, maxLon, maxLat]
533
- */
534
- setWindDataWithImageBase64(windData) {
535
- const image = createImageFromBase64(windData.image);
536
- image.onload = () => {
537
- windData.image = image;
538
- this.setWind(windData);
539
- };
540
- }
541
- /**
542
- * @param
543
- * @returns
544
- */
545
- getTexturePointSampler(type = `magnitude`) {
546
- if (type === `magnitude`) {
547
- if (!this.texturePointSampler) {
548
- this._createTexturePointSampler();
549
- this._setCoorcinatesDataCalculatorData();
550
- }
551
- return this.texturePointSampler;
552
- }
553
- else if (type === `angle`) {
554
- if (!this.texturePointSamplerAngle) {
555
- this._createTexturePointSamplerAngle();
556
- this._setCoorcinatesDataCalculatorData();
557
- }
558
- return this.texturePointSamplerAngle;
559
- }
560
- else {
561
- throw new Error(`WindPlugin.getTexturePointSampler: type must be either 'magnitude' or 'angle'.`);
562
- }
563
- }
564
- _createTexturePointSamplerAngle() {
565
- const { bbox, width, height } = this._windDataMeta;
566
- this.texturePointSamplerAngle = new TexturePointSampler(bbox, width, height);
567
- }
568
- _createTexturePointSampler() {
569
- const { bbox, width, height } = this._windDataMeta;
570
- this.texturePointSampler = new TexturePointSampler(bbox, width, height);
571
- }
572
- _setCoorcinatesDataCalculatorData() {
573
- if (!this.windData) {
574
- return;
575
- }
576
- if (this.texturePointSamplerAngle) {
577
- const angle = imageToRadianAngle(this.windData);
578
- this.texturePointSamplerAngle.updateTextureData(0, angle, angle);
579
- }
580
- if (this.texturePointSampler) {
581
- const magnitude = imageToMagnitude(this.windData);
582
- this.texturePointSampler.updateTextureData(0, magnitude, magnitude);
583
- }
584
- }
585
- // -----------------------------------------------
586
- // --- inner methods ---
587
- // -----------------------------------------------
588
- _latLongToPixelXY(latitude, longitude) {
589
- return {
590
- x: (longitude + 180) / 360,
591
- y: (90 - latitude) / 180
592
- };
593
- }
594
- _loadBoundingBoxData(minX, minY, maxX, maxY) {
595
- // console.log("minX", minX, "minY", minY, "maxX", maxX, "maxY", maxY)
596
- const gl = this.gl;
597
- const bboxMatrix = new Float32Array([
598
- maxX - minX, 0, 0,
599
- 0, maxY - minY, 0,
600
- minX, minY, 1
601
- ]);
602
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
603
- gl.useProgram(this.drawSphereProgram.program);
604
- gl.uniformMatrix3fv(this.drawSphereProgram.u_bbox_matrix, false, bboxMatrix);
605
- gl.useProgram(this.drawFlatProgram.program);
606
- gl.uniformMatrix3fv(this.drawFlatProgram.u_bbox_matrix, false, bboxMatrix);
607
- gl.useProgram(currentProgram);
608
- }
609
- // When the screen is moved, the particles are reset to random positions.
610
- // During movement plugin should not draw the particles.
611
- _doDraw() {
612
- const globe = this.globe;
613
- if (this.windData === null) {
614
- return false;
615
- }
616
- if (globe.api_IsScreenMoving() || this._lastLOD !== globe.api_GetCurrentLODWithDecimal()) {
617
- if (!this._screenMoved) {
618
- this._screenMoved = true;
619
- }
620
- this._lastLOD = globe.api_GetCurrentLODWithDecimal();
621
- return false;
622
- }
623
- else {
624
- if (this._screenMoved) {
625
- this._loadSpeedFactor();
626
- this.resize();
627
- this._screenMoved = false;
628
- }
629
- return true;
630
- }
631
- }
632
- _draw() {
633
- const gl = this.gl;
634
- this._drawScreen();
635
- this._updateParticles();
636
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
637
- }
638
- // globe calls `draw3D` method on each frame
639
- draw3D(projMatrix, modelviewMatrix, transPos) {
640
- const gl = this.gl;
641
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
642
- this.transPos.set([transPos.x, transPos.y, transPos.z], 0);
643
- this.projMatrix = projMatrix;
644
- this.modelviewMatrix = modelviewMatrix;
645
- gl.activeTexture(gl.TEXTURE0);
646
- if (this._doDraw()) {
647
- const depthTest = gl.isEnabled(gl.DEPTH_TEST);
648
- if (depthTest)
649
- gl.disable(gl.DEPTH_TEST);
650
- // if (gl.disable(gl.STENCIL_TEST); //
651
- this._draw();
652
- if (depthTest)
653
- gl.enable(gl.DEPTH_TEST);
654
- }
655
- gl.bindFramebuffer(gl.FRAMEBUFFER, null);
656
- // test visuals
657
- // gl.viewport(0, 0, 100, 100);
658
- // this._drawTexture(this.windTexture, 1.0);
659
- // gl.viewport(0, 100, 100, 100);
660
- // this._drawTexture(this.particleStateTexture0, 1.0);
661
- // gl.viewport(0, 200, 100, 100);
662
- // this._drawTexture(this.colorRampTexture, 1.0);
663
- this._resetMachineStates();
664
- this.globe.DrawRender();
665
- }
666
- _drawScreen() {
667
- const { gl, globe } = this;
668
- bindTexture(gl, this.windTexture, 0);
669
- bindTexture(gl, this.particleStateTexture0, 1);
670
- // draw the screen into a temporary framebuffer to retain it as the background on the next frame
671
- bindFramebuffer(gl, this.framebuffer, this.screenTexture);
672
- gl.viewport(0, 0, globe.api_ScrW(), globe.api_ScrH());
673
- gl.enable(gl.BLEND);
674
- gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); // non-premultiplied alpha
675
- this._drawTexture(this.backgroundTexture, this._fadeOpacity);
676
- this._drawParticles();
677
- bindFramebuffer(gl, null);
678
- this._drawTexture(this.screenTexture, 1.0);
679
- gl.disable(gl.BLEND);
680
- // save the current screen as the background for the next frame
681
- const temp = this.backgroundTexture;
682
- this.backgroundTexture = this.screenTexture;
683
- this.screenTexture = temp;
684
- }
685
- _drawTexture(texture, opacity) {
686
- const gl = this.gl;
687
- const program = this.screenProgram;
688
- gl.useProgram(program.program);
689
- bindAttribute(gl, this.quadBuffer, program.a_pos, 2);
690
- bindTexture(gl, texture, 2);
691
- gl.uniform1i(program.u_screen, 2);
692
- gl.uniform1f(program.u_opacity, opacity);
693
- gl.drawArrays(gl.TRIANGLES, 0, 6);
694
- }
695
- _loadRampMax() {
696
- const { gl } = this;
697
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
698
- // console.log("load ramp max", this._rampMax);
699
- gl.useProgram(this.drawSphereProgram.program);
700
- gl.uniform1f(this.drawSphereProgram.u_color_ramp_max, this._rampMax);
701
- gl.useProgram(this.drawFlatProgram.program);
702
- gl.uniform1f(this.drawFlatProgram.u_color_ramp_max, this._rampMax);
703
- gl.useProgram(currentProgram);
704
- }
705
- _drawParticlesSphere() {
706
- const { gl, projMatrix, modelviewMatrix, transPos } = this;
707
- const program = this.drawSphereProgram;
708
- gl.useProgram(program.program);
709
- bindAttribute(gl, this.particleIndexBuffer, program.a_index, 1);
710
- bindTexture(gl, this.colorRampTexture, 2);
711
- gl.uniform1i(program.u_wind, 0);
712
- gl.uniform1i(program.u_particles, 1);
713
- gl.uniform1i(program.u_color_ramp, 2);
714
- gl.uniformMatrix4fv(program.uProjectionMatrix, false, projMatrix);
715
- gl.uniformMatrix4fv(program.uModelViewMatrix, false, modelviewMatrix);
716
- gl.uniform3fv(program.uTranslate, transPos);
717
- gl.drawArrays(gl.POINTS, 0, this._numParticles);
718
- }
719
- _drawParticlesFlat() {
720
- const { gl, transPos, projMatrix, globe } = this;
721
- const program = this.drawFlatProgram;
722
- gl.useProgram(program.program);
723
- bindAttribute(gl, this.particleIndexBuffer, program.a_index, 1);
724
- bindTexture(gl, this.colorRampTexture, 2);
725
- gl.uniform1i(program.u_wind, 0);
726
- gl.uniform1i(program.u_particles, 1);
727
- gl.uniform1i(program.u_color_ramp, 2);
728
- gl.uniform2f(program.u_scrWH, globe.api_ScrW(), globe.api_ScrH());
729
- const { width, height } = globe.api_GetCurrentWorldWH();
730
- gl.uniform2f(program.u_mapWH, width, height);
731
- gl.uniformMatrix4fv(program.uProjectionMatrix, false, projMatrix);
732
- gl.uniform3fv(program.uTranslate, transPos);
733
- gl.drawArrays(gl.POINTS, 0, this._numParticles);
734
- }
735
- // globe calls `setGeometry` method on map projection change. FLAT or SPHERE
736
- setGeometry() {
737
- if (this.windData === null) {
738
- return;
739
- }
740
- const { globe, gl } = this;
741
- const geometry = globe.api_GetCurrentGeometry();
742
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
743
- if (geometry === globe.api_GeometryTypes().SPHERE) {
744
- this._drawParticles = this._drawParticlesSphere;
745
- gl.useProgram(this.drawSphereProgram.program);
746
- gl.uniform2f(this.drawSphereProgram.u_wind_min, this.windData.uMin, this.windData.vMin);
747
- gl.uniform2f(this.drawSphereProgram.u_wind_max, this.windData.uMax, this.windData.vMax);
748
- }
749
- else if (geometry === globe.api_GeometryTypes().FLAT) {
750
- this._drawParticles = this._drawParticlesFlat;
751
- gl.useProgram(this.drawFlatProgram.program);
752
- gl.uniform2f(this.drawFlatProgram.u_wind_min, this.windData.uMin, this.windData.vMin);
753
- gl.uniform2f(this.drawFlatProgram.u_wind_max, this.windData.uMax, this.windData.vMax);
754
- }
755
- else {
756
- alert("Geometry is not supported");
757
- }
758
- gl.useProgram(currentProgram);
759
- this.resize();
760
- }
761
- _updateParticles() {
762
- const gl = this.gl;
763
- const program = this.updateProgram;
764
- gl.useProgram(program.program);
765
- gl.viewport(0, 0, this.particleStateResolution, this.particleStateResolution);
766
- bindAttribute(gl, this.quadBuffer, program.a_pos, 2);
767
- gl.uniform1i(program.u_wind, 0);
768
- gl.uniform1i(program.u_particles, 1);
769
- bindAttribute(gl, this.quadBuffer, program.a_pos, 2);
770
- gl.uniform1f(program.u_rand_seed, Math.random());
771
- bindFramebuffer(gl, this.framebuffer, this.particleStateTexture1);
772
- gl.drawArrays(gl.TRIANGLES, 0, 6);
773
- // swap the particle state textures so the new one becomes the current one
774
- const temp = this.particleStateTexture0;
775
- this.particleStateTexture0 = this.particleStateTexture1;
776
- this.particleStateTexture1 = temp;
777
- }
778
- _resetMachineStates() {
779
- const { gl, globe } = this;
780
- // gl.disable(gl.DEPTH_TEST);
781
- // gl.disable(gl.STENCIL_TEST);
782
- gl.activeTexture(gl.TEXTURE0);
783
- gl.viewport(0, 0, globe.api_ScrW(), globe.api_ScrH());
784
- }
785
- _loadHeight() {
786
- const { gl, drawSphereProgram } = this;
787
- if (gl == null)
788
- return;
789
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
790
- gl.useProgram(drawSphereProgram.program);
791
- gl.uniform1f(drawSphereProgram.u_height, this._height);
792
- gl.useProgram(currentProgram);
793
- }
794
- _loadMinMaxSpeed() {
795
- const { gl, drawSphereProgram, drawFlatProgram } = this;
796
- if (gl == null)
797
- return;
798
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
799
- gl.useProgram(drawSphereProgram.program);
800
- gl.uniform1f(drawSphereProgram.u_min_speed, this._minSpeed);
801
- gl.uniform1f(drawSphereProgram.u_max_speed, this._maxSpeed);
802
- gl.useProgram(drawFlatProgram.program);
803
- gl.uniform1f(drawFlatProgram.u_min_speed, this._minSpeed);
804
- gl.uniform1f(drawFlatProgram.u_max_speed, this._maxSpeed);
805
- gl.useProgram(currentProgram);
806
- }
807
- _loadFadeOpacity() {
808
- const { gl, screenProgram } = this;
809
- if (gl == null) {
810
- return;
811
- }
812
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
813
- gl.useProgram(screenProgram.program);
814
- gl.uniform1f(screenProgram.u_opacity, this._fadeOpacity);
815
- gl.useProgram(currentProgram);
816
- }
817
- _loadSpeedFactor() {
818
- const { gl, globe, updateProgram } = this;
819
- if (gl == null) {
820
- return;
821
- }
822
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
823
- gl.useProgram(updateProgram.program);
824
- gl.uniform1f(updateProgram.u_speed_factor, this._speedFactor / Math.ceil(Math.sqrt(globe.api_GetCurrentLOD())));
825
- gl.useProgram(currentProgram);
826
- }
827
- _loadDropRate() {
828
- const { gl, updateProgram } = this;
829
- if (gl == null) {
830
- return;
831
- }
832
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
833
- gl.useProgram(updateProgram.program);
834
- gl.uniform1f(updateProgram.u_drop_rate, this._dropRate);
835
- gl.useProgram(currentProgram);
836
- }
837
- _loadDropRateBump() {
838
- const { gl, updateProgram } = this;
839
- if (gl == null) {
840
- return;
841
- }
842
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
843
- gl.useProgram(updateProgram.program);
844
- gl.uniform1f(updateProgram.u_drop_rate_bump, this._dropRateBump);
845
- gl.useProgram(currentProgram);
846
- }
847
- _loadBaseOpacity() {
848
- const { gl, drawSphereProgram, drawFlatProgram } = this;
849
- if (gl == null) {
850
- return;
851
- }
852
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
853
- gl.useProgram(drawSphereProgram.program);
854
- gl.uniform1f(drawSphereProgram.u_base_opacity, this._baseOpacity);
855
- gl.useProgram(drawFlatProgram.program);
856
- gl.uniform1f(drawFlatProgram.u_base_opacity, this._baseOpacity);
857
- gl.useProgram(currentProgram);
858
- }
859
- _loadPointSize() {
860
- const { gl, drawSphereProgram, drawFlatProgram } = this;
861
- if (gl == null) {
862
- return;
863
- }
864
- const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
865
- gl.useProgram(drawSphereProgram.program);
866
- gl.uniform1f(drawSphereProgram.u_point_size, this._pointSize);
867
- gl.useProgram(drawFlatProgram.program);
868
- gl.uniform1f(drawFlatProgram.u_point_size, this._pointSize);
869
- gl.useProgram(currentProgram);
870
- }
871
- // globe plugin init methodu
872
- init(globe, gl) {
873
- // console.log("init wind plugin")
874
- this.globe = globe;
875
- this.gl = gl;
876
- this.drawSphereProgram = createProgramWrapper(gl, drawSphereVert, drawFrag);
877
- this.screenProgram = createProgramWrapper(gl, quadVert, screenFrag);
878
- this.updateProgram = createProgramWrapper(gl, quadVert, updateFrag);
879
- this.drawFlatProgram = createProgramWrapper(gl, drawFlatVert, drawFrag);
880
- this.quadBuffer = createBuffer(gl, new Float32Array([0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1]));
881
- this.framebuffer = gl.createFramebuffer();
882
- this._loadFadeOpacity();
883
- this._loadSpeedFactor();
884
- this._loadDropRate();
885
- this._loadDropRateBump();
886
- this._loadBaseOpacity();
887
- this._loadPointSize();
888
- this._loadMinMaxSpeed();
889
- this._loadHeight();
890
- if (this._legendData != null) {
891
- this.setLegend(this._legendData);
892
- }
893
- this.numParticles = this._numParticles;
894
- this.resize();
895
- }
896
- resize(a, b) {
897
- const { gl, globe } = this;
898
- const emptyPixels = new Uint8Array(globe.api_ScrW() * globe.api_ScrH() * 4);
899
- // screen textures to hold the drawn screen for the previous and the current frame
900
- this.backgroundTexture = createTexture(gl, gl.LINEAR, emptyPixels, globe.api_ScrW(), globe.api_ScrH());
901
- this.screenTexture = createTexture(gl, gl.LINEAR, emptyPixels, globe.api_ScrW(), globe.api_ScrH());
902
- gl.viewport(0, 0, globe.api_ScrW(), globe.api_ScrH());
903
- }
904
- // globe calls this `free` on plugin object unregistration
905
- free() {
906
- const gl = this.gl;
907
- gl.deleteBuffer(this.quadBuffer);
908
- gl.deleteFramebuffer(this.framebuffer);
909
- gl.deleteTexture(this.backgroundTexture);
910
- gl.deleteTexture(this.screenTexture);
911
- gl.deleteTexture(this.particleStateTexture0);
912
- gl.deleteTexture(this.particleStateTexture1);
913
- gl.deleteTexture(this.windTexture);
914
- gl.deleteTexture(this.colorRampTexture);
915
- gl.deleteProgram(this.drawSphereProgram.program);
916
- gl.deleteProgram(this.screenProgram.program);
917
- gl.deleteProgram(this.updateProgram.program);
918
- gl.deleteProgram(this.drawFlatProgram.program);
919
- gl.deleteBuffer(this.particleIndexBuffer);
920
- }
921
- }
320
+ }`;
321
+
322
+
323
+
324
+ const defaultRampColors = [
325
+ [0.0, '#5e4fa2'],
326
+ [0.08, '#3288bd'],
327
+ [0.2, '#66c2a5'],
328
+ [0.3, '#abdda4'],
329
+ [0.4, '#e6f598'],
330
+ [0.5, '#fee08b'],
331
+ [0.6, '#fdae61'],
332
+ [0.7, '#f46d43'],
333
+ [1.0, '#d53e4f']
334
+
335
+ ];
336
+
337
+
338
+ const windyLegendData = {
339
+ "thresholds": [0, 3, 3, 5, 5, 7, 10, 10, 13, 15, 15, 17, 20, 20, 25, 25, 30],
340
+ // 0 5 10 20 30 40 60
341
+ // "thresholds": [0, 5, 5, 10, 10, 15, 15, 20, 20, 25, 25, 30, 30, 35, 40, 60],
342
+ "values": [
343
+ "#6271B8",
344
+ "#6271B8",
345
+ "#6271B8",
346
+ "#6271B8",
347
+ "#3D6EA3",
348
+ "#4A94AA",
349
+ "#4A9294",
350
+ "#4D8E7C",
351
+ "#4CA44C",
352
+ "#67A436",
353
+ "#A28740",
354
+ "#A26D5C",
355
+ "#8D3F5C",
356
+ "#974B91",
357
+ "#5F64A0",
358
+ "#5B88A1",
359
+ "#5B88A1"
360
+ ]
361
+ };
362
+
363
+
364
+ export default class WindPlugin {
365
+
366
+ /**
367
+ * @param {String} id
368
+ * @param {Object} windDataMeta
369
+ * @param {number} windDataMeta.width - image width
370
+ * @param {number} windDataMeta.height - image height
371
+ * @param {Array} windDataMeta.bbox - bounding box [minLon, minLat, maxLon, maxLat]
372
+ * @param {Object} options
373
+ * @param {number} options.fadeOpacity - how fast the particle trails fade on each frame | between 0 - 1 | default 0.746
374
+ * @param {number} options.speedFactor - how fast the particles move | between 0 - 1 | default 0.6
375
+ * @param {number} options.dropRate - how often the particles move to a random place | between 0 - 1 | default 0.007
376
+ * @param {number} options.dropRateBump - drop rate increase relative to individual particle speed
377
+ * @param {number} options.baseOpacity - opacity of drawn particle trails | between 0 - 1 | default 1.0
378
+ * @param {number} options.pointSize - draw size of each particle | positive number | default 2.0
379
+ * @param {number} options.minSpeed - minimum speed value | positive number | default 0.0
380
+ * @param {number} options.maxSpeed - maximum speed value | positive number | default 1000.0
381
+ * @param {number} options.height - height of the particles | number | default 0.0
382
+ * @param {number} numParticles - number of particles | positive integer
383
+ * @param {LegendData} options.legendData - legend data
384
+ */
385
+ constructor(id,
386
+ windDataMeta,
387
+ {
388
+ fadeOpacity = 0.746,
389
+ speedFactor = 0.6,
390
+ dropRate = 0.007,
391
+ dropRateBump = 0.001,
392
+ baseOpacity = 1.0,
393
+ pointSize = 2.0,
394
+ minSpeed = 0.0,
395
+ maxSpeed = 1000.0,
396
+ height = 0.0,
397
+ numParticles = 40000,
398
+ legendData = windyLegendData
399
+ } = {}) {
400
+
401
+ this.id = id;
402
+ this._windDataMeta = windDataMeta;
403
+ this.globe = null;
404
+ this.gl = null;
405
+
406
+
407
+ this._screenMoved = false;
408
+ this.projMatrix = null;
409
+ this.modelviewMatrix = null;
410
+ this.transPos = new Float32Array([0, 0, 0]);
411
+ this.windData = null;
412
+
413
+ this._fadeOpacity = fadeOpacity;
414
+ this._speedFactor = speedFactor;
415
+ this._dropRate = dropRate;
416
+ this._dropRateBump = dropRateBump;
417
+ this._baseOpacity = baseOpacity;
418
+ this._pointSize = pointSize;
419
+ this._minSpeed = minSpeed;
420
+ this._maxSpeed = maxSpeed;
421
+ this._height = height;
422
+ this._numParticles = numParticles;
423
+ this._legendData = legendData;
424
+
425
+ this.lastdatas = [[], [], { x: 0, y: 0, z: 0 }];
426
+
427
+ this._drawParticles = this._drawParticlesSphere
428
+
429
+ this._lastLOD = 0;
430
+ }
431
+
432
+ // Uniforms are loaded once, on initiation and when they are changed.
433
+
434
+ set height(value) {
435
+ this._height = value;
436
+ this._loadHeight();
437
+ }
438
+
439
+
440
+ set minSpeed(value) {
441
+ this._minSpeed = value;
442
+ this._loadMinMaxSpeed();
443
+ }
444
+
445
+
446
+ set maxSpeed(value) {
447
+ this._maxSpeed = value;
448
+ this._loadMinMaxSpeed();
449
+ }
450
+
451
+
452
+ set fadeOpacity(value) {
453
+ this._fadeOpacity = value;
454
+ this._loadFadeOpacity();
455
+ }
456
+
457
+
458
+ set speedFactor(value) {
459
+ this._speedFactor = value;
460
+ this._loadSpeedFactor()
461
+ }
462
+
463
+
464
+ set dropRate(value) {
465
+ this._dropRate = value;
466
+ this._loadDropRate();
467
+ }
468
+
469
+
470
+ set dropRateBump(value) {
471
+ this._dropRateBump = value;
472
+ this._loadDropRateBump();
473
+ }
474
+
475
+
476
+ set baseOpacity(value) {
477
+ this._baseOpacity = value;
478
+ this._loadBaseOpacity();
479
+ }
480
+
481
+
482
+ set pointSize(value) {
483
+ this._pointSize = value;
484
+ this._loadPointSize();
485
+ }
486
+
487
+
488
+
489
+
490
+ // Color ramp methods.
491
+ // Color ramp is used to colorize the particles according to their speed.
492
+ // Legend max value is used to normalize the speed values.
493
+
494
+ setColorRamp(colors) {
495
+ // lookup texture for colorizing the particles according to their speed
496
+ if (this.gl == null) {
497
+ return;
498
+ }
499
+ this.colorRampTexture = createTexture(this.gl, this.gl.LINEAR, getColorRamp(colors), 16, 16);
500
+ }
501
+
502
+ setColorRampDefault() {
503
+ const { uMax, uMin, vMax, vMin } = this.windData;
504
+ const maxSpeed = Math.sqrt(
505
+ uMax * uMax + vMax * vMax
506
+ )
507
+ const minSpeed = Math.sqrt(
508
+ uMin * uMin + vMin * vMin
509
+ )
510
+ // console.log("maxSpeed", maxSpeed, "minSpeed", minSpeed);
511
+ this._rampMax = maxSpeed > minSpeed ? maxSpeed : minSpeed;
512
+ this._loadRampMax();
513
+ this.setColorRamp(defaultRampColors);
514
+
515
+ }
516
+
517
+ /**
518
+ *
519
+ * @param {*} legendData
520
+ * @param {Array} legendData.thresholds - list of integers
521
+ * @param {Array} legendData.values - list of colors in hex format like #ff0000 (alpha is not included)
522
+ *
523
+ */
524
+ setLegend(legendData) {
525
+ const { gl } = this;
526
+ if (gl === null) {
527
+ throw new Error("wind plugin. setColorRampFromService is called before plugin is registered.");
528
+ }
529
+ const { thresholds, values } = legendData;
530
+ if (thresholds.length === 0 || values.length === 0) {
531
+ return;
532
+ }
533
+ thresholds.push(thresholds[thresholds.length - 1]);
534
+ values.push(values[values.length - 1]);
535
+
536
+ const maxVelocity = thresholds[thresholds.length - 1];
537
+
538
+ const colors = [];
539
+ for (let i = 0; i < thresholds.length; i++) {
540
+ colors.push([
541
+ thresholds[i] / maxVelocity,
542
+ values[i]
543
+ ]);
544
+ }
545
+ const colorRamp = getColorRamp(colors);
546
+ this.colorRampTexture = createTexture(gl, gl.LINEAR, colorRamp, 16, 16);
547
+ this._rampMax = maxVelocity;
548
+ this._loadRampMax();
549
+ }
550
+
551
+
552
+
553
+
554
+ // particle coordinates are encoded into RGBA texture
555
+
556
+ set numParticles(numParticles) {
557
+ const gl = this.gl;
558
+ // we create a square texture where each pixel will hold a particle position encoded as RGBA
559
+ const particleRes = this.particleStateResolution = Math.ceil(Math.sqrt(numParticles));
560
+ this._numParticles = particleRes * particleRes;
561
+
562
+ const particleState = new Uint8Array(this._numParticles * 4);
563
+ for (let i = 0; i < particleState.length; i++) {
564
+ particleState[i] = Math.floor(Math.random() * 256); // randomize the initial particle positions
565
+ }
566
+ // textures to hold the particle state for the current and the next frame
567
+ this.particleStateTexture0 = createTexture(gl, gl.NEAREST, particleState, particleRes, particleRes);
568
+ this.particleStateTexture1 = createTexture(gl, gl.NEAREST, particleState, particleRes, particleRes);
569
+
570
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
571
+ gl.useProgram(this.updateProgram.program);
572
+ gl.uniform1f(this.updateProgram.u_wind_res, this.particleStateResolution);
573
+ gl.useProgram(this.drawSphereProgram.program);
574
+ gl.uniform1f(this.drawSphereProgram.u_particles_res, this.particleStateResolution);
575
+ gl.useProgram(this.drawFlatProgram.program);
576
+ gl.uniform1f(this.drawFlatProgram.u_particles_res, this.particleStateResolution);
577
+ gl.useProgram(currentProgram);
578
+
579
+ // console.log("numParticles", this._numParticles);
580
+ const particleIndices = new Float32Array(this._numParticles);
581
+ for (let i = 0; i < this._numParticles; i++) particleIndices[i] = i;
582
+ this.particleIndexBuffer = createBuffer(gl, particleIndices);
583
+ }
584
+
585
+
586
+ get numParticles() {
587
+ return this._numParticles;
588
+ }
589
+
590
+ // Vector field texture is used to calculate the particle movement.
591
+ /**
592
+ * @param {Object} windData
593
+ * @param {HTMLImageElement} windData.image - image element
594
+
595
+ * @param {number} windData.uMin - minimum u value
596
+ * @param {number} windData.vMin - minimum v value
597
+ * @param {number} windData.uMax - maximum u value
598
+ * @param {number} windData.vMax - maximum v value
599
+ */
600
+ setWind(windData) {
601
+ if (windData === null) {
602
+ return;
603
+ }
604
+ const windDataMeta = this._windDataMeta;
605
+ const gl = this.gl;
606
+ this.windData = windData;
607
+ this.windTexture = createTexture(gl, gl.LINEAR, windData.image);
608
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
609
+ gl.useProgram(this.updateProgram.program);
610
+
611
+ gl.uniform2f(this.updateProgram.u_wind_res, windDataMeta.width, windDataMeta.height);
612
+ gl.uniform2f(this.updateProgram.u_wind_min, this.windData.uMin, this.windData.vMin);
613
+ gl.uniform2f(this.updateProgram.u_wind_max, this.windData.uMax, this.windData.vMax);
614
+ this.setGeometry();
615
+ console.log("windData bbox", windData.bbox);
616
+ const minXY = this._latLongToPixelXY(windDataMeta.bbox[1], windDataMeta.bbox[0]);
617
+ const maxXY = this._latLongToPixelXY(windDataMeta.bbox[3], windDataMeta.bbox[2]);
618
+ this._loadBoundingBoxData(minXY.x, minXY.y, maxXY.x, maxXY.y);
619
+
620
+ gl.useProgram(currentProgram);
621
+
622
+
623
+ this._setCoorcinatesDataCalculatorData();
624
+ this.resize();
625
+ }
626
+
627
+ // Vector field texture is used to calculate the particle movement.
628
+ /**
629
+ * @param {Object} windData
630
+ * @param {BaseInt64} windData.image - image element
631
+ * @param {number} windData.width - image width
632
+ * @param {number} windData.height - image height
633
+ * @param {number} windData.uMin - minimum u value
634
+ * @param {number} windData.vMin - minimum v value
635
+ * @param {number} windData.uMax - maximum u value
636
+ * @param {number} windData.vMax - maximum v value
637
+ * @param {Array} windData.bbox - bounding box [minLon, minLat, maxLon, maxLat]
638
+ */
639
+ setWindDataWithImageBase64(windData) {
640
+ const image = createImageFromBase64(windData.image)
641
+ image.onload = () => {
642
+ windData.image = image;
643
+ this.setWind(windData);
644
+ }
645
+ }
646
+
647
+ /**
648
+ * @param
649
+ * @returns
650
+ */
651
+ getTexturePointSampler(type = `magnitude`) {
652
+ if (type === `magnitude`) {
653
+ if (!this.texturePointSampler) {
654
+ this._createTexturePointSampler();
655
+ this._setCoorcinatesDataCalculatorData();
656
+ }
657
+ return this.texturePointSampler;
658
+ } else if (type === `angle`) {
659
+ if (!this.texturePointSamplerAngle) {
660
+ this._createTexturePointSamplerAngle();
661
+ this._setCoorcinatesDataCalculatorData();
662
+
663
+ }
664
+ return this.texturePointSamplerAngle;
665
+ } else {
666
+ throw new Error(`WindPlugin.getTexturePointSampler: type must be either 'magnitude' or 'angle'.`);
667
+ }
668
+
669
+ }
670
+
671
+
672
+ _createTexturePointSamplerAngle() {
673
+ const { bbox, width, height } = this._windDataMeta
674
+ this.texturePointSamplerAngle = new TexturePointSampler(bbox, width, height);
675
+ }
676
+
677
+ _createTexturePointSampler() {
678
+ const { bbox, width, height } = this._windDataMeta;
679
+ this.texturePointSampler = new TexturePointSampler(bbox, width, height);
680
+ }
681
+
682
+
683
+ _setCoorcinatesDataCalculatorData() {
684
+ if (!this.windData) {
685
+ return;
686
+ }
687
+ if (this.texturePointSamplerAngle) {
688
+ const angle = imageToRadianAngle(this.windData);
689
+ this.texturePointSamplerAngle.updateTextureData(0, angle, angle);
690
+ }
691
+ if (this.texturePointSampler) {
692
+ const magnitude = imageToMagnitude(this.windData);
693
+ this.texturePointSampler.updateTextureData(0, magnitude, magnitude);
694
+ }
695
+ }
696
+
697
+
698
+ // -----------------------------------------------
699
+ // --- inner methods ---
700
+ // -----------------------------------------------
701
+
702
+ _latLongToPixelXY(latitude, longitude) {
703
+ return {
704
+ x: (longitude + 180) / 360,
705
+ y: (90 - latitude) / 180
706
+ };
707
+ }
708
+
709
+ _loadBoundingBoxData(minX, minY, maxX, maxY) {
710
+ // console.log("minX", minX, "minY", minY, "maxX", maxX, "maxY", maxY)
711
+ const gl = this.gl;
712
+
713
+ const bboxMatrix = new Float32Array([
714
+ maxX - minX, 0, 0,
715
+ 0, maxY - minY, 0,
716
+ minX, minY, 1
717
+ ])
718
+
719
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
720
+ gl.useProgram(this.drawSphereProgram.program);
721
+ gl.uniformMatrix3fv(this.drawSphereProgram.u_bbox_matrix, false, bboxMatrix);
722
+ gl.useProgram(this.drawFlatProgram.program);
723
+ gl.uniformMatrix3fv(this.drawFlatProgram.u_bbox_matrix, false, bboxMatrix);
724
+ gl.useProgram(currentProgram);
725
+
726
+ }
727
+
728
+ // When the screen is moved, the particles are reset to random positions.
729
+ // During movement plugin should not draw the particles.
730
+
731
+ _doDraw() {
732
+ const globe = this.globe;
733
+ if (this.windData === null) {
734
+ return false;
735
+ }
736
+ if (globe.api_IsScreenMoving() || this._lastLOD !== globe.api_GetCurrentLODWithDecimal()) {
737
+ if (!this._screenMoved) {
738
+ this._screenMoved = true;
739
+ }
740
+ this._lastLOD = globe.api_GetCurrentLODWithDecimal();
741
+ return false;
742
+ } else {
743
+ if (this._screenMoved) {
744
+ this._loadSpeedFactor();
745
+ this.resize();
746
+ this._screenMoved = false;
747
+ }
748
+ return true;
749
+ }
750
+ }
751
+
752
+
753
+ _draw() {
754
+ const gl = this.gl;
755
+ this._drawScreen();
756
+ this._updateParticles();
757
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
758
+ }
759
+
760
+
761
+ // globe calls `draw3D` method on each frame
762
+ draw3D(projMatrix, modelviewMatrix, transPos) {
763
+ const gl = this.gl;
764
+ if (this._doDraw()) {
765
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
766
+ this.transPos.set([transPos.x, transPos.y, transPos.z], 0);
767
+ this.projMatrix = projMatrix;
768
+ this.modelviewMatrix = modelviewMatrix;
769
+
770
+ gl.activeTexture(gl.TEXTURE0);
771
+ const depthTest = gl.isEnabled(gl.DEPTH_TEST);
772
+ if (depthTest) gl.disable(gl.DEPTH_TEST);
773
+ // if (gl.disable(gl.STENCIL_TEST); //
774
+ this._draw();
775
+ if (depthTest) gl.enable(gl.DEPTH_TEST);
776
+
777
+
778
+ // test visuals
779
+ // gl.viewport(0, 0, 100, 100);
780
+ // this._drawTexture(this.windTexture, 1.0);
781
+ // gl.viewport(0, 100, 100, 100);
782
+ // this._drawTexture(this.particleStateTexture0, 1.0);
783
+ // gl.viewport(0, 200, 100, 100);
784
+ // this._drawTexture(this.colorRampTexture, 1.0);
785
+
786
+ this._resetMachineStates();
787
+ this.globe.DrawRender();
788
+ }
789
+
790
+ }
791
+
792
+ _drawScreen() {
793
+ const { gl, globe } = this;
794
+
795
+ bindTexture(gl, this.windTexture, 0);
796
+ bindTexture(gl, this.particleStateTexture0, 1);
797
+
798
+ // draw the screen into a temporary framebuffer to retain it as the background on the next frame
799
+ bindFramebuffer(gl, this.framebuffer, this.screenTexture);
800
+ gl.viewport(0, 0, globe.api_ScrW(), globe.api_ScrH());
801
+ gl.enable(gl.BLEND);
802
+ gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA); // non-premultiplied alpha
803
+ this._drawTexture(this.backgroundTexture, this._fadeOpacity);
804
+
805
+ this._drawParticles();
806
+
807
+ bindFramebuffer(gl, null);
808
+ this._drawTexture(this.screenTexture, 1.0);
809
+ // gl.disable(gl.BLEND);
810
+ // save the current screen as the background for the next frame
811
+ const temp = this.backgroundTexture;
812
+ this.backgroundTexture = this.screenTexture;
813
+ this.screenTexture = temp;
814
+
815
+
816
+
817
+ }
818
+
819
+
820
+ _drawTexture(texture, opacity) {
821
+ const gl = this.gl;
822
+ const program = this.screenProgram;
823
+ gl.useProgram(program.program);
824
+
825
+ bindAttribute(gl, this.quadBuffer, program.a_pos, 2);
826
+ bindTexture(gl, texture, 2);
827
+ gl.uniform1i(program.u_screen, 2);
828
+ gl.uniform1f(program.u_opacity, opacity);
829
+
830
+ gl.drawArrays(gl.TRIANGLES, 0, 6);
831
+
832
+
833
+ gl.bindTexture(gl.TEXTURE_2D, null);
834
+ gl.activeTexture(gl.TEXTURE0);
835
+
836
+ }
837
+
838
+ _loadRampMax() {
839
+ const { gl } = this;
840
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
841
+ // console.log("load ramp max", this._rampMax);
842
+ gl.useProgram(this.drawSphereProgram.program);
843
+ gl.uniform1f(this.drawSphereProgram.u_color_ramp_max, this._rampMax);
844
+ gl.useProgram(this.drawFlatProgram.program);
845
+ gl.uniform1f(this.drawFlatProgram.u_color_ramp_max, this._rampMax);
846
+ gl.useProgram(currentProgram);
847
+ }
848
+
849
+
850
+ _drawParticlesSphere() {
851
+
852
+ const { gl, projMatrix, modelviewMatrix, transPos } = this;
853
+ const program = this.drawSphereProgram;
854
+ gl.useProgram(program.program);
855
+
856
+ bindAttribute(gl, this.particleIndexBuffer, program.a_index, 1);
857
+ bindTexture(gl, this.colorRampTexture, 2);
858
+
859
+ gl.uniform1i(program.u_wind, 0);
860
+ gl.uniform1i(program.u_particles, 1);
861
+ gl.uniform1i(program.u_color_ramp, 2);
862
+
863
+ gl.uniformMatrix4fv(program.uProjectionMatrix, false, projMatrix);
864
+ gl.uniformMatrix4fv(program.uModelViewMatrix, false, modelviewMatrix);
865
+ gl.uniform3fv(program.uTranslate, transPos);
866
+
867
+ gl.drawArrays(gl.POINTS, 0, this._numParticles);
868
+ }
869
+
870
+
871
+ _drawParticlesFlat() {
872
+ const { gl, transPos, projMatrix, globe } = this;
873
+ const program = this.drawFlatProgram;
874
+ gl.useProgram(program.program);
875
+
876
+ bindAttribute(gl, this.particleIndexBuffer, program.a_index, 1);
877
+ bindTexture(gl, this.colorRampTexture, 2);
878
+
879
+ gl.uniform1i(program.u_wind, 0);
880
+ gl.uniform1i(program.u_particles, 1);
881
+ gl.uniform1i(program.u_color_ramp, 2);
882
+
883
+ gl.uniform2f(program.u_scrWH, globe.api_ScrW(), globe.api_ScrH());
884
+ const { width, height } = globe.api_GetCurrentWorldWH();
885
+ gl.uniform2f(program.u_mapWH, width, height);
886
+
887
+ gl.uniformMatrix4fv(program.uProjectionMatrix, false, projMatrix);
888
+ gl.uniform3fv(program.uTranslate, transPos);
889
+
890
+ gl.drawArrays(gl.POINTS, 0, this._numParticles);
891
+ }
892
+
893
+ // globe calls `setGeometry` method on map projection change. FLAT or SPHERE
894
+
895
+ setGeometry() {
896
+ if (this.windData === null) {
897
+ return;
898
+ }
899
+ const { globe, gl } = this;
900
+ const geometry = globe.api_GetCurrentGeometry()
901
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
902
+ if (geometry === globe.api_GeometryTypes().SPHERE) {
903
+ this._drawParticles = this._drawParticlesSphere;
904
+ gl.useProgram(this.drawSphereProgram.program);
905
+ gl.uniform2f(this.drawSphereProgram.u_wind_min, this.windData.uMin, this.windData.vMin);
906
+ gl.uniform2f(this.drawSphereProgram.u_wind_max, this.windData.uMax, this.windData.vMax);
907
+ } else if (geometry === globe.api_GeometryTypes().FLAT) {
908
+ this._drawParticles = this._drawParticlesFlat;
909
+ gl.useProgram(this.drawFlatProgram.program);
910
+ gl.uniform2f(this.drawFlatProgram.u_wind_min, this.windData.uMin, this.windData.vMin);
911
+ gl.uniform2f(this.drawFlatProgram.u_wind_max, this.windData.uMax, this.windData.vMax);
912
+ } else {
913
+ alert("Geometry is not supported");
914
+ }
915
+ gl.useProgram(currentProgram);
916
+ this.resize();
917
+ }
918
+
919
+ _updateParticles() {
920
+ const gl = this.gl;
921
+ const program = this.updateProgram;
922
+ gl.disable(gl.BLEND);
923
+
924
+ gl.useProgram(program.program);
925
+ gl.viewport(0, 0, this.particleStateResolution, this.particleStateResolution);
926
+
927
+ bindAttribute(gl, this.quadBuffer, program.a_pos, 2);
928
+
929
+ gl.uniform1i(program.u_wind, 0);
930
+ gl.uniform1i(program.u_particles, 1);
931
+
932
+ bindAttribute(gl, this.quadBuffer, program.a_pos, 2);
933
+
934
+ gl.uniform1f(program.u_rand_seed, Math.random());
935
+
936
+ bindFramebuffer(gl, this.framebuffer, this.particleStateTexture1);
937
+
938
+ gl.drawArrays(gl.TRIANGLES, 0, 6);
939
+
940
+ // swap the particle state textures so the new one becomes the current one
941
+ const temp = this.particleStateTexture0;
942
+ this.particleStateTexture0 = this.particleStateTexture1;
943
+ this.particleStateTexture1 = temp;
944
+
945
+ // Add this line to restore the viewport
946
+
947
+ gl.viewport(0, 0, this.globe.api_ScrW(), this.globe.api_ScrH());
948
+ }
949
+
950
+
951
+
952
+ _resetMachineStates() {
953
+ const { gl, globe } = this;
954
+
955
+ // Unbind all textures
956
+ for (let i = 0; i < 3; i++) {
957
+ gl.activeTexture(gl.TEXTURE0 + i);
958
+ gl.bindTexture(gl.TEXTURE_2D, null);
959
+ }
960
+ gl.activeTexture(gl.TEXTURE0);
961
+
962
+ // Unbind buffers
963
+ gl.bindBuffer(gl.ARRAY_BUFFER, null);
964
+ gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
965
+ gl.bindVertexArray(null); // Add this line
966
+ gl.enable(gl.BLEND);
967
+ defaultblendfunction(gl);
968
+ gl.bindFramebuffer(gl.FRAMEBUFFER, null);
969
+ gl.viewport(0, 0, globe.api_ScrW(), globe.api_ScrH());
970
+ }
971
+
972
+
973
+ _loadHeight() {
974
+ const { gl, drawSphereProgram } = this;
975
+ if (gl == null) return;
976
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
977
+ gl.useProgram(drawSphereProgram.program);
978
+ gl.uniform1f(drawSphereProgram.u_height, this._height);
979
+ gl.useProgram(currentProgram);
980
+ }
981
+
982
+
983
+ _loadMinMaxSpeed() {
984
+ const { gl, drawSphereProgram, drawFlatProgram } = this;
985
+ if (gl == null) return;
986
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
987
+ gl.useProgram(drawSphereProgram.program);
988
+ gl.uniform1f(drawSphereProgram.u_min_speed, this._minSpeed);
989
+ gl.uniform1f(drawSphereProgram.u_max_speed, this._maxSpeed);
990
+ gl.useProgram(drawFlatProgram.program);
991
+ gl.uniform1f(drawFlatProgram.u_min_speed, this._minSpeed);
992
+ gl.uniform1f(drawFlatProgram.u_max_speed, this._maxSpeed);
993
+ gl.useProgram(currentProgram);
994
+ }
995
+
996
+ _loadFadeOpacity() {
997
+ const { gl, screenProgram } = this;
998
+ if (gl == null) {
999
+ return;
1000
+ }
1001
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
1002
+ gl.useProgram(screenProgram.program);
1003
+ gl.uniform1f(screenProgram.u_opacity, this._fadeOpacity);
1004
+ gl.useProgram(currentProgram);
1005
+
1006
+ }
1007
+
1008
+ _loadSpeedFactor() {
1009
+ const { gl, globe, updateProgram } = this;
1010
+ if (gl == null) {
1011
+ return;
1012
+ }
1013
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
1014
+ gl.useProgram(updateProgram.program);
1015
+ gl.uniform1f(updateProgram.u_speed_factor, this._speedFactor / Math.ceil(Math.sqrt(globe.api_GetCurrentLOD())));
1016
+ gl.useProgram(currentProgram);
1017
+ }
1018
+
1019
+ _loadDropRate() {
1020
+ const { gl, updateProgram } = this;
1021
+ if (gl == null) {
1022
+ return;
1023
+ }
1024
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
1025
+ gl.useProgram(updateProgram.program);
1026
+ gl.uniform1f(updateProgram.u_drop_rate, this._dropRate);
1027
+ gl.useProgram(currentProgram);
1028
+ }
1029
+
1030
+ _loadDropRateBump() {
1031
+ const { gl, updateProgram } = this;
1032
+ if (gl == null) {
1033
+ return;
1034
+ }
1035
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
1036
+ gl.useProgram(updateProgram.program);
1037
+ gl.uniform1f(updateProgram.u_drop_rate_bump, this._dropRateBump);
1038
+ gl.useProgram(currentProgram);
1039
+ }
1040
+
1041
+
1042
+ _loadBaseOpacity() {
1043
+ const { gl, drawSphereProgram, drawFlatProgram } = this;
1044
+ if (gl == null) {
1045
+ return;
1046
+ }
1047
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
1048
+ gl.useProgram(drawSphereProgram.program);
1049
+ gl.uniform1f(drawSphereProgram.u_base_opacity, this._baseOpacity);
1050
+ gl.useProgram(drawFlatProgram.program);
1051
+ gl.uniform1f(drawFlatProgram.u_base_opacity, this._baseOpacity);
1052
+ gl.useProgram(currentProgram);
1053
+ }
1054
+
1055
+
1056
+ _loadPointSize() {
1057
+ const { gl, drawSphereProgram, drawFlatProgram } = this;
1058
+ if (gl == null) {
1059
+ return;
1060
+ }
1061
+ const currentProgram = gl.getParameter(gl.CURRENT_PROGRAM);
1062
+ gl.useProgram(drawSphereProgram.program);
1063
+ gl.uniform1f(drawSphereProgram.u_point_size, this._pointSize);
1064
+ gl.useProgram(drawFlatProgram.program);
1065
+ gl.uniform1f(drawFlatProgram.u_point_size, this._pointSize);
1066
+ gl.useProgram(currentProgram);
1067
+ }
1068
+
1069
+
1070
+ // globe plugin init methodu
1071
+ init(globe, gl) {
1072
+ // console.log("init wind plugin")
1073
+ this.globe = globe;
1074
+ this.gl = gl;
1075
+
1076
+ this.drawSphereProgram = createProgramWrapper(gl, drawSphereVert, drawFrag);
1077
+ this.screenProgram = createProgramWrapper(gl, quadVert, screenFrag);
1078
+ this.updateProgram = createProgramWrapper(gl, quadVert, updateFrag);
1079
+
1080
+ this.drawFlatProgram = createProgramWrapper(gl, drawFlatVert, drawFrag);
1081
+ this.quadBuffer = createBuffer(gl, new Float32Array([0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 1]));
1082
+ this.framebuffer = gl.createFramebuffer();
1083
+
1084
+ this._loadFadeOpacity();
1085
+ this._loadSpeedFactor();
1086
+ this._loadDropRate();
1087
+ this._loadDropRateBump();
1088
+ this._loadBaseOpacity();
1089
+ this._loadPointSize();
1090
+ this._loadMinMaxSpeed();
1091
+ this._loadHeight();
1092
+
1093
+
1094
+ if (this._legendData != null) {
1095
+ this.setLegend(this._legendData);
1096
+ }
1097
+ this.numParticles = this._numParticles;
1098
+ this.resize();
1099
+ }
1100
+
1101
+ resize(a, b) {
1102
+ const { gl, globe } = this;
1103
+
1104
+ const emptyPixels = new Uint8Array(globe.api_ScrW() * globe.api_ScrH() * 4);
1105
+ // screen textures to hold the drawn screen for the previous and the current frame
1106
+ this.backgroundTexture = createTexture(gl, gl.LINEAR, emptyPixels, globe.api_ScrW(), globe.api_ScrH());
1107
+ this.screenTexture = createTexture(gl, gl.LINEAR, emptyPixels, globe.api_ScrW(), globe.api_ScrH());
1108
+ gl.viewport(0, 0, globe.api_ScrW(), globe.api_ScrH());
1109
+
1110
+ }
1111
+
1112
+
1113
+ // globe calls this `free` on plugin object unregistration
1114
+
1115
+ free() {
1116
+ const gl = this.gl;
1117
+ gl.deleteBuffer(this.quadBuffer);
1118
+ gl.deleteFramebuffer(this.framebuffer);
1119
+ gl.deleteTexture(this.backgroundTexture);
1120
+ gl.deleteTexture(this.screenTexture);
1121
+ gl.deleteTexture(this.particleStateTexture0);
1122
+ gl.deleteTexture(this.particleStateTexture1);
1123
+ gl.deleteTexture(this.windTexture);
1124
+ gl.deleteTexture(this.colorRampTexture);
1125
+ gl.deleteProgram(this.drawSphereProgram.program);
1126
+ gl.deleteProgram(this.screenProgram.program);
1127
+ gl.deleteProgram(this.updateProgram.program);
1128
+ gl.deleteProgram(this.drawFlatProgram.program);
1129
+ gl.deleteBuffer(this.particleIndexBuffer);
1130
+
1131
+ }
1132
+ }
1133
+