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,612 @@
1
+ import * as THREE from 'three';
2
+ import Earcut from 'earcut';
3
+ import { FEATURE_TYPES } from "../Core/Feature.js";
4
+ import ReferLayerProperties from "../Layer/ReferencingLayerProperties.js";
5
+ import { deprecatedFeature2MeshOptions } from "../Core/Deprecated/Undeprecator.js";
6
+ import { Extent, Coordinates, OrientationUtils } from '@itowns/geographic';
7
+ import Style, { StyleContext } from "../Core/Style.js";
8
+ const coord = new Coordinates('EPSG:4326', 0, 0, 0);
9
+ const context = new StyleContext();
10
+ const defaultStyle = new Style();
11
+ let style;
12
+ const dim_ref = new THREE.Vector2();
13
+ const dim = new THREE.Vector2();
14
+ const normal = new THREE.Vector3();
15
+ const baseCoord = new THREE.Vector3();
16
+ const topCoord = new THREE.Vector3();
17
+ const inverseScale = new THREE.Vector3();
18
+ const extent = new Extent('EPSG:4326', 0, 0, 0, 0);
19
+ const _color = new THREE.Color();
20
+ const maxValueUint8 = 2 ** 8 - 1;
21
+ const maxValueUint16 = 2 ** 16 - 1;
22
+ const maxValueUint32 = 2 ** 32 - 1;
23
+ const crsWGS84 = 'EPSG:4326';
24
+ class FeatureMesh extends THREE.Group {
25
+ #currentCrs;
26
+ #originalCrs;
27
+ #collection = (() => new THREE.Group())();
28
+ #place = (() => new THREE.Group())();
29
+ constructor(meshes, collection) {
30
+ super();
31
+ this.meshes = new THREE.Group().add(...meshes);
32
+ this.#collection = new THREE.Group().add(this.meshes);
33
+ this.#collection.quaternion.copy(collection.quaternion);
34
+ this.#collection.position.copy(collection.position);
35
+ this.#collection.scale.copy(collection.scale);
36
+ this.#collection.updateMatrix();
37
+ this.#originalCrs = collection.crs;
38
+ this.#currentCrs = this.#originalCrs;
39
+ this.extent = collection.extent;
40
+ this.add(this.#place.add(this.#collection));
41
+ }
42
+ as(crs) {
43
+ if (this.#currentCrs !== crs) {
44
+ this.#currentCrs = crs;
45
+ if (crs == this.#originalCrs) {
46
+ // reset transformation
47
+ this.place.position.set(0, 0, 0);
48
+ this.position.set(0, 0, 0);
49
+ this.scale.set(1, 1, 1);
50
+ this.quaternion.identity();
51
+ } else {
52
+ // calculate the scale transformation to transform the feature.extent
53
+ // to feature.extent.as(crs)
54
+ coord.crs = this.#originalCrs;
55
+ // TODO: An extent here could be either a geographic extent (for
56
+ // features from WFS) or a tiled extent (for features from MVT).
57
+ // Unify both behavior.
58
+ if (this.extent.isExtent) {
59
+ extent.copy(this.extent).applyMatrix4(this.#collection.matrix);
60
+ extent.as(coord.crs, extent);
61
+ } else {
62
+ this.extent.toExtent(coord.crs, extent);
63
+ }
64
+ extent.spatialEuclideanDimensions(dim_ref);
65
+ extent.planarDimensions(dim);
66
+ if (dim.x && dim.y) {
67
+ this.scale.copy(dim_ref).divide(dim).setZ(1);
68
+ }
69
+
70
+ // Position and orientation
71
+ // remove original position
72
+ this.#place.position.copy(this.#collection.position).negate();
73
+
74
+ // get mesh coordinate
75
+ coord.setFromVector3(this.#collection.position);
76
+
77
+ // get method to calculate orientation
78
+ const crsInput = this.#originalCrs == 'EPSG:3857' ? crsWGS84 : this.#originalCrs;
79
+ const crs2crs = OrientationUtils.quaternionFromCRSToCRS(crsInput, crs);
80
+ // calculate orientation to crs
81
+ crs2crs(coord.as(crsWGS84), this.quaternion);
82
+
83
+ // transform position to crs
84
+ coord.as(crs, coord).toVector3(this.position);
85
+ }
86
+ }
87
+ return this;
88
+ }
89
+ }
90
+ function toColor(color) {
91
+ if (color) {
92
+ if (color.type == 'Color') {
93
+ return color;
94
+ } else {
95
+ return _color.set(color);
96
+ }
97
+ } else {
98
+ return _color.set(Math.random() * 0xffffff);
99
+ }
100
+ }
101
+ function getIntArrayFromSize(data, size) {
102
+ if (size <= maxValueUint8) {
103
+ return new Uint8Array(data);
104
+ } else if (size <= maxValueUint16) {
105
+ return new Uint16Array(data);
106
+ } else {
107
+ return new Uint32Array(data);
108
+ }
109
+ }
110
+ function separateMeshes(object3D) {
111
+ const meshes = [];
112
+ object3D.updateMatrixWorld();
113
+ object3D.traverse(element => {
114
+ if (element instanceof THREE.Mesh) {
115
+ element.updateMatrixWorld();
116
+ element.geometry.applyMatrix4(element.matrixWorld);
117
+ meshes.push(element);
118
+ }
119
+ });
120
+ return meshes;
121
+ }
122
+
123
+ /**
124
+ * Add indices for the side faces.
125
+ * We loop over the contour and create a side face made of two triangles.
126
+ *
127
+ * For a ring made of (n) coordinates, there are (n*2) vertices.
128
+ * The (n) first vertices are on the roof, the (n) other vertices are on the floor.
129
+ *
130
+ * If index (i) is on the roof, index (i+length) is on the floor.
131
+ *
132
+ * @param {number[]} indices - Array of indices to push to
133
+ * @param {number} length - Total vertices count in the geom (excluding the extrusion ones)
134
+ * @param {number} offset
135
+ * @param {number} count
136
+ * @param {boolean} isClockWise - Wrapping direction
137
+ */
138
+ function addExtrudedPolygonSideFaces(indices, length, offset, count, isClockWise) {
139
+ // loop over contour length, and for each point of the contour,
140
+ // add indices to make two triangle, that make the side face
141
+ const startIndice = indices.length;
142
+ indices.length += (count - 1) * 6;
143
+ for (let i = offset, j = startIndice; i < offset + count - 1; ++i, ++j) {
144
+ if (isClockWise) {
145
+ // first triangle indices
146
+ indices[j] = i;
147
+ indices[++j] = i + length;
148
+ indices[++j] = i + 1;
149
+ // second triangle indices
150
+ indices[++j] = i + 1;
151
+ indices[++j] = i + length;
152
+ indices[++j] = i + length + 1;
153
+ } else {
154
+ // first triangle indices
155
+ indices[j] = i + length;
156
+ indices[++j] = i;
157
+ indices[++j] = i + length + 1;
158
+ // second triangle indices
159
+ indices[++j] = i + length + 1;
160
+ indices[++j] = i;
161
+ indices[++j] = i + 1;
162
+ }
163
+ }
164
+ }
165
+ function featureToPoint(feature, options) {
166
+ const ptsIn = feature.vertices;
167
+ const colors = new Uint8Array(ptsIn.length);
168
+ const batchIds = new Uint32Array(ptsIn.length);
169
+ const batchId = options.batchId || ((p, id) => id);
170
+ let featureId = 0;
171
+ const vertices = new Float32Array(ptsIn);
172
+ inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse);
173
+ normal.set(0, 0, 1).multiply(inverseScale);
174
+ const pointMaterialSize = [];
175
+ context.setFeature(feature);
176
+ for (const geometry of feature.geometries) {
177
+ const start = geometry.indices[0].offset;
178
+ const count = geometry.indices[0].count;
179
+ const id = batchId(geometry.properties, featureId);
180
+ context.setGeometry(geometry);
181
+ for (let v = start * 3, j = start; j < start + count; v += 3, j += 1) {
182
+ if (feature.normals) {
183
+ normal.fromArray(feature.normals, v).multiply(inverseScale);
184
+ }
185
+ const localCoord = context.setLocalCoordinatesFromArray(feature.vertices, v);
186
+ style.setContext(context);
187
+ const {
188
+ base_altitude,
189
+ color,
190
+ radius
191
+ } = style.point;
192
+ coord.copy(localCoord).applyMatrix4(context.collection.matrixWorld);
193
+ if (coord.crs == 'EPSG:4978') {
194
+ // altitude convertion from geocentered to elevation (from ground)
195
+ coord.as('EPSG:4326', coord);
196
+ }
197
+
198
+ // Calculate the new coordinates using the elevation shift (baseCoord)
199
+ baseCoord.copy(normal).multiplyScalar(base_altitude - coord.z).add(localCoord)
200
+ // and update the geometry buffer (vertices).
201
+ .toArray(vertices, v);
202
+ toColor(color).multiplyScalar(255).toArray(colors, v);
203
+ if (!pointMaterialSize.includes(radius)) {
204
+ pointMaterialSize.push(radius);
205
+ }
206
+ batchIds[j] = id;
207
+ }
208
+ featureId++;
209
+ }
210
+ const geom = new THREE.BufferGeometry();
211
+ geom.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
212
+ geom.setAttribute('color', new THREE.BufferAttribute(colors, 3, true));
213
+ geom.setAttribute('batchId', new THREE.BufferAttribute(batchIds, 1));
214
+ options.pointMaterial.size = pointMaterialSize[0];
215
+ if (pointMaterialSize.length > 1) {
216
+ // TODO CREATE material for each feature
217
+ console.warn('Too many differents point.radius, only the first one will be used');
218
+ }
219
+ return new THREE.Points(geom, options.pointMaterial);
220
+ }
221
+ function featureToLine(feature, options) {
222
+ const ptsIn = feature.vertices;
223
+ const colors = new Uint8Array(ptsIn.length);
224
+ const count = ptsIn.length / 3;
225
+ const batchIds = new Uint32Array(count);
226
+ const batchId = options.batchId || ((p, id) => id);
227
+ let featureId = 0;
228
+ const vertices = new Float32Array(ptsIn.length);
229
+ const geom = new THREE.BufferGeometry();
230
+ const lineMaterialWidth = [];
231
+ context.setFeature(feature);
232
+ const countIndices = (count - feature.geometries.length) * 2;
233
+ const indices = getIntArrayFromSize(countIndices, count);
234
+ let i = 0;
235
+ inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse);
236
+ normal.set(0, 0, 1).multiply(inverseScale);
237
+ // Multi line case
238
+ for (const geometry of feature.geometries) {
239
+ context.setGeometry(geometry);
240
+ const id = batchId(geometry.properties, featureId);
241
+ const start = geometry.indices[0].offset;
242
+ // To avoid integer overflow with indice value (16 bits)
243
+ if (start > 0xffff) {
244
+ console.warn('Feature to Line: integer overflow, too many points in lines');
245
+ break;
246
+ }
247
+ const count = geometry.indices[0].count;
248
+ const end = start + count;
249
+ for (let v = start * 3, j = start; j < end; v += 3, j += 1) {
250
+ if (j < end - 1) {
251
+ if (j < 0xffff) {
252
+ indices[i++] = j;
253
+ indices[i++] = j + 1;
254
+ } else {
255
+ break;
256
+ }
257
+ }
258
+ if (feature.normals) {
259
+ normal.fromArray(feature.normals, v).multiply(inverseScale);
260
+ }
261
+ const localCoord = context.setLocalCoordinatesFromArray(feature.vertices, v);
262
+ style.setContext(context);
263
+ const {
264
+ base_altitude,
265
+ color,
266
+ width
267
+ } = style.stroke;
268
+ coord.copy(localCoord).applyMatrix4(context.collection.matrixWorld);
269
+ if (coord.crs == 'EPSG:4978') {
270
+ // altitude convertion from geocentered to elevation (from ground)
271
+ coord.as('EPSG:4326', coord);
272
+ }
273
+
274
+ // Calculate the new coordinates using the elevation shift (baseCoord)
275
+ baseCoord.copy(normal).multiplyScalar(base_altitude - coord.z).add(localCoord)
276
+ // and update the geometry buffer (vertices).
277
+ .toArray(vertices, v);
278
+ toColor(color).multiplyScalar(255).toArray(colors, v);
279
+ if (!lineMaterialWidth.includes(width)) {
280
+ lineMaterialWidth.push(width);
281
+ }
282
+ batchIds[j] = id;
283
+ }
284
+ featureId++;
285
+ }
286
+ options.lineMaterial.linewidth = lineMaterialWidth[0];
287
+ if (lineMaterialWidth.length > 1) {
288
+ // TODO CREATE material for each feature
289
+ console.warn('Too many differents stroke.width, only the first one will be used');
290
+ }
291
+ geom.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
292
+ geom.setAttribute('color', new THREE.BufferAttribute(colors, 3, true));
293
+ geom.setAttribute('batchId', new THREE.BufferAttribute(batchIds, 1));
294
+ geom.setIndex(new THREE.BufferAttribute(indices, 1));
295
+ return new THREE.LineSegments(geom, options.lineMaterial);
296
+ }
297
+ function featureToPolygon(feature, options) {
298
+ const vertices = new Float32Array(feature.vertices);
299
+ const colors = new Uint8Array(feature.vertices.length);
300
+ const indices = [];
301
+ const batchIds = new Uint32Array(vertices.length / 3);
302
+ const batchId = options.batchId || ((p, id) => id);
303
+ context.setFeature(feature);
304
+ inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse);
305
+ normal.set(0, 0, 1).multiply(inverseScale);
306
+ let featureId = 0;
307
+ for (const geometry of feature.geometries) {
308
+ const start = geometry.indices[0].offset;
309
+ // To avoid integer overflow with index value (32 bits)
310
+ if (start > maxValueUint32) {
311
+ console.warn('Feature to Polygon: integer overflow, too many points in polygons');
312
+ break;
313
+ }
314
+ context.setGeometry(geometry);
315
+ const lastIndice = geometry.indices.slice(-1)[0];
316
+ const end = lastIndice.offset + lastIndice.count;
317
+ const startIn = start * 3;
318
+ const id = batchId(geometry.properties, featureId);
319
+ for (let i = startIn, b = start; i < startIn + (end - start) * 3; i += 3, b += 1) {
320
+ if (feature.normals) {
321
+ normal.fromArray(feature.normals, i).multiply(inverseScale);
322
+ }
323
+ const localCoord = context.setLocalCoordinatesFromArray(feature.vertices, i);
324
+ style.setContext(context);
325
+ const {
326
+ base_altitude,
327
+ color
328
+ } = style.fill;
329
+ coord.copy(localCoord).applyMatrix4(context.collection.matrixWorld);
330
+ if (coord.crs == 'EPSG:4978') {
331
+ // altitude convertion from geocentered to elevation (from ground)
332
+ coord.as('EPSG:4326', coord);
333
+ }
334
+
335
+ // Calculate the new coordinates using the elevation shift (baseCoord)
336
+ baseCoord.copy(normal).multiplyScalar(base_altitude - coord.z).add(localCoord)
337
+ // and update the geometry buffer (vertices).
338
+ .toArray(vertices, i);
339
+ toColor(color).multiplyScalar(255).toArray(colors, i);
340
+ batchIds[b] = id;
341
+ }
342
+ featureId++;
343
+ const geomVertices = vertices.slice(start * 3, end * 3);
344
+ const holesOffsets = geometry.indices.map(i => i.offset - start).slice(1);
345
+ const triangles = Earcut(geomVertices, holesOffsets, 3);
346
+ const startIndice = indices.length;
347
+ indices.length += triangles.length;
348
+ for (let i = 0; i < triangles.length; i++) {
349
+ indices[startIndice + i] = triangles[i] + start;
350
+ }
351
+ }
352
+ const geom = new THREE.BufferGeometry();
353
+ geom.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
354
+ geom.setAttribute('color', new THREE.BufferAttribute(colors, 3, true));
355
+ geom.setAttribute('batchId', new THREE.BufferAttribute(batchIds, 1));
356
+ geom.setIndex(new THREE.BufferAttribute(getIntArrayFromSize(indices, vertices.length / 3), 1));
357
+ return new THREE.Mesh(geom, options.polygonMaterial);
358
+ }
359
+ function area(contour, offset, count) {
360
+ offset *= 3;
361
+ const n = offset + count * 3;
362
+ let a = 0.0;
363
+ for (let p = n - 3, q = offset; q < n; p = q, q += 3) {
364
+ a += contour[p] * contour[q + 1] - contour[q] * contour[p + 1];
365
+ }
366
+ return a * 0.5;
367
+ }
368
+ function featureToExtrudedPolygon(feature, options) {
369
+ const ptsIn = feature.vertices;
370
+ const vertices = new Float32Array(ptsIn.length * 2);
371
+ const totalVertices = ptsIn.length / 3;
372
+ const colors = new Uint8Array(ptsIn.length * 2);
373
+ const indices = [];
374
+ const batchIds = new Uint32Array(vertices.length / 3);
375
+ const batchId = options.batchId || ((p, id) => id);
376
+ let featureId = 0;
377
+ context.setFeature(feature);
378
+ inverseScale.setFromMatrixScale(context.collection.matrixWorldInverse);
379
+ normal.set(0, 0, 1).multiply(inverseScale);
380
+ coord.setCrs(context.collection.crs);
381
+ for (const geometry of feature.geometries) {
382
+ context.setGeometry(geometry);
383
+ const start = geometry.indices[0].offset;
384
+ const lastIndice = geometry.indices.slice(-1)[0];
385
+ const end = lastIndice.offset + lastIndice.count;
386
+ const count = end - start;
387
+ const isClockWise = geometry.indices[0].ccw ?? area(ptsIn, start, count) < 0;
388
+ const startIn = start * 3;
389
+ const startTop = start + totalVertices;
390
+ const id = batchId(geometry.properties, featureId);
391
+ for (let i = startIn, t = startIn + ptsIn.length, b = start; i < startIn + count * 3; i += 3, t += 3, b += 1) {
392
+ if (feature.normals) {
393
+ normal.fromArray(feature.normals, i).multiply(inverseScale);
394
+ }
395
+ const localCoord = context.setLocalCoordinatesFromArray(ptsIn, i);
396
+ style.setContext(context);
397
+ const {
398
+ base_altitude,
399
+ extrusion_height,
400
+ color
401
+ } = style.fill;
402
+ coord.copy(localCoord).applyMatrix4(context.collection.matrixWorld);
403
+ if (coord.crs == 'EPSG:4978') {
404
+ // altitude convertion from geocentered to elevation (from ground)
405
+ coord.as('EPSG:4326', coord);
406
+ }
407
+
408
+ // Calculate the new base coordinates using the elevation shift (baseCoord)
409
+ baseCoord.copy(normal).multiplyScalar(base_altitude - coord.z).add(localCoord)
410
+ // and update the geometry buffer (vertices).
411
+ .toArray(vertices, i);
412
+ batchIds[b] = id;
413
+
414
+ // populate top geometry buffers
415
+ topCoord.copy(normal).multiplyScalar(extrusion_height).add(baseCoord).toArray(vertices, t);
416
+ batchIds[b + totalVertices] = id;
417
+
418
+ // coloring base and top mesh
419
+ const meshColor = toColor(color).multiplyScalar(255);
420
+ meshColor.toArray(colors, t); // top
421
+ meshColor.multiplyScalar(0.5).toArray(colors, i); // base is half dark
422
+ }
423
+ featureId++;
424
+ const geomVertices = vertices.slice(startTop * 3, (end + totalVertices) * 3);
425
+ const holesOffsets = geometry.indices.map(i => i.offset - start).slice(1);
426
+ const triangles = Earcut(geomVertices, holesOffsets, 3);
427
+ const startIndice = indices.length;
428
+ indices.length += triangles.length;
429
+ for (let i = 0; i < triangles.length; i++) {
430
+ indices[startIndice + i] = triangles[i] + startTop;
431
+ }
432
+
433
+ // add extruded contour
434
+ addExtrudedPolygonSideFaces(indices, totalVertices, geometry.indices[0].offset, geometry.indices[0].count, isClockWise);
435
+
436
+ // add extruded holes
437
+ for (let i = 1; i < geometry.indices.length; i++) {
438
+ const indice = geometry.indices[i];
439
+ addExtrudedPolygonSideFaces(indices, totalVertices, indice.offset, indice.count, !(indice.ccw ?? isClockWise));
440
+ }
441
+ }
442
+ const geom = new THREE.BufferGeometry();
443
+ geom.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
444
+ geom.setAttribute('color', new THREE.BufferAttribute(colors, 3, true));
445
+ geom.setAttribute('batchId', new THREE.BufferAttribute(batchIds, 1));
446
+ geom.setIndex(new THREE.BufferAttribute(getIntArrayFromSize(indices, vertices.length / 3), 1));
447
+ return new THREE.Mesh(geom, options.polygonMaterial);
448
+ }
449
+
450
+ /**
451
+ * Created Instanced object from mesh
452
+ *
453
+ * @param {THREE.MESH} mesh Model 3D to instanciate
454
+ * @param {*} count number of instances to create (int)
455
+ * @param {*} ptsIn positions of instanced (array double)
456
+ * @returns {THREE.InstancedMesh} Instanced mesh
457
+ */
458
+ function createInstancedMesh(mesh, count, ptsIn) {
459
+ const instancedMesh = new THREE.InstancedMesh(mesh.geometry, mesh.material, count);
460
+ let index = 0;
461
+ for (let i = 0; i < count * 3; i += 3) {
462
+ const mat = new THREE.Matrix4();
463
+ mat.setPosition(ptsIn[i], ptsIn[i + 1], ptsIn[i + 2]);
464
+ instancedMesh.setMatrixAt(index, mat);
465
+ index++;
466
+ }
467
+ instancedMesh.instanceMatrix.needsUpdate = true;
468
+ return instancedMesh;
469
+ }
470
+
471
+ /**
472
+ * Convert a {@link Feature} of type POINT to a Instanced meshes
473
+ *
474
+ * @param {Object} feature
475
+ * @returns {THREE.Mesh} mesh or GROUP of THREE.InstancedMesh
476
+ */
477
+ function pointsToInstancedMeshes(feature) {
478
+ const ptsIn = feature.vertices;
479
+ const count = feature.geometries.length;
480
+ const modelObject = style.point.model.object;
481
+ if (modelObject instanceof THREE.Mesh) {
482
+ return createInstancedMesh(modelObject, count, ptsIn);
483
+ } else if (modelObject instanceof THREE.Object3D) {
484
+ const group = new THREE.Group();
485
+ // Get independent meshes from more complexe object
486
+ const meshes = separateMeshes(modelObject);
487
+ meshes.forEach(mesh => group.add(createInstancedMesh(mesh, count, ptsIn)));
488
+ return group;
489
+ } else {
490
+ throw new Error('The format of the model object provided in the style (layer.style.point.model.object) is not supported. Only THREE.Mesh or THREE.Object3D are supported.');
491
+ }
492
+ }
493
+
494
+ /**
495
+ * Convert a {@link Feature} to a Mesh
496
+ * @param {Feature} feature - the feature to convert
497
+ * @param {Object} options - options controlling the conversion
498
+ *
499
+ * @return {THREE.Mesh} mesh or GROUP of THREE.InstancedMesh
500
+ */
501
+ function featureToMesh(feature, options) {
502
+ if (!feature.vertices) {
503
+ return;
504
+ }
505
+ let mesh;
506
+ switch (feature.type) {
507
+ case FEATURE_TYPES.POINT:
508
+ if (style.point?.model?.object) {
509
+ try {
510
+ mesh = pointsToInstancedMeshes(feature);
511
+ mesh.isInstancedMesh = true;
512
+ } catch (e) {
513
+ mesh = featureToPoint(feature, options);
514
+ }
515
+ } else {
516
+ mesh = featureToPoint(feature, options);
517
+ }
518
+ break;
519
+ case FEATURE_TYPES.LINE:
520
+ mesh = featureToLine(feature, options);
521
+ break;
522
+ case FEATURE_TYPES.POLYGON:
523
+ if (style.fill && Object.keys(style.fill).includes('extrusion_height')) {
524
+ mesh = featureToExtrudedPolygon(feature, options);
525
+ } else {
526
+ mesh = featureToPolygon(feature, options);
527
+ }
528
+ break;
529
+ default:
530
+ }
531
+ if (!mesh.isInstancedMesh) {
532
+ mesh.material.vertexColors = true;
533
+ mesh.material.color = new THREE.Color(0xffffff);
534
+ }
535
+ mesh.feature = feature;
536
+ return mesh;
537
+ }
538
+
539
+ /**
540
+ * @module Feature2Mesh
541
+ */
542
+ export default {
543
+ /**
544
+ * Return a function that converts [Features]{@link module:GeoJsonParser} to Meshes. Feature collection will be converted to a
545
+ * a THREE.Group.
546
+ *
547
+ * @param {Object} options - options controlling the conversion
548
+ * @param {function} [options.batchId] - optional function to create batchId attribute.
549
+ * It is passed the feature property and the feature index. As the batchId is using an unsigned int structure on 32 bits,
550
+ * the batchId could be between 0 and 4,294,967,295.
551
+ * @param {StyleOptions} [options.style] - optional style properties. Only needed if the convert is used without instancing
552
+ * a layer beforehand.
553
+ * @return {function}
554
+ * @example <caption>Example usage of batchId with featureId.</caption>
555
+ * view.addLayer({
556
+ * id: 'WFS Buildings',
557
+ * type: 'geometry',
558
+ * update: itowns.FeatureProcessing.update,
559
+ * convert: itowns.Feature2Mesh.convert({
560
+ * batchId: (property, featureId) => featureId,
561
+ * }),
562
+ * filter: acceptFeature,
563
+ * source,
564
+ * });
565
+ *
566
+ * @example <caption>Example usage of batchId with property.</caption>
567
+ * view.addLayer({
568
+ * id: 'WFS Buildings',
569
+ * type: 'geometry',
570
+ * update: itowns.FeatureProcessing.update,
571
+ * convert: itowns.Feature2Mesh.convert({
572
+ * batchId: (property, featureId) => property.house ? 10 : featureId,
573
+ * }),
574
+ * filter: acceptFeature,
575
+ * source,
576
+ * });
577
+ */
578
+ convert() {
579
+ let options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
580
+ deprecatedFeature2MeshOptions(options);
581
+ return function (collection) {
582
+ if (!collection) {
583
+ return;
584
+ }
585
+ if (!options.pointMaterial) {
586
+ // Opacity and wireframe refered with layer properties
587
+ // TODO: next step is move these properties to Style
588
+ options.pointMaterial = ReferLayerProperties(new THREE.PointsMaterial(), this);
589
+ options.lineMaterial = ReferLayerProperties(new THREE.LineBasicMaterial(), this);
590
+ options.polygonMaterial = ReferLayerProperties(new THREE.MeshBasicMaterial(), this);
591
+ }
592
+
593
+ // In the case we didn't instanciate the layer (this) before the convert, we can pass
594
+ // style properties (@link StyleOptions) using options.style.
595
+ // This is usually done in some tests and if you want to use Feature2Mesh.convert()
596
+ // as in examples/source_file_gpx_3d.html.
597
+ style = this?.style || (options.style ? new Style(options.style) : defaultStyle);
598
+ context.setCollection(collection);
599
+ const features = collection.features;
600
+ if (!features || features.length == 0) {
601
+ return;
602
+ }
603
+ const meshes = features.map(feature => {
604
+ const mesh = featureToMesh(feature, options);
605
+ mesh.layer = this;
606
+ return mesh;
607
+ });
608
+ const featureNode = new FeatureMesh(meshes, collection);
609
+ return featureNode;
610
+ };
611
+ }
612
+ };