@loaders.gl/tile-converter 4.2.0-alpha.4 → 4.2.0-alpha.5

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 (144) hide show
  1. package/dist/3d-tiles-converter/3d-tiles-converter.d.ts +3 -3
  2. package/dist/3d-tiles-converter/3d-tiles-converter.d.ts.map +1 -1
  3. package/dist/3d-tiles-converter/3d-tiles-converter.js +329 -293
  4. package/dist/3d-tiles-converter/helpers/b3dm-converter.js +257 -200
  5. package/dist/3d-tiles-converter/helpers/i3s-obb-to-3d-tiles-obb.js +14 -5
  6. package/dist/3d-tiles-converter/helpers/load-i3s.js +106 -75
  7. package/dist/3d-tiles-converter/helpers/texture-atlas.js +44 -21
  8. package/dist/3d-tiles-converter/json-templates/tileset.js +32 -33
  9. package/dist/constants.js +0 -1
  10. package/dist/converter-cli.js +255 -234
  11. package/dist/converter.min.cjs +80 -90
  12. package/dist/deps-installer/deps-installer.js +72 -58
  13. package/dist/i3s-converter/helpers/attribute-metadata-info.js +207 -153
  14. package/dist/i3s-converter/helpers/batch-ids-extensions.d.ts +1 -1
  15. package/dist/i3s-converter/helpers/batch-ids-extensions.d.ts.map +1 -1
  16. package/dist/i3s-converter/helpers/batch-ids-extensions.js +145 -103
  17. package/dist/i3s-converter/helpers/coordinate-converter.js +100 -65
  18. package/dist/i3s-converter/helpers/create-scene-server-path.js +14 -9
  19. package/dist/i3s-converter/helpers/feature-attributes.js +168 -105
  20. package/dist/i3s-converter/helpers/geometry-attributes.d.ts +1 -1
  21. package/dist/i3s-converter/helpers/geometry-attributes.d.ts.map +1 -1
  22. package/dist/i3s-converter/helpers/geometry-attributes.js +204 -212
  23. package/dist/i3s-converter/helpers/geometry-converter.d.ts +2 -2
  24. package/dist/i3s-converter/helpers/geometry-converter.d.ts.map +1 -1
  25. package/dist/i3s-converter/helpers/geometry-converter.js +1148 -829
  26. package/dist/i3s-converter/helpers/gltf-attributes.d.ts +1 -1
  27. package/dist/i3s-converter/helpers/gltf-attributes.d.ts.map +1 -1
  28. package/dist/i3s-converter/helpers/gltf-attributes.js +111 -97
  29. package/dist/i3s-converter/helpers/load-3d-tiles.js +103 -66
  30. package/dist/i3s-converter/helpers/node-debug.js +98 -54
  31. package/dist/i3s-converter/helpers/node-index-document.d.ts +3 -3
  32. package/dist/i3s-converter/helpers/node-index-document.d.ts.map +1 -1
  33. package/dist/i3s-converter/helpers/node-index-document.js +248 -177
  34. package/dist/i3s-converter/helpers/node-pages.d.ts +1 -1
  35. package/dist/i3s-converter/helpers/node-pages.d.ts.map +1 -1
  36. package/dist/i3s-converter/helpers/node-pages.js +299 -194
  37. package/dist/i3s-converter/helpers/preprocess-3d-tiles.d.ts +1 -1
  38. package/dist/i3s-converter/helpers/preprocess-3d-tiles.d.ts.map +1 -1
  39. package/dist/i3s-converter/helpers/preprocess-3d-tiles.js +92 -60
  40. package/dist/i3s-converter/helpers/progress.js +134 -83
  41. package/dist/i3s-converter/helpers/tileset-traversal.d.ts +1 -1
  42. package/dist/i3s-converter/helpers/tileset-traversal.d.ts.map +1 -1
  43. package/dist/i3s-converter/helpers/tileset-traversal.js +24 -13
  44. package/dist/i3s-converter/i3s-converter.d.ts +7 -7
  45. package/dist/i3s-converter/i3s-converter.d.ts.map +1 -1
  46. package/dist/i3s-converter/i3s-converter.js +1044 -898
  47. package/dist/i3s-converter/json-templates/geometry-definitions.js +70 -79
  48. package/dist/i3s-converter/json-templates/layers.js +120 -121
  49. package/dist/i3s-converter/json-templates/metadata.js +19 -20
  50. package/dist/i3s-converter/json-templates/node.js +73 -71
  51. package/dist/i3s-converter/json-templates/scene-server.js +25 -26
  52. package/dist/i3s-converter/json-templates/shared-resources.js +107 -108
  53. package/dist/i3s-converter/json-templates/store.js +96 -94
  54. package/dist/i3s-converter/types.js +35 -23
  55. package/dist/i3s-server/app.js +15 -12
  56. package/dist/i3s-server/bin/www.js +14 -7
  57. package/dist/i3s-server/controllers/index-controller.js +18 -15
  58. package/dist/i3s-server/controllers/slpk-controller.js +22 -11
  59. package/dist/i3s-server/routes/index.js +9 -8
  60. package/dist/i3s-server/routes/slpk-router.js +18 -17
  61. package/dist/i3s-server/utils/create-scene-server.js +15 -10
  62. package/dist/i3s-server/utils/server-utils.js +49 -32
  63. package/dist/index.cjs +292 -851
  64. package/dist/index.cjs.map +7 -0
  65. package/dist/index.d.ts +2 -2
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +0 -1
  68. package/dist/lib/json-schemas/conversion-dump-json-schema.js +243 -421
  69. package/dist/lib/utils/cli-utils.js +63 -36
  70. package/dist/lib/utils/compress-util.js +20 -15
  71. package/dist/lib/utils/conversion-dump.d.ts +1 -1
  72. package/dist/lib/utils/conversion-dump.d.ts.map +1 -1
  73. package/dist/lib/utils/conversion-dump.js +209 -187
  74. package/dist/lib/utils/file-utils.js +122 -74
  75. package/dist/lib/utils/geometry-utils.js +13 -7
  76. package/dist/lib/utils/lod-conversion-utils.js +65 -33
  77. package/dist/lib/utils/queue.js +12 -13
  78. package/dist/lib/utils/statistic-utills.d.ts +6 -23
  79. package/dist/lib/utils/statistic-utills.js +64 -59
  80. package/dist/lib/utils/write-queue.d.ts +2 -2
  81. package/dist/lib/utils/write-queue.d.ts.map +1 -1
  82. package/dist/lib/utils/write-queue.js +68 -86
  83. package/dist/pgm-loader.js +16 -12
  84. package/dist/slpk-extractor/slpk-extractor.js +59 -50
  85. package/dist/slpk-extractor-cli.js +82 -59
  86. package/package.json +18 -18
  87. package/dist/3d-tiles-converter/3d-tiles-converter.js.map +0 -1
  88. package/dist/3d-tiles-converter/helpers/b3dm-converter.js.map +0 -1
  89. package/dist/3d-tiles-converter/helpers/i3s-obb-to-3d-tiles-obb.js.map +0 -1
  90. package/dist/3d-tiles-converter/helpers/load-i3s.js.map +0 -1
  91. package/dist/3d-tiles-converter/helpers/texture-atlas.js.map +0 -1
  92. package/dist/3d-tiles-converter/json-templates/tileset.js.map +0 -1
  93. package/dist/constants.js.map +0 -1
  94. package/dist/converter-cli.js.map +0 -1
  95. package/dist/deps-installer/deps-installer.js.map +0 -1
  96. package/dist/i3s-converter/helpers/attribute-metadata-info.js.map +0 -1
  97. package/dist/i3s-converter/helpers/batch-ids-extensions.js.map +0 -1
  98. package/dist/i3s-converter/helpers/coordinate-converter.js.map +0 -1
  99. package/dist/i3s-converter/helpers/create-scene-server-path.js.map +0 -1
  100. package/dist/i3s-converter/helpers/feature-attributes.js.map +0 -1
  101. package/dist/i3s-converter/helpers/geometry-attributes.js.map +0 -1
  102. package/dist/i3s-converter/helpers/geometry-converter.js.map +0 -1
  103. package/dist/i3s-converter/helpers/gltf-attributes.js.map +0 -1
  104. package/dist/i3s-converter/helpers/load-3d-tiles.js.map +0 -1
  105. package/dist/i3s-converter/helpers/node-debug.js.map +0 -1
  106. package/dist/i3s-converter/helpers/node-index-document.js.map +0 -1
  107. package/dist/i3s-converter/helpers/node-pages.js.map +0 -1
  108. package/dist/i3s-converter/helpers/preprocess-3d-tiles.js.map +0 -1
  109. package/dist/i3s-converter/helpers/progress.js.map +0 -1
  110. package/dist/i3s-converter/helpers/tileset-traversal.js.map +0 -1
  111. package/dist/i3s-converter/i3s-converter.js.map +0 -1
  112. package/dist/i3s-converter/json-templates/geometry-definitions.js.map +0 -1
  113. package/dist/i3s-converter/json-templates/layers.js.map +0 -1
  114. package/dist/i3s-converter/json-templates/metadata.js.map +0 -1
  115. package/dist/i3s-converter/json-templates/node.js.map +0 -1
  116. package/dist/i3s-converter/json-templates/scene-server.js.map +0 -1
  117. package/dist/i3s-converter/json-templates/shared-resources.js.map +0 -1
  118. package/dist/i3s-converter/json-templates/store.js.map +0 -1
  119. package/dist/i3s-converter/types.js.map +0 -1
  120. package/dist/i3s-server/README.md +0 -63
  121. package/dist/i3s-server/app.js.map +0 -1
  122. package/dist/i3s-server/bin/www.js.map +0 -1
  123. package/dist/i3s-server/certs/cert.pem +0 -19
  124. package/dist/i3s-server/certs/key.pem +0 -27
  125. package/dist/i3s-server/controllers/index-controller.js.map +0 -1
  126. package/dist/i3s-server/controllers/slpk-controller.js.map +0 -1
  127. package/dist/i3s-server/routes/index.js.map +0 -1
  128. package/dist/i3s-server/routes/slpk-router.js.map +0 -1
  129. package/dist/i3s-server/utils/create-scene-server.js.map +0 -1
  130. package/dist/i3s-server/utils/server-utils.js.map +0 -1
  131. package/dist/index.js.map +0 -1
  132. package/dist/lib/json-schemas/conversion-dump-json-schema.js.map +0 -1
  133. package/dist/lib/utils/cli-utils.js.map +0 -1
  134. package/dist/lib/utils/compress-util.js.map +0 -1
  135. package/dist/lib/utils/conversion-dump.js.map +0 -1
  136. package/dist/lib/utils/file-utils.js.map +0 -1
  137. package/dist/lib/utils/geometry-utils.js.map +0 -1
  138. package/dist/lib/utils/lod-conversion-utils.js.map +0 -1
  139. package/dist/lib/utils/queue.js.map +0 -1
  140. package/dist/lib/utils/statistic-utills.js.map +0 -1
  141. package/dist/lib/utils/write-queue.js.map +0 -1
  142. package/dist/pgm-loader.js.map +0 -1
  143. package/dist/slpk-extractor/slpk-extractor.js.map +0 -1
  144. package/dist/slpk-extractor-cli.js.map +0 -1
@@ -13,6 +13,7 @@ import { checkPropertiesLength, flattenPropertyTableByFeatureIds } from "./featu
13
13
  import { GL } from '@loaders.gl/math';
14
14
  import { generateSyntheticIndices } from "../../lib/utils/geometry-utils.js";
15
15
  import { EXT_FEATURE_METADATA, EXT_STRUCTURAL_METADATA } from '@loaders.gl/gltf';
16
+ // Spec - https://github.com/Esri/i3s-spec/blob/master/docs/1.7/pbrMetallicRoughness.cmn.md
16
17
  const DEFAULT_ROUGHNESS_FACTOR = 1;
17
18
  const DEFAULT_METALLIC_FACTOR = 1;
18
19
  const VALUES_PER_VERTEX = 3;
@@ -22,927 +23,1245 @@ const STRING_TYPE = 'string';
22
23
  const SHORT_INT_TYPE = 'Int32';
23
24
  const DOUBLE_TYPE = 'Float64';
24
25
  const OBJECT_ID_TYPE = 'Oid32';
26
+ /*
27
+ * 'CUSTOM_ATTRIBUTE_2' - Attribute name which includes batch info and used by New York map.
28
+ * _BATCHID - Default attribute name which includes batch info.
29
+ * BATCHID - Legacy attribute name which includes batch info.
30
+ */
25
31
  const BATCHED_ID_POSSIBLE_ATTRIBUTE_NAMES = ['CUSTOM_ATTRIBUTE_2', '_BATCHID', 'BATCHID'];
26
32
  let scratchVector = new Vector3();
33
+ /**
34
+ * Convert binary data from b3dm file to i3s resources
35
+ *
36
+ * @param tileContent - 3d tile content
37
+ * @param tileTransform - transformation matrix of the tile, calculated recursively multiplying
38
+ * transform of all parent tiles and transform of the current tile
39
+ * @param tileBoundingVolume - initialized bounding volume of the source tile
40
+ * @param addNodeToNodePage - function to add new node to node pages
41
+ * @param propertyTable - batch table (corresponding to feature attributes data)
42
+ * @param featuresHashArray - hash array of features that is needed to not to mix up same features in parent and child nodes
43
+ * @param attributeStorageInfo - attributes metadata from 3DSceneLayer json
44
+ * @param draco - is converter should create draco compressed geometry
45
+ * @param generateBoundingVolumes - is converter should create accurate bounding voulmes from geometry attributes
46
+ * @param shouldMergeMaterials - Try to merge similar materials to be able to merge meshes into one node
47
+ * @param geoidHeightModel - model to convert elevation from elipsoidal to geoid
48
+ * @param libraries - dynamicaly loaded 3rd-party libraries
49
+ * @param metadataClass `- user selected feature metadata class name`
50
+ * @returns Array of node resources to create one or more i3s nodes
51
+ */
27
52
  export default async function convertB3dmToI3sGeometry(tileContent, tileTransform, tileBoundingVolume, addNodeToNodePage, propertyTable, featuresHashArray, attributeStorageInfo, draco, generateBoundingVolumes, shouldMergeMaterials, geoidHeightModel, libraries, metadataClass) {
28
- var _tileContent$gltf;
29
- const useCartesianPositions = generateBoundingVolumes;
30
- const materialAndTextureList = await convertMaterials((_tileContent$gltf = tileContent.gltf) === null || _tileContent$gltf === void 0 ? void 0 : _tileContent$gltf.materials, shouldMergeMaterials);
31
- const dataForAttributesConversion = prepareDataForAttributesConversion(tileContent, tileTransform, tileBoundingVolume);
32
- const featureTexture = getTextureByMetadataClass(tileContent, metadataClass);
33
- const convertedAttributesMap = await convertAttributes(dataForAttributesConversion, materialAndTextureList, useCartesianPositions, featureTexture);
34
- if (generateBoundingVolumes) {
35
- _generateBoundingVolumesFromGeometry(convertedAttributesMap, geoidHeightModel);
36
- }
37
- const result = [];
38
- for (const materialAndTexture of materialAndTextureList) {
39
- const originarMaterialId = materialAndTexture.mergedMaterials[0].originalMaterialId;
40
- if (!convertedAttributesMap.has(originarMaterialId)) {
41
- continue;
42
- }
43
- const convertedAttributes = convertedAttributesMap.get(originarMaterialId);
44
- if (!convertedAttributes) {
45
- continue;
46
- }
47
- const {
48
- material,
49
- texture
50
- } = materialAndTexture;
51
- const nodeId = await addNodeToNodePage();
52
- result.push(await _makeNodeResources({
53
- convertedAttributes,
54
- material,
55
- texture,
56
- tileContent,
57
- nodeId,
58
- featuresHashArray,
59
- propertyTable,
60
- attributeStorageInfo,
61
- draco,
62
- libraries
63
- }));
64
- }
65
- if (!result.length) {
66
- return null;
67
- }
68
- return result;
53
+ const useCartesianPositions = generateBoundingVolumes;
54
+ const materialAndTextureList = await convertMaterials(tileContent.gltf?.materials, shouldMergeMaterials);
55
+ const dataForAttributesConversion = prepareDataForAttributesConversion(tileContent, tileTransform, tileBoundingVolume);
56
+ const featureTexture = getTextureByMetadataClass(tileContent, metadataClass);
57
+ const convertedAttributesMap = await convertAttributes(dataForAttributesConversion, materialAndTextureList, useCartesianPositions, featureTexture);
58
+ /** Usage of worker here brings more overhead than advantage */
59
+ // const convertedAttributesMap: Map<string, ConvertedAttributes> =
60
+ // await transformI3SAttributesOnWorker(dataForAttributesConversion, {
61
+ // reuseWorkers: true,
62
+ // _nodeWorkers: true,
63
+ // useCartesianPositions,
64
+ // source: workerSource.I3SAttributes
65
+ // });
66
+ if (generateBoundingVolumes) {
67
+ _generateBoundingVolumesFromGeometry(convertedAttributesMap, geoidHeightModel);
68
+ }
69
+ const result = [];
70
+ for (const materialAndTexture of materialAndTextureList) {
71
+ const originarMaterialId = materialAndTexture.mergedMaterials[0].originalMaterialId;
72
+ if (!convertedAttributesMap.has(originarMaterialId)) {
73
+ continue; // eslint-disable-line no-continue
74
+ }
75
+ const convertedAttributes = convertedAttributesMap.get(originarMaterialId);
76
+ if (!convertedAttributes) {
77
+ continue;
78
+ }
79
+ const { material, texture } = materialAndTexture;
80
+ const nodeId = await addNodeToNodePage();
81
+ result.push(await _makeNodeResources({
82
+ convertedAttributes,
83
+ material,
84
+ texture,
85
+ tileContent,
86
+ nodeId,
87
+ featuresHashArray,
88
+ propertyTable,
89
+ attributeStorageInfo,
90
+ draco,
91
+ libraries
92
+ }));
93
+ }
94
+ if (!result.length) {
95
+ return null;
96
+ }
97
+ return result;
69
98
  }
99
+ /**
100
+ * Create bounding volumes based on positions
101
+ * @param convertedAttributesMap - geometry attributes map
102
+ * @param geoidHeightModel - geoid height model to convert elevation from elipsoidal to geoid
103
+ */
70
104
  function _generateBoundingVolumesFromGeometry(convertedAttributesMap, geoidHeightModel) {
71
- for (const attributes of convertedAttributesMap.values()) {
72
- const boundingVolumes = createBoundingVolumesFromGeometry(attributes.positions, geoidHeightModel);
73
- attributes.boundingVolumes = boundingVolumes;
74
- const cartographicOrigin = boundingVolumes.obb.center;
75
- for (let index = 0; index < attributes.positions.length; index += VALUES_PER_VERTEX) {
76
- const vertex = attributes.positions.subarray(index, index + VALUES_PER_VERTEX);
77
- Ellipsoid.WGS84.cartesianToCartographic(Array.from(vertex), scratchVector);
78
- scratchVector[2] = scratchVector[2] - geoidHeightModel.getHeight(scratchVector[1], scratchVector[0]);
79
- scratchVector = scratchVector.subtract(cartographicOrigin);
80
- attributes.positions.set(scratchVector, index);
81
- }
82
- }
83
- }
84
- async function _makeNodeResources(_ref) {
85
- var _tileContent$gltf2;
86
- let {
87
- convertedAttributes,
88
- material,
89
- texture,
90
- tileContent,
91
- nodeId,
92
- featuresHashArray,
93
- propertyTable,
94
- attributeStorageInfo,
95
- draco,
96
- libraries
97
- } = _ref;
98
- const boundingVolumes = convertedAttributes.boundingVolumes;
99
- const vertexCount = convertedAttributes.positions.length / VALUES_PER_VERTEX;
100
- const {
101
- faceRange,
102
- featureIds,
103
- positions,
104
- normals,
105
- colors,
106
- uvRegions,
107
- texCoords,
108
- featureCount
109
- } = generateAttributes(convertedAttributes);
110
- let featureIdsMap = {};
111
- if (propertyTable) {
112
- featureIdsMap = makeFeatureIdsUnique(featureIds, convertedAttributes.featureIndices, featuresHashArray, propertyTable);
113
- }
114
- const header = new Uint32Array(2);
115
- const typedFeatureIds = generateBigUint64Array(featureIds);
116
- header.set([vertexCount, featureCount], 0);
117
- const fileBuffer = new Uint8Array(concatenateArrayBuffers(header.buffer, positions.buffer, normals.buffer, texture ? texCoords.buffer : new ArrayBuffer(0), colors.buffer, uvRegions, typedFeatureIds.buffer, faceRange.buffer));
118
- const compressedGeometry = draco ? generateCompressedGeometry(vertexCount, convertedAttributes, {
119
- positions,
120
- normals,
121
- texCoords: texture ? texCoords : new Float32Array(0),
122
- colors,
123
- uvRegions,
124
- featureIds,
125
- faceRange
126
- }, libraries) : null;
127
- let attributes = [];
128
- if (attributeStorageInfo && propertyTable) {
129
- attributes = convertPropertyTableToAttributeBuffers(featureIds, featureIdsMap, propertyTable, attributeStorageInfo);
130
- }
131
- return {
132
- nodeId,
133
- geometry: fileBuffer,
134
- compressedGeometry,
135
- texture,
136
- hasUvRegions: Boolean(uvRegions.length),
137
- sharedResources: getSharedResources(((_tileContent$gltf2 = tileContent.gltf) === null || _tileContent$gltf2 === void 0 ? void 0 : _tileContent$gltf2.materials) || [], nodeId),
138
- meshMaterial: material,
139
- vertexCount,
140
- attributes,
141
- featureCount,
142
- boundingVolumes
143
- };
105
+ for (const attributes of convertedAttributesMap.values()) {
106
+ const boundingVolumes = createBoundingVolumesFromGeometry(attributes.positions, geoidHeightModel);
107
+ attributes.boundingVolumes = boundingVolumes;
108
+ const cartographicOrigin = boundingVolumes.obb.center;
109
+ for (let index = 0; index < attributes.positions.length; index += VALUES_PER_VERTEX) {
110
+ const vertex = attributes.positions.subarray(index, index + VALUES_PER_VERTEX);
111
+ Ellipsoid.WGS84.cartesianToCartographic(Array.from(vertex), scratchVector);
112
+ scratchVector[2] =
113
+ scratchVector[2] - geoidHeightModel.getHeight(scratchVector[1], scratchVector[0]);
114
+ scratchVector = scratchVector.subtract(cartographicOrigin);
115
+ attributes.positions.set(scratchVector, index);
116
+ }
117
+ }
144
118
  }
145
- export async function convertAttributes(attributesData, materialAndTextureList, useCartesianPositions, featureTexture) {
146
- const {
147
- nodes,
148
- images,
149
- cartographicOrigin,
150
- cartesianModelMatrix
151
- } = attributesData;
152
- const attributesMap = new Map();
153
- for (const materialAndTexture of materialAndTextureList) {
154
- const attributes = {
155
- positions: new Float32Array(0),
156
- normals: new Float32Array(0),
157
- texCoords: new Float32Array(0),
158
- colors: new Uint8Array(0),
159
- uvRegions: new Uint16Array(0),
160
- featureIndicesGroups: [],
161
- featureIndices: [],
162
- boundingVolumes: null,
163
- mergedMaterials: materialAndTexture.mergedMaterials
164
- };
165
- for (const mergedMaterial of materialAndTexture.mergedMaterials) {
166
- attributesMap.set(mergedMaterial.originalMaterialId, attributes);
119
+ /**
120
+ *
121
+ * @param params
122
+ * @param params.convertedAttributes - Converted geometry attributes
123
+ * @param params.material - I3S PBR-like material definition
124
+ * @param params.texture - texture content
125
+ * @param params.tileContent - 3DTiles decoded content
126
+ * @param params.nodeId - new node ID
127
+ * @param params.featuresHashArray - hash array of features that is needed to not to mix up same features in parent and child nodes
128
+ * @param params.propertyTable - batch table (corresponding to feature attributes data)
129
+ * @param params.attributeStorageInfo - attributes metadata from 3DSceneLayer json
130
+ * @param params.draco - is converter should create draco compressed geometry
131
+ * @param libraries - dynamicaly loaded 3rd-party libraries
132
+ * @returns Array of I3S node resources
133
+ */
134
+ async function _makeNodeResources({ convertedAttributes, material, texture, tileContent, nodeId, featuresHashArray, propertyTable, attributeStorageInfo, draco, libraries }) {
135
+ const boundingVolumes = convertedAttributes.boundingVolumes;
136
+ const vertexCount = convertedAttributes.positions.length / VALUES_PER_VERTEX;
137
+ const { faceRange, featureIds, positions, normals, colors, uvRegions, texCoords, featureCount } = generateAttributes(convertedAttributes);
138
+ let featureIdsMap = {};
139
+ if (propertyTable) {
140
+ /**
141
+ * 3DTiles has featureIndices unique only for one tile.
142
+ * In I3S featureIds are unique layer-wide. We create featureIds from all feature properties.
143
+ * If 3DTiles features has equal set of properties they are considered as same feature in I3S.
144
+ */
145
+ featureIdsMap = makeFeatureIdsUnique(featureIds, convertedAttributes.featureIndices, featuresHashArray, propertyTable);
167
146
  }
168
- }
169
- convertNodes(nodes, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, undefined, featureTexture);
170
- for (const attrKey of attributesMap.keys()) {
171
- const attributes = attributesMap.get(attrKey);
172
- if (!attributes) {
173
- continue;
147
+ const header = new Uint32Array(2);
148
+ const typedFeatureIds = generateBigUint64Array(featureIds);
149
+ header.set([vertexCount, featureCount], 0);
150
+ const fileBuffer = new Uint8Array(concatenateArrayBuffers(header.buffer, positions.buffer, normals.buffer, texture ? texCoords.buffer : new ArrayBuffer(0), colors.buffer, uvRegions, typedFeatureIds.buffer, faceRange.buffer));
151
+ const compressedGeometry = draco
152
+ ? generateCompressedGeometry(vertexCount, convertedAttributes, {
153
+ positions,
154
+ normals,
155
+ texCoords: texture ? texCoords : new Float32Array(0),
156
+ colors,
157
+ uvRegions,
158
+ featureIds,
159
+ faceRange
160
+ }, libraries)
161
+ : null;
162
+ let attributes = [];
163
+ if (attributeStorageInfo && propertyTable) {
164
+ attributes = convertPropertyTableToAttributeBuffers(featureIds, featureIdsMap, propertyTable, attributeStorageInfo);
174
165
  }
175
- if (attributes.positions.length === 0) {
176
- attributesMap.delete(attrKey);
177
- continue;
166
+ return {
167
+ nodeId,
168
+ geometry: fileBuffer,
169
+ compressedGeometry,
170
+ texture,
171
+ hasUvRegions: Boolean(uvRegions.length),
172
+ sharedResources: getSharedResources(tileContent.gltf?.materials || [], nodeId),
173
+ meshMaterial: material,
174
+ vertexCount,
175
+ attributes,
176
+ featureCount,
177
+ boundingVolumes
178
+ };
179
+ }
180
+ /**
181
+ * Convert attributes from the gltf nodes tree to i3s plain geometry
182
+ * @param attributesData - geometry attributes from gltf
183
+ * @param materialAndTextureList - array of data about materials and textures of the content
184
+ * @param useCartesianPositions - convert positions to absolute cartesian coordinates instead of cartographic offsets.
185
+ * Cartesian coordinates will be required for creating bounding voulmest from geometry positions
186
+ * @param featureTexture - feature texture key
187
+ * @returns map of converted geometry attributes
188
+ */
189
+ export async function convertAttributes(attributesData, materialAndTextureList, useCartesianPositions, featureTexture) {
190
+ const { nodes, images, cartographicOrigin, cartesianModelMatrix } = attributesData;
191
+ const attributesMap = new Map();
192
+ for (const materialAndTexture of materialAndTextureList) {
193
+ const attributes = {
194
+ positions: new Float32Array(0),
195
+ normals: new Float32Array(0),
196
+ texCoords: new Float32Array(0),
197
+ colors: new Uint8Array(0),
198
+ uvRegions: new Uint16Array(0),
199
+ featureIndicesGroups: [],
200
+ featureIndices: [],
201
+ boundingVolumes: null,
202
+ mergedMaterials: materialAndTexture.mergedMaterials
203
+ };
204
+ for (const mergedMaterial of materialAndTexture.mergedMaterials) {
205
+ attributesMap.set(mergedMaterial.originalMaterialId, attributes);
206
+ }
178
207
  }
179
- if (attributes.featureIndicesGroups) {
180
- attributes.featureIndices = attributes.featureIndicesGroups.reduce((acc, value) => acc.concat(value));
181
- delete attributes.featureIndicesGroups;
208
+ convertNodes(nodes, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, undefined, featureTexture);
209
+ for (const attrKey of attributesMap.keys()) {
210
+ const attributes = attributesMap.get(attrKey);
211
+ if (!attributes) {
212
+ continue;
213
+ }
214
+ if (attributes.positions.length === 0) {
215
+ attributesMap.delete(attrKey);
216
+ continue; // eslint-disable-line no-continue
217
+ }
218
+ if (attributes.featureIndicesGroups) {
219
+ attributes.featureIndices = attributes.featureIndicesGroups.reduce((acc, value) => acc.concat(value));
220
+ delete attributes.featureIndicesGroups;
221
+ }
182
222
  }
183
- }
184
- return attributesMap;
223
+ return attributesMap;
185
224
  }
186
- function convertNodes(nodes, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions) {
187
- let matrix = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : new Matrix4([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
188
- let featureTexture = arguments.length > 7 ? arguments[7] : undefined;
189
- if (nodes) {
190
- for (const node of nodes) {
191
- convertNode(node, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, matrix, featureTexture);
225
+ /**
226
+ * glTF has hierarchical structure of nodes. This function converts nodes starting from those which are in gltf scene object.
227
+ * The goal is applying tranformation matrix for all children. Functions "convertNodes" and "convertNode" work together recursively.
228
+ * @param nodes - gltf nodes array
229
+ * @param images - gltf images array
230
+ * @param cartographicOrigin - cartographic origin of bounding volume
231
+ * @param cartesianModelMatrix - cartesian model matrix to convert coordinates to cartographic
232
+ * @param attributesMap - for recursive concatenation of attributes
233
+ * @param useCartesianPositions - convert positions to absolute cartesian coordinates instead of cartographic offsets.
234
+ * Cartesian coordinates will be required for creating bounding voulmest from geometry positions
235
+ * @param matrix - transformation matrix - cumulative transformation matrix formed from all parent node matrices
236
+ * @param featureTexture - feature texture key
237
+ * @returns {void}
238
+ */
239
+ function convertNodes(nodes, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, matrix = new Matrix4([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]), featureTexture) {
240
+ if (nodes) {
241
+ for (const node of nodes) {
242
+ convertNode(node, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, matrix, featureTexture);
243
+ }
192
244
  }
193
- }
194
245
  }
246
+ /**
247
+ * Generate transformation matrix for node
248
+ * Aapply all gltf transformations to initial transformation matrix.
249
+ * @param node
250
+ * @param matrix
251
+ */
195
252
  function getCompositeTransformationMatrix(node, matrix) {
196
- let transformationMatrix = matrix;
197
- const {
198
- matrix: nodeMatrix,
199
- rotation,
200
- scale,
201
- translation
202
- } = node;
203
- if (nodeMatrix) {
204
- transformationMatrix = matrix.multiplyRight(nodeMatrix);
205
- }
206
- if (translation) {
207
- transformationMatrix = transformationMatrix.translate(translation);
208
- }
209
- if (rotation) {
210
- transformationMatrix = transformationMatrix.rotateXYZ(rotation);
211
- }
212
- if (scale) {
213
- transformationMatrix = transformationMatrix.scale(scale);
214
- }
215
- return transformationMatrix;
216
- }
217
- function convertNode(node, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions) {
218
- let matrix = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : new Matrix4([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
219
- let featureTexture = arguments.length > 7 ? arguments[7] : undefined;
220
- const transformationMatrix = getCompositeTransformationMatrix(node, matrix);
221
- const mesh = node.mesh;
222
- if (mesh) {
223
- convertMesh(mesh, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, transformationMatrix, featureTexture);
224
- }
225
- convertNodes(node.children || [], images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, transformationMatrix, featureTexture);
226
- }
227
- function convertMesh(mesh, images, cartographicOrigin, cartesianModelMatrix, attributesMap) {
228
- let useCartesianPositions = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : false;
229
- let matrix = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : new Matrix4([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
230
- let featureTexture = arguments.length > 7 ? arguments[7] : undefined;
231
- for (const primitive of mesh.primitives) {
232
- let outputAttributes = null;
233
- let materialUvRegion;
234
- if (primitive.material) {
235
- var _outputAttributes, _outputAttributes$mer;
236
- outputAttributes = attributesMap.get(primitive.material.id);
237
- materialUvRegion = (_outputAttributes = outputAttributes) === null || _outputAttributes === void 0 ? void 0 : (_outputAttributes$mer = _outputAttributes.mergedMaterials.find(_ref2 => {
238
- var _primitive$material;
239
- let {
240
- originalMaterialId
241
- } = _ref2;
242
- return originalMaterialId === ((_primitive$material = primitive.material) === null || _primitive$material === void 0 ? void 0 : _primitive$material.id);
243
- })) === null || _outputAttributes$mer === void 0 ? void 0 : _outputAttributes$mer.uvRegion;
244
- } else if (attributesMap.has('default')) {
245
- outputAttributes = attributesMap.get('default');
246
- }
247
- assert(outputAttributes !== null, 'Primitive - material mapping failed');
248
- assert(primitive.mode === undefined || primitive.mode === GL.TRIANGLES || primitive.mode === GL.TRIANGLE_STRIP, `Primitive - unsupported mode ${primitive.mode}`);
249
- const attributes = primitive.attributes;
250
- if (!outputAttributes) {
251
- continue;
252
- }
253
- const indices = normalizeIndices(primitive);
254
- outputAttributes.positions = concatenateTypedArrays(outputAttributes.positions, transformVertexArray({
255
- vertices: attributes.POSITION.value,
256
- cartographicOrigin,
257
- cartesianModelMatrix,
258
- nodeMatrix: matrix,
259
- indices,
260
- attributeSpecificTransformation: transformVertexPositions,
261
- useCartesianPositions
262
- }));
263
- outputAttributes.normals = concatenateTypedArrays(outputAttributes.normals, transformVertexArray({
264
- vertices: attributes.NORMAL && attributes.NORMAL.value,
265
- cartographicOrigin,
266
- cartesianModelMatrix,
267
- nodeMatrix: matrix,
268
- indices,
269
- attributeSpecificTransformation: transformVertexNormals,
270
- useCartesianPositions: false
271
- }));
272
- outputAttributes.texCoords = concatenateTypedArrays(outputAttributes.texCoords, flattenTexCoords(attributes.TEXCOORD_0 && attributes.TEXCOORD_0.value, indices));
273
- outputAttributes.colors = concatenateTypedArrays(outputAttributes.colors, flattenColors(attributes.COLOR_0, indices));
274
- if (materialUvRegion) {
275
- outputAttributes.uvRegions = concatenateTypedArrays(outputAttributes.uvRegions, createUvRegion(materialUvRegion, indices));
276
- }
277
- outputAttributes.featureIndicesGroups = outputAttributes.featureIndicesGroups || [];
278
- outputAttributes.featureIndicesGroups.push(flattenBatchIds(getBatchIds(attributes, primitive, images, featureTexture), indices));
279
- }
253
+ let transformationMatrix = matrix;
254
+ const { matrix: nodeMatrix, rotation, scale, translation } = node;
255
+ if (nodeMatrix) {
256
+ transformationMatrix = matrix.multiplyRight(nodeMatrix);
257
+ }
258
+ if (translation) {
259
+ transformationMatrix = transformationMatrix.translate(translation);
260
+ }
261
+ if (rotation) {
262
+ transformationMatrix = transformationMatrix.rotateXYZ(rotation);
263
+ }
264
+ if (scale) {
265
+ transformationMatrix = transformationMatrix.scale(scale);
266
+ }
267
+ return transformationMatrix;
280
268
  }
269
+ /**
270
+ * Convert all primitives of node and all children nodes
271
+ * @param node - gltf node
272
+ * @param images - gltf images array
273
+ * @param cartographicOrigin - cartographic origin of bounding volume
274
+ * @param cartesianModelMatrix - cartesian model matrix to convert coordinates to cartographic
275
+ * @param attributesMap Map<{positions: Float32Array, normals: Float32Array, texCoords: Float32Array, colors: Uint8Array, featureIndices: Array}> - for recursive concatenation of
276
+ * attributes
277
+ * @param useCartesianPositions - convert positions to absolute cartesian coordinates instead of cartographic offsets.
278
+ * Cartesian coordinates will be required for creating bounding voulmest from geometry positions
279
+ * @param matrix - transformation matrix - cumulative transformation matrix formed from all parent node matrices
280
+ * @param featureTexture - feature texture key
281
+ */
282
+ function convertNode(node, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, matrix = new Matrix4([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]), featureTexture) {
283
+ const transformationMatrix = getCompositeTransformationMatrix(node, matrix);
284
+ const mesh = node.mesh;
285
+ if (mesh) {
286
+ convertMesh(mesh, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, transformationMatrix, featureTexture);
287
+ }
288
+ convertNodes(node.children || [], images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions, transformationMatrix, featureTexture);
289
+ }
290
+ /**
291
+ * Convert all primitives of the mesh
292
+ * @param mesh - gltf mesh data
293
+ * @param images - gltf images array
294
+ * @param cartographicOrigin - cartographic origin of bounding volume
295
+ * @param cartesianModelMatrix - cartesian model matrix to convert coordinates to cartographic
296
+ * @param attributesMap Map<{positions: Float32Array, normals: Float32Array, texCoords: Float32Array, colors: Uint8Array, featureIndices: Array}> - for recursive concatenation of
297
+ * attributes
298
+ * @param useCartesianPositions - convert positions to absolute cartesian coordinates instead of cartographic offsets.
299
+ * Cartesian coordinates will be required for creating bounding voulmest from geometry positions
300
+ * @param attributesMap Map<{positions: Float32Array, normals: Float32Array, texCoords: Float32Array, colors: Uint8Array, featureIndices: Array}> - for recursive concatenation of
301
+ * attributes
302
+ * @param matrix - transformation matrix - cumulative transformation matrix formed from all parent node matrices
303
+ * @param featureTexture - feature texture key
304
+ */
305
+ function convertMesh(mesh, images, cartographicOrigin, cartesianModelMatrix, attributesMap, useCartesianPositions = false, matrix = new Matrix4([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]), featureTexture) {
306
+ for (const primitive of mesh.primitives) {
307
+ let outputAttributes = null;
308
+ let materialUvRegion;
309
+ if (primitive.material) {
310
+ outputAttributes = attributesMap.get(primitive.material.id);
311
+ materialUvRegion = outputAttributes?.mergedMaterials.find(({ originalMaterialId }) => originalMaterialId === primitive.material?.id)?.uvRegion;
312
+ }
313
+ else if (attributesMap.has('default')) {
314
+ outputAttributes = attributesMap.get('default');
315
+ }
316
+ assert(outputAttributes !== null, 'Primitive - material mapping failed');
317
+ // Per the spec https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#_mesh_primitive_mode
318
+ // GL.TRIANGLES is default. So in case `mode` is `undefined`, it is 'TRIANGLES'
319
+ assert(primitive.mode === undefined ||
320
+ primitive.mode === GL.TRIANGLES ||
321
+ primitive.mode === GL.TRIANGLE_STRIP, `Primitive - unsupported mode ${primitive.mode}`);
322
+ const attributes = primitive.attributes;
323
+ if (!outputAttributes) {
324
+ continue;
325
+ }
326
+ const indices = normalizeIndices(primitive);
327
+ outputAttributes.positions = concatenateTypedArrays(outputAttributes.positions, transformVertexArray({
328
+ vertices: attributes.POSITION.value,
329
+ cartographicOrigin,
330
+ cartesianModelMatrix,
331
+ nodeMatrix: matrix,
332
+ indices,
333
+ attributeSpecificTransformation: transformVertexPositions,
334
+ useCartesianPositions
335
+ }));
336
+ outputAttributes.normals = concatenateTypedArrays(outputAttributes.normals, transformVertexArray({
337
+ vertices: attributes.NORMAL && attributes.NORMAL.value,
338
+ cartographicOrigin,
339
+ cartesianModelMatrix,
340
+ nodeMatrix: matrix,
341
+ indices,
342
+ attributeSpecificTransformation: transformVertexNormals,
343
+ useCartesianPositions: false
344
+ }));
345
+ outputAttributes.texCoords = concatenateTypedArrays(outputAttributes.texCoords, flattenTexCoords(attributes.TEXCOORD_0 && attributes.TEXCOORD_0.value, indices));
346
+ outputAttributes.colors = concatenateTypedArrays(outputAttributes.colors, flattenColors(attributes.COLOR_0, indices));
347
+ if (materialUvRegion) {
348
+ outputAttributes.uvRegions = concatenateTypedArrays(outputAttributes.uvRegions, createUvRegion(materialUvRegion, indices));
349
+ }
350
+ outputAttributes.featureIndicesGroups = outputAttributes.featureIndicesGroups || [];
351
+ outputAttributes.featureIndicesGroups.push(flattenBatchIds(getBatchIds(attributes, primitive, images, featureTexture), indices));
352
+ }
353
+ }
354
+ /**
355
+ * Converts TRIANGLE-STRIPS to independent TRIANGLES
356
+ * @param primitive - the primitive to get the indices from
357
+ * @returns indices of vertices of the independent triangles
358
+ */
281
359
  function normalizeIndices(primitive) {
282
- var _primitive$indices;
283
- let indices = (_primitive$indices = primitive.indices) === null || _primitive$indices === void 0 ? void 0 : _primitive$indices.value;
284
- if (!indices) {
285
- const positions = primitive.attributes.POSITION.value;
286
- return generateSyntheticIndices(positions.length / VALUES_PER_VERTEX);
287
- }
288
- if (indices && primitive.mode === GL.TRIANGLE_STRIP) {
289
- const TypedArrayConstructor = indices.constructor;
290
- const newIndices = new TypedArrayConstructor((indices.length - 2) * 3);
291
- let triangleIndex = 0;
292
- let currentTriangle = indices.slice(0, 3);
293
- newIndices.set(currentTriangle, 0);
294
- for (let i = 1; i + 2 < indices.length; i++) {
295
- triangleIndex += 3;
296
- currentTriangle = indices.slice(i, i + 3);
297
- if (i % 2 === 0) {
298
- newIndices.set(currentTriangle, triangleIndex);
299
- } else {
300
- newIndices.set(currentTriangle.reverse(), triangleIndex);
301
- }
302
- }
303
- indices = newIndices;
304
- }
305
- return indices;
360
+ let indices = primitive.indices?.value;
361
+ if (!indices) {
362
+ const positions = primitive.attributes.POSITION.value;
363
+ return generateSyntheticIndices(positions.length / VALUES_PER_VERTEX);
364
+ }
365
+ if (indices && primitive.mode === GL.TRIANGLE_STRIP) {
366
+ /*
367
+ TRIANGLE_STRIP geometry contains n+2 vertices for n triangles;
368
+ TRIANGLE geometry contains n*3 vertices for n triangles.
369
+ The conversion from TRIANGLE_STRIP to TRIANGLE implies duplicating adjacent vertices.
370
+ */
371
+ const TypedArrayConstructor = indices.constructor;
372
+ const newIndices = new TypedArrayConstructor((indices.length - 2) * 3);
373
+ // Copy the first triangle indices with no modification like [i0, i1, i2, ...] -> [i0, i1, i2, ...]
374
+ let triangleIndex = 0;
375
+ let currentTriangle = indices.slice(0, 3);
376
+ newIndices.set(currentTriangle, 0);
377
+ // The rest triangle indices are being taken from strips using the following logic:
378
+ // [i1, i2, i3, i4, i5, i6, ...] -> [i3, i2, i1, i2, i3, i4, i5, i4, i3, i4, i5, i6, ...]
379
+ for (let i = 1; i + 2 < indices.length; i++) {
380
+ triangleIndex += 3;
381
+ currentTriangle = indices.slice(i, i + 3);
382
+ if (i % 2 === 0) {
383
+ newIndices.set(currentTriangle, triangleIndex);
384
+ }
385
+ else {
386
+ // The following "reverce" is necessary to calculate normals correctly
387
+ newIndices.set(currentTriangle.reverse(), triangleIndex);
388
+ }
389
+ }
390
+ indices = newIndices;
391
+ }
392
+ return indices;
306
393
  }
394
+ /**
395
+ * Convert vertices attributes (POSITIONS or NORMALS) to i3s compatible format
396
+ * @param args
397
+ * @param args.vertices - gltf primitive POSITION or NORMAL attribute
398
+ * @param args.cartographicOrigin - cartographic origin coordinates
399
+ * @param args.cartesianModelMatrix - a cartesian model matrix to transform coordnates from cartesian to cartographic format
400
+ * @param args.nodeMatrix - a gltf node transformation matrix - cumulative transformation matrix formed from all parent node matrices
401
+ * @param args.indices - gltf primitive indices
402
+ * @param args.attributeSpecificTransformation - function to do attribute - specific transformations
403
+ * @param args.useCartesianPositions - use coordinates as it is.
404
+ * @returns
405
+ */
307
406
  function transformVertexArray(args) {
308
- const {
309
- vertices,
310
- indices,
311
- attributeSpecificTransformation
312
- } = args;
313
- const newVertices = new Float32Array(indices.length * VALUES_PER_VERTEX);
314
- if (!vertices) {
407
+ const { vertices, indices, attributeSpecificTransformation } = args;
408
+ const newVertices = new Float32Array(indices.length * VALUES_PER_VERTEX);
409
+ if (!vertices) {
410
+ return newVertices;
411
+ }
412
+ for (let i = 0; i < indices.length; i++) {
413
+ const coordIndex = indices[i] * VALUES_PER_VERTEX;
414
+ const vertex = vertices.subarray(coordIndex, coordIndex + VALUES_PER_VERTEX);
415
+ let vertexVector = new Vector3(Array.from(vertex));
416
+ vertexVector = attributeSpecificTransformation(vertexVector, args);
417
+ newVertices[i * VALUES_PER_VERTEX] = vertexVector.x;
418
+ newVertices[i * VALUES_PER_VERTEX + 1] = vertexVector.y;
419
+ newVertices[i * VALUES_PER_VERTEX + 2] = vertexVector.z;
420
+ }
315
421
  return newVertices;
316
- }
317
- for (let i = 0; i < indices.length; i++) {
318
- const coordIndex = indices[i] * VALUES_PER_VERTEX;
319
- const vertex = vertices.subarray(coordIndex, coordIndex + VALUES_PER_VERTEX);
320
- let vertexVector = new Vector3(Array.from(vertex));
321
- vertexVector = attributeSpecificTransformation(vertexVector, args);
322
- newVertices[i * VALUES_PER_VERTEX] = vertexVector.x;
323
- newVertices[i * VALUES_PER_VERTEX + 1] = vertexVector.y;
324
- newVertices[i * VALUES_PER_VERTEX + 2] = vertexVector.z;
325
- }
326
- return newVertices;
327
422
  }
423
+ /**
424
+ * Trasform positions vector with the attribute specific transformations
425
+ * @param vertexVector - source positions vector to transform
426
+ * @param calleeArgs
427
+ * @param calleeArgs.cartesianModelMatrix - a cartesian model matrix to transform coordnates from cartesian to cartographic format
428
+ * @param calleeArgs.cartographicOrigin - cartographic origin coordinates
429
+ * @param calleeArgs.nodeMatrix - a gltf node transformation matrix - cumulative transformation matrix formed from all parent node matrices
430
+ * @param calleeArgs.useCartesianPositions - use coordinates as it is.
431
+ * @returns transformed positions vector
432
+ */
328
433
  function transformVertexPositions(vertexVector, calleeArgs) {
329
- const {
330
- cartesianModelMatrix,
331
- cartographicOrigin,
332
- nodeMatrix,
333
- useCartesianPositions
334
- } = calleeArgs;
335
- if (nodeMatrix) {
336
- vertexVector = vertexVector.transform(nodeMatrix);
337
- }
338
- vertexVector = vertexVector.transform(cartesianModelMatrix);
339
- if (useCartesianPositions) {
434
+ const { cartesianModelMatrix, cartographicOrigin, nodeMatrix, useCartesianPositions } = calleeArgs;
435
+ if (nodeMatrix) {
436
+ vertexVector = vertexVector.transform(nodeMatrix);
437
+ }
438
+ vertexVector = vertexVector.transform(cartesianModelMatrix);
439
+ if (useCartesianPositions) {
440
+ return vertexVector;
441
+ }
442
+ Ellipsoid.WGS84.cartesianToCartographic([vertexVector[0], vertexVector[1], vertexVector[2]], vertexVector);
443
+ vertexVector = vertexVector.subtract(cartographicOrigin);
340
444
  return vertexVector;
341
- }
342
- Ellipsoid.WGS84.cartesianToCartographic([vertexVector[0], vertexVector[1], vertexVector[2]], vertexVector);
343
- vertexVector = vertexVector.subtract(cartographicOrigin);
344
- return vertexVector;
345
445
  }
446
+ /**
447
+ * Trasform normals vector with the attribute specific transformations
448
+ * @param vertexVector - source normals vector to transform
449
+ * @param calleeArgs
450
+ * @param calleeArgs.cartesianModelMatrix - a cartesian model matrix to transform coordnates from cartesian to cartographic format
451
+ * @param calleeArgs.nodeMatrix - a gltf node transformation matrix - cumulative transformation matrix formed from all parent node matrices
452
+ * @returns transformed normals vector
453
+ */
346
454
  function transformVertexNormals(vertexVector, calleeArgs) {
347
- const {
348
- cartesianModelMatrix,
349
- nodeMatrix
350
- } = calleeArgs;
351
- if (nodeMatrix) {
352
- vertexVector = vertexVector.transformAsVector(nodeMatrix);
353
- }
354
- vertexVector = vertexVector.transformAsVector(cartesianModelMatrix);
355
- return vertexVector;
455
+ const { cartesianModelMatrix, nodeMatrix } = calleeArgs;
456
+ if (nodeMatrix) {
457
+ vertexVector = vertexVector.transformAsVector(nodeMatrix);
458
+ }
459
+ vertexVector = vertexVector.transformAsVector(cartesianModelMatrix);
460
+ return vertexVector;
356
461
  }
462
+ /**
463
+ * Convert uv0 (texture coordinates) from coords based on indices to plain arrays, compatible with i3s
464
+ * @param texCoords - gltf primitive TEXCOORD_0 attribute
465
+ * @param indices - gltf primitive indices
466
+ * @returns flattened texture coordinates
467
+ */
357
468
  function flattenTexCoords(texCoords, indices) {
358
- const newTexCoords = new Float32Array(indices.length * VALUES_PER_TEX_COORD);
359
- if (!texCoords) {
360
- newTexCoords.fill(1);
469
+ const newTexCoords = new Float32Array(indices.length * VALUES_PER_TEX_COORD);
470
+ if (!texCoords) {
471
+ // We need dummy UV0s because it is required in 1.6
472
+ // https://github.com/Esri/i3s-spec/blob/master/docs/1.6/vertexAttribute.cmn.md
473
+ newTexCoords.fill(1);
474
+ return newTexCoords;
475
+ }
476
+ for (let i = 0; i < indices.length; i++) {
477
+ const coordIndex = indices[i] * VALUES_PER_TEX_COORD;
478
+ const texCoord = texCoords.subarray(coordIndex, coordIndex + VALUES_PER_TEX_COORD);
479
+ newTexCoords[i * VALUES_PER_TEX_COORD] = texCoord[0];
480
+ newTexCoords[i * VALUES_PER_TEX_COORD + 1] = texCoord[1];
481
+ }
361
482
  return newTexCoords;
362
- }
363
- for (let i = 0; i < indices.length; i++) {
364
- const coordIndex = indices[i] * VALUES_PER_TEX_COORD;
365
- const texCoord = texCoords.subarray(coordIndex, coordIndex + VALUES_PER_TEX_COORD);
366
- newTexCoords[i * VALUES_PER_TEX_COORD] = texCoord[0];
367
- newTexCoords[i * VALUES_PER_TEX_COORD + 1] = texCoord[1];
368
- }
369
- return newTexCoords;
370
483
  }
484
+ /**
485
+ * Convert color from COLOR_0 based on indices to plain arrays, compatible with i3s
486
+ * @param colorsAttribute - gltf primitive COLOR_0 attribute
487
+ * @param indices - gltf primitive indices
488
+ * @returns flattened colors attribute
489
+ */
371
490
  function flattenColors(colorsAttribute, indices) {
372
- const components = (colorsAttribute === null || colorsAttribute === void 0 ? void 0 : colorsAttribute.components) || VALUES_PER_COLOR_ELEMENT;
373
- const newColors = new Uint8Array(indices.length * components);
374
- if (!colorsAttribute) {
375
- newColors.fill(255);
491
+ const components = colorsAttribute?.components || VALUES_PER_COLOR_ELEMENT;
492
+ const newColors = new Uint8Array(indices.length * components);
493
+ if (!colorsAttribute) {
494
+ // Vertex color multiplies by material color so it must be normalized 1 by default
495
+ newColors.fill(255);
496
+ return newColors;
497
+ }
498
+ const colors = colorsAttribute.value;
499
+ for (let i = 0; i < indices.length; i++) {
500
+ const colorIndex = indices[i] * components;
501
+ const color = colors.subarray(colorIndex, colorIndex + components);
502
+ const colorUint8 = new Uint8Array(components);
503
+ for (let j = 0; j < color.length; j++) {
504
+ colorUint8[j] = color[j] * 255;
505
+ }
506
+ newColors.set(colorUint8, i * components);
507
+ }
376
508
  return newColors;
377
- }
378
- const colors = colorsAttribute.value;
379
- for (let i = 0; i < indices.length; i++) {
380
- const colorIndex = indices[i] * components;
381
- const color = colors.subarray(colorIndex, colorIndex + components);
382
- const colorUint8 = new Uint8Array(components);
383
- for (let j = 0; j < color.length; j++) {
384
- colorUint8[j] = color[j] * 255;
385
- }
386
- newColors.set(colorUint8, i * components);
387
- }
388
- return newColors;
389
509
  }
510
+ /**
511
+ * Create per-vertex uv-region array
512
+ * @param materialUvRegion - uv-region fragment for a single vertex
513
+ * @param indices - geometry indices data
514
+ * @returns - uv-region array
515
+ */
390
516
  function createUvRegion(materialUvRegion, indices) {
391
- const result = new Uint16Array(indices.length * 4);
392
- for (let i = 0; i < result.length; i += 4) {
393
- result.set(materialUvRegion, i);
394
- }
395
- return result;
517
+ const result = new Uint16Array(indices.length * 4);
518
+ for (let i = 0; i < result.length; i += 4) {
519
+ result.set(materialUvRegion, i);
520
+ }
521
+ return result;
396
522
  }
523
+ /**
524
+ * Flatten batchedIds list based on indices to right ordered array, compatible with i3s
525
+ * @param batchedIds - gltf primitive
526
+ * @param indices - gltf primitive indices
527
+ * @returns flattened batch ids
528
+ */
397
529
  function flattenBatchIds(batchedIds, indices) {
398
- if (!batchedIds.length || !indices.length) {
399
- return [];
400
- }
401
- const newBatchIds = [];
402
- for (let i = 0; i < indices.length; i++) {
403
- const coordIndex = indices[i];
404
- newBatchIds.push(batchedIds[coordIndex]);
405
- }
406
- return newBatchIds;
530
+ if (!batchedIds.length || !indices.length) {
531
+ return [];
532
+ }
533
+ const newBatchIds = [];
534
+ for (let i = 0; i < indices.length; i++) {
535
+ const coordIndex = indices[i];
536
+ newBatchIds.push(batchedIds[coordIndex]);
537
+ }
538
+ return newBatchIds;
407
539
  }
540
+ /**
541
+ * Get batchIds for featureIds creation
542
+ * @param attributes - gltf accessors
543
+ * @param primitive - gltf primitive data
544
+ * @param images - gltf texture images
545
+ * @param featureTexture - feature texture key
546
+ * @return batch IDs
547
+ */
408
548
  function getBatchIds(attributes, primitive, images, featureTexture) {
409
- const batchIds = handleBatchIdsExtensions(attributes, primitive, images, featureTexture);
410
- if (batchIds.length) {
411
- return batchIds;
412
- }
413
- for (let index = 0; index < BATCHED_ID_POSSIBLE_ATTRIBUTE_NAMES.length; index++) {
414
- const possibleBatchIdAttributeName = BATCHED_ID_POSSIBLE_ATTRIBUTE_NAMES[index];
415
- if (attributes[possibleBatchIdAttributeName] && attributes[possibleBatchIdAttributeName].value) {
416
- return attributes[possibleBatchIdAttributeName].value;
417
- }
418
- }
419
- return [];
420
- }
421
- async function convertMaterials() {
422
- let sourceMaterials = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
423
- let shouldMergeMaterials = arguments.length > 1 ? arguments[1] : undefined;
424
- let materials = [];
425
- for (const sourceMaterial of sourceMaterials) {
426
- materials.push(convertMaterial(sourceMaterial));
427
- }
428
- if (shouldMergeMaterials) {
429
- materials = await mergeAllMaterials(materials);
430
- }
431
- return materials;
549
+ const batchIds = handleBatchIdsExtensions(attributes, primitive, images, featureTexture);
550
+ if (batchIds.length) {
551
+ return batchIds;
552
+ }
553
+ for (let index = 0; index < BATCHED_ID_POSSIBLE_ATTRIBUTE_NAMES.length; index++) {
554
+ const possibleBatchIdAttributeName = BATCHED_ID_POSSIBLE_ATTRIBUTE_NAMES[index];
555
+ if (attributes[possibleBatchIdAttributeName] &&
556
+ attributes[possibleBatchIdAttributeName].value) {
557
+ return attributes[possibleBatchIdAttributeName].value;
558
+ }
559
+ }
560
+ return [];
432
561
  }
562
+ /**
563
+ * Convert GLTF material to I3S material definitions and textures
564
+ * @param sourceMaterials Source GLTF materials
565
+ * @param shouldMergeMaterials - if true - the converter will try to merge similar materials
566
+ * to be able to merge primitives having those materials
567
+ * @returns Array of Couples I3SMaterialDefinition + texture content
568
+ */
569
+ async function convertMaterials(sourceMaterials = [], shouldMergeMaterials) {
570
+ let materials = [];
571
+ for (const sourceMaterial of sourceMaterials) {
572
+ materials.push(convertMaterial(sourceMaterial));
573
+ }
574
+ if (shouldMergeMaterials) {
575
+ materials = await mergeAllMaterials(materials);
576
+ }
577
+ return materials;
578
+ }
579
+ /**
580
+ * Merge materials when possible
581
+ * @param materials materials array
582
+ * @returns merged materials array
583
+ */
433
584
  async function mergeAllMaterials(materials) {
434
- const result = [];
435
- while (materials.length > 0) {
436
- let newMaterial = materials.splice(0, 1)[0];
437
- const mergedIndices = [];
438
- for (let i = 0; i < materials.length; i++) {
439
- const material = materials[i];
440
- if (newMaterial.texture && material.texture || !newMaterial.texture && !material.texture) {
441
- newMaterial = await mergeMaterials(newMaterial, material);
442
- mergedIndices.push(i);
443
- }
444
- }
445
- if (newMaterial.texture && mergedIndices.length) {
446
- var _newMaterial$mergedMa, _newMaterial$mergedMa2;
447
- const newWidth = (_newMaterial$mergedMa = newMaterial.mergedMaterials) === null || _newMaterial$mergedMa === void 0 ? void 0 : _newMaterial$mergedMa.reduce((accum, _ref3) => {
448
- let {
449
- textureSize
450
- } = _ref3;
451
- return accum + ((textureSize === null || textureSize === void 0 ? void 0 : textureSize.width) || 0);
452
- }, 0);
453
- const newHeight = (_newMaterial$mergedMa2 = newMaterial.mergedMaterials) === null || _newMaterial$mergedMa2 === void 0 ? void 0 : _newMaterial$mergedMa2.reduce((accum, _ref4) => {
454
- let {
455
- textureSize
456
- } = _ref4;
457
- return Math.max(accum, (textureSize === null || textureSize === void 0 ? void 0 : textureSize.height) || 0);
458
- }, 0);
459
- let currentX = -1;
460
- for (const aTextureMetadata of newMaterial.mergedMaterials) {
461
- if (aTextureMetadata.textureSize) {
462
- const newX = currentX + 1 + aTextureMetadata.textureSize.width / newWidth * 2 ** (Uint16Array.BYTES_PER_ELEMENT * 8) - 1;
463
- aTextureMetadata.uvRegion = new Uint16Array([currentX + 1, 0, newX, aTextureMetadata.textureSize.height / newHeight * 2 ** (Uint16Array.BYTES_PER_ELEMENT * 8) - 1]);
464
- currentX = newX;
465
- }
466
- }
467
- newMaterial.texture.image.width = newWidth;
468
- newMaterial.texture.image.height = newHeight;
469
- }
470
- for (const index of mergedIndices.reverse()) {
471
- materials.splice(index, 1);
472
- }
473
- result.push(newMaterial);
474
- }
475
- if (!result.length) {
476
- result.push({
477
- material: getDefaultMaterial(),
478
- mergedMaterials: [{
479
- originalMaterialId: 'default'
480
- }]
481
- });
482
- }
483
- return result;
585
+ const result = [];
586
+ while (materials.length > 0) {
587
+ let newMaterial = materials.splice(0, 1)[0];
588
+ const mergedIndices = [];
589
+ for (let i = 0; i < materials.length; i++) {
590
+ const material = materials[i];
591
+ if ((newMaterial.texture && material.texture) ||
592
+ (!newMaterial.texture && !material.texture)) {
593
+ newMaterial = await mergeMaterials(newMaterial, material);
594
+ mergedIndices.push(i);
595
+ }
596
+ }
597
+ if (newMaterial.texture && mergedIndices.length) {
598
+ const newWidth = newMaterial.mergedMaterials?.reduce((accum, { textureSize }) => accum + (textureSize?.width || 0), 0);
599
+ const newHeight = newMaterial.mergedMaterials?.reduce((accum, { textureSize }) => Math.max(accum, textureSize?.height || 0), 0);
600
+ let currentX = -1;
601
+ for (const aTextureMetadata of newMaterial.mergedMaterials) {
602
+ if (aTextureMetadata.textureSize) {
603
+ const newX = currentX +
604
+ 1 +
605
+ (aTextureMetadata.textureSize.width / newWidth) *
606
+ 2 ** (Uint16Array.BYTES_PER_ELEMENT * 8) -
607
+ 1;
608
+ aTextureMetadata.uvRegion = new Uint16Array([
609
+ currentX + 1,
610
+ 0,
611
+ newX,
612
+ (aTextureMetadata.textureSize.height / newHeight) *
613
+ 2 ** (Uint16Array.BYTES_PER_ELEMENT * 8) -
614
+ 1
615
+ ]);
616
+ currentX = newX;
617
+ }
618
+ }
619
+ newMaterial.texture.image.width = newWidth;
620
+ newMaterial.texture.image.height = newHeight;
621
+ }
622
+ for (const index of mergedIndices.reverse()) {
623
+ materials.splice(index, 1);
624
+ }
625
+ result.push(newMaterial);
626
+ }
627
+ if (!result.length) {
628
+ result.push({
629
+ material: getDefaultMaterial(),
630
+ mergedMaterials: [{ originalMaterialId: 'default' }]
631
+ });
632
+ }
633
+ return result;
484
634
  }
635
+ /**
636
+ * Merge 2 materials including texture
637
+ * @param material1
638
+ * @param material2
639
+ * @returns
640
+ */
485
641
  async function mergeMaterials(material1, material2) {
486
- var _material1$texture, _material2$texture;
487
- if ((_material1$texture = material1.texture) !== null && _material1$texture !== void 0 && _material1$texture.bufferView && (_material2$texture = material2.texture) !== null && _material2$texture !== void 0 && _material2$texture.bufferView && material1.mergedMaterials && material2.mergedMaterials) {
488
- const buffer1 = Buffer.from(material1.texture.bufferView.data);
489
- const buffer2 = Buffer.from(material2.texture.bufferView.data);
490
- try {
491
- const {
492
- joinImages
493
- } = await import('join-images');
494
- const sharpData = await joinImages([buffer1, buffer2], {
495
- direction: 'horizontal'
496
- });
497
- material1.texture.bufferView.data = await sharpData.toFormat(material1.texture.mimeType === 'image/png' ? 'png' : 'jpeg').toBuffer();
498
- } catch (error) {
499
- console.log('Join images into a texture atlas has failed. Consider usage `--split-nodes` option. (See documentation https://loaders.gl/modules/tile-converter/docs/cli-reference/tile-converter)');
500
- throw error;
501
- }
502
- material1.material.pbrMetallicRoughness.baseColorTexture.textureSetDefinitionId = 1;
503
- }
504
- material1.mergedMaterials = material1.mergedMaterials.concat(material2.mergedMaterials);
505
- return material1;
642
+ if (material1.texture?.bufferView &&
643
+ material2.texture?.bufferView &&
644
+ material1.mergedMaterials &&
645
+ material2.mergedMaterials) {
646
+ const buffer1 = Buffer.from(material1.texture.bufferView.data);
647
+ const buffer2 = Buffer.from(material2.texture.bufferView.data);
648
+ try {
649
+ // @ts-ignore
650
+ const { joinImages } = await import('join-images');
651
+ const sharpData = await joinImages([buffer1, buffer2], { direction: 'horizontal' });
652
+ material1.texture.bufferView.data = await sharpData
653
+ .toFormat(material1.texture.mimeType === 'image/png' ? 'png' : 'jpeg')
654
+ .toBuffer();
655
+ }
656
+ catch (error) {
657
+ console.log('Join images into a texture atlas has failed. Consider usage `--split-nodes` option. (See documentation https://loaders.gl/modules/tile-converter/docs/cli-reference/tile-converter)');
658
+ throw error;
659
+ }
660
+ // @ts-ignore
661
+ material1.material.pbrMetallicRoughness.baseColorTexture.textureSetDefinitionId = 1;
662
+ }
663
+ material1.mergedMaterials = material1.mergedMaterials.concat(material2.mergedMaterials);
664
+ return material1;
506
665
  }
666
+ /**
667
+ * Convert texture and material from gltf 2.0 material object
668
+ * @param sourceMaterial - material object
669
+ * @returns I3S material definition and texture
670
+ */
507
671
  function convertMaterial(sourceMaterial) {
508
- var _sourceMaterial$emiss, _sourceMaterial$pbrMe, _sourceMaterial$pbrMe2, _sourceMaterial$pbrMe3;
509
- const material = {
510
- doubleSided: sourceMaterial.doubleSided,
511
- emissiveFactor: (_sourceMaterial$emiss = sourceMaterial.emissiveFactor) === null || _sourceMaterial$emiss === void 0 ? void 0 : _sourceMaterial$emiss.map(c => Math.round(c * 255)),
512
- alphaMode: convertAlphaMode(sourceMaterial.alphaMode),
513
- pbrMetallicRoughness: {
514
- roughnessFactor: (sourceMaterial === null || sourceMaterial === void 0 ? void 0 : (_sourceMaterial$pbrMe = sourceMaterial.pbrMetallicRoughness) === null || _sourceMaterial$pbrMe === void 0 ? void 0 : _sourceMaterial$pbrMe.roughnessFactor) || DEFAULT_ROUGHNESS_FACTOR,
515
- metallicFactor: (sourceMaterial === null || sourceMaterial === void 0 ? void 0 : (_sourceMaterial$pbrMe2 = sourceMaterial.pbrMetallicRoughness) === null || _sourceMaterial$pbrMe2 === void 0 ? void 0 : _sourceMaterial$pbrMe2.metallicFactor) || DEFAULT_METALLIC_FACTOR
516
- }
517
- };
518
- let texture;
519
- if (sourceMaterial !== null && sourceMaterial !== void 0 && (_sourceMaterial$pbrMe3 = sourceMaterial.pbrMetallicRoughness) !== null && _sourceMaterial$pbrMe3 !== void 0 && _sourceMaterial$pbrMe3.baseColorTexture) {
520
- texture = sourceMaterial.pbrMetallicRoughness.baseColorTexture.texture.source;
521
- material.pbrMetallicRoughness.baseColorTexture = {
522
- textureSetDefinitionId: 0
523
- };
524
- } else if (sourceMaterial.emissiveTexture) {
525
- texture = sourceMaterial.emissiveTexture.texture.source;
526
- material.pbrMetallicRoughness.baseColorTexture = {
527
- textureSetDefinitionId: 0
528
- };
529
- }
530
- sourceMaterial.id = Number.isFinite(sourceMaterial.id) ? sourceMaterial.id : uuidv4();
531
- let mergedMaterials = [{
532
- originalMaterialId: sourceMaterial.id
533
- }];
534
- if (!texture) {
535
- var _sourceMaterial$pbrMe4;
536
- const baseColorFactor = sourceMaterial === null || sourceMaterial === void 0 ? void 0 : (_sourceMaterial$pbrMe4 = sourceMaterial.pbrMetallicRoughness) === null || _sourceMaterial$pbrMe4 === void 0 ? void 0 : _sourceMaterial$pbrMe4.baseColorFactor;
537
- material.pbrMetallicRoughness.baseColorFactor = baseColorFactor && baseColorFactor.map(c => Math.round(c * 255)) || undefined;
538
- } else {
539
- mergedMaterials[0].textureSize = {
540
- width: texture.image.width,
541
- height: texture.image.height
672
+ const material = {
673
+ doubleSided: sourceMaterial.doubleSided,
674
+ emissiveFactor: sourceMaterial.emissiveFactor?.map((c) => Math.round(c * 255)),
675
+ // It is in upper case in GLTF: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#alpha-coverage
676
+ // But it is in lower case in I3S: https://github.com/Esri/i3s-spec/blob/master/docs/1.7/materialDefinitions.cmn.md
677
+ alphaMode: convertAlphaMode(sourceMaterial.alphaMode),
678
+ pbrMetallicRoughness: {
679
+ roughnessFactor: sourceMaterial?.pbrMetallicRoughness?.roughnessFactor || DEFAULT_ROUGHNESS_FACTOR,
680
+ metallicFactor: sourceMaterial?.pbrMetallicRoughness?.metallicFactor || DEFAULT_METALLIC_FACTOR
681
+ }
542
682
  };
543
- }
544
- return {
545
- material,
546
- texture,
547
- mergedMaterials
548
- };
683
+ let texture;
684
+ if (sourceMaterial?.pbrMetallicRoughness?.baseColorTexture) {
685
+ texture = sourceMaterial.pbrMetallicRoughness.baseColorTexture.texture.source;
686
+ material.pbrMetallicRoughness.baseColorTexture = {
687
+ textureSetDefinitionId: 0
688
+ };
689
+ }
690
+ else if (sourceMaterial.emissiveTexture) {
691
+ texture = sourceMaterial.emissiveTexture.texture.source;
692
+ // ArcGIS webscene doesn't show emissiveTexture but shows baseColorTexture
693
+ material.pbrMetallicRoughness.baseColorTexture = {
694
+ textureSetDefinitionId: 0
695
+ };
696
+ }
697
+ sourceMaterial.id = Number.isFinite(sourceMaterial.id) ? sourceMaterial.id : uuidv4();
698
+ let mergedMaterials = [{ originalMaterialId: sourceMaterial.id }];
699
+ if (!texture) {
700
+ // Should use default baseColorFactor if it is not present in source material
701
+ // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#reference-pbrmetallicroughness
702
+ const baseColorFactor = sourceMaterial?.pbrMetallicRoughness?.baseColorFactor;
703
+ material.pbrMetallicRoughness.baseColorFactor =
704
+ (baseColorFactor && baseColorFactor.map((c) => Math.round(c * 255))) || undefined;
705
+ }
706
+ else {
707
+ mergedMaterials[0].textureSize = { width: texture.image.width, height: texture.image.height };
708
+ }
709
+ return { material, texture, mergedMaterials };
549
710
  }
711
+ /**
712
+ * Converts from `alphaMode` material property from GLTF to I3S format
713
+ * @param gltfAlphaMode glTF material `alphaMode` property
714
+ * @returns I3SMaterialDefinition.alphaMode property
715
+ */
550
716
  function convertAlphaMode(gltfAlphaMode) {
551
- switch (gltfAlphaMode) {
552
- case 'OPAQUE':
553
- return 'opaque';
554
- case 'MASK':
555
- return 'mask';
556
- case 'BLEND':
557
- return 'blend';
558
- default:
559
- return 'opaque';
560
- }
717
+ switch (gltfAlphaMode) {
718
+ case 'OPAQUE':
719
+ return 'opaque';
720
+ case 'MASK':
721
+ return 'mask';
722
+ case 'BLEND':
723
+ return 'blend';
724
+ default:
725
+ return 'opaque';
726
+ }
561
727
  }
728
+ /**
729
+ * Form default I3SMaterialDefinition
730
+ * @returns I3S material definition
731
+ */
562
732
  function getDefaultMaterial() {
563
- return {
564
- alphaMode: 'opaque',
565
- pbrMetallicRoughness: {
566
- metallicFactor: 1,
567
- roughnessFactor: 1
568
- }
569
- };
733
+ return {
734
+ alphaMode: 'opaque',
735
+ pbrMetallicRoughness: {
736
+ metallicFactor: 1,
737
+ roughnessFactor: 1
738
+ }
739
+ };
570
740
  }
741
+ /**
742
+ * Form "sharedResources" from gltf materials array
743
+ * @param gltfMaterials - GLTF materials array
744
+ * @param nodeId - I3S node ID
745
+ * @returns {materialDefinitionInfos: Object[], textureDefinitionInfos: Object[]} -
746
+ * 2 arrays in format of i3s sharedResources data https://github.com/Esri/i3s-spec/blob/master/docs/1.7/sharedResource.cmn.md
747
+ */
571
748
  function getSharedResources(gltfMaterials, nodeId) {
572
- const i3sResources = {};
573
- if (!gltfMaterials || !gltfMaterials.length) {
749
+ const i3sResources = {};
750
+ if (!gltfMaterials || !gltfMaterials.length) {
751
+ return i3sResources;
752
+ }
753
+ i3sResources.materialDefinitionInfos = [];
754
+ for (const gltfMaterial of gltfMaterials) {
755
+ const { materialDefinitionInfo, textureDefinitionInfo } = convertGLTFMaterialToI3sSharedResources(gltfMaterial, nodeId);
756
+ i3sResources.materialDefinitionInfos.push(materialDefinitionInfo);
757
+ if (textureDefinitionInfo) {
758
+ i3sResources.textureDefinitionInfos = i3sResources.textureDefinitionInfos || [];
759
+ i3sResources.textureDefinitionInfos.push(textureDefinitionInfo);
760
+ }
761
+ }
574
762
  return i3sResources;
575
- }
576
- i3sResources.materialDefinitionInfos = [];
577
- for (const gltfMaterial of gltfMaterials) {
578
- const {
579
- materialDefinitionInfo,
580
- textureDefinitionInfo
581
- } = convertGLTFMaterialToI3sSharedResources(gltfMaterial, nodeId);
582
- i3sResources.materialDefinitionInfos.push(materialDefinitionInfo);
583
- if (textureDefinitionInfo) {
584
- i3sResources.textureDefinitionInfos = i3sResources.textureDefinitionInfos || [];
585
- i3sResources.textureDefinitionInfos.push(textureDefinitionInfo);
586
- }
587
- }
588
- return i3sResources;
589
763
  }
764
+ /**
765
+ * Convert gltf material into I3S sharedResources data
766
+ * @param gltfMaterial - gltf material data
767
+ * @param nodeId - I3S node ID
768
+ * @returns - Couple {materialDefinitionInfo, textureDefinitionInfo} extracted from gltf material data
769
+ */
590
770
  function convertGLTFMaterialToI3sSharedResources(gltfMaterial, nodeId) {
591
- var _gltfMaterial$pbrMeta;
592
- const texture = (gltfMaterial === null || gltfMaterial === void 0 ? void 0 : (_gltfMaterial$pbrMeta = gltfMaterial.pbrMetallicRoughness) === null || _gltfMaterial$pbrMeta === void 0 ? void 0 : _gltfMaterial$pbrMeta.baseColorTexture) || gltfMaterial.emissiveTexture;
593
- let textureDefinitionInfo = null;
594
- if (texture) {
595
- textureDefinitionInfo = extractSharedResourcesTextureInfo(texture.texture, nodeId);
596
- }
597
- const {
598
- baseColorFactor,
599
- metallicFactor
600
- } = (gltfMaterial === null || gltfMaterial === void 0 ? void 0 : gltfMaterial.pbrMetallicRoughness) || {};
601
- let colorFactor = baseColorFactor;
602
- if ((!baseColorFactor || baseColorFactor[3] === 0) && gltfMaterial.emissiveFactor) {
603
- colorFactor = gltfMaterial.emissiveFactor;
604
- colorFactor[3] = colorFactor[3] || 1;
605
- }
606
- return {
607
- materialDefinitionInfo: extractSharedResourcesMaterialInfo(colorFactor || [1, 1, 1, 1], metallicFactor),
608
- textureDefinitionInfo
609
- };
610
- }
611
- function extractSharedResourcesMaterialInfo(baseColorFactor) {
612
- let metallicFactor = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
613
- const matDielectricColorComponent = 0.04 / 255;
614
- const black = new Vector4(0, 0, 0, 1);
615
- const unitVector = new Vector4(1, 1, 1, 1);
616
- const dielectricSpecular = new Vector4(matDielectricColorComponent, matDielectricColorComponent, matDielectricColorComponent, 0);
617
- const baseColorVector = new Vector4(baseColorFactor);
618
- const firstOperand = unitVector.subtract(dielectricSpecular).multiply(baseColorVector);
619
- const diffuse = firstOperand.lerp(firstOperand, black, metallicFactor);
620
- dielectricSpecular[3] = 1;
621
- const specular = dielectricSpecular.lerp(dielectricSpecular, baseColorVector, metallicFactor);
622
- return {
623
- params: {
624
- diffuse: diffuse.toArray(),
625
- specular: specular.toArray(),
626
- renderMode: 'solid'
627
- }
628
- };
771
+ const texture = gltfMaterial?.pbrMetallicRoughness?.baseColorTexture || gltfMaterial.emissiveTexture;
772
+ let textureDefinitionInfo = null;
773
+ if (texture) {
774
+ textureDefinitionInfo = extractSharedResourcesTextureInfo(texture.texture, nodeId);
775
+ }
776
+ const { baseColorFactor, metallicFactor } = gltfMaterial?.pbrMetallicRoughness || {};
777
+ let colorFactor = baseColorFactor;
778
+ // If alpha channel is 0 try to get emissive factor from gltf material.
779
+ if ((!baseColorFactor || baseColorFactor[3] === 0) && gltfMaterial.emissiveFactor) {
780
+ colorFactor = gltfMaterial.emissiveFactor;
781
+ colorFactor[3] = colorFactor[3] || 1;
782
+ }
783
+ return {
784
+ materialDefinitionInfo: extractSharedResourcesMaterialInfo(colorFactor || [1, 1, 1, 1], metallicFactor),
785
+ textureDefinitionInfo
786
+ };
787
+ }
788
+ /**
789
+ * Form "materialDefinition" which is part of "sharedResouces"
790
+ * https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#materials
791
+ * See formulas in appendix "Appendix B: BRDF Implementation":
792
+ * const dielectricSpecular = rgb(0.04, 0.04, 0.04)
793
+ * const black = rgb(0, 0, 0)
794
+ * cdiff = lerp(baseColor.rgb * (1 - dielectricSpecular.r), black, metallic)
795
+ * F0 = lerp(dieletricSpecular, baseColor.rgb, metallic)
796
+ *
797
+ * Assumption: F0 - specular in i3s ("specular reflection" <-> "reflectance value at normal incidence")
798
+ * cdiff - diffuse in i3s ("Diffuse color" <-> "'c' diffuse" (c means color?))
799
+ * @param baseColorFactor - RGBA color in 0..1 format
800
+ * @param metallicFactor - "metallicFactor" attribute of gltf material object
801
+ * @returns material definition info for I3S shared resource
802
+ */
803
+ function extractSharedResourcesMaterialInfo(baseColorFactor, metallicFactor = 1) {
804
+ const matDielectricColorComponent = 0.04 / 255; // Color from rgb (255) to 0..1 resolution
805
+ // All color resolutions are 0..1
806
+ const black = new Vector4(0, 0, 0, 1);
807
+ const unitVector = new Vector4(1, 1, 1, 1);
808
+ const dielectricSpecular = new Vector4(matDielectricColorComponent, matDielectricColorComponent, matDielectricColorComponent, 0);
809
+ const baseColorVector = new Vector4(baseColorFactor);
810
+ // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material
811
+ // Formulas for Cdiff & F0
812
+ const firstOperand = unitVector.subtract(dielectricSpecular).multiply(baseColorVector);
813
+ const diffuse = firstOperand.lerp(firstOperand, black, metallicFactor);
814
+ dielectricSpecular[3] = 1;
815
+ const specular = dielectricSpecular.lerp(dielectricSpecular, baseColorVector, metallicFactor);
816
+ return {
817
+ params: {
818
+ // @ts-expect-error NumericArray
819
+ diffuse: diffuse.toArray(),
820
+ // @ts-expect-error NumericArray
821
+ specular: specular.toArray(),
822
+ renderMode: 'solid'
823
+ }
824
+ };
629
825
  }
826
+ /**
827
+ * Form "textureDefinition" which is part of "sharedResouces"
828
+ * @param texture - texture image info
829
+ * @param nodeId - I3S node ID
830
+ * @returns texture definition infor for shared resource
831
+ */
630
832
  function extractSharedResourcesTextureInfo(texture, nodeId) {
631
- var _texture$source, _texture$source2, _texture$source3, _texture$source4;
632
- return {
633
- encoding: texture !== null && texture !== void 0 && (_texture$source = texture.source) !== null && _texture$source !== void 0 && _texture$source.mimeType ? [texture.source.mimeType] : undefined,
634
- images: [{
635
- id: generateImageId(texture, nodeId),
636
- size: (_texture$source2 = texture.source) === null || _texture$source2 === void 0 ? void 0 : _texture$source2.image.width,
637
- length: (_texture$source3 = texture.source) !== null && _texture$source3 !== void 0 && _texture$source3.image.data.length ? [(_texture$source4 = texture.source) === null || _texture$source4 === void 0 ? void 0 : _texture$source4.image.data.length] : undefined
638
- }]
639
- };
833
+ return {
834
+ encoding: texture?.source?.mimeType ? [texture.source.mimeType] : undefined,
835
+ images: [
836
+ {
837
+ // 'i3s' has just size which is width of the image. Images are supposed to be square.
838
+ // https://github.com/Esri/i3s-spec/blob/master/docs/1.7/image.cmn.md
839
+ id: generateImageId(texture, nodeId),
840
+ size: texture.source?.image.width,
841
+ length: texture.source?.image.data.length ? [texture.source?.image.data.length] : undefined
842
+ }
843
+ ]
844
+ };
640
845
  }
846
+ /**
847
+ * Formula for calculating imageId:
848
+ * https://github.com/Esri/i3s-spec/blob/0a6366a9249b831db8436c322f8d27521e86cf07/format/Indexed%203d%20Scene%20Layer%20Format%20Specification.md#generating-image-ids
849
+ * @param texture - texture image info
850
+ * @param nodeId - I3S node ID
851
+ * @returns calculate image ID according to the spec
852
+ */
641
853
  function generateImageId(texture, nodeId) {
642
- var _texture$source5;
643
- const {
644
- width,
645
- height
646
- } = ((_texture$source5 = texture.source) === null || _texture$source5 === void 0 ? void 0 : _texture$source5.image) || {};
647
- if (!width || !height) {
648
- return '';
649
- }
650
- const levelCountOfTexture = 1;
651
- const indexOfLevel = 0;
652
- const indexOfTextureInStore = nodeId + 1;
653
- const zerosCount = 32 - indexOfTextureInStore.toString(2).length;
654
- const rightHalf = '0'.repeat(zerosCount).concat(indexOfTextureInStore.toString(2));
655
- const shiftedLevelCountOfTexture = levelCountOfTexture << 28;
656
- const shiftedIndexOfLevel = indexOfLevel << 24;
657
- const shiftedWidth = width - 1 << 12;
658
- const shiftedHeight = height - 1 << 0;
659
- const leftHalf = shiftedLevelCountOfTexture + shiftedIndexOfLevel + shiftedWidth + shiftedHeight;
660
- const imageId = BigInt(`0b${leftHalf.toString(2)}${rightHalf}`);
661
- return imageId.toString();
854
+ const { width, height } = texture.source?.image || {};
855
+ if (!width || !height) {
856
+ return '';
857
+ }
858
+ const levelCountOfTexture = 1;
859
+ const indexOfLevel = 0;
860
+ const indexOfTextureInStore = nodeId + 1;
861
+ const zerosCount = 32 - indexOfTextureInStore.toString(2).length;
862
+ const rightHalf = '0'.repeat(zerosCount).concat(indexOfTextureInStore.toString(2));
863
+ const shiftedLevelCountOfTexture = levelCountOfTexture << 28;
864
+ const shiftedIndexOfLevel = indexOfLevel << 24;
865
+ const shiftedWidth = (width - 1) << 12;
866
+ const shiftedHeight = (height - 1) << 0;
867
+ const leftHalf = shiftedLevelCountOfTexture + shiftedIndexOfLevel + shiftedWidth + shiftedHeight;
868
+ const imageId = BigInt(`0b${leftHalf.toString(2)}${rightHalf}`);
869
+ return imageId.toString();
662
870
  }
871
+ /**
872
+ * Make all feature ids unique through all nodes in layout.
873
+ * @param featureIds
874
+ * @param featureIndices
875
+ * @param featuresHashArray
876
+ * @param batchTable
877
+ * @returns propertyTable indices to map featureIds
878
+ */
663
879
  function makeFeatureIdsUnique(featureIds, featureIndices, featuresHashArray, batchTable) {
664
- const replaceMap = getFeaturesReplaceMap(featureIds, batchTable, featuresHashArray);
665
- replaceIndicesByUnique(featureIndices, replaceMap);
666
- replaceIndicesByUnique(featureIds, replaceMap);
667
- return replaceMap;
880
+ const replaceMap = getFeaturesReplaceMap(featureIds, batchTable, featuresHashArray);
881
+ replaceIndicesByUnique(featureIndices, replaceMap);
882
+ replaceIndicesByUnique(featureIds, replaceMap);
883
+ return replaceMap;
668
884
  }
885
+ /**
886
+ * Generate replace map to make featureIds unique.
887
+ * @param featureIds
888
+ * @param batchTable
889
+ * @param featuresHashArray
890
+ * @returns
891
+ */
669
892
  function getFeaturesReplaceMap(featureIds, batchTable, featuresHashArray) {
670
- const featureMap = {};
671
- for (let index = 0; index < featureIds.length; index++) {
672
- const oldFeatureId = featureIds[index];
673
- const uniqueFeatureId = getOrCreateUniqueFeatureId(index, batchTable, featuresHashArray);
674
- featureMap[oldFeatureId.toString()] = uniqueFeatureId;
675
- }
676
- return featureMap;
893
+ const featureMap = {};
894
+ for (let index = 0; index < featureIds.length; index++) {
895
+ const oldFeatureId = featureIds[index];
896
+ const uniqueFeatureId = getOrCreateUniqueFeatureId(index, batchTable, featuresHashArray);
897
+ featureMap[oldFeatureId.toString()] = uniqueFeatureId;
898
+ }
899
+ return featureMap;
677
900
  }
901
+ /**
902
+ * Generates string for unique batch id creation.
903
+ * @param batchTable
904
+ * @param index
905
+ * @returns
906
+ */
678
907
  function generateStringFromBatchTableByIndex(batchTable, index) {
679
- let str = '';
680
- for (const key in batchTable) {
681
- str += batchTable[key][index];
682
- }
683
- return str;
908
+ let str = '';
909
+ for (const key in batchTable) {
910
+ str += batchTable[key][index];
911
+ }
912
+ return str;
684
913
  }
914
+ /**
915
+ * Return already exited featureId or creates and returns new to support unique feature ids throw nodes.
916
+ * @param index
917
+ * @param batchTable
918
+ * @param featuresHashArray
919
+ * @returns
920
+ */
685
921
  function getOrCreateUniqueFeatureId(index, batchTable, featuresHashArray) {
686
- const batchTableStr = generateStringFromBatchTableByIndex(batchTable, index);
687
- const hash = md5(batchTableStr);
688
- if (featuresHashArray.includes(hash)) {
689
- return featuresHashArray.indexOf(hash);
690
- }
691
- return featuresHashArray.push(hash) - 1;
922
+ const batchTableStr = generateStringFromBatchTableByIndex(batchTable, index);
923
+ const hash = md5(batchTableStr);
924
+ if (featuresHashArray.includes(hash)) {
925
+ return featuresHashArray.indexOf(hash);
926
+ }
927
+ return featuresHashArray.push(hash) - 1;
692
928
  }
929
+ /**
930
+ * Do replacement of indices for making them unique through all nodes.
931
+ * @param indicesArray
932
+ * @param featureMap
933
+ * @returns
934
+ */
693
935
  function replaceIndicesByUnique(indicesArray, featureMap) {
694
- for (let index = 0; index < indicesArray.length; index++) {
695
- indicesArray[index] = featureMap[indicesArray[index]];
696
- }
936
+ for (let index = 0; index < indicesArray.length; index++) {
937
+ indicesArray[index] = featureMap[indicesArray[index]];
938
+ }
697
939
  }
940
+ /**
941
+ * Convert property table data to attribute buffers.
942
+ * @param featureIds
943
+ * @param propertyTable - table with metadata for particular feature.
944
+ * @param attributeStorageInfo
945
+ * @returns - Array of file buffers.
946
+ */
698
947
  function convertPropertyTableToAttributeBuffers(featureIds, featureIdsMap, propertyTable, attributeStorageInfo) {
699
- const attributeBuffers = [];
700
- const needFlattenPropertyTable = checkPropertiesLength(featureIds, propertyTable);
701
- const properties = needFlattenPropertyTable ? flattenPropertyTableByFeatureIds(featureIdsMap, propertyTable) : propertyTable;
702
- const propertyTableWithObjectIds = {
703
- OBJECTID: featureIds,
704
- ...properties
705
- };
706
- for (const propertyName in propertyTableWithObjectIds) {
707
- const type = getAttributeType(propertyName, attributeStorageInfo);
708
- if (type) {
709
- const value = propertyTableWithObjectIds[propertyName];
710
- const attributeBuffer = generateAttributeBuffer(type, value);
711
- attributeBuffers.push(attributeBuffer);
712
- }
713
- }
714
- return attributeBuffers;
948
+ const attributeBuffers = [];
949
+ const needFlattenPropertyTable = checkPropertiesLength(featureIds, propertyTable);
950
+ const properties = needFlattenPropertyTable
951
+ ? flattenPropertyTableByFeatureIds(featureIdsMap, propertyTable)
952
+ : propertyTable;
953
+ const propertyTableWithObjectIds = {
954
+ OBJECTID: featureIds,
955
+ ...properties
956
+ };
957
+ for (const propertyName in propertyTableWithObjectIds) {
958
+ const type = getAttributeType(propertyName, attributeStorageInfo);
959
+ if (type) {
960
+ const value = propertyTableWithObjectIds[propertyName];
961
+ const attributeBuffer = generateAttributeBuffer(type, value);
962
+ attributeBuffers.push(attributeBuffer);
963
+ }
964
+ }
965
+ return attributeBuffers;
715
966
  }
967
+ /**
968
+ * Generates attribute buffer based on attribute type
969
+ * @param type
970
+ * @param value
971
+ */
716
972
  function generateAttributeBuffer(type, value) {
717
- let attributeBuffer;
718
- switch (type) {
719
- case OBJECT_ID_TYPE:
720
- case SHORT_INT_TYPE:
721
- attributeBuffer = generateShortIntegerAttributeBuffer(value);
722
- break;
723
- case DOUBLE_TYPE:
724
- attributeBuffer = generateDoubleAttributeBuffer(value);
725
- break;
726
- case STRING_TYPE:
727
- attributeBuffer = generateStringAttributeBuffer(value);
728
- break;
729
- default:
730
- attributeBuffer = generateStringAttributeBuffer(value);
731
- }
732
- return attributeBuffer;
973
+ let attributeBuffer;
974
+ switch (type) {
975
+ case OBJECT_ID_TYPE:
976
+ case SHORT_INT_TYPE:
977
+ attributeBuffer = generateShortIntegerAttributeBuffer(value);
978
+ break;
979
+ case DOUBLE_TYPE:
980
+ attributeBuffer = generateDoubleAttributeBuffer(value);
981
+ break;
982
+ case STRING_TYPE:
983
+ attributeBuffer = generateStringAttributeBuffer(value);
984
+ break;
985
+ default:
986
+ attributeBuffer = generateStringAttributeBuffer(value);
987
+ }
988
+ return attributeBuffer;
733
989
  }
990
+ /**
991
+ * Return attribute type.
992
+ * @param key
993
+ * @param attributeStorageInfo
994
+ * @returns attribute type.
995
+ */
734
996
  function getAttributeType(key, attributeStorageInfo) {
735
- const attribute = attributeStorageInfo.find(attr => attr.name === key);
736
- if (!attribute) {
737
- console.error(`attribute is null, key=${key}, attributeStorageInfo=${JSON.stringify(attributeStorageInfo, null, 2)}`);
738
- return '';
739
- }
740
- if (!attribute.attributeValues) {
741
- console.error(`attributeValues is null, attribute=${attribute}`);
742
- return '';
743
- }
744
- return attribute.attributeValues.valueType;
997
+ const attribute = attributeStorageInfo.find((attr) => attr.name === key);
998
+ if (!attribute) {
999
+ console.error(`attribute is null, key=${key}, attributeStorageInfo=${JSON.stringify(attributeStorageInfo, null, 2)}`);
1000
+ return '';
1001
+ }
1002
+ if (!attribute.attributeValues) {
1003
+ console.error(`attributeValues is null, attribute=${attribute}`);
1004
+ return '';
1005
+ }
1006
+ return attribute.attributeValues.valueType;
745
1007
  }
1008
+ /**
1009
+ * Convert short integer to attribute arrayBuffer.
1010
+ * @param featureIds
1011
+ * @returns - Buffer with objectId data.
1012
+ */
746
1013
  function generateShortIntegerAttributeBuffer(featureIds) {
747
- const count = new Uint32Array([featureIds.length]);
748
- const valuesArray = new Uint32Array(featureIds);
749
- return concatenateArrayBuffers(count.buffer, valuesArray.buffer);
1014
+ const count = new Uint32Array([featureIds.length]);
1015
+ const valuesArray = new Uint32Array(featureIds);
1016
+ return concatenateArrayBuffers(count.buffer, valuesArray.buffer);
750
1017
  }
1018
+ /**
1019
+ * Convert double to attribute arrayBuffer.
1020
+ * @param featureIds
1021
+ * @returns - Buffer with objectId data.
1022
+ */
751
1023
  function generateDoubleAttributeBuffer(featureIds) {
752
- const count = new Uint32Array([featureIds.length]);
753
- const padding = new Uint8Array(4);
754
- const valuesArray = new Float64Array(featureIds);
755
- return concatenateArrayBuffers(count.buffer, padding.buffer, valuesArray.buffer);
1024
+ const count = new Uint32Array([featureIds.length]);
1025
+ const padding = new Uint8Array(4);
1026
+ const valuesArray = new Float64Array(featureIds);
1027
+ return concatenateArrayBuffers(count.buffer, padding.buffer, valuesArray.buffer);
756
1028
  }
1029
+ /**
1030
+ * Convert batch table attributes to array buffer with batch table data.
1031
+ * @param batchAttributes
1032
+ * @returns - Buffer with batch table data.
1033
+ */
757
1034
  function generateStringAttributeBuffer(batchAttributes) {
758
- const stringCountArray = new Uint32Array([batchAttributes.length]);
759
- let totalNumberOfBytes = 0;
760
- const stringSizesArray = new Uint32Array(batchAttributes.length);
761
- const stringBufferArray = [];
762
- for (let index = 0; index < batchAttributes.length; index++) {
763
- const currentString = `${String(batchAttributes[index])}\0`;
764
- const currentStringBuffer = Buffer.from(currentString);
765
- const currentStringSize = currentStringBuffer.length;
766
- totalNumberOfBytes += currentStringSize;
767
- stringSizesArray[index] = currentStringSize;
768
- stringBufferArray.push(currentStringBuffer);
769
- }
770
- const totalBytes = new Uint32Array([totalNumberOfBytes]);
771
- return concatenateArrayBuffers(stringCountArray.buffer, totalBytes.buffer, stringSizesArray.buffer, ...stringBufferArray);
1035
+ const stringCountArray = new Uint32Array([batchAttributes.length]);
1036
+ let totalNumberOfBytes = 0;
1037
+ const stringSizesArray = new Uint32Array(batchAttributes.length);
1038
+ const stringBufferArray = [];
1039
+ for (let index = 0; index < batchAttributes.length; index++) {
1040
+ const currentString = `${String(batchAttributes[index])}\0`;
1041
+ const currentStringBuffer = Buffer.from(currentString);
1042
+ const currentStringSize = currentStringBuffer.length;
1043
+ totalNumberOfBytes += currentStringSize;
1044
+ stringSizesArray[index] = currentStringSize;
1045
+ stringBufferArray.push(currentStringBuffer);
1046
+ }
1047
+ const totalBytes = new Uint32Array([totalNumberOfBytes]);
1048
+ return concatenateArrayBuffers(stringCountArray.buffer, totalBytes.buffer, stringSizesArray.buffer, ...stringBufferArray);
772
1049
  }
1050
+ /**
1051
+ * Convert featureIds to BigUint64Array.
1052
+ * @param featureIds
1053
+ * @returns - Array of feature ids in BigUint64 format.
1054
+ */
773
1055
  function generateBigUint64Array(featureIds) {
774
- const typedFeatureIds = new BigUint64Array(featureIds.length);
775
- for (let index = 0; index < featureIds.length; index++) {
776
- typedFeatureIds[index] = BigInt(featureIds[index]);
777
- }
778
- return typedFeatureIds;
1056
+ const typedFeatureIds = new BigUint64Array(featureIds.length);
1057
+ for (let index = 0; index < featureIds.length; index++) {
1058
+ typedFeatureIds[index] = BigInt(featureIds[index]);
1059
+ }
1060
+ return typedFeatureIds;
779
1061
  }
1062
+ /**
1063
+ * Generates draco compressed geometry
1064
+ * @param vertexCount
1065
+ * @param convertedAttributes - get rid of this argument here
1066
+ * @param attributes - geometry attributes to compress
1067
+ * @param libraries - dynamicaly loaded 3rd-party libraries
1068
+ * @returns - Compressed geometry.
1069
+ */
780
1070
  async function generateCompressedGeometry(vertexCount, convertedAttributes, attributes, libraries) {
781
- const {
782
- positions,
783
- normals,
784
- texCoords,
785
- colors,
786
- uvRegions,
787
- featureIds,
788
- faceRange
789
- } = attributes;
790
- const indices = new Uint32Array(vertexCount);
791
- for (let index = 0; index < indices.length; index++) {
792
- indices.set([index], index);
793
- }
794
- const featureIndices = new Uint32Array(convertedAttributes.featureIndices.length ? convertedAttributes.featureIndices : vertexCount);
795
- const featureIndex = generateFeatureIndexAttribute(featureIndices, faceRange);
796
- const compressedAttributes = {
797
- positions,
798
- normals,
799
- colors,
800
- 'feature-index': featureIndex
801
- };
802
- if (texCoords.length) {
803
- compressedAttributes.texCoords = texCoords;
804
- }
805
- const attributesMetadata = {
806
- 'feature-index': {
807
- 'i3s-attribute-type': 'feature-index',
808
- 'i3s-feature-ids': new Int32Array(featureIds)
809
- }
810
- };
811
- if (uvRegions.length) {
812
- compressedAttributes['uv-region'] = uvRegions;
813
- attributesMetadata['uv-region'] = {
814
- 'i3s-attribute-type': 'uv-region'
1071
+ const { positions, normals, texCoords, colors, uvRegions, featureIds, faceRange } = attributes;
1072
+ const indices = new Uint32Array(vertexCount);
1073
+ for (let index = 0; index < indices.length; index++) {
1074
+ indices.set([index], index);
1075
+ }
1076
+ const featureIndices = new Uint32Array(convertedAttributes.featureIndices.length ? convertedAttributes.featureIndices : vertexCount);
1077
+ const featureIndex = generateFeatureIndexAttribute(featureIndices, faceRange);
1078
+ const compressedAttributes = {
1079
+ positions,
1080
+ normals,
1081
+ colors,
1082
+ 'feature-index': featureIndex
815
1083
  };
816
- }
817
- return encode({
818
- attributes: compressedAttributes,
819
- indices
820
- }, DracoWriterWorker, {
821
- ...DracoWriterWorker.options,
822
- reuseWorkers: true,
823
- _nodeWorkers: true,
824
- modules: libraries,
825
- useLocalLibraries: true,
826
- draco: {
827
- method: 'MESH_SEQUENTIAL_ENCODING',
828
- attributesMetadata
829
- },
830
- ['draco-writer']: {
831
- workerUrl: './modules/draco/dist/draco-writer-worker-node.js'
832
- }
833
- });
1084
+ if (texCoords.length) {
1085
+ compressedAttributes.texCoords = texCoords;
1086
+ }
1087
+ const attributesMetadata = {
1088
+ 'feature-index': {
1089
+ 'i3s-attribute-type': 'feature-index',
1090
+ 'i3s-feature-ids': new Int32Array(featureIds)
1091
+ }
1092
+ };
1093
+ if (uvRegions.length) {
1094
+ compressedAttributes['uv-region'] = uvRegions;
1095
+ attributesMetadata['uv-region'] = {
1096
+ 'i3s-attribute-type': 'uv-region'
1097
+ };
1098
+ }
1099
+ return encode({ attributes: compressedAttributes, indices },
1100
+ // @ts-expect-error if encoded supports worker writer, we should update its type signature
1101
+ DracoWriterWorker, {
1102
+ ...DracoWriterWorker.options,
1103
+ reuseWorkers: true,
1104
+ _nodeWorkers: true,
1105
+ modules: libraries,
1106
+ useLocalLibraries: true,
1107
+ draco: {
1108
+ method: 'MESH_SEQUENTIAL_ENCODING',
1109
+ attributesMetadata
1110
+ },
1111
+ ['draco-writer']: {
1112
+ // We need to load local fs workers because nodejs can't load workers from the Internet
1113
+ workerUrl: './modules/draco/dist/draco-writer-worker-node.js'
1114
+ }
1115
+ });
834
1116
  }
1117
+ /**
1118
+ * Generates ordered feature indices based on face range
1119
+ * @param featureIndex
1120
+ * @param faceRange
1121
+ * @returns
1122
+ */
835
1123
  function generateFeatureIndexAttribute(featureIndex, faceRange) {
836
- const orderedFeatureIndices = new Uint32Array(featureIndex.length);
837
- let fillIndex = 0;
838
- let startIndex = 0;
839
- for (let index = 1; index < faceRange.length; index += 2) {
840
- const endIndex = (faceRange[index] + 1) * VALUES_PER_VERTEX;
841
- orderedFeatureIndices.fill(fillIndex, startIndex, endIndex);
842
- fillIndex++;
843
- startIndex = endIndex + 1;
844
- }
845
- return orderedFeatureIndices;
1124
+ const orderedFeatureIndices = new Uint32Array(featureIndex.length);
1125
+ let fillIndex = 0;
1126
+ let startIndex = 0;
1127
+ for (let index = 1; index < faceRange.length; index += 2) {
1128
+ const endIndex = (faceRange[index] + 1) * VALUES_PER_VERTEX;
1129
+ orderedFeatureIndices.fill(fillIndex, startIndex, endIndex);
1130
+ fillIndex++;
1131
+ startIndex = endIndex + 1;
1132
+ }
1133
+ return orderedFeatureIndices;
846
1134
  }
1135
+ /**
1136
+ * Find property table in tile
1137
+ * For example it can be batchTable for b3dm files or property table in gLTF extension.
1138
+ * @param tileContent - 3DTiles tile content
1139
+ * @param metadataClass - user selected feature metadata class name
1140
+ * @return batch table from b3dm / feature properties from EXT_FEATURE_METADATA or EXT_STRUCTURAL_METADATA.
1141
+ */
847
1142
  export function getPropertyTable(tileContent, metadataClass) {
848
- if (!tileContent) {
849
- return null;
850
- }
851
- let propertyTable;
852
- const batchTableJson = tileContent.batchTableJson;
853
- if (batchTableJson) {
854
- return batchTableJson;
855
- }
856
- const {
857
- extensionName,
858
- extension
859
- } = getPropertyTableExtension(tileContent);
860
- switch (extensionName) {
861
- case EXT_STRUCTURAL_METADATA:
862
- {
863
- propertyTable = getPropertyTableFromExtStructuralMetadata(extension, metadataClass);
864
- return propertyTable;
865
- }
866
- case EXT_FEATURE_METADATA:
867
- {
868
- propertyTable = getPropertyTableFromExtFeatureMetadata(extension, metadataClass);
869
- return propertyTable;
870
- }
871
- default:
872
- return null;
873
- }
1143
+ if (!tileContent) {
1144
+ return null;
1145
+ }
1146
+ let propertyTable;
1147
+ const batchTableJson = tileContent.batchTableJson;
1148
+ if (batchTableJson) {
1149
+ return batchTableJson;
1150
+ }
1151
+ const { extensionName, extension } = getPropertyTableExtension(tileContent);
1152
+ switch (extensionName) {
1153
+ case EXT_STRUCTURAL_METADATA: {
1154
+ propertyTable = getPropertyTableFromExtStructuralMetadata(extension, metadataClass);
1155
+ return propertyTable;
1156
+ }
1157
+ case EXT_FEATURE_METADATA: {
1158
+ propertyTable = getPropertyTableFromExtFeatureMetadata(extension, metadataClass);
1159
+ return propertyTable;
1160
+ }
1161
+ default:
1162
+ return null;
1163
+ }
874
1164
  }
1165
+ /**
1166
+ * Handles EXT_structural_metadata to get property table.
1167
+ * @param extension - Global level of EXT_STRUCTURAL_METADATA extension.
1168
+ * @param metadataClass - User selected feature metadata class name.
1169
+ * @returns {FeatureTableJson | null} Property table or null if the extension can't be handled properly.
1170
+ */
875
1171
  function getPropertyTableFromExtStructuralMetadata(extension, metadataClass) {
876
- if (extension.propertyTables) {
877
- for (const propertyTable of extension.propertyTables) {
878
- if (propertyTable.class === metadataClass || !metadataClass) {
879
- return getPropertyData(propertyTable);
880
- }
881
- }
882
- }
883
- if (extension.propertyTextures) {
884
- for (const propertyTexture of extension.propertyTextures) {
885
- if (propertyTexture.class === metadataClass || !metadataClass) {
886
- return getPropertyData(propertyTexture);
887
- }
888
- }
889
- }
890
- return null;
1172
+ /**
1173
+ * Note, 3dTiles is able to have multiple featureId attributes and multiple feature tables.
1174
+ * In I3S we should decide which featureIds attribute will be passed to geometry data.
1175
+ * So, we take only the feature table / feature texture to generate attributes storage info object.
1176
+ * If the user has selected the metadataClass, the table with the corresponding class will be used,
1177
+ * or just the first one otherwise.
1178
+ */
1179
+ if (extension.propertyTables) {
1180
+ for (const propertyTable of extension.propertyTables) {
1181
+ if (propertyTable.class === metadataClass || !metadataClass) {
1182
+ return getPropertyData(propertyTable);
1183
+ }
1184
+ }
1185
+ }
1186
+ if (extension.propertyTextures) {
1187
+ for (const propertyTexture of extension.propertyTextures) {
1188
+ if (propertyTexture.class === metadataClass || !metadataClass) {
1189
+ return getPropertyData(propertyTexture);
1190
+ }
1191
+ }
1192
+ }
1193
+ return null;
891
1194
  }
1195
+ /**
1196
+ * Handles EXT_feature_metadata to get property table.
1197
+ * @param extension - Global level of EXT_FEATURE_METADATA extension.
1198
+ * @param metadataClass - User selected feature metadata class name.
1199
+ * @returns {FeatureTableJson | null} Property table or null if the extension can't be handled properly.
1200
+ */
892
1201
  function getPropertyTableFromExtFeatureMetadata(extension, metadataClass) {
893
- if (extension.featureTables) {
894
- for (const featureTableName in extension.featureTables) {
895
- const featureTable = extension.featureTables[featureTableName];
896
- if (featureTable.class === metadataClass || !metadataClass) {
897
- return getPropertyData(featureTable);
898
- }
899
- }
900
- }
901
- if (extension.featureTextures) {
902
- for (const featureTextureName in extension.featureTextures) {
903
- const featureTexture = extension.featureTextures[featureTextureName];
904
- if (featureTexture.class === metadataClass || !metadataClass) {
905
- return getPropertyData(featureTexture);
906
- }
907
- }
908
- }
909
- return null;
1202
+ /**
1203
+ * Note, 3dTiles is able to have multiple featureId attributes and multiple feature tables.
1204
+ * In I3S we should decide which featureIds attribute will be passed to geometry data.
1205
+ * So, we take only the feature table / feature texture to generate attributes storage info object.
1206
+ * If the user has selected the metadataClass, the table with the corresponding class will be used,
1207
+ * or just the first one otherwise.
1208
+ */
1209
+ if (extension.featureTables) {
1210
+ for (const featureTableName in extension.featureTables) {
1211
+ const featureTable = extension.featureTables[featureTableName];
1212
+ if (featureTable.class === metadataClass || !metadataClass) {
1213
+ return getPropertyData(featureTable);
1214
+ }
1215
+ }
1216
+ }
1217
+ if (extension.featureTextures) {
1218
+ for (const featureTextureName in extension.featureTextures) {
1219
+ const featureTexture = extension.featureTextures[featureTextureName];
1220
+ if (featureTexture.class === metadataClass || !metadataClass) {
1221
+ return getPropertyData(featureTexture);
1222
+ }
1223
+ }
1224
+ }
1225
+ return null;
910
1226
  }
1227
+ /**
1228
+ * Gets data from Property Table or Property Texture
1229
+ * @param featureObject - property table or texture from the extension
1230
+ * @returns Table containing property data
1231
+ */
911
1232
  function getPropertyData(featureObject) {
912
- const propertyTableWithData = {};
913
- for (const propertyName in featureObject.properties) {
914
- propertyTableWithData[propertyName] = featureObject.properties[propertyName].data;
915
- }
916
- return propertyTableWithData;
1233
+ const propertyTableWithData = {};
1234
+ for (const propertyName in featureObject.properties) {
1235
+ propertyTableWithData[propertyName] = featureObject.properties[propertyName].data;
1236
+ }
1237
+ return propertyTableWithData;
917
1238
  }
1239
+ /**
1240
+ * Check extensions which can be with property table inside.
1241
+ * @param tileContent - 3DTiles tile content
1242
+ */
918
1243
  function getPropertyTableExtension(tileContent) {
919
- var _tileContent$gltf3, _tileContent$gltf5, _tileContent$gltf5$ex;
920
- const extensionsWithPropertyTables = [EXT_FEATURE_METADATA, EXT_STRUCTURAL_METADATA];
921
- const extensionsUsed = tileContent === null || tileContent === void 0 ? void 0 : (_tileContent$gltf3 = tileContent.gltf) === null || _tileContent$gltf3 === void 0 ? void 0 : _tileContent$gltf3.extensionsUsed;
922
- if (!extensionsUsed) {
923
- return {
924
- extensionName: null,
925
- extension: null
926
- };
927
- }
928
- let extensionName = '';
929
- for (const extensionItem of (tileContent === null || tileContent === void 0 ? void 0 : (_tileContent$gltf4 = tileContent.gltf) === null || _tileContent$gltf4 === void 0 ? void 0 : _tileContent$gltf4.extensionsUsed) || []) {
930
- var _tileContent$gltf4;
931
- if (extensionsWithPropertyTables.includes(extensionItem)) {
932
- extensionName = extensionItem;
933
- break;
934
- }
935
- }
936
- if (!extensionName) {
937
- return {
938
- extensionName: null,
939
- extension: null
940
- };
941
- }
942
- const extension = tileContent === null || tileContent === void 0 ? void 0 : (_tileContent$gltf5 = tileContent.gltf) === null || _tileContent$gltf5 === void 0 ? void 0 : (_tileContent$gltf5$ex = _tileContent$gltf5.extensions) === null || _tileContent$gltf5$ex === void 0 ? void 0 : _tileContent$gltf5$ex[extensionName];
943
- return {
944
- extensionName,
945
- extension
946
- };
947
- }
948
- //# sourceMappingURL=geometry-converter.js.map
1244
+ const extensionsWithPropertyTables = [EXT_FEATURE_METADATA, EXT_STRUCTURAL_METADATA];
1245
+ const extensionsUsed = tileContent?.gltf?.extensionsUsed;
1246
+ if (!extensionsUsed) {
1247
+ return { extensionName: null, extension: null };
1248
+ }
1249
+ let extensionName = '';
1250
+ for (const extensionItem of tileContent?.gltf?.extensionsUsed || []) {
1251
+ if (extensionsWithPropertyTables.includes(extensionItem)) {
1252
+ extensionName = extensionItem;
1253
+ /*
1254
+ It returns the first extension containing the property table.
1255
+ We assume that there can be only one extension containing the property table:
1256
+ either EXT_FEATURE_METADATA, which is a depricated extension,
1257
+ or EXT_STRUCTURAL_METADATA.
1258
+ */
1259
+ break;
1260
+ }
1261
+ }
1262
+ if (!extensionName) {
1263
+ return { extensionName: null, extension: null };
1264
+ }
1265
+ const extension = tileContent?.gltf?.extensions?.[extensionName];
1266
+ return { extensionName, extension };
1267
+ }