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,227 @@
1
+ import * as THREE from 'three';
2
+ import GeometryLayer from "./GeometryLayer.js";
3
+ import OrientedImageMaterial from "../Renderer/OrientedImageMaterial.js";
4
+ import GeoJsonParser from "../Parser/GeoJsonParser.js";
5
+ import CameraCalibrationParser from "../Parser/CameraCalibrationParser.js";
6
+ import { Coordinates, OrientationUtils } from '@itowns/geographic';
7
+ const coord = new Coordinates('EPSG:4978', 0, 0, 0);
8
+ const commandCancellation = cmd => cmd.requester.id !== cmd.layer.currentPano.id;
9
+ function updatePano(context, camera, layer) {
10
+ const newPano = layer.mostNearPano(camera.position);
11
+ // detection of oriented image change
12
+ const currentId = layer.currentPano ? layer.currentPano.id : undefined;
13
+ if (newPano && currentId != newPano.id) {
14
+ layer.currentPano = newPano;
15
+
16
+ // callback to indicate current pano has changed
17
+ layer.onPanoChanged({
18
+ previousPanoPosition: layer.getPreviousPano() ? layer.getPreviousPano().position : undefined,
19
+ currentPanoPosition: layer.getCurrentPano().position,
20
+ nextPanoPosition: layer.getNextPano().position
21
+ });
22
+ // prepare informations about the needed textures
23
+ const panoCameras = newPano.geometries[0].properties.idSensors;
24
+ const imagesInfo = layer.cameras.map(cam => ({
25
+ cameraId: cam.name,
26
+ panoId: newPano.id,
27
+ as: () => {}
28
+ })).filter(info => !panoCameras || panoCameras.includes(info.cameraId));
29
+ const command = {
30
+ layer,
31
+ // put informations about image URL as extent to be used by generic DataSourceProvider, OrientedImageSource will use that.
32
+ extentsSource: imagesInfo,
33
+ view: context.view,
34
+ requester: newPano,
35
+ earlyDropFunction: commandCancellation
36
+ };
37
+
38
+ // async call to scheduler to get textures
39
+ context.scheduler.execute(command).then(textures => {
40
+ if (newPano.id === layer.currentPano.id) {
41
+ layer.material.setTextures(textures, newPano, layer.getCamerasNameFromFeature(newPano));
42
+ layer.material.updateUniforms(context.camera.camera3D);
43
+ context.view.notifyChange(layer, true);
44
+ }
45
+ }, () => {});
46
+ }
47
+ }
48
+ function updateBackground(layer) {
49
+ if (layer.background && layer.currentPano) {
50
+ layer.background.position.copy(layer.currentPano.position);
51
+ layer.background.updateMatrixWorld();
52
+ layer.background.material = layer.material || layer.background.material;
53
+ }
54
+ }
55
+ function createBackground(radius) {
56
+ if (!radius || radius <= 0) {
57
+ return undefined;
58
+ }
59
+ const geometry = new THREE.SphereGeometry(radius, 32, 32);
60
+ const material = new THREE.MeshPhongMaterial({
61
+ color: 0x7777ff,
62
+ side: THREE.DoubleSide,
63
+ transparent: true,
64
+ opacity: 0.5,
65
+ wireframe: true
66
+ });
67
+ const sphere = new THREE.Mesh(geometry, material);
68
+ sphere.visible = true;
69
+ sphere.name = 'OrientedImageBackground';
70
+ return sphere;
71
+ }
72
+
73
+ /**
74
+ * OrientedImageLayer loads oriented images, and project these textures on the scene.
75
+ * It is design to create an immersive view. </br>
76
+ * It loads a set of panoramic position and orientation,
77
+ * a set of camera calibration file (it's the same set of camera for each panoramic),
78
+ * and a set of texture (each image for each camera for each panoramic), all organised in an {@link OrientedImageSource}. </br>
79
+ * It creates an {@link OrientedImageMaterial} used to do projective texture mapping on the scene.
80
+ * @extends GeometryLayer
81
+ */
82
+ class OrientedImageLayer extends GeometryLayer {
83
+ /**
84
+ * @param { string } id - The id of the layer, a unique name.
85
+ * @param { Object } config - configuration of the layer
86
+ * @param { number } config.backgroundDistance - Radius in meter of the sphere used as a background
87
+ * @param { function } config.onPanoChanged - callback fired when current panoramic changes
88
+ * @param { string } config.crs - crs projection of the view
89
+ * @param { string } config.orientation - Json object, using GeoJSon format to represent points,
90
+ * it's a set of panoramic position and orientation.
91
+ * @param { string } config.calibrations - Json object, representing a set of camera.
92
+ * see {@link CameraCalibrationParser}
93
+ * @param { OrientedImageSource } config.source - Source used to build url of texture for each oriented image,
94
+ * a tecture is need for each camera, for each panoramic.
95
+ */
96
+ constructor(id) {
97
+ let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
98
+ const {
99
+ backgroundDistance,
100
+ background = createBackground(backgroundDistance),
101
+ onPanoChanged = () => {},
102
+ getCamerasNameFromFeature = () => {},
103
+ ...geometryOptions
104
+ } = config;
105
+ if (config.projection) {
106
+ console.warn('OrientedImageLayer projection parameter is deprecated, use crs instead.');
107
+ config.crs = config.crs || config.projection;
108
+ }
109
+ super(id, new THREE.Group(), geometryOptions);
110
+ this.isOrientedImageLayer = true;
111
+ this.background = background;
112
+ if (this.background) {
113
+ // Add layer id to easily identify the objects later on (e.g. to delete the geometries when deleting the layer)
114
+ this.background.layer = this.background.layer ?? {};
115
+ this.background.layer.id = this.background.layer.id ?? id;
116
+ this.object3d.add(this.background);
117
+ }
118
+
119
+ // currentPano is the current point, means it's the closest from the camera
120
+ this.currentPano = undefined;
121
+
122
+ // store a callback to fire event when current panoramic change
123
+ this.onPanoChanged = onPanoChanged;
124
+
125
+ // function to get cameras name from panoramic feature
126
+ this.getCamerasNameFromFeature = getCamerasNameFromFeature;
127
+ const resolve = this.addInitializationStep();
128
+ this.mergeFeatures = false;
129
+ this.filteringExtent = false;
130
+ this.accurate = true;
131
+ const options = {
132
+ out: this
133
+ };
134
+
135
+ // panos is an array of feature point, representing many panoramics.
136
+ // for each point, there is a position and a quaternion attribute.
137
+ this.source.whenReady.then(metadata => GeoJsonParser.parse(config.orientation || metadata.orientation, options).then(orientation => {
138
+ this.panos = orientation.features;
139
+
140
+ // the crs input is parsed in geojson parser
141
+ // and returned in options.in
142
+ const crsIn = options.in.crs;
143
+ const crsOut = config.crs;
144
+ const crs2crs = OrientationUtils.quaternionFromCRSToCRS(crsIn, crsOut);
145
+ const quat = new THREE.Quaternion();
146
+
147
+ // add position and quaternion attributes from point feature
148
+ let i = 0;
149
+ for (const pano of this.panos) {
150
+ // set position
151
+ coord.crs = pano.crs;
152
+ coord.setFromArray(pano.vertices).applyMatrix4(orientation.matrix);
153
+ pano.position = coord.toVector3();
154
+
155
+ // set quaternion
156
+ crs2crs(coord, quat);
157
+ pano.quaternion = OrientationUtils.quaternionFromAttitude(pano.geometries[0].properties).premultiply(quat);
158
+ pano.id = pano.geometries[0].properties.id;
159
+ pano.index = i++;
160
+ }
161
+ }).then(() => {
162
+ // array of cameras, represent the projective texture configuration for each panoramic.
163
+ CameraCalibrationParser.parse(config.calibration || metadata.calibration, config).then(cameras => {
164
+ this.cameras = cameras;
165
+ // create the material
166
+ this.material = new OrientedImageMaterial(this.cameras, config);
167
+ resolve();
168
+ });
169
+ }));
170
+ }
171
+
172
+ // eslint-disable-next-line
173
+ update() {}
174
+ set boostLight(value) {
175
+ this.material.uniforms.boostLight.value = value;
176
+ }
177
+ get boostLight() {
178
+ return this.material.uniforms.boostLight.value;
179
+ }
180
+ preUpdate(context) {
181
+ updatePano(context, context.camera.camera3D, this);
182
+ this.material.updateUniforms(context.camera.camera3D);
183
+ updateBackground(this);
184
+ }
185
+ getNextPano() {
186
+ const index = (this.currentPano.index + 1) % this.panos.length;
187
+ return this.panos[index];
188
+ }
189
+ getCurrentPano() {
190
+ return this.currentPano;
191
+ }
192
+ getPreviousPano() {
193
+ const index = (this.currentPano.index - 1) % this.panos.length;
194
+ return this.panos[index];
195
+ }
196
+
197
+ /**
198
+ * Delete background, but doesn't delete OrientedImageLayer.material. For the moment, this material visibility is set to false.
199
+ * You need to replace OrientedImageLayer.material applied on each object, if you want to continue displaying them.
200
+ * This issue (see #1018 {@link https://github.com/iTowns/itowns/issues/1018}) will be fixed when OrientedImageLayer will be a ColorLayer.
201
+ * @param {boolean} [clearCache=false] Whether to clear the layer cache or not
202
+ */
203
+ delete(clearCache) {
204
+ if (this.background) {
205
+ // only delete geometries if it has some
206
+ super.delete();
207
+ }
208
+ if (clearCache) {
209
+ this.cache.clear();
210
+ }
211
+ this.material.visible = false;
212
+ console.warn('You need to replace OrientedImageLayer.material applied on each object. This issue will be fixed when OrientedImageLayer will be a ColorLayer. the material visibility is set to false. To follow issue see https://github.com/iTowns/itowns/issues/1018');
213
+ }
214
+ mostNearPano(position) {
215
+ let minDistance = Infinity;
216
+ let nearPano;
217
+ for (const pano of this.panos) {
218
+ const distance = position.distanceTo(pano.position);
219
+ if (distance < minDistance) {
220
+ minDistance = distance;
221
+ nearPano = pano;
222
+ }
223
+ }
224
+ return nearPano;
225
+ }
226
+ }
227
+ export default OrientedImageLayer;
@@ -0,0 +1,405 @@
1
+ import * as THREE from 'three';
2
+ import GeometryLayer from "./GeometryLayer.js";
3
+ import PointsMaterial, { PNTS_MODE } from "../Renderer/PointsMaterial.js";
4
+ import Picking from "../Core/Picking.js";
5
+ const point = new THREE.Vector3();
6
+ const bboxMesh = new THREE.Mesh();
7
+ const box3 = new THREE.Box3();
8
+ bboxMesh.geometry.boundingBox = box3;
9
+ function initBoundingBox(elt, layer) {
10
+ elt.tightbbox.getSize(box3.max);
11
+ box3.max.multiplyScalar(0.5);
12
+ box3.min.copy(box3.max).negate();
13
+ elt.obj.boxHelper = new THREE.BoxHelper(bboxMesh);
14
+ elt.obj.boxHelper.geometry = elt.obj.boxHelper.geometry.toNonIndexed();
15
+ elt.obj.boxHelper.computeLineDistances();
16
+ elt.obj.boxHelper.material = elt.childrenBitField ? new THREE.LineDashedMaterial({
17
+ dashSize: 0.25,
18
+ gapSize: 0.25
19
+ }) : new THREE.LineBasicMaterial();
20
+ elt.obj.boxHelper.material.color.setHex(0);
21
+ elt.obj.boxHelper.material.linewidth = 2;
22
+ elt.obj.boxHelper.frustumCulled = false;
23
+ elt.obj.boxHelper.position.copy(elt.tightbbox.min).add(box3.max);
24
+ elt.obj.boxHelper.autoUpdateMatrix = false;
25
+ layer.bboxes.add(elt.obj.boxHelper);
26
+ elt.obj.boxHelper.updateMatrix();
27
+ elt.obj.boxHelper.updateMatrixWorld();
28
+ }
29
+ function computeSSEPerspective(context, pointSize, spacing, elt, distance) {
30
+ if (distance <= 0) {
31
+ return Infinity;
32
+ }
33
+ const pointSpacing = spacing / 2 ** elt.depth;
34
+ // Estimate the onscreen distance between 2 points
35
+ const onScreenSpacing = context.camera.preSSE * pointSpacing / distance;
36
+ // [ P1 ]--------------[ P2 ]
37
+ // <---------------------> = pointsSpacing (in world coordinates)
38
+ // ~ onScreenSpacing (in pixels)
39
+ // <------> = pointSize (in pixels)
40
+ return Math.max(0.0, onScreenSpacing - pointSize);
41
+ }
42
+ function computeSSEOrthographic(context, pointSize, spacing, elt) {
43
+ const pointSpacing = spacing / 2 ** elt.depth;
44
+
45
+ // Given an identity view matrix, project pointSpacing from world space to
46
+ // clip space. v' = vVP = vP
47
+ const v = new THREE.Vector4(pointSpacing);
48
+ v.applyMatrix4(context.camera.camera3D.projectionMatrix);
49
+
50
+ // We map v' to the screen space and calculate the distance to the origin.
51
+ const dx = v.x * 0.5 * context.camera.width;
52
+ const dy = v.y * 0.5 * context.camera.height;
53
+ const distance = Math.sqrt(dx * dx + dy * dy);
54
+ return Math.max(0.0, distance - pointSize);
55
+ }
56
+ function computeScreenSpaceError(context, pointSize, spacing, elt, distance) {
57
+ if (context.camera.camera3D.isOrthographicCamera) {
58
+ return computeSSEOrthographic(context, pointSize, spacing, elt);
59
+ }
60
+ return computeSSEPerspective(context, pointSize, spacing, elt, distance);
61
+ }
62
+ function markForDeletion(elt) {
63
+ if (elt.obj) {
64
+ elt.obj.visible = false;
65
+ }
66
+ if (!elt.notVisibleSince) {
67
+ elt.notVisibleSince = Date.now();
68
+ // Set .sse to an invalid value
69
+ elt.sse = -1;
70
+ }
71
+ for (const child of elt.children) {
72
+ markForDeletion(child);
73
+ }
74
+ }
75
+ function changeIntensityRange(layer) {
76
+ layer.material.intensityRange?.set(layer.minIntensityRange, layer.maxIntensityRange);
77
+ }
78
+ function changeElevationRange(layer) {
79
+ layer.material.elevationRange?.set(layer.minElevationRange, layer.maxElevationRange);
80
+ }
81
+ function changeAngleRange(layer) {
82
+ layer.material.angleRange?.set(layer.minAngleRange, layer.maxAngleRange);
83
+ }
84
+
85
+ /**
86
+ * The basis for all point clouds related layers.
87
+ *
88
+ * @property {boolean} isPointCloudLayer - Used to checkout whether this layer
89
+ * is a PointCloudLayer. Default is `true`. You should not change this, as it is
90
+ * used internally for optimisation.
91
+ * @property {THREE.Group|THREE.Object3D} group - Contains the created
92
+ * `THREE.Points` meshes, usually with an instance of a `THREE.Points` per node.
93
+ * @property {THREE.Group|THREE.Object3D} bboxes - Contains the bounding boxes
94
+ * (`THREE.Box3`) of the tree, usually one per node.
95
+ * @property {number} octreeDepthLimit - The depth limit at which to stop
96
+ * browsing the octree. Can be used to limit the browsing, without having to
97
+ * edit manually the source of the point cloud. No limit by default (`-1`).
98
+ * @property {number} [pointBudget=2000000] - Maximum number of points to
99
+ * display at the same time. This influences the performance of rendering.
100
+ * Default to two millions points.
101
+ * @property {number} [sseThreshold=2] - Threshold of the **S**creen **S**pace
102
+ * **E**rror. Default to `2`.
103
+ * @property {number} [pointSize=4] - The size (in pixels) of the points.
104
+ * Default to `4`.
105
+ * @property {THREE.Material|PointsMaterial} [material=new PointsMaterial] - The
106
+ * material to use to display the points of the cloud. Be default it is a new
107
+ * `PointsMaterial`.
108
+ * @property {number} [mode=PNTS_MODE.COLOR] - The displaying mode of the points.
109
+ * Values are specified in `PointsMaterial`.
110
+ * @property {number} [minIntensityRange=0] - The minimal intensity of the
111
+ * layer. Changing this value will affect the material, if it has the
112
+ * corresponding uniform. The value is normalized between 0 and 1.
113
+ * @property {number} [maxIntensityRange=1] - The maximal intensity of the
114
+ * layer. Changing this value will affect the material, if it has the
115
+ * corresponding uniform. The value is normalized between 0 and 1.
116
+ *
117
+ * @extends GeometryLayer
118
+ */
119
+ class PointCloudLayer extends GeometryLayer {
120
+ /**
121
+ * Constructs a new instance of point cloud layer.
122
+ * Constructs a new instance of a Point Cloud Layer. This should not be used
123
+ * directly, but rather implemented using `extends`.
124
+ *
125
+ * @param {string} id - The id of the layer, that should be unique. It is
126
+ * not mandatory, but an error will be emitted if this layer is added a
127
+ * {@link View} that already has a layer going by that id.
128
+ * @param {Object} [config] - Optional configuration, all elements in it
129
+ * will be merged as is in the layer. For example, if the configuration
130
+ * contains three elements `name, protocol, extent`, these elements will be
131
+ * available using `layer.name` or something else depending on the property
132
+ * name. See the list of properties to know which one can be specified.
133
+ * @param {Source} config.source - Description and options of the source See @Layer.
134
+ * @param {number} [options.minElevationRange] - Min value for the elevation range (default value will be taken from the source.metadata).
135
+ * @param {number} [options.maxElevationRange] - Max value for the elevation range (default value will be taken from the source.metadata).
136
+ */
137
+ constructor(id) {
138
+ let config = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
139
+ const {
140
+ object3d = new THREE.Group(),
141
+ group = new THREE.Group(),
142
+ bboxes = new THREE.Group(),
143
+ octreeDepthLimit = -1,
144
+ pointBudget = 2000000,
145
+ pointSize = 2,
146
+ sseThreshold = 2,
147
+ minIntensityRange = 1,
148
+ maxIntensityRange = 65536,
149
+ minElevationRange,
150
+ maxElevationRange,
151
+ minAngleRange = -90,
152
+ maxAngleRange = 90,
153
+ material = {},
154
+ mode = PNTS_MODE.COLOR,
155
+ ...geometryLayerConfig
156
+ } = config;
157
+ super(id, object3d, geometryLayerConfig);
158
+
159
+ /**
160
+ * @type {boolean}
161
+ * @readonly
162
+ */
163
+ this.isPointCloudLayer = true;
164
+ this.protocol = 'pointcloud';
165
+ this.group = group;
166
+ this.object3d.add(this.group);
167
+ this.bboxes = bboxes || new THREE.Group();
168
+ this.bboxes.visible = false;
169
+ this.object3d.add(this.bboxes);
170
+ this.group.updateMatrixWorld();
171
+
172
+ // default config
173
+ /**
174
+ * @type {number}
175
+ */
176
+ this.octreeDepthLimit = octreeDepthLimit;
177
+
178
+ /**
179
+ * @type {number}
180
+ */
181
+ this.pointBudget = pointBudget;
182
+
183
+ /**
184
+ * @type {number}
185
+ */
186
+ this.pointSize = pointSize;
187
+
188
+ /**
189
+ * @type {number}
190
+ */
191
+ this.sseThreshold = sseThreshold;
192
+ this.defineLayerProperty('minIntensityRange', minIntensityRange, changeIntensityRange);
193
+ this.defineLayerProperty('maxIntensityRange', maxIntensityRange, changeIntensityRange);
194
+ this.defineLayerProperty('minElevationRange', minElevationRange, changeElevationRange);
195
+ this.defineLayerProperty('maxElevationRange', maxElevationRange, changeElevationRange);
196
+ this.defineLayerProperty('minAngleRange', minAngleRange, changeAngleRange);
197
+ this.defineLayerProperty('maxAngleRange', maxAngleRange, changeAngleRange);
198
+
199
+ /**
200
+ * @type {THREE.Material}
201
+ */
202
+ this.material = material;
203
+ if (!this.material.isMaterial) {
204
+ this.material.intensityRange = new THREE.Vector2(this.minIntensityRange, this.maxIntensityRange);
205
+ this.material.elevationRange = new THREE.Vector2(this.minElevationRange, this.maxElevationRange);
206
+ this.material.angleRange = new THREE.Vector2(this.minAngleRange, this.maxAngleRange);
207
+ this.material = new PointsMaterial(this.material);
208
+ }
209
+ this.mode = mode || PNTS_MODE.COLOR;
210
+
211
+ /**
212
+ * @type {PointCloudNode | undefined}
213
+ */
214
+ this.root = undefined;
215
+ }
216
+ preUpdate(context, changeSources) {
217
+ // See https://cesiumjs.org/hosted-apps/massiveworlds/downloads/Ring/WorldScaleTerrainRendering.pptx
218
+ // slide 17
219
+ context.camera.preSSE = context.camera.height / (2 * Math.tan(THREE.MathUtils.degToRad(context.camera.camera3D.fov) * 0.5));
220
+ if (this.material) {
221
+ this.material.visible = this.visible;
222
+ this.material.opacity = this.opacity;
223
+ this.material.transparent = this.opacity < 1 || this.material.userData.needTransparency[this.material.mode];
224
+ this.material.size = this.pointSize;
225
+ this.material.scale = context.camera.preSSE;
226
+ if (this.material.updateUniforms) {
227
+ this.material.updateUniforms();
228
+ }
229
+ }
230
+
231
+ // lookup lowest common ancestor of changeSources
232
+ let commonAncestor;
233
+ for (const source of changeSources.values()) {
234
+ if (source.isCamera || source == this) {
235
+ // if the change is caused by a camera move, no need to bother
236
+ // to find common ancestor: we need to update the whole tree:
237
+ // some invisible tiles may now be visible
238
+ return [this.root];
239
+ }
240
+ if (source.obj === undefined) {
241
+ continue;
242
+ }
243
+ // filter sources that belong to our layer
244
+ if (source.obj.isPoints && source.obj.layer == this) {
245
+ if (!commonAncestor) {
246
+ commonAncestor = source;
247
+ } else {
248
+ commonAncestor = source.findCommonAncestor(commonAncestor);
249
+ if (!commonAncestor) {
250
+ return [this.root];
251
+ }
252
+ }
253
+ }
254
+ }
255
+ if (commonAncestor) {
256
+ return [commonAncestor];
257
+ }
258
+
259
+ // Start updating from hierarchy root
260
+ return [this.root];
261
+ }
262
+ update(context, layer, elt) {
263
+ elt.visible = false;
264
+ if (this.octreeDepthLimit >= 0 && this.octreeDepthLimit < elt.depth) {
265
+ markForDeletion(elt);
266
+ return;
267
+ }
268
+
269
+ // pick the best bounding box
270
+ const bbox = elt.tightbbox ? elt.tightbbox : elt.bbox;
271
+ elt.visible = context.camera.isBox3Visible(bbox, this.object3d.matrixWorld);
272
+ if (!elt.visible) {
273
+ markForDeletion(elt);
274
+ return;
275
+ }
276
+ elt.notVisibleSince = undefined;
277
+ point.copy(context.camera.camera3D.position).sub(this.object3d.getWorldPosition(new THREE.Vector3()));
278
+ point.applyQuaternion(this.object3d.getWorldQuaternion(new THREE.Quaternion()).invert());
279
+
280
+ // only load geometry if this elements has points
281
+ if (elt.numPoints !== 0) {
282
+ if (elt.obj) {
283
+ elt.obj.visible = true;
284
+ } else if (!elt.promise) {
285
+ const distance = Math.max(0.001, bbox.distanceToPoint(point));
286
+ // Increase priority of nearest node
287
+ const priority = computeScreenSpaceError(context, layer.pointSize, layer.spacing, elt, distance) / distance;
288
+ elt.promise = context.scheduler.execute({
289
+ layer,
290
+ requester: elt,
291
+ view: context.view,
292
+ priority,
293
+ redraw: true,
294
+ earlyDropFunction: cmd => !cmd.requester.visible || !this.visible
295
+ }).then(pts => {
296
+ elt.obj = pts;
297
+ // store tightbbox to avoid ping-pong (bbox = larger => visible, tight => invisible)
298
+ elt.tightbbox = pts.tightbbox;
299
+
300
+ // make sure to add it here, otherwise it might never
301
+ // be added nor cleaned
302
+ this.group.add(elt.obj);
303
+ elt.obj.updateMatrixWorld(true);
304
+ }).catch(err => {
305
+ if (!err.isCancelledCommandException) {
306
+ return err;
307
+ }
308
+ }).finally(() => {
309
+ elt.promise = null;
310
+ });
311
+ }
312
+ }
313
+ if (elt.children && elt.children.length) {
314
+ const distance = bbox.distanceToPoint(point);
315
+ elt.sse = computeScreenSpaceError(context, layer.pointSize, layer.spacing, elt, distance) / this.sseThreshold;
316
+ if (elt.sse >= 1) {
317
+ return elt.children;
318
+ } else {
319
+ for (const child of elt.children) {
320
+ markForDeletion(child);
321
+ }
322
+ }
323
+ }
324
+ }
325
+ postUpdate() {
326
+ this.displayedCount = 0;
327
+ for (const pts of this.group.children) {
328
+ if (pts.visible) {
329
+ const count = pts.geometry.attributes.position.count;
330
+ pts.geometry.setDrawRange(0, count);
331
+ this.displayedCount += count;
332
+ }
333
+ }
334
+ if (this.displayedCount > this.pointBudget) {
335
+ // 2 different point count limit implementation, depending on the potree source
336
+ if (this.supportsProgressiveDisplay) {
337
+ // In this format, points are evenly distributed within a node,
338
+ // so we can draw a percentage of each node and still get a correct
339
+ // representation
340
+ const reduction = this.pointBudget / this.displayedCount;
341
+ for (const pts of this.group.children) {
342
+ if (pts.visible) {
343
+ const count = Math.floor(pts.geometry.drawRange.count * reduction);
344
+ if (count > 0) {
345
+ pts.geometry.setDrawRange(0, count);
346
+ } else {
347
+ pts.visible = false;
348
+ }
349
+ }
350
+ }
351
+ this.displayedCount *= reduction;
352
+ } else {
353
+ // This format doesn't require points to be evenly distributed, so
354
+ // we're going to sort the nodes by "importance" (= on screen size)
355
+ // and display only the first N nodes
356
+ this.group.children.sort((p1, p2) => p2.userData.node.sse - p1.userData.node.sse);
357
+ let limitHit = false;
358
+ this.displayedCount = 0;
359
+ for (const pts of this.group.children) {
360
+ const count = pts.geometry.attributes.position.count;
361
+ if (limitHit || this.displayedCount + count > this.pointBudget) {
362
+ pts.visible = false;
363
+ limitHit = true;
364
+ } else {
365
+ this.displayedCount += count;
366
+ }
367
+ }
368
+ }
369
+ }
370
+ const now = Date.now();
371
+ for (let i = this.group.children.length - 1; i >= 0; i--) {
372
+ const obj = this.group.children[i];
373
+ if (!obj.visible && now - obj.userData.node.notVisibleSince > 10000) {
374
+ // remove from group
375
+ this.group.children.splice(i, 1);
376
+
377
+ // no need to dispose obj.material, as it is shared by all objects of this layer
378
+ obj.geometry.dispose();
379
+ obj.material = null;
380
+ obj.geometry = null;
381
+ obj.userData.node.obj = null;
382
+ }
383
+ }
384
+ }
385
+ pickObjectsAt(view, mouse, radius) {
386
+ let target = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
387
+ return Picking.pickPointsAt(view, mouse, radius, this, target);
388
+ }
389
+ getObjectToUpdateForAttachedLayers(meta) {
390
+ if (meta.obj) {
391
+ const p = meta.parent;
392
+ if (p && p.obj) {
393
+ return {
394
+ element: meta.obj,
395
+ parent: p.obj
396
+ };
397
+ } else {
398
+ return {
399
+ element: meta.obj
400
+ };
401
+ }
402
+ }
403
+ }
404
+ }
405
+ export default PointCloudLayer;