@cesium/engine 17.0.0 → 18.0.0

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 (150) hide show
  1. package/Build/ThirdParty/Workers/z-worker-pako.js +1 -1
  2. package/Build/Workers/{chunk-4TIR5UPL.js → chunk-2AXPWBJG.js} +1 -1
  3. package/Build/Workers/{chunk-CAKU6ZPR.js → chunk-2CAYUY2B.js} +8 -8
  4. package/Build/Workers/{chunk-PS3OKVOQ.js → chunk-2NKHPWDY.js} +12 -12
  5. package/Build/Workers/{chunk-VXAKU6EV.js → chunk-2QNKQVUT.js} +5 -5
  6. package/Build/Workers/{chunk-ZTBZMRQT.js → chunk-3RKVDGRF.js} +8 -8
  7. package/Build/Workers/{chunk-J2HTPGC7.js → chunk-4DSTDGY3.js} +2 -2
  8. package/Build/Workers/{chunk-P2N5IDWU.js → chunk-4MIY63FM.js} +13 -13
  9. package/Build/Workers/{chunk-XYGBWBD5.js → chunk-5OL6XFNS.js} +1 -1
  10. package/Build/Workers/{chunk-E7SMO47Q.js → chunk-5WVF4BK7.js} +5 -5
  11. package/Build/Workers/{chunk-SQKTLDSD.js → chunk-AEGAQ6FD.js} +12 -12
  12. package/Build/Workers/{chunk-4BGKMM7J.js → chunk-CES6DHHV.js} +1 -1
  13. package/Build/Workers/{chunk-MAIKDIBS.js → chunk-DINSLSJD.js} +6 -6
  14. package/Build/Workers/{chunk-G6JUXZUO.js → chunk-EHB2OCQX.js} +4 -4
  15. package/Build/Workers/{chunk-SSLK4U3V.js → chunk-EXEZHW3P.js} +4 -4
  16. package/Build/Workers/{chunk-236N6BJN.js → chunk-F4ZAKN4V.js} +2 -2
  17. package/Build/Workers/{chunk-HBNWBMAM.js → chunk-FPYBD2P5.js} +1 -1
  18. package/Build/Workers/{chunk-5PC577K7.js → chunk-GAUF4F5A.js} +16 -16
  19. package/Build/Workers/{chunk-YE6MDFDA.js → chunk-GM55YFOB.js} +14 -14
  20. package/Build/Workers/{chunk-HGEGZ67N.js → chunk-GS55WTRX.js} +6 -6
  21. package/Build/Workers/{chunk-LPGOAMYH.js → chunk-HQE7KSR4.js} +2 -2
  22. package/Build/Workers/{chunk-IFIS4CVK.js → chunk-HS76VTVY.js} +2 -2
  23. package/Build/Workers/{chunk-7IIMO7ZZ.js → chunk-I3BAKR4S.js} +16 -16
  24. package/Build/Workers/{chunk-PM4Q36WX.js → chunk-IWGMVQGG.js} +9 -9
  25. package/Build/Workers/{chunk-UFV3YUIN.js → chunk-IWZK2AIH.js} +5 -5
  26. package/Build/Workers/{chunk-QJTIOB2Z.js → chunk-J4MAVE6J.js} +6 -6
  27. package/Build/Workers/{chunk-RX65FA7X.js → chunk-JF4IYQYC.js} +7 -7
  28. package/Build/Workers/{chunk-7PHT4JHA.js → chunk-KGXTCGX3.js} +12 -12
  29. package/Build/Workers/{chunk-K5JTWJPE.js → chunk-KNA65INJ.js} +4 -4
  30. package/Build/Workers/{chunk-CBFJVM6I.js → chunk-LIPZUYNR.js} +11 -11
  31. package/Build/Workers/{chunk-C5DCX2YQ.js → chunk-LWJWWCZ4.js} +6 -6
  32. package/Build/Workers/{chunk-KE45DUDH.js → chunk-LZFUG3GO.js} +10 -10
  33. package/Build/Workers/{chunk-P57PR3WM.js → chunk-MFGVMUN3.js} +5 -5
  34. package/Build/Workers/{chunk-QUFN3GEO.js → chunk-NR7KSD56.js} +3 -3
  35. package/Build/Workers/{chunk-MS5NWDHH.js → chunk-NVJ5JRIM.js} +2 -2
  36. package/Build/Workers/{chunk-CQMXZF4A.js → chunk-NWSAYFZG.js} +6 -6
  37. package/Build/Workers/{chunk-5YEFZMMM.js → chunk-OATG6PKE.js} +12 -12
  38. package/Build/Workers/{chunk-UX5JWAC7.js → chunk-OIHAAHBK.js} +10 -10
  39. package/Build/Workers/{chunk-DRMXU674.js → chunk-QN7JEOQV.js} +5 -5
  40. package/Build/Workers/{chunk-77MDEA47.js → chunk-QUSP3NWA.js} +5 -5
  41. package/Build/Workers/{chunk-GO3RZX2R.js → chunk-RWFYLWSF.js} +9 -9
  42. package/Build/Workers/{chunk-UZ6KXWCA.js → chunk-SIOVJ4L5.js} +4 -4
  43. package/Build/Workers/{chunk-OBHLT34A.js → chunk-T6B6H3GR.js} +7 -7
  44. package/Build/Workers/{chunk-IKDQX7DY.js → chunk-UK33ZN4K.js} +4 -4
  45. package/Build/Workers/{chunk-2BJXFXD7.js → chunk-VGJSKEIB.js} +4 -4
  46. package/Build/Workers/{chunk-5RPUEFSA.js → chunk-W6CPO62M.js} +6 -6
  47. package/Build/Workers/{chunk-37PA7GZW.js → chunk-WGHUCZMX.js} +7 -7
  48. package/Build/Workers/{chunk-UC7KH56Y.js → chunk-WTFZHPUW.js} +5 -5
  49. package/Build/Workers/{chunk-NZSBSY5K.js → chunk-WYCR5DWQ.js} +2 -2
  50. package/Build/Workers/{chunk-WP3IJYLC.js → chunk-YV37YFUM.js} +9 -9
  51. package/Build/Workers/{chunk-LVGYWPEX.js → chunk-ZADJ6FRX.js} +4 -4
  52. package/Build/Workers/{chunk-CC2WKHIW.js → chunk-ZIP27IX6.js} +6 -6
  53. package/Build/Workers/combineGeometry.js +21 -21
  54. package/Build/Workers/createBoxGeometry.js +15 -15
  55. package/Build/Workers/createBoxOutlineGeometry.js +13 -13
  56. package/Build/Workers/createCircleGeometry.js +23 -23
  57. package/Build/Workers/createCircleOutlineGeometry.js +16 -16
  58. package/Build/Workers/createCoplanarPolygonGeometry.js +30 -30
  59. package/Build/Workers/createCoplanarPolygonOutlineGeometry.js +28 -28
  60. package/Build/Workers/createCorridorGeometry.js +26 -26
  61. package/Build/Workers/createCorridorOutlineGeometry.js +25 -25
  62. package/Build/Workers/createCylinderGeometry.js +17 -17
  63. package/Build/Workers/createCylinderOutlineGeometry.js +15 -15
  64. package/Build/Workers/createEllipseGeometry.js +23 -23
  65. package/Build/Workers/createEllipseOutlineGeometry.js +16 -16
  66. package/Build/Workers/createEllipsoidGeometry.js +16 -16
  67. package/Build/Workers/createEllipsoidOutlineGeometry.js +15 -15
  68. package/Build/Workers/createFrustumGeometry.js +15 -15
  69. package/Build/Workers/createFrustumOutlineGeometry.js +15 -15
  70. package/Build/Workers/createGeometry.js +21 -21
  71. package/Build/Workers/createGroundPolylineGeometry.js +19 -19
  72. package/Build/Workers/createPlaneGeometry.js +13 -13
  73. package/Build/Workers/createPlaneOutlineGeometry.js +12 -12
  74. package/Build/Workers/createPolygonGeometry.js +29 -29
  75. package/Build/Workers/createPolygonOutlineGeometry.js +27 -27
  76. package/Build/Workers/createPolylineGeometry.js +22 -22
  77. package/Build/Workers/createPolylineVolumeGeometry.js +28 -28
  78. package/Build/Workers/createPolylineVolumeOutlineGeometry.js +24 -24
  79. package/Build/Workers/createRectangleGeometry.js +24 -24
  80. package/Build/Workers/createRectangleOutlineGeometry.js +17 -17
  81. package/Build/Workers/createSimplePolylineGeometry.js +20 -20
  82. package/Build/Workers/createSphereGeometry.js +16 -16
  83. package/Build/Workers/createSphereOutlineGeometry.js +15 -15
  84. package/Build/Workers/createTaskProcessorWorker.js +3 -3
  85. package/Build/Workers/createVectorTileClampedPolylines.js +12 -12
  86. package/Build/Workers/createVectorTileGeometries.js +21 -21
  87. package/Build/Workers/createVectorTilePoints.js +11 -11
  88. package/Build/Workers/createVectorTilePolygons.js +19 -19
  89. package/Build/Workers/createVectorTilePolylines.js +12 -12
  90. package/Build/Workers/createVerticesFromGoogleEarthEnterpriseBuffer.js +19 -19
  91. package/Build/Workers/createVerticesFromHeightmap.js +19 -19
  92. package/Build/Workers/createVerticesFromQuantizedTerrainMesh.js +16 -16
  93. package/Build/Workers/createWallGeometry.js +21 -21
  94. package/Build/Workers/createWallOutlineGeometry.js +20 -20
  95. package/Build/Workers/decodeDraco.js +10 -10
  96. package/Build/Workers/decodeGoogleEarthEnterprisePacket.js +5 -5
  97. package/Build/Workers/decodeI3S.js +9 -9
  98. package/Build/Workers/transcodeKTX2.js +25 -6
  99. package/Build/Workers/transferTypedArrayTest.js +1 -1
  100. package/Build/Workers/upsampleQuantizedTerrainMesh.js +19 -19
  101. package/Source/Core/Ion.js +1 -1
  102. package/Source/Core/PixelFormat.js +36 -0
  103. package/Source/Renderer/Texture3D.js +568 -0
  104. package/Source/Renderer/createUniform.js +1 -0
  105. package/Source/Renderer/createUniformArray.js +1 -0
  106. package/Source/Scene/ArcGisMapService.js +1 -1
  107. package/Source/Scene/Cesium3DTileset.js +138 -0
  108. package/Source/Scene/GlobeSurfaceShaderSet.js +4 -5
  109. package/Source/Scene/ImageryLayer.js +19 -4
  110. package/Source/Scene/ImageryLayerCollection.js +3 -2
  111. package/Source/Scene/Model/CartesianRectangle.js +108 -0
  112. package/Source/Scene/Model/ImageryConfiguration.js +25 -0
  113. package/Source/Scene/Model/ImageryCoverage.js +488 -0
  114. package/Source/Scene/Model/ImageryFlags.js +31 -0
  115. package/Source/Scene/Model/ImageryInput.js +60 -0
  116. package/Source/Scene/Model/ImageryPipelineStage.js +1040 -0
  117. package/Source/Scene/Model/MappedPositions.js +84 -0
  118. package/Source/Scene/Model/Model.js +38 -0
  119. package/Source/Scene/Model/ModelImagery.js +403 -0
  120. package/Source/Scene/Model/ModelImageryMapping.js +489 -0
  121. package/Source/Scene/Model/ModelPrimitiveImagery.js +972 -0
  122. package/Source/Scene/Model/ModelReader.js +810 -0
  123. package/Source/Scene/Model/ModelRenderResources.js +13 -0
  124. package/Source/Scene/Model/ModelRuntimePrimitive.js +19 -0
  125. package/Source/Scene/Model/ModelSceneGraph.js +133 -27
  126. package/Source/Scene/Model/NodeRenderResources.js +10 -0
  127. package/Source/Scene/ModelComponents.js +10 -0
  128. package/Source/Scene/VoxelPrimitive.js +22 -37
  129. package/Source/Scene/VoxelRenderResources.js +1 -1
  130. package/Source/Scene/processVoxelProperties.js +41 -100
  131. package/Source/Shaders/Builtin/Functions/windowToEyeCoordinates.glsl +0 -1
  132. package/Source/Shaders/Builtin/Functions/windowToEyeCoordinates.js +0 -1
  133. package/Source/Shaders/GlobeFS.glsl +22 -0
  134. package/Source/Shaders/GlobeFS.js +22 -0
  135. package/Source/Shaders/Model/MaterialStageFS.glsl +4 -0
  136. package/Source/Shaders/Model/MaterialStageFS.js +4 -0
  137. package/Source/Shaders/Model/ModelVS.glsl +4 -0
  138. package/Source/Shaders/Model/ModelVS.js +4 -0
  139. package/Source/Shaders/Voxels/IntersectDepth.glsl +10 -3
  140. package/Source/Shaders/Voxels/IntersectDepth.js +10 -3
  141. package/Source/Shaders/Voxels/Intersection.glsl +1 -3
  142. package/Source/Shaders/Voxels/Intersection.js +1 -3
  143. package/Source/Shaders/Voxels/IntersectionUtils.glsl +1 -0
  144. package/Source/Shaders/Voxels/IntersectionUtils.js +1 -0
  145. package/Source/Shaders/Voxels/VoxelFS.glsl +37 -24
  146. package/Source/Shaders/Voxels/VoxelFS.js +37 -24
  147. package/Source/ThirdParty/Workers/z-worker-pako.js +1 -1
  148. package/index.d.ts +50 -2
  149. package/index.js +100 -88
  150. package/package.json +3 -3
@@ -0,0 +1,810 @@
1
+ import Check from "../../Core/Check.js";
2
+ import defined from "../../Core/defined.js";
3
+ import Cartesian2 from "../../Core/Cartesian2.js";
4
+ import Cartesian3 from "../../Core/Cartesian3.js";
5
+ import Cartesian4 from "../../Core/Cartesian4.js";
6
+ import DeveloperError from "../../Core/DeveloperError.js";
7
+ import ComponentDatatype from "../../Core/ComponentDatatype.js";
8
+ import AttributeCompression from "../../Core/AttributeCompression.js";
9
+ import IndexDatatype from "../../Core/IndexDatatype.js";
10
+ import PrimitiveType from "../../Core/PrimitiveType.js";
11
+ import Matrix4 from "../../Core/Matrix4.js";
12
+
13
+ import AttributeType from "../AttributeType.js";
14
+
15
+ /**
16
+ * A class for reading the data from a <code>ModelComponents.Attribute</code>.
17
+ *
18
+ * NOTE: Much of the functionality here already exists, scattered in many places.
19
+ * In most cases, the functionality is tailored for "one case" (like only handling
20
+ * positions, or only normals, or not considering quantization, or not handling
21
+ * interleaved buffers, ...). In many cases, the functionality is tailored for an
22
+ * 'accessor' (and often, the functions also expect the 'gltf' to be given).
23
+ * Most of what is done here (and in the existing functions) is pretty low-level
24
+ * and generic, though: The functions could often be fed with some (count, type,
25
+ * componentType), and there could be convenience functions that EITHER take these
26
+ * values from an 'accessor' OR from an 'attribute'. The tl;dr: Large parts of
27
+ * this could be "nicer", or "more generic", and "better" along all dimensions
28
+ * of this term. Just give me time...
29
+ *
30
+ * NOTE: The fact that all this has to operate on TypedArray is unfortunate.
31
+ * Most of the subsequent processing could operate on some abstraction of
32
+ * that. The fact that that TypedArrays can be read/written as "bulk", and
33
+ * then offer access that is "as efficient as it can be" could be a
34
+ * justification, as part of the performance-genericity trade-off
35
+ *
36
+ * NOTE: All this does not properly handle MATn types. There should be SOME
37
+ * abstraction for element- and component-wise access of the data. See
38
+ * https://github.com/javagl/JglTF/blob/84ce6d019fec3b75b6af1649bbe834005b2c620f/jgltf-model/src/main/java/de/javagl/jgltf/model/AbstractAccessorData.java#L149
39
+ *
40
+ * @private
41
+ */
42
+ class ModelReader {
43
+ /**
44
+ * Reads the data of the given atttribute into a typed array.
45
+ *
46
+ * This will read the data into a compact, flat array with the data
47
+ * type corresponding to the data type of the attribute.
48
+ *
49
+ * If the attribute is contained in an interleaved buffer, marked as
50
+ * 'normalized', quantized, or oct-encoded, then it will be deinterleaved,
51
+ * normalization will be applied, it will be dequantized and oct-decoded
52
+ * as necessary.
53
+ *
54
+ * The result will be THE actual attribute data.
55
+ *
56
+ * @param {ModelComponents.Attribute} attribute The attribute
57
+ * @returns {TypedArray} The attribute data
58
+ */
59
+ static readAttributeAsTypedArray(attribute) {
60
+ //>>includeStart('debug', pragmas.debug);
61
+ Check.defined("attribute", attribute);
62
+ //>>includeEnd('debug');
63
+
64
+ // Obtain a compact (non-interleaved) typed array that contains
65
+ // the components.
66
+ const compactTypedArray =
67
+ ModelReader.readAttributeAsRawCompactTypedArray(attribute);
68
+
69
+ // If the attribute is not normalized and the data is not quantized
70
+ // and not normalized, then this can be returned directly
71
+ const normalized = attribute.normalized;
72
+ const quantization = attribute.quantization;
73
+ if (!defined(quantization) && !normalized) {
74
+ return compactTypedArray;
75
+ }
76
+
77
+ const elementType = attribute.type;
78
+ const elementCount = attribute.count;
79
+
80
+ // If the attribute is normalized, normalize the data from
81
+ // the typed array
82
+ let normalizedTypedArray = compactTypedArray;
83
+ if (normalized) {
84
+ // Note that although this is called "dequantize", it does
85
+ // not really "dequantize" based on the quantization. It only
86
+ // performs the conversion from the (normalized) integer
87
+ // component types into floating point.
88
+ normalizedTypedArray = AttributeCompression.dequantize(
89
+ compactTypedArray,
90
+ attribute.componentDatatype,
91
+ elementType,
92
+ elementCount,
93
+ );
94
+ }
95
+
96
+ if (!defined(quantization)) {
97
+ return normalizedTypedArray;
98
+ }
99
+ // Now, this one actually DOES dequantize...
100
+ const dequantizedTypedArray = ModelReader.dequantize(
101
+ normalizedTypedArray,
102
+ elementCount,
103
+ elementType,
104
+ quantization,
105
+ );
106
+ return dequantizedTypedArray;
107
+ }
108
+
109
+ /**
110
+ * Read the data of the given attribute into a compact typed array.
111
+ *
112
+ * If the attribute is stored as interleaved data, then the result
113
+ * will be the deinterleaved data. If the data is quantized or
114
+ * normalized, then the resulting data will be the "raw" data,
115
+ * without applying normalization or dequantization.
116
+ *
117
+ * @param {ModelComponents.Attribute} attribute The attribute
118
+ * @returns {TypedArray} The raw attribute data
119
+ */
120
+ static readAttributeAsRawCompactTypedArray(attribute) {
121
+ //>>includeStart('debug', pragmas.debug);
122
+ Check.defined("attribute", attribute);
123
+ //>>includeEnd('debug');
124
+
125
+ const elementType = attribute.type;
126
+ const elementCount = attribute.count;
127
+
128
+ const componentsPerElement =
129
+ AttributeType.getNumberOfComponents(elementType);
130
+ const totalComponentCount = elementCount * componentsPerElement;
131
+
132
+ // If the data is quantized, use the quantized component type
133
+ let componentType = attribute.componentDatatype;
134
+ const quantization = attribute.quantization;
135
+ if (defined(quantization)) {
136
+ componentType = quantization.componentDatatype;
137
+ }
138
+ const buffer = attribute.buffer;
139
+
140
+ // If the byte stride is the default (i.e. the total element size),
141
+ // then just fetch the whole buffer data into a typed array of the
142
+ // desired target type, and return it
143
+ const byteOffset = attribute.byteOffset;
144
+ const byteStride = attribute.byteStride;
145
+ const bytesPerComponent = ComponentDatatype.getSizeInBytes(componentType);
146
+ const defaultByteStride = componentsPerElement * bytesPerComponent;
147
+ if (!defined(byteStride) || byteStride === defaultByteStride) {
148
+ const typedArray = ComponentDatatype.createTypedArray(
149
+ componentType,
150
+ totalComponentCount,
151
+ );
152
+ buffer.getBufferData(typedArray, byteOffset);
153
+ return typedArray;
154
+ }
155
+
156
+ // Fetch the whole buffer in its raw form, to pick out the
157
+ // interleaved values.
158
+ // Note: When ALL attributes have to be fetched from an
159
+ // interleaved buffer, then this getBufferData call will
160
+ // be performed multiple times. It would be preferable to
161
+ // have ONE "TypedArray[] getThemFrom(buffer)" call that
162
+ // returns all of the (interleaved) attributes at once,
163
+ // but this requires abstractions that we don't have.
164
+ const fullTypedArray = new Uint8Array(buffer.sizeInBytes);
165
+ buffer.getBufferData(fullTypedArray);
166
+
167
+ // Read the components of each element, and write them into
168
+ // a typed array in a compact form
169
+ const compactTypedArray = ComponentDatatype.createTypedArray(
170
+ componentType,
171
+ totalComponentCount,
172
+ );
173
+ const elementByteStride = byteStride ?? defaultByteStride;
174
+ const dataView = new DataView(
175
+ fullTypedArray.buffer,
176
+ fullTypedArray.byteOffset,
177
+ fullTypedArray.byteLength,
178
+ );
179
+ const components = new Array(componentsPerElement);
180
+ const componentsReader = ModelReader.createComponentsReader(componentType);
181
+ for (let i = 0; i < elementCount; ++i) {
182
+ const elementByteOffset = byteOffset + i * elementByteStride;
183
+ componentsReader(
184
+ dataView,
185
+ elementByteOffset,
186
+ componentsPerElement,
187
+ components,
188
+ );
189
+ for (let j = 0; j < componentsPerElement; ++j) {
190
+ compactTypedArray[i * componentsPerElement + j] = components[j];
191
+ }
192
+ }
193
+ return compactTypedArray;
194
+ }
195
+
196
+ /**
197
+ * Dequantize the data from the given input array, based on the given
198
+ * quantization information, and return the result.
199
+ *
200
+ * This assumes that normalization has already been applied. This means that
201
+ * when the <code>quantization.normalized</code> flag is <code>true</code>,
202
+ * then the input is assumed to contain floating point values in the range
203
+ * [-1, 1].
204
+ *
205
+ * @param {TypedArray} quantizedTypedArray The quantized typed array
206
+ * @param {number} elementCount The number of elements
207
+ * @param {AttributeType} elementType The element type
208
+ * @param {ModelComponents.Quantization} quantization The quantization
209
+ * @returns {TypedArray} The result
210
+ * @throws DeveloperError When the element type is not SCALAR, VEC2,
211
+ * VEC3, or VEC4
212
+ */
213
+ static dequantize(
214
+ quantizedTypedArray,
215
+ elementCount,
216
+ elementType,
217
+ quantization,
218
+ ) {
219
+ //>>includeStart('debug', pragmas.debug);
220
+ Check.defined("quantizedTypedArray", quantizedTypedArray);
221
+ Check.typeOf.number.greaterThanOrEquals("elementCount", elementCount, 0);
222
+ Check.defined("elementType", elementType);
223
+ Check.defined("quantization", quantization);
224
+ //>>includeEnd('debug');
225
+
226
+ if (quantization.octEncoded) {
227
+ const dequantizedTypedArray = ModelReader.octDecode(
228
+ quantizedTypedArray,
229
+ elementCount,
230
+ quantization.normalizationRange,
231
+ undefined,
232
+ );
233
+ if (quantization.octEncodedZXY) {
234
+ ModelReader.convertZxyToXyz(
235
+ dequantizedTypedArray,
236
+ dequantizedTypedArray,
237
+ );
238
+ }
239
+ return dequantizedTypedArray;
240
+ }
241
+
242
+ // These could be generalized, if the offset/stepSize were not
243
+ // CartesianX objects, but arrays...
244
+ const stepSize = quantization.quantizedVolumeStepSize;
245
+ const offset = quantization.quantizedVolumeOffset;
246
+ if (elementType === AttributeType.SCALAR) {
247
+ return ModelReader.dequantize1D(
248
+ quantizedTypedArray,
249
+ elementCount,
250
+ stepSize,
251
+ offset,
252
+ undefined,
253
+ );
254
+ }
255
+ if (elementType === AttributeType.VEC2) {
256
+ return ModelReader.dequantize2D(
257
+ quantizedTypedArray,
258
+ elementCount,
259
+ stepSize,
260
+ offset,
261
+ undefined,
262
+ );
263
+ }
264
+ if (elementType === AttributeType.VEC3) {
265
+ return ModelReader.dequantize3D(
266
+ quantizedTypedArray,
267
+ elementCount,
268
+ stepSize,
269
+ offset,
270
+ undefined,
271
+ );
272
+ }
273
+ if (elementType === AttributeType.VEC4) {
274
+ return ModelReader.dequantize4D(
275
+ quantizedTypedArray,
276
+ elementCount,
277
+ stepSize,
278
+ offset,
279
+ undefined,
280
+ );
281
+ }
282
+ throw new DeveloperError(
283
+ `Element type for dequantization must be SCALAR, VEC2, VEC3, or VEC4, but is ${elementType}`,
284
+ );
285
+ }
286
+
287
+ /**
288
+ * Decode oct-encoded normals from the given input, and write the
289
+ * result into the given output, allocating and returning a new
290
+ * array if the result was undefined.
291
+ *
292
+ * This will apply the <code>AttributeCompression.octDecodeInRange</code>
293
+ * function to each three components of the input.
294
+ *
295
+ * @param {TypedArray} quantizedTypedArray The input
296
+ * @param {number} elementCount The number of elements
297
+ * @param {number} normalizationRange The normalization range
298
+ * @param {TypedArray} [dequantizedTypedArray] The result
299
+ * @returns {TypedArray} The result
300
+ */
301
+ static octDecode(
302
+ quantizedTypedArray,
303
+ elementCount,
304
+ normalizationRange,
305
+ dequantizedTypedArray,
306
+ ) {
307
+ //>>includeStart('debug', pragmas.debug);
308
+ Check.defined("quantizedTypedArray", quantizedTypedArray);
309
+ Check.typeOf.number.greaterThanOrEquals("elementCount", elementCount, 0);
310
+ Check.typeOf.number.greaterThan(
311
+ "normalizationRange",
312
+ normalizationRange,
313
+ 0,
314
+ );
315
+ //>>includeEnd('debug');
316
+
317
+ if (!defined(dequantizedTypedArray)) {
318
+ dequantizedTypedArray = new Float32Array(quantizedTypedArray.length);
319
+ }
320
+ const c = new Cartesian3();
321
+ for (let i = 0; i < elementCount; i++) {
322
+ Cartesian3.unpack(quantizedTypedArray, i * 3, c);
323
+ AttributeCompression.octDecodeInRange(c, normalizationRange, c);
324
+ Cartesian3.pack(dequantizedTypedArray, c, i * 3);
325
+ }
326
+ return dequantizedTypedArray;
327
+ }
328
+
329
+ /**
330
+ * Swizzle all three consecutive elements in the given input array
331
+ * from (z, x, y) to (x, y, z), and write the result into the
332
+ * given output array, creating a new array if the given output
333
+ * array was undefined.
334
+ *
335
+ * @param {TypedArray} input The input
336
+ * @param {number} elementCount The number of elements
337
+ * @param {TypedArray} [output] The result
338
+ * @returns {TypedArray} The result
339
+ */
340
+ static convertZxyToXyz(input, elementCount, output) {
341
+ //>>includeStart('debug', pragmas.debug);
342
+ Check.defined("input", input);
343
+ Check.typeOf.number.greaterThanOrEquals("elementCount", elementCount, 0);
344
+ //>>includeEnd('debug');
345
+
346
+ if (!defined(output)) {
347
+ output = new Float32Array(input.length);
348
+ }
349
+ let offset = 0;
350
+ for (let i = 0; i < elementCount; i++, offset += 3) {
351
+ const z = input[offset + 0];
352
+ const x = input[offset + 1];
353
+ const y = input[offset + 2];
354
+ output[offset + 0] = x;
355
+ output[offset + 1] = y;
356
+ output[offset + 2] = z;
357
+ }
358
+ return output;
359
+ }
360
+
361
+ /**
362
+ * Dequantize the given quantized array, based on the given quantization
363
+ * information, and write the result into the given output array, creating
364
+ * the output array if it was undefined.
365
+ *
366
+ * This will simply fill the output array with
367
+ * <code>output[i] = input[i] * stepSize + offset</code>
368
+ *
369
+ * @param {TypedArray} quantizedTypedArray The quantized array
370
+ * @param {number} elementCount The number of elements
371
+ * @param {number} stepSize The quantization step size
372
+ * @param {number} offset The quantization offset
373
+ * @param {TypedArray} [dequantizedTypedArray] The result
374
+ * @returns {TypedArray} The result
375
+ */
376
+ static dequantize1D(
377
+ quantizedTypedArray,
378
+ elementCount,
379
+ stepSize,
380
+ offset,
381
+ dequantizedTypedArray,
382
+ ) {
383
+ //>>includeStart('debug', pragmas.debug);
384
+ Check.defined("quantizedTypedArray", quantizedTypedArray);
385
+ Check.typeOf.number.greaterThanOrEquals("elementCount", elementCount, 0);
386
+ Check.defined("stepSize", stepSize);
387
+ Check.defined("offset", offset);
388
+ //>>includeEnd('debug');
389
+
390
+ if (!defined(dequantizedTypedArray)) {
391
+ dequantizedTypedArray = new Float32Array(quantizedTypedArray.length);
392
+ }
393
+ for (let i = 0; i < elementCount; i++) {
394
+ const q = quantizedTypedArray[i];
395
+ const d = q * stepSize + offset;
396
+ dequantizedTypedArray[i] = d;
397
+ }
398
+ return dequantizedTypedArray;
399
+ }
400
+
401
+ /**
402
+ * Dequantize the given quantized array, based on the given quantization
403
+ * information, and write the result into the given output array, creating
404
+ * the output array if it was undefined.
405
+ *
406
+ * This will simply fill the output array with
407
+ * <code>output[i] = input[i] * stepSize + offset</code>
408
+ * when interpreting the input and output as arrays of Cartesian2.
409
+ *
410
+ * @param {TypedArray} quantizedTypedArray The quantized array
411
+ * @param {number} elementCount The number of elements
412
+ * @param {Cartesian2} stepSize The quantization step size
413
+ * @param {Cartesian2} offset The quantization offset
414
+ * @param {TypedArray} [dequantizedTypedArray] The result
415
+ * @returns {TypedArray} The result
416
+ */
417
+ static dequantize2D(
418
+ quantizedTypedArray,
419
+ elementCount,
420
+ stepSize,
421
+ offset,
422
+ dequantizedTypedArray,
423
+ ) {
424
+ //>>includeStart('debug', pragmas.debug);
425
+ Check.defined("quantizedTypedArray", quantizedTypedArray);
426
+ Check.typeOf.number.greaterThanOrEquals("elementCount", elementCount, 0);
427
+ Check.defined("stepSize", stepSize);
428
+ Check.defined("offset", offset);
429
+ //>>includeEnd('debug');
430
+
431
+ if (!defined(dequantizedTypedArray)) {
432
+ dequantizedTypedArray = new Float32Array(quantizedTypedArray.length);
433
+ }
434
+ const c = new Cartesian2();
435
+ for (let i = 0; i < elementCount; i++) {
436
+ Cartesian2.unpack(quantizedTypedArray, i * 2, c);
437
+ Cartesian2.multiplyComponents(c, stepSize, c);
438
+ Cartesian2.add(c, offset, c);
439
+ Cartesian2.pack(c, dequantizedTypedArray, i * 2);
440
+ }
441
+ return dequantizedTypedArray;
442
+ }
443
+
444
+ /**
445
+ * Dequantize the given quantized array, based on the given quantization
446
+ * information, and write the result into the given output array, creating
447
+ * the output array if it was undefined.
448
+ *
449
+ * This will simply fill the output array with
450
+ * <code>output[i] = input[i] * stepSize + offset</code>
451
+ * when interpreting the input and output as arrays of Cartesian3.
452
+ *
453
+ * @param {TypedArray} quantizedTypedArray The quantized array
454
+ * @param {number} elementCount The number of elements
455
+ * @param {Cartesian3} stepSize The quantization step size
456
+ * @param {Cartesian3} offset The quantization offset
457
+ * @param {TypedArray} [dequantizedTypedArray] The result
458
+ * @returns {TypedArray} The result
459
+ */
460
+ static dequantize3D(
461
+ quantizedTypedArray,
462
+ elementCount,
463
+ stepSize,
464
+ offset,
465
+ dequantizedTypedArray,
466
+ ) {
467
+ //>>includeStart('debug', pragmas.debug);
468
+ Check.defined("quantizedTypedArray", quantizedTypedArray);
469
+ Check.typeOf.number.greaterThanOrEquals("elementCount", elementCount, 0);
470
+ Check.defined("stepSize", stepSize);
471
+ Check.defined("offset", offset);
472
+ //>>includeEnd('debug');
473
+
474
+ if (!defined(dequantizedTypedArray)) {
475
+ dequantizedTypedArray = new Float32Array(quantizedTypedArray.length);
476
+ }
477
+ const c = new Cartesian3();
478
+ for (let i = 0; i < elementCount; i++) {
479
+ Cartesian3.unpack(quantizedTypedArray, i * 3, c);
480
+ Cartesian3.multiplyComponents(c, stepSize, c);
481
+ Cartesian3.add(c, offset, c);
482
+ Cartesian3.pack(c, dequantizedTypedArray, i * 3);
483
+ }
484
+ return dequantizedTypedArray;
485
+ }
486
+
487
+ /**
488
+ * Dequantize the given quantized array, based on the given quantization
489
+ * information, and write the result into the given output array, creating
490
+ * the output array if it was undefined.
491
+ *
492
+ * This will simply fill the output array with
493
+ * <code>output[i] = input[i] * stepSize + offset</code>
494
+ * when interpreting the input and output as arrays of Cartesian4.
495
+ *
496
+ * @param {TypedArray} quantizedTypedArray The quantized array
497
+ * @param {number} elementCount The number of elements
498
+ * @param {Cartesian4} stepSize The quantization step size
499
+ * @param {Cartesian4} offset The quantization offset
500
+ * @param {TypedArray} [dequantizedTypedArray] The result
501
+ * @returns {TypedArray} The result
502
+ */
503
+ static dequantize4D(
504
+ quantizedTypedArray,
505
+ elementCount,
506
+ stepSize,
507
+ offset,
508
+ dequantizedTypedArray,
509
+ ) {
510
+ //>>includeStart('debug', pragmas.debug);
511
+ Check.defined("quantizedTypedArray", quantizedTypedArray);
512
+ Check.typeOf.number.greaterThanOrEquals("elementCount", elementCount, 0);
513
+ Check.defined("stepSize", stepSize);
514
+ Check.defined("offset", offset);
515
+ //>>includeEnd('debug');
516
+
517
+ if (!defined(dequantizedTypedArray)) {
518
+ dequantizedTypedArray = new Float32Array(quantizedTypedArray.length);
519
+ }
520
+ const c = new Cartesian4();
521
+ for (let i = 0; i < elementCount; i++) {
522
+ Cartesian4.unpack(quantizedTypedArray, i * 4, c);
523
+ Cartesian4.multiplyComponents(c, stepSize, c);
524
+ Cartesian4.add(c, offset, c);
525
+ Cartesian4.pack(c, dequantizedTypedArray, i * 4);
526
+ }
527
+ return dequantizedTypedArray;
528
+ }
529
+
530
+ /**
531
+ * Reads and returns a value with the given type
532
+ * at the given byte offset from the data view, in little-endian
533
+ * order
534
+ * @callback ComponentsReaderCallback
535
+ * @param {DataView} dataView Typed data view into a binary buffer
536
+ * @param {number} byteOffset The offset, in bytes, from the start of the view to read the data from
537
+ * @param {number} numberOfComponents The number of components to read
538
+ * @param {number[]} result The array in which to read the result
539
+ */
540
+
541
+ /**
542
+ * Creates a function that reads the specified number of components with
543
+ * the given type from the given data view, in little-endian
544
+ * order, and writes them into a given result array.
545
+ *
546
+ * @param {ComponentDatatype} componentType The component type
547
+ * @returns {ComponentsReaderCallback} The reader
548
+ */
549
+ static createComponentsReader(componentType) {
550
+ const componentReader = ModelReader.createComponentReader(componentType);
551
+ const sizeInBytes = ComponentDatatype.getSizeInBytes(componentType);
552
+ return function (dataView, byteOffset, numberOfComponents, result) {
553
+ let offset = byteOffset;
554
+ for (let i = 0; i < numberOfComponents; ++i) {
555
+ result[i] = componentReader(dataView, offset);
556
+ offset += sizeInBytes;
557
+ }
558
+ };
559
+ }
560
+
561
+ /**
562
+ * Reads and returns a value with the given type
563
+ * at the given byte offset from the data view, in little-endian
564
+ * order
565
+ * @callback ComponentReaderCallback
566
+ * @param {DataView} dataView Typed data view into a binary buffer
567
+ * @param {number} byteOffset The offset, in bytes, from the start of the view to read the data from
568
+ * @returns {number|BigInt} The value read from the dataView
569
+ */
570
+
571
+ /**
572
+ * Creates a function that reads and returns a value with the given type
573
+ * at the given byte offset from the data view, in little-endian
574
+ * order
575
+ * @param {ComponentDatatype} componentType The component type
576
+ * @returns {ComponentReaderCallback} The reader
577
+ */
578
+ static createComponentReader(componentType) {
579
+ switch (componentType) {
580
+ case ComponentDatatype.BYTE:
581
+ return function (dataView, byteOffset) {
582
+ return dataView.getInt8(byteOffset);
583
+ };
584
+ case ComponentDatatype.UNSIGNED_BYTE:
585
+ return function (dataView, byteOffset) {
586
+ return dataView.getUint8(byteOffset);
587
+ };
588
+ case ComponentDatatype.SHORT:
589
+ return function (dataView, byteOffset) {
590
+ return dataView.getInt16(byteOffset, true);
591
+ };
592
+ case ComponentDatatype.UNSIGNED_SHORT:
593
+ return function (dataView, byteOffset) {
594
+ return dataView.getUint16(byteOffset, true);
595
+ };
596
+ case ComponentDatatype.INT:
597
+ return function (dataView, byteOffset) {
598
+ return dataView.getInt32(byteOffset, true);
599
+ };
600
+ case ComponentDatatype.UNSIGNED_INT:
601
+ return function (dataView, byteOffset) {
602
+ return dataView.getUint32(byteOffset, true);
603
+ };
604
+ case ComponentDatatype.FLOAT:
605
+ return function (dataView, byteOffset) {
606
+ return dataView.getFloat32(byteOffset, true);
607
+ };
608
+ case ComponentDatatype.DOUBLE:
609
+ return function (dataView, byteOffset) {
610
+ return dataView.getFloat64(byteOffset, true);
611
+ };
612
+ }
613
+ throw new DeveloperError(
614
+ `The componentType must be a valid ComponentDatatype, but is ${componentType}`,
615
+ );
616
+ }
617
+
618
+ /**
619
+ * Transform the elements of the given array with the given 4x4 matrix,
620
+ * interpreting each 3 consecutive elements as a 3D point, and write
621
+ * the result into the given result array, creating the result array
622
+ * if it was undefined.
623
+ *
624
+ * @param {TypedArray} input The input array
625
+ * @param {Matrix4} matrix The matrix
626
+ * @param {TypedArray} [result] The result
627
+ * @returns {TypedArray} The result
628
+ */
629
+ static transform3D(input, matrix, result) {
630
+ //>>includeStart('debug', pragmas.debug);
631
+ Check.defined("input", input);
632
+ Check.defined("matrix", matrix);
633
+ //>>includeEnd('debug');
634
+
635
+ if (!defined(result)) {
636
+ result = new Float32Array(input.length);
637
+ }
638
+ const c = new Cartesian3();
639
+ const elementCount = input.length / 3;
640
+ for (let i = 0; i < elementCount; i++) {
641
+ Cartesian3.unpack(input, i * 3, c);
642
+ Matrix4.multiplyByPoint(matrix, c, c);
643
+ Cartesian3.pack(c, result, i * 3);
644
+ }
645
+ return result;
646
+ }
647
+
648
+ /**
649
+ * Read the indices values from the given primitive indices, and
650
+ * return them as a typed array.
651
+ *
652
+ * If the given object already has a <code>typedArray/code> property, then it
653
+ * is assumed that this contains the proper indices, and they are returned.
654
+ *
655
+ * Otherwise, this reads the data from the <code>buffer</code> of the given
656
+ * primitive indices object, into a typed array with a type that matches the
657
+ * <code>indexDataType</code>, and returns it.
658
+ *
659
+ * Clients may not modify the returned typed array.
660
+ *
661
+ * @param {ModelComponents.Indices} primitiveIndices The primitive indices
662
+ * @returns {TypedArray} The indices values
663
+ * @throws {DeveloperError} If the <code>indexDataType</code> of the given
664
+ * object is neither <code>UNSIGNED_BYTE</code>, nor <code>UNSIGNED_SHORT</code>,
665
+ * nor <code>UNSIGNED_INT</code>
666
+ */
667
+ static readIndicesAsTypedArray(primitiveIndices) {
668
+ const existingIndices = primitiveIndices.typedArray;
669
+ if (defined(existingIndices)) {
670
+ return existingIndices;
671
+ }
672
+ const indicesBuffer = primitiveIndices.buffer;
673
+ const indicesCount = primitiveIndices.count;
674
+ const indexDatatype = primitiveIndices.indexDatatype;
675
+ const indices = ModelReader.createIndexTypedArray(
676
+ indexDatatype,
677
+ indicesCount,
678
+ );
679
+ indicesBuffer.getBufferData(indices);
680
+ return indices;
681
+ }
682
+
683
+ /**
684
+ * Read the indices values from the given primitive indices object, and return
685
+ * them as a typed array of triangle vertex indices.
686
+ *
687
+ * If the given primitive type is <code>TRIANGLES</code>, then the indices
688
+ * values will be read from the given object, and returned.
689
+ *
690
+ * If the primitive type is <code>TRIANGLE_STRIP</code> or <code>TRIANGLE_FAN</code>,
691
+ * then the original indices values, will be read, converted into triangle indices
692
+ * (i.e. their equivalent <code>TRIANGLES</code> representation), and the result
693
+ * will be returned.
694
+ *
695
+ * The type of the returned array will match the <code>indexDataType</code>
696
+ * of the given object.
697
+ *
698
+ * Clients may not modify the returned typed array.
699
+ *
700
+ * @param {ModelComponents.Indices} primitiveIndices The primitive indices
701
+ * @returns {TypedArray} The indices, converted to triangle indices if necessary
702
+ * @throws {DeveloperError} If the <code>indexDataType</code> of the given
703
+ * object is neither <code>UNSIGNED_BYTE</code>, nor <code>UNSIGNED_SHORT</code>,
704
+ * nor <code>UNSIGNED_INT</code>, or the given <code>primitiveType</code>
705
+ * is neither <code>TRIANGLES</code>, nor <code>TRIANGLE_STRIP</code>,
706
+ * nor <code>TRIANGLE_FAN</code>
707
+ */
708
+ static readIndicesAsTriangleIndicesTypedArray(
709
+ primitiveIndices,
710
+ primitiveType,
711
+ ) {
712
+ const originalIndices =
713
+ ModelReader.readIndicesAsTypedArray(primitiveIndices);
714
+ if (primitiveType === PrimitiveType.TRIANGLES) {
715
+ return originalIndices;
716
+ }
717
+ if (primitiveType === PrimitiveType.TRIANGLE_STRIP) {
718
+ const triangleIndices =
719
+ ModelReader.convertTriangleStripToTriangleIndices(originalIndices);
720
+ return triangleIndices;
721
+ }
722
+ if (primitiveType === PrimitiveType.TRIANGLE_FAN) {
723
+ const triangleIndices =
724
+ ModelReader.convertTriangleFanToTriangleIndices(originalIndices);
725
+ return triangleIndices;
726
+ }
727
+ throw new DeveloperError(
728
+ `The primitiveType must be TRIANGLES (${PrimitiveType.TRIANGLES}, ` +
729
+ `TRIANGLE_STRIP (${PrimitiveType.TRIANGLE_STRIP}, or ` +
730
+ `TRIANGLE_FAN (${PrimitiveType.TRIANGLE_FAN}, but is ${primitiveType}`,
731
+ );
732
+ }
733
+
734
+ /**
735
+ * Converts the given indices from a <code>TRIANGLE_STRIP</code> representation
736
+ * into a <code>TRIANGLES</code> representation, and returns the result.
737
+ *
738
+ * The type of the result will be the same as the type of the input array.
739
+ *
740
+ * @param {TypedArray} indices The input indices
741
+ * @returns {TypedArray} The resulting triangle indices
742
+ */
743
+ static convertTriangleStripToTriangleIndices(indices) {
744
+ const triangleIndices = indices.constructor((indices.length - 2) * 3);
745
+ for (let i = 0; i < indices.length - 2; i++) {
746
+ if (i % 2 === 1) {
747
+ triangleIndices[i * 3 + 0] = indices[i + 0];
748
+ triangleIndices[i * 3 + 1] = indices[i + 2];
749
+ triangleIndices[i * 3 + 2] = indices[i + 1];
750
+ } else {
751
+ triangleIndices[i * 3 + 0] = indices[i + 0];
752
+ triangleIndices[i * 3 + 1] = indices[i + 1];
753
+ triangleIndices[i * 3 + 2] = indices[i + 2];
754
+ }
755
+ }
756
+ return triangleIndices;
757
+ }
758
+
759
+ /**
760
+ * Converts the given indices from a <code>TRIANGLE_FAN</code> representation
761
+ * into a <code>TRIANGLES</code> representation, and returns the result.
762
+ *
763
+ * The type of the result will be the same as the type of the input array.
764
+ *
765
+ * @param {TypedArray} indices The input indices
766
+ * @returns {TypedArray} The resulting triangle indices
767
+ */
768
+ static convertTriangleFanToTriangleIndices(indices) {
769
+ const triangleIndices = indices.constructor((indices.length - 2) * 3);
770
+ for (let i = 0; i < indices.length - 2; i++) {
771
+ triangleIndices[i * 3 + 0] = indices[i + 0];
772
+ triangleIndices[i * 3 + 1] = indices[i + 1];
773
+ triangleIndices[i * 3 + 2] = indices[i + 2];
774
+ }
775
+ return triangleIndices;
776
+ }
777
+
778
+ /**
779
+ * Create a typed array with a type that matches the given index data type,
780
+ * and the given size.
781
+ *
782
+ * @param {number} indexDatatype The <code>IndexDataType</code>
783
+ * @param {number} size The size of the array that will be created
784
+ * @returns {TypedArray} The typed array
785
+ * @throws {DeveloperError} If the <code>indexDataType</code> is neither
786
+ * <code>UNSIGNED_BYTE</code>, nor <code>UNSIGNED_SHORT</code>,
787
+ * nor <code>UNSIGNED_INT</code>, or the size is negative.
788
+ */
789
+ static createIndexTypedArray(indexDatatype, size) {
790
+ //>>includeStart('debug', pragmas.debug);
791
+ Check.typeOf.number.greaterThanOrEquals("size", size, 0);
792
+ //>>includeEnd('debug');
793
+
794
+ switch (indexDatatype) {
795
+ case IndexDatatype.UNSIGNED_BYTE:
796
+ return new Uint8Array(size);
797
+ case IndexDatatype.UNSIGNED_SHORT:
798
+ return new Uint16Array(size);
799
+ case IndexDatatype.UNSIGNED_INT:
800
+ return new Uint32Array(size);
801
+ }
802
+ throw new DeveloperError(
803
+ `The indexDatatype must be UNSIGNED_BYTE (${IndexDatatype.UNSIGNED_BYTE}, ` +
804
+ `UNSIGNED_SHORT (${IndexDatatype.UNSIGNED_SHORT}, or ` +
805
+ `UNSIGNED_INT (${IndexDatatype.UNSIGNED_INT}, but is ${indexDatatype}`,
806
+ );
807
+ }
808
+ }
809
+
810
+ export default ModelReader;