itowns 2.45.1-next.0 → 2.45.1-next.1

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 (185) hide show
  1. package/dist/455.js +2 -0
  2. package/dist/455.js.map +1 -0
  3. package/dist/debug.js +3 -0
  4. package/dist/debug.js.LICENSE.txt +13 -0
  5. package/dist/debug.js.map +1 -0
  6. package/dist/itowns.js +3 -0
  7. package/dist/itowns.js.LICENSE.txt +5 -0
  8. package/dist/itowns.js.map +1 -0
  9. package/dist/itowns_lasparser.js +2 -0
  10. package/dist/itowns_lasparser.js.map +1 -0
  11. package/dist/itowns_lasworker.js +2 -0
  12. package/dist/itowns_lasworker.js.map +1 -0
  13. package/dist/itowns_potree2worker.js +2 -0
  14. package/dist/itowns_potree2worker.js.map +1 -0
  15. package/dist/itowns_widgets.js +2 -0
  16. package/dist/itowns_widgets.js.map +1 -0
  17. package/lib/Controls/FirstPersonControls.js +308 -0
  18. package/lib/Controls/FlyControls.js +175 -0
  19. package/lib/Controls/GlobeControls.js +1178 -0
  20. package/lib/Controls/PlanarControls.js +1025 -0
  21. package/lib/Controls/StateControl.js +432 -0
  22. package/lib/Controls/StreetControls.js +392 -0
  23. package/lib/Converter/Feature2Mesh.js +612 -0
  24. package/lib/Converter/Feature2Texture.js +174 -0
  25. package/lib/Converter/convertToTile.js +70 -0
  26. package/lib/Converter/textureConverter.js +43 -0
  27. package/lib/Core/3DTiles/C3DTBatchTable.js +131 -0
  28. package/lib/Core/3DTiles/C3DTBatchTableHierarchyExtension.js +96 -0
  29. package/lib/Core/3DTiles/C3DTBoundingVolume.js +156 -0
  30. package/lib/Core/3DTiles/C3DTExtensions.js +97 -0
  31. package/lib/Core/3DTiles/C3DTFeature.js +110 -0
  32. package/lib/Core/3DTiles/C3DTilesEnums.js +20 -0
  33. package/lib/Core/3DTiles/C3DTileset.js +99 -0
  34. package/lib/Core/3DTiles/utils/BinaryPropertyAccessor.js +100 -0
  35. package/lib/Core/AnimationPlayer.js +142 -0
  36. package/lib/Core/CopcNode.js +174 -0
  37. package/lib/Core/Deprecated/Undeprecator.js +74 -0
  38. package/lib/Core/EntwinePointTileNode.js +126 -0
  39. package/lib/Core/Feature.js +488 -0
  40. package/lib/Core/Geographic/GeoidGrid.js +108 -0
  41. package/lib/Core/Label.js +222 -0
  42. package/lib/Core/MainLoop.js +209 -0
  43. package/lib/Core/Picking.js +255 -0
  44. package/lib/Core/PointCloudNode.js +42 -0
  45. package/lib/Core/Potree2Node.js +206 -0
  46. package/lib/Core/Potree2PointAttributes.js +139 -0
  47. package/lib/Core/PotreeNode.js +101 -0
  48. package/lib/Core/Prefab/Globe/Atmosphere.js +293 -0
  49. package/lib/Core/Prefab/Globe/GlobeLayer.js +152 -0
  50. package/lib/Core/Prefab/Globe/GlobeTileBuilder.js +110 -0
  51. package/lib/Core/Prefab/Globe/SkyShader.js +78 -0
  52. package/lib/Core/Prefab/GlobeView.js +155 -0
  53. package/lib/Core/Prefab/Planar/PlanarLayer.js +59 -0
  54. package/lib/Core/Prefab/Planar/PlanarTileBuilder.js +71 -0
  55. package/lib/Core/Prefab/PlanarView.js +62 -0
  56. package/lib/Core/Prefab/TileBuilder.js +82 -0
  57. package/lib/Core/Prefab/computeBufferTileGeometry.js +248 -0
  58. package/lib/Core/Scheduler/Cache.js +17 -0
  59. package/lib/Core/Scheduler/CancelledCommandException.js +15 -0
  60. package/lib/Core/Scheduler/Scheduler.js +294 -0
  61. package/lib/Core/Style.js +660 -0
  62. package/lib/Core/StyleOptions.js +486 -0
  63. package/lib/Core/System/Capabilities.js +63 -0
  64. package/lib/Core/Tile/Tile.js +205 -0
  65. package/lib/Core/Tile/TileGrid.js +49 -0
  66. package/lib/Core/TileGeometry.js +124 -0
  67. package/lib/Core/TileMesh.js +108 -0
  68. package/lib/Core/View.js +1115 -0
  69. package/lib/Layer/C3DTilesLayer.js +459 -0
  70. package/lib/Layer/ColorLayer.js +154 -0
  71. package/lib/Layer/CopcLayer.js +63 -0
  72. package/lib/Layer/ElevationLayer.js +139 -0
  73. package/lib/Layer/EntwinePointTileLayer.js +71 -0
  74. package/lib/Layer/FeatureGeometryLayer.js +77 -0
  75. package/lib/Layer/GeoidLayer.js +80 -0
  76. package/lib/Layer/GeometryLayer.js +233 -0
  77. package/lib/Layer/InfoLayer.js +64 -0
  78. package/lib/Layer/LabelLayer.js +469 -0
  79. package/lib/Layer/Layer.js +335 -0
  80. package/lib/Layer/LayerUpdateState.js +89 -0
  81. package/lib/Layer/LayerUpdateStrategy.js +80 -0
  82. package/lib/Layer/OGC3DTilesLayer.js +543 -0
  83. package/lib/Layer/OrientedImageLayer.js +227 -0
  84. package/lib/Layer/PointCloudLayer.js +405 -0
  85. package/lib/Layer/Potree2Layer.js +171 -0
  86. package/lib/Layer/PotreeLayer.js +72 -0
  87. package/lib/Layer/RasterLayer.js +37 -0
  88. package/lib/Layer/ReferencingLayerProperties.js +62 -0
  89. package/lib/Layer/TiledGeometryLayer.js +459 -0
  90. package/lib/Loader/LASLoader.js +193 -0
  91. package/lib/Loader/Potree2BrotliLoader.js +261 -0
  92. package/lib/Loader/Potree2Loader.js +207 -0
  93. package/lib/Main.js +113 -0
  94. package/lib/MainBundle.js +4 -0
  95. package/lib/Parser/B3dmParser.js +174 -0
  96. package/lib/Parser/CameraCalibrationParser.js +94 -0
  97. package/lib/Parser/GDFParser.js +72 -0
  98. package/lib/Parser/GTXParser.js +75 -0
  99. package/lib/Parser/GeoJsonParser.js +212 -0
  100. package/lib/Parser/GpxParser.js +25 -0
  101. package/lib/Parser/ISGParser.js +71 -0
  102. package/lib/Parser/KMLParser.js +25 -0
  103. package/lib/Parser/LASParser.js +137 -0
  104. package/lib/Parser/MapBoxUrlParser.js +83 -0
  105. package/lib/Parser/PntsParser.js +131 -0
  106. package/lib/Parser/Potree2BinParser.js +92 -0
  107. package/lib/Parser/PotreeBinParser.js +106 -0
  108. package/lib/Parser/PotreeCinParser.js +29 -0
  109. package/lib/Parser/ShapefileParser.js +78 -0
  110. package/lib/Parser/VectorTileParser.js +215 -0
  111. package/lib/Parser/XbilParser.js +120 -0
  112. package/lib/Parser/deprecated/LegacyGLTFLoader.js +1386 -0
  113. package/lib/Parser/iGLTFLoader.js +168 -0
  114. package/lib/Process/3dTilesProcessing.js +304 -0
  115. package/lib/Process/FeatureProcessing.js +76 -0
  116. package/lib/Process/LayeredMaterialNodeProcessing.js +229 -0
  117. package/lib/Process/ObjectRemovalHelper.js +97 -0
  118. package/lib/Process/handlerNodeError.js +23 -0
  119. package/lib/Provider/3dTilesProvider.js +149 -0
  120. package/lib/Provider/DataSourceProvider.js +24 -0
  121. package/lib/Provider/Fetcher.js +233 -0
  122. package/lib/Provider/PointCloudProvider.js +45 -0
  123. package/lib/Provider/TileProvider.js +16 -0
  124. package/lib/Provider/URLBuilder.js +116 -0
  125. package/lib/Renderer/Camera.js +281 -0
  126. package/lib/Renderer/Color.js +56 -0
  127. package/lib/Renderer/ColorLayersOrdering.js +115 -0
  128. package/lib/Renderer/CommonMaterial.js +31 -0
  129. package/lib/Renderer/Label2DRenderer.js +192 -0
  130. package/lib/Renderer/LayeredMaterial.js +243 -0
  131. package/lib/Renderer/OBB.js +150 -0
  132. package/lib/Renderer/OrientedImageCamera.js +118 -0
  133. package/lib/Renderer/OrientedImageMaterial.js +167 -0
  134. package/lib/Renderer/PointsMaterial.js +485 -0
  135. package/lib/Renderer/RasterTile.js +243 -0
  136. package/lib/Renderer/RenderMode.js +31 -0
  137. package/lib/Renderer/Shader/ShaderChunk.js +160 -0
  138. package/lib/Renderer/Shader/ShaderUtils.js +47 -0
  139. package/lib/Renderer/SphereHelper.js +17 -0
  140. package/lib/Renderer/WebXR.js +51 -0
  141. package/lib/Renderer/c3DEngine.js +214 -0
  142. package/lib/Source/C3DTilesGoogleSource.js +74 -0
  143. package/lib/Source/C3DTilesIonSource.js +54 -0
  144. package/lib/Source/C3DTilesSource.js +30 -0
  145. package/lib/Source/CopcSource.js +126 -0
  146. package/lib/Source/EntwinePointTileSource.js +72 -0
  147. package/lib/Source/FileSource.js +188 -0
  148. package/lib/Source/OGC3DTilesGoogleSource.js +29 -0
  149. package/lib/Source/OGC3DTilesIonSource.js +34 -0
  150. package/lib/Source/OGC3DTilesSource.js +21 -0
  151. package/lib/Source/OrientedImageSource.js +59 -0
  152. package/lib/Source/Potree2Source.js +167 -0
  153. package/lib/Source/PotreeSource.js +82 -0
  154. package/lib/Source/Source.js +202 -0
  155. package/lib/Source/TMSSource.js +144 -0
  156. package/lib/Source/VectorTilesSource.js +182 -0
  157. package/lib/Source/WFSSource.js +170 -0
  158. package/lib/Source/WMSSource.js +167 -0
  159. package/lib/Source/WMTSSource.js +92 -0
  160. package/lib/ThreeExtended/capabilities/WebGL.js +69 -0
  161. package/lib/ThreeExtended/libs/ktx-parse.module.js +506 -0
  162. package/lib/ThreeExtended/libs/zstddec.module.js +29 -0
  163. package/lib/ThreeExtended/loaders/DDSLoader.js +200 -0
  164. package/lib/ThreeExtended/loaders/DRACOLoader.js +400 -0
  165. package/lib/ThreeExtended/loaders/GLTFLoader.js +2879 -0
  166. package/lib/ThreeExtended/loaders/KTX2Loader.js +709 -0
  167. package/lib/ThreeExtended/math/ColorSpaces.js +59 -0
  168. package/lib/ThreeExtended/utils/BufferGeometryUtils.js +846 -0
  169. package/lib/ThreeExtended/utils/WorkerPool.js +70 -0
  170. package/lib/Utils/CameraUtils.js +554 -0
  171. package/lib/Utils/DEMUtils.js +350 -0
  172. package/lib/Utils/FeaturesUtils.js +156 -0
  173. package/lib/Utils/Gradients.js +16 -0
  174. package/lib/Utils/ThreeUtils.js +115 -0
  175. package/lib/Utils/gui/C3DTilesStyle.js +218 -0
  176. package/lib/Utils/gui/Main.js +7 -0
  177. package/lib/Utils/gui/Minimap.js +152 -0
  178. package/lib/Utils/gui/Navigation.js +245 -0
  179. package/lib/Utils/gui/Scale.js +104 -0
  180. package/lib/Utils/gui/Searchbar.js +234 -0
  181. package/lib/Utils/gui/Widget.js +80 -0
  182. package/lib/Utils/placeObjectOnGround.js +136 -0
  183. package/lib/Worker/LASLoaderWorker.js +19 -0
  184. package/lib/Worker/Potree2Worker.js +21 -0
  185. package/package.json +2 -2
@@ -0,0 +1,222 @@
1
+ import * as THREE from 'three';
2
+ import DEMUtils from "../Utils/DEMUtils.js";
3
+ import { Coordinates } from '@itowns/geographic';
4
+ const coord = new Coordinates('EPSG:4326');
5
+ let rect;
6
+
7
+ // set it once
8
+ let STYLE_TRANSFORM = '';
9
+ if (document.documentElement.style.transform !== undefined) {
10
+ STYLE_TRANSFORM = 'transform';
11
+ } else if (document.documentElement.style.webkitTransform !== undefined) {
12
+ STYLE_TRANSFORM = 'webkitTransform';
13
+ } else if (document.documentElement.style.mozTransform !== undefined) {
14
+ STYLE_TRANSFORM = 'mozTransform';
15
+ } else if (document.documentElement.style.oTransform !== undefined) {
16
+ STYLE_TRANSFORM = 'oTransform';
17
+ } else {
18
+ STYLE_TRANSFORM = 'transform';
19
+ }
20
+
21
+ /**
22
+ * An object that handles the display of a text and/or an icon, linked to a 3D
23
+ * position. The content of the `Label` is managed through a DOM object, in a
24
+ * `<div>` handled by the `Label2DRenderer`.
25
+ *
26
+ * @property {boolean} isLabel - Used to checkout whether this object is a
27
+ * Label. Default is true. You should not change this, as it is used internally
28
+ * for optimisation.
29
+ * @property {Element} content - The DOM object that contains the content of the
30
+ * label. The style and the position are applied on this object. All labels
31
+ * contain the `itowns-label` class, as well as a specific class related to the
32
+ * layer linked to it: `itowns-label-[layer-id]` (replace `[layer-id]` by the
33
+ * correct string).
34
+ * @property {THREE.Vector3} position - The position in the 3D world of the
35
+ * label.
36
+ * @property {number} padding - sets the padding area on all four sides of an element at once.
37
+ * @property {Coordinates} coordinates - The coordinates of the label.
38
+ * @property {number} order - Order of the label that will be read from the
39
+ * style. It helps sorting and prioritizing a Label during render.
40
+ */
41
+ class Label extends THREE.Object3D {
42
+ /**
43
+ * @param {Element|string} content - The content; can be a
44
+ * string, with or without HTML tags in it, or it can be an Element.
45
+ * @param {Coordinates} coordinates - The world coordinates, where to place
46
+ * the Label.
47
+ * @param {Style} style - The style to apply to the content. Once the style
48
+ * is applied, it cannot be changed directly. However, if it really needed,
49
+ * it can be accessed through `label.content.style`, but it is highly
50
+ * discouraged to do so.
51
+ */
52
+ constructor() {
53
+ let content = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
54
+ let coordinates = arguments.length > 1 ? arguments[1] : undefined;
55
+ let style = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
56
+ if (coordinates == undefined) {
57
+ throw new Error('coordinates are mandatory to add a Label');
58
+ }
59
+ if (arguments.length > 3) {
60
+ console.warn('Deprecated argument sprites in Label constructor. Sprites must be configured in style argument.');
61
+ }
62
+ super();
63
+ let _visible = this.visible;
64
+ // can't do an ES6 setter/getter here
65
+ Object.defineProperty(this, 'visible', {
66
+ set(v) {
67
+ if (v != _visible) {
68
+ // avoid changing the style
69
+ _visible = v;
70
+ this.content.style.display = v ? 'block' : 'none';
71
+ // TODO: add smooth transition for fade in/out
72
+ }
73
+ },
74
+ get() {
75
+ return _visible;
76
+ }
77
+ });
78
+ this.isLabel = true;
79
+ this.coordinates = coordinates;
80
+ this.projectedPosition = {
81
+ x: 0,
82
+ y: 0
83
+ };
84
+ this.boundaries = {
85
+ left: 0,
86
+ right: 0,
87
+ top: 0,
88
+ bottom: 0
89
+ };
90
+ if (typeof content === 'string') {
91
+ this.content = document.createElement('div');
92
+ this.content.textContent = style.text.field;
93
+ } else {
94
+ this.content = content.cloneNode(true);
95
+ }
96
+ this.content.classList.add('itowns-label');
97
+ this.content.style.userSelect = 'none';
98
+ this.content.style.position = 'absolute';
99
+ if (style.isStyle) {
100
+ this.anchor = style.getTextAnchorPosition();
101
+ this.styleOffset = style.text.offset;
102
+ if (typeof content === 'string') {
103
+ if (style.text.haloWidth > 0) {
104
+ this.content.classList.add('itowns-stroke-single');
105
+ }
106
+ style.applyToHTML(this.content).then(icon => {
107
+ if (icon) {
108
+ // Not sure if that test is needed...
109
+ this.icon = icon;
110
+ }
111
+ });
112
+ }
113
+ } else {
114
+ this.anchor = [0, 0];
115
+ this.styleOffset = [0, 0];
116
+ }
117
+ this.iconOffset = {
118
+ left: 0,
119
+ right: 0,
120
+ top: 0,
121
+ bottom: 0
122
+ };
123
+ this.zoom = {
124
+ min: style.zoom && style.zoom.min != undefined ? style.zoom.min : 2,
125
+ max: style.zoom && style.zoom.max != undefined ? style.zoom.max : 24
126
+ };
127
+ this.order = style.order || 0;
128
+ // Padding value, to avoid labels being too close to each other.
129
+ this.padding = 2;
130
+ }
131
+
132
+ /**
133
+ * Moves a label on the screen, using screen coordinates. It updates the
134
+ * boundaries as it moves it.
135
+ *
136
+ * @param {number} x - X coordinates in pixels, from left.
137
+ * @param {number} y - Y coordinates in pixels, from top.
138
+ */
139
+ updateProjectedPosition(x, y) {
140
+ const X = Math.round(x);
141
+ const Y = Math.round(y);
142
+ if (X != this.projectedPosition.x || Y != this.projectedPosition.y) {
143
+ this.projectedPosition.x = X;
144
+ this.projectedPosition.y = Y;
145
+ this.boundaries.left = x + this.offset.left - this.padding;
146
+ this.boundaries.right = x + this.offset.right + this.padding;
147
+ this.boundaries.top = y + this.offset.top - this.padding;
148
+ this.boundaries.bottom = y + this.offset.bottom + this.padding;
149
+
150
+ // The boundaries of the label are the union of the boundaries of the text
151
+ // and the boundaries of the icon, if it exists.
152
+ // Checking if this.icon is not only zeros is mandatory, to prevent case
153
+ // when a boundary is set to x or y coordinate
154
+ if (this.iconOffset.left !== 0 || this.iconOffset.right !== 0 || this.iconOffset.top !== 0 || this.iconOffset.bottom !== 0) {
155
+ this.boundaries.left = Math.min(this.boundaries.left, x + this.iconOffset.left);
156
+ this.boundaries.right = Math.max(this.boundaries.right, x + this.iconOffset.right);
157
+ this.boundaries.top = Math.min(this.boundaries.top, y + this.iconOffset.top);
158
+ this.boundaries.bottom = Math.max(this.boundaries.bottom, y + this.iconOffset.bottom);
159
+ }
160
+ }
161
+ }
162
+ updateCSSPosition() {
163
+ // translate all content according to its given anchor
164
+ this.content.style[STYLE_TRANSFORM] = `translate(${this.projectedPosition.x + this.offset.left}px, ${this.projectedPosition.y + this.offset.top}px)`;
165
+
166
+ // translate possible icon inside content to cancel anchoring on it, so that it can later be positioned
167
+ // according to its own anchor
168
+ if (this.icon) {
169
+ this.icon.style[STYLE_TRANSFORM] = `translate(${-this.offset.left}px, ${-this.offset.top}px)`;
170
+ }
171
+ }
172
+
173
+ /**
174
+ * Updates the screen dimensions of the label, using
175
+ * `getBoundingClientRect`. It updates `width` and `height` property of the
176
+ * label, and the boundaries.
177
+ */
178
+ initDimensions() {
179
+ if (!this.offset) {
180
+ rect = this.content.getBoundingClientRect();
181
+ const width = Math.round(rect.width);
182
+ const height = Math.round(rect.height);
183
+ this.offset = {
184
+ left: width * this.anchor[0] + this.styleOffset[0],
185
+ top: height * this.anchor[1] + this.styleOffset[1]
186
+ };
187
+ this.offset.right = this.offset.left + width;
188
+ this.offset.bottom = this.offset.top + height;
189
+ if (this.icon) {
190
+ rect = this.icon.getBoundingClientRect();
191
+ this.iconOffset = {
192
+ left: Math.floor(rect.x),
193
+ top: Math.floor(rect.y),
194
+ right: Math.ceil(rect.x + rect.width),
195
+ bottom: Math.ceil(rect.y + rect.height)
196
+ };
197
+ }
198
+ }
199
+ }
200
+ update3dPosition(crs) {
201
+ this.coordinates.as(crs, coord).toVector3(this.position);
202
+ this.updateMatrixWorld();
203
+ }
204
+ updateElevationFromLayer(layer, nodes) {
205
+ if (layer.attachedLayers.filter(l => l.isElevationLayer).length == 0) {
206
+ return;
207
+ }
208
+ let elevation = Math.max(0, DEMUtils.getElevationValueAt(layer, this.coordinates, DEMUtils.FAST_READ_Z, nodes));
209
+ if (isNaN(elevation)) {
210
+ elevation = Math.max(0, DEMUtils.getElevationValueAt(layer, this.coordinates, DEMUtils.FAST_READ_Z));
211
+ }
212
+ if (!isNaN(elevation) && elevation != this.coordinates.z) {
213
+ this.coordinates.z = elevation;
214
+ }
215
+ }
216
+ updateHorizonCullingPoint() {
217
+ if (this.horizonCullingPoint) {
218
+ this.getWorldPosition(this.horizonCullingPoint);
219
+ }
220
+ }
221
+ }
222
+ export default Label;
@@ -0,0 +1,209 @@
1
+ import { EventDispatcher } from 'three';
2
+ export const RENDERING_PAUSED = 0;
3
+ export const RENDERING_SCHEDULED = 1;
4
+
5
+ /**
6
+ * MainLoop's update events list that are fired using
7
+ * {@link View#execFrameRequesters}.
8
+ *
9
+ * @property UPDATE_START {string} fired at the start of the update
10
+ * @property BEFORE_CAMERA_UPDATE {string} fired before the camera update
11
+ * @property AFTER_CAMERA_UPDATE {string} fired after the camera update
12
+ * @property BEFORE_LAYER_UPDATE {string} fired before the layer update
13
+ * @property AFTER_LAYER_UPDATE {string} fired after the layer update
14
+ * @property BEFORE_RENDER {string} fired before the render
15
+ * @property AFTER_RENDER {string} fired after the render
16
+ * @property UPDATE_END {string} fired at the end of the update
17
+ */
18
+
19
+ export const MAIN_LOOP_EVENTS = {
20
+ UPDATE_START: 'update_start',
21
+ BEFORE_CAMERA_UPDATE: 'before_camera_update',
22
+ AFTER_CAMERA_UPDATE: 'after_camera_update',
23
+ BEFORE_LAYER_UPDATE: 'before_layer_update',
24
+ AFTER_LAYER_UPDATE: 'after_layer_update',
25
+ BEFORE_RENDER: 'before_render',
26
+ AFTER_RENDER: 'after_render',
27
+ UPDATE_END: 'update_end'
28
+ };
29
+ function updateElements(context, geometryLayer, elements) {
30
+ if (!elements) {
31
+ return;
32
+ }
33
+ for (const element of elements) {
34
+ // update element
35
+ // TODO find a way to notify attachedLayers when geometryLayer deletes some elements
36
+ // and then update Debug.js:addGeometryLayerDebugFeatures
37
+ const newElementsToUpdate = geometryLayer.update(context, geometryLayer, element);
38
+ const sub = geometryLayer.getObjectToUpdateForAttachedLayers(element);
39
+ if (sub) {
40
+ if (sub.element) {
41
+ // update attached layers
42
+ for (const attachedLayer of geometryLayer.attachedLayers) {
43
+ if (attachedLayer.ready) {
44
+ attachedLayer.update(context, attachedLayer, sub.element, sub.parent);
45
+ }
46
+ }
47
+ } else if (sub.elements) {
48
+ for (let i = 0; i < sub.elements.length; i++) {
49
+ if (!sub.elements[i].isObject3D) {
50
+ throw new Error(`
51
+ Invalid object for attached layer to update.
52
+ Must be a THREE.Object and have a THREE.Material`);
53
+ }
54
+ // update attached layers
55
+ for (const attachedLayer of geometryLayer.attachedLayers) {
56
+ if (attachedLayer.ready) {
57
+ attachedLayer.update(context, attachedLayer, sub.elements[i], sub.parent);
58
+ }
59
+ }
60
+ }
61
+ }
62
+ }
63
+ updateElements(context, geometryLayer, newElementsToUpdate);
64
+ }
65
+ }
66
+ function filterChangeSources(updateSources, geometryLayer) {
67
+ let fullUpdate = false;
68
+ const filtered = new Set();
69
+ updateSources.forEach(src => {
70
+ if (src === geometryLayer || src.isCamera) {
71
+ geometryLayer.info.clear();
72
+ fullUpdate = true;
73
+ } else if (src.layer === geometryLayer) {
74
+ filtered.add(src);
75
+ }
76
+ });
77
+ return fullUpdate ? new Set([geometryLayer]) : filtered;
78
+ }
79
+ class MainLoop extends EventDispatcher {
80
+ #needsRedraw = false;
81
+ #updateLoopRestarted = true;
82
+ #lastTimestamp = 0;
83
+ constructor(scheduler, engine) {
84
+ super();
85
+ this.renderingState = RENDERING_PAUSED;
86
+ this.scheduler = scheduler;
87
+ this.gfxEngine = engine; // TODO: remove me
88
+ }
89
+ scheduleViewUpdate(view, forceRedraw) {
90
+ this.#needsRedraw |= forceRedraw;
91
+ if (this.renderingState !== RENDERING_SCHEDULED) {
92
+ this.renderingState = RENDERING_SCHEDULED;
93
+ // TODO Fix asynchronization between xr and MainLoop render loops.
94
+ // WebGLRenderer#setAnimationLoop must be used for WebXR projects.
95
+ // (see WebXR#initializeWebXR).
96
+ if (!this.gfxEngine.renderer.xr.isPresenting) {
97
+ requestAnimationFrame(timestamp => {
98
+ this.step(view, timestamp);
99
+ });
100
+ }
101
+ }
102
+ }
103
+ #update(view, updateSources, dt) {
104
+ const context = {
105
+ camera: view.camera,
106
+ engine: this.gfxEngine,
107
+ scheduler: this.scheduler,
108
+ view
109
+ };
110
+
111
+ // replace layer with their parent where needed
112
+ updateSources.forEach(src => {
113
+ const layer = src.layer || src;
114
+ if (layer.isLayer && layer.parent) {
115
+ updateSources.add(layer.parent);
116
+ }
117
+ });
118
+ for (const geometryLayer of view.getLayers((x, y) => !y)) {
119
+ context.geometryLayer = geometryLayer;
120
+ if (geometryLayer.ready && geometryLayer.visible && !geometryLayer.frozen) {
121
+ view.execFrameRequesters(MAIN_LOOP_EVENTS.BEFORE_LAYER_UPDATE, dt, this.#updateLoopRestarted, geometryLayer);
122
+
123
+ // Filter updateSources that are relevant for the geometryLayer
124
+ const srcs = filterChangeSources(updateSources, geometryLayer);
125
+ if (srcs.size > 0) {
126
+ // pre update attached layer
127
+ for (const attachedLayer of geometryLayer.attachedLayers) {
128
+ if (attachedLayer.ready && attachedLayer.preUpdate) {
129
+ attachedLayer.preUpdate(context, srcs);
130
+ }
131
+ }
132
+ // `preUpdate` returns an array of elements to update
133
+ const elementsToUpdate = geometryLayer.preUpdate(context, srcs);
134
+ // `update` is called in `updateElements`.
135
+ updateElements(context, geometryLayer, elementsToUpdate);
136
+ // `postUpdate` is called when this geom layer update process is finished
137
+ geometryLayer.postUpdate(context, geometryLayer, updateSources);
138
+ }
139
+
140
+ // Clear the cache of expired resources
141
+
142
+ view.execFrameRequesters(MAIN_LOOP_EVENTS.AFTER_LAYER_UPDATE, dt, this.#updateLoopRestarted, geometryLayer);
143
+ }
144
+ }
145
+ }
146
+ step(view, timestamp) {
147
+ const dt = timestamp - this.#lastTimestamp;
148
+ view._executeFrameRequestersRemovals();
149
+ view.execFrameRequesters(MAIN_LOOP_EVENTS.UPDATE_START, dt, this.#updateLoopRestarted);
150
+ const willRedraw = this.#needsRedraw;
151
+ this.#lastTimestamp = timestamp;
152
+
153
+ // Reset internal state before calling _update (so future calls to View.notifyChange()
154
+ // can properly change it)
155
+ this.#needsRedraw = false;
156
+ this.renderingState = RENDERING_PAUSED;
157
+ const updateSources = new Set(view._changeSources);
158
+ view._changeSources.clear();
159
+
160
+ // update camera
161
+ const dim = this.gfxEngine.getWindowSize();
162
+ view.execFrameRequesters(MAIN_LOOP_EVENTS.BEFORE_CAMERA_UPDATE, dt, this.#updateLoopRestarted);
163
+ view.camera.update(dim.x, dim.y);
164
+ view.execFrameRequesters(MAIN_LOOP_EVENTS.AFTER_CAMERA_UPDATE, dt, this.#updateLoopRestarted);
165
+
166
+ // Disable camera's matrix auto update to make sure the camera's
167
+ // world matrix is never updated mid-update.
168
+ // Otherwise inconsistencies can appear because object visibility
169
+ // testing and object drawing could be performed using different
170
+ // camera matrixWorld.
171
+ // Note: this is required at least because WEBGLRenderer calls
172
+ // camera.updateMatrixWorld()
173
+ const oldAutoUpdate = view.camera3D.matrixAutoUpdate;
174
+ view.camera3D.matrixAutoUpdate = false;
175
+
176
+ // update data-structure
177
+ this.#update(view, updateSources, dt);
178
+ if (this.scheduler.commandsWaitingExecutionCount() == 0) {
179
+ this.dispatchEvent({
180
+ type: 'command-queue-empty'
181
+ });
182
+ }
183
+
184
+ // Redraw *only* if needed.
185
+ // (redraws only happen when this.#needsRedraw is true, which in turn only happens when
186
+ // view.notifyChange() is called with redraw=true)
187
+ // As such there's no continuous update-loop, instead we use a ad-hoc update/render
188
+ // mechanism.
189
+ if (willRedraw) {
190
+ this.#renderView(view, dt);
191
+ }
192
+
193
+ // next time, we'll consider that we've just started the loop if we are still PAUSED now
194
+ this.#updateLoopRestarted = this.renderingState === RENDERING_PAUSED;
195
+ view.camera3D.matrixAutoUpdate = oldAutoUpdate;
196
+ view.execFrameRequesters(MAIN_LOOP_EVENTS.UPDATE_END, dt, this.#updateLoopRestarted);
197
+ }
198
+ #renderView(view, dt) {
199
+ view.execFrameRequesters(MAIN_LOOP_EVENTS.BEFORE_RENDER, dt, this.#updateLoopRestarted);
200
+ if (view.render) {
201
+ view.render();
202
+ } else {
203
+ // use default rendering method
204
+ this.gfxEngine.renderView(view);
205
+ }
206
+ view.execFrameRequesters(MAIN_LOOP_EVENTS.AFTER_RENDER, dt, this.#updateLoopRestarted);
207
+ }
208
+ }
209
+ export default MainLoop;
@@ -0,0 +1,255 @@
1
+ import * as THREE from 'three';
2
+ import RenderMode from "../Renderer/RenderMode.js";
3
+ import { unpack1K } from "../Renderer/LayeredMaterial.js";
4
+ import { Coordinates } from '@itowns/geographic';
5
+ const depthRGBA = new THREE.Vector4();
6
+ // TileMesh picking support function
7
+ function screenCoordsToNodeId(view, tileLayer, viewCoords) {
8
+ let radius = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
9
+ const dim = view.mainLoop.gfxEngine.getWindowSize();
10
+ viewCoords = viewCoords || new THREE.Vector2(Math.floor(dim.x / 2), Math.floor(dim.y / 2));
11
+ const restore = tileLayer.level0Nodes.map(n => RenderMode.push(n, RenderMode.MODES.ID));
12
+ const buffer = view.mainLoop.gfxEngine.renderViewToBuffer({
13
+ camera: view.camera,
14
+ scene: tileLayer.object3d
15
+ }, {
16
+ x: viewCoords.x - radius,
17
+ y: viewCoords.y - radius,
18
+ width: 1 + radius * 2,
19
+ height: 1 + radius * 2
20
+ });
21
+ restore.forEach(r => r());
22
+ const ids = [];
23
+ traversePickingCircle(radius, (x, y) => {
24
+ const idx = (y * 2 * radius + x) * 4;
25
+ const data = buffer.slice(idx, idx + 4 || undefined);
26
+ depthRGBA.fromArray(data).divideScalar(255.0);
27
+ const unpack = unpack1K(depthRGBA, 256 ** 3);
28
+ const _id = Math.round(unpack);
29
+ if (!ids.includes(_id)) {
30
+ ids.push(_id);
31
+ }
32
+ });
33
+ return ids;
34
+ }
35
+ function traversePickingCircle(radius, callback) {
36
+ // iterate on radius so we get closer to the mouse
37
+ // results first.
38
+ // Result traversal order for radius=2
39
+ // --3--
40
+ // -323-
41
+ // 32123
42
+ // -323
43
+ // --3--
44
+ let prevSq;
45
+ for (let r = 0; r <= radius; r++) {
46
+ const sq = r * r;
47
+ for (let x = -r; x <= r; x++) {
48
+ const sqx = x * x;
49
+ for (let y = -r; y <= r; y++) {
50
+ const dist = sqx + y * y;
51
+ // skip if too far
52
+ if (dist > sq) {
53
+ continue;
54
+ }
55
+ // skip if belongs to previous
56
+ if (dist <= prevSq) {
57
+ continue;
58
+ }
59
+ if (callback(x, y) === false) {
60
+ return;
61
+ }
62
+ }
63
+ }
64
+ prevSq = sq;
65
+ }
66
+ }
67
+ function findLayerInParent(obj) {
68
+ if (obj.layer) {
69
+ return obj.layer;
70
+ }
71
+ if (obj.parent) {
72
+ return findLayerInParent(obj.parent);
73
+ }
74
+ }
75
+ const raycaster = new THREE.Raycaster();
76
+ const normalized = new THREE.Vector2();
77
+ const pointPos = new THREE.Vector3();
78
+ const pointPosCoord = new Coordinates('EPSG:4978'); // default crs, will be set to view crs when used
79
+ const cameraPos = new THREE.Vector3();
80
+ const cameraPosCoord = new Coordinates('EPSG:4978'); // default crs, will be set to view crs when used
81
+
82
+ /**
83
+ * @module Picking
84
+ *
85
+ * Implement various picking methods for geometry layers.
86
+ * These methods are not meant to be used directly, see View.pickObjectsAt
87
+ * instead.
88
+ *
89
+ * All the methods here takes the same parameters:
90
+ * - the View instance
91
+ * - view coordinates (in pixels) where picking should be done
92
+ * - radius (in pixels) of the picking circle
93
+ * - layer: the geometry layer used for picking
94
+ */
95
+ export default {
96
+ pickTilesAt(view, viewCoords, radius, layer) {
97
+ let results = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
98
+ const _ids = screenCoordsToNodeId(view, layer, viewCoords, radius);
99
+ const extractResult = node => {
100
+ if (_ids.includes(node.id) && node.isTileMesh) {
101
+ results.push({
102
+ object: node,
103
+ layer
104
+ });
105
+ }
106
+ };
107
+ for (const n of layer.level0Nodes) {
108
+ n.traverse(extractResult);
109
+ }
110
+ return results;
111
+ },
112
+ pickPointsAt(view, viewCoords, radius, layer) {
113
+ let result = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
114
+ if (!layer.root) {
115
+ return;
116
+ }
117
+
118
+ // enable picking mode for points material
119
+ layer.object3d.traverse(o => {
120
+ if (o.isPoints && o.baseId) {
121
+ o.material.enablePicking(true);
122
+ }
123
+ });
124
+
125
+ // render 1 pixel
126
+ // TODO: support more than 1 pixel selection
127
+ const buffer = view.mainLoop.gfxEngine.renderViewToBuffer({
128
+ camera: view.camera,
129
+ scene: layer.object3d
130
+ }, {
131
+ x: viewCoords.x - radius,
132
+ y: viewCoords.y - radius,
133
+ width: 1 + radius * 2,
134
+ height: 1 + radius * 2
135
+ });
136
+ const candidates = [];
137
+ traversePickingCircle(radius, (x, y) => {
138
+ const idx = (y * 2 * radius + x) * 4;
139
+ const data = buffer.slice(idx, idx + 4);
140
+
141
+ // see PotreeProvider and the construction of unique_id
142
+ const objId = data[0] << 8 | data[1];
143
+ const index = data[2] << 8 | data[3];
144
+ const r = {
145
+ objId,
146
+ index
147
+ };
148
+ for (let i = 0; i < candidates.length; i++) {
149
+ if (candidates[i].objId == r.objId && candidates[i].index == r.index) {
150
+ return;
151
+ }
152
+ }
153
+ candidates.push(r);
154
+ });
155
+ layer.object3d.traverse(o => {
156
+ if (o.isPoints && o.baseId) {
157
+ // disable picking mode
158
+ o.material.enablePicking(false);
159
+
160
+ // if baseId matches objId, the clicked point belongs to `o`
161
+ for (let i = 0; i < candidates.length; i++) {
162
+ if (candidates[i].objId == o.baseId) {
163
+ // Get point position: get the picked point from the buffer geometry and apply local to world
164
+ // transform of the picked object
165
+ pointPos.fromBufferAttribute(o.geometry.attributes.position, candidates[i].index);
166
+ o.localToWorld(pointPos);
167
+ // Compute distance to the camera
168
+ pointPosCoord.setCrs(view.referenceCrs);
169
+ pointPosCoord.setFromVector3(pointPos);
170
+ view.camera3D.getWorldPosition(cameraPos);
171
+ cameraPosCoord.setCrs(view.referenceCrs);
172
+ cameraPosCoord.setFromVector3(cameraPos);
173
+ const dist = pointPosCoord.spatialEuclideanDistanceTo(cameraPosCoord);
174
+ result.push({
175
+ object: o,
176
+ point: pointPos.clone(),
177
+ // the position of the point in the 3D view. Same name and value than what's returned by pickObjectsAt
178
+ index: candidates[i].index,
179
+ distance: dist,
180
+ layer
181
+ });
182
+ }
183
+ }
184
+ }
185
+ });
186
+ return result;
187
+ },
188
+ /*
189
+ * Default picking method. Uses THREE.Raycaster
190
+ */
191
+ pickObjectsAt(view, viewCoords, radius, object) {
192
+ let target = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];
193
+ // Raycaster use NDC coordinate
194
+ view.viewToNormalizedCoords(viewCoords, normalized);
195
+ if (radius < 0) {
196
+ raycaster.setFromCamera(normalized, view.camera3D);
197
+ const intersects = raycaster.intersectObject(object, true);
198
+ for (const inter of intersects) {
199
+ inter.layer = findLayerInParent(inter.object);
200
+ target.push(inter);
201
+ }
202
+ return target;
203
+ }
204
+ // Instead of doing N raycast (1 per x,y returned by traversePickingCircle),
205
+ // we force render the zone of interest.
206
+ // Then we'll only do raycasting for the pixels where something was drawn.
207
+ const zone = {
208
+ x: viewCoords.x - radius,
209
+ y: viewCoords.y - radius,
210
+ width: 1 + radius * 2,
211
+ height: 1 + radius * 2
212
+ };
213
+ const pixels = view.mainLoop.gfxEngine.renderViewToBuffer({
214
+ scene: object,
215
+ camera: view.camera
216
+ }, zone);
217
+ const clearColor = new THREE.Color();
218
+ view.mainLoop.gfxEngine.renderer.getClearColor(clearColor);
219
+ const clearR = Math.round(255 * clearColor.r);
220
+ const clearG = Math.round(255 * clearColor.g);
221
+ const clearB = Math.round(255 * clearColor.b);
222
+
223
+ // Raycaster use NDC coordinate
224
+ const tmp = normalized.clone();
225
+ traversePickingCircle(radius, (x, y) => {
226
+ // x, y are offset from the center of the picking circle,
227
+ // and pixels is a square where 0, 0 is the top-left corner.
228
+ // So we need to shift x,y by radius.
229
+
230
+ const offset = ((y + radius) * (radius * 2 + 1) + (x + radius)) * 4;
231
+ const r = pixels[offset];
232
+ const g = pixels[offset + 1];
233
+ const b = pixels[offset + 2];
234
+ // Use approx. test to avoid rounding error or to behave
235
+ // differently depending on hardware rounding mode.
236
+ if (Math.abs(clearR - r) <= 1 && Math.abs(clearG - g) <= 1 && Math.abs(clearB - b) <= 1) {
237
+ // skip because nothing has been rendered here
238
+ return;
239
+ }
240
+
241
+ // Perform raycasting
242
+ tmp.setX(normalized.x + x / view.camera.width).setY(normalized.y + y / view.camera.height);
243
+ raycaster.setFromCamera(tmp, view.camera3D);
244
+ const intersects = raycaster.intersectObject(object, true);
245
+ for (const inter of intersects) {
246
+ inter.layer = findLayerInParent(inter.object);
247
+ target.push(inter);
248
+ }
249
+
250
+ // Stop at first hit
251
+ return target.length == 0;
252
+ });
253
+ return target;
254
+ }
255
+ };