@itwin/core-frontend 4.1.0-dev.6 → 4.1.0-dev.8

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 (123) hide show
  1. package/lib/cjs/BriefcaseTxns.d.ts +2 -2
  2. package/lib/cjs/BriefcaseTxns.d.ts.map +1 -1
  3. package/lib/cjs/BriefcaseTxns.js +3 -3
  4. package/lib/cjs/BriefcaseTxns.js.map +1 -1
  5. package/lib/cjs/GraphicalEditingScope.d.ts +2 -2
  6. package/lib/cjs/GraphicalEditingScope.d.ts.map +1 -1
  7. package/lib/cjs/GraphicalEditingScope.js +1 -1
  8. package/lib/cjs/GraphicalEditingScope.js.map +1 -1
  9. package/lib/cjs/IpcApp.d.ts.map +1 -1
  10. package/lib/cjs/IpcApp.js +3 -3
  11. package/lib/cjs/IpcApp.js.map +1 -1
  12. package/lib/cjs/LocalhostIpcApp.js +1 -1
  13. package/lib/cjs/LocalhostIpcApp.js.map +1 -1
  14. package/lib/cjs/NativeApp.d.ts.map +1 -1
  15. package/lib/cjs/NativeApp.js +3 -3
  16. package/lib/cjs/NativeApp.js.map +1 -1
  17. package/lib/cjs/core-frontend.d.ts +3 -0
  18. package/lib/cjs/core-frontend.d.ts.map +1 -1
  19. package/lib/cjs/core-frontend.js +3 -0
  20. package/lib/cjs/core-frontend.js.map +1 -1
  21. package/lib/cjs/imdl/ImdlGraphicsCreator.d.ts +19 -0
  22. package/lib/cjs/imdl/ImdlGraphicsCreator.d.ts.map +1 -0
  23. package/lib/cjs/imdl/ImdlGraphicsCreator.js +328 -0
  24. package/lib/cjs/imdl/ImdlGraphicsCreator.js.map +1 -0
  25. package/lib/cjs/imdl/ImdlModel.d.ts +181 -0
  26. package/lib/cjs/imdl/ImdlModel.d.ts.map +1 -0
  27. package/lib/cjs/imdl/ImdlModel.js +10 -0
  28. package/lib/cjs/imdl/ImdlModel.js.map +1 -0
  29. package/lib/cjs/imdl/ImdlParser.d.ts +40 -0
  30. package/lib/cjs/imdl/ImdlParser.d.ts.map +1 -0
  31. package/lib/cjs/imdl/ImdlParser.js +996 -0
  32. package/lib/cjs/imdl/ImdlParser.js.map +1 -0
  33. package/lib/cjs/imdl/ImdlSchema.d.ts +380 -0
  34. package/lib/cjs/imdl/ImdlSchema.d.ts.map +1 -0
  35. package/lib/cjs/imdl/ImdlSchema.js +10 -0
  36. package/lib/cjs/imdl/ImdlSchema.js.map +1 -0
  37. package/lib/cjs/render/primitives/AuxChannelTable.d.ts +4 -0
  38. package/lib/cjs/render/primitives/AuxChannelTable.d.ts.map +1 -1
  39. package/lib/cjs/render/primitives/AuxChannelTable.js +33 -0
  40. package/lib/cjs/render/primitives/AuxChannelTable.js.map +1 -1
  41. package/lib/cjs/render/primitives/VertexTable.d.ts +3 -3
  42. package/lib/cjs/render/primitives/VertexTable.d.ts.map +1 -1
  43. package/lib/cjs/render/primitives/VertexTable.js.map +1 -1
  44. package/lib/cjs/render/primitives/VertexTableSplitter.d.ts +5 -1
  45. package/lib/cjs/render/primitives/VertexTableSplitter.d.ts.map +1 -1
  46. package/lib/cjs/render/primitives/VertexTableSplitter.js +13 -12
  47. package/lib/cjs/render/primitives/VertexTableSplitter.js.map +1 -1
  48. package/lib/cjs/tile/IModelTile.d.ts.map +1 -1
  49. package/lib/cjs/tile/IModelTile.js +14 -15
  50. package/lib/cjs/tile/IModelTile.js.map +1 -1
  51. package/lib/cjs/tile/ImdlReader.d.ts +11 -420
  52. package/lib/cjs/tile/ImdlReader.d.ts.map +1 -1
  53. package/lib/cjs/tile/ImdlReader.js +64 -902
  54. package/lib/cjs/tile/ImdlReader.js.map +1 -1
  55. package/lib/cjs/tile/map/MapTile.d.ts +6 -1
  56. package/lib/cjs/tile/map/MapTile.d.ts.map +1 -1
  57. package/lib/cjs/tile/map/MapTile.js +8 -7
  58. package/lib/cjs/tile/map/MapTile.js.map +1 -1
  59. package/lib/cjs/tile/map/MapTileTree.d.ts.map +1 -1
  60. package/lib/cjs/tile/map/MapTileTree.js +8 -2
  61. package/lib/cjs/tile/map/MapTileTree.js.map +1 -1
  62. package/lib/esm/BriefcaseTxns.d.ts +2 -2
  63. package/lib/esm/BriefcaseTxns.d.ts.map +1 -1
  64. package/lib/esm/BriefcaseTxns.js +3 -3
  65. package/lib/esm/BriefcaseTxns.js.map +1 -1
  66. package/lib/esm/GraphicalEditingScope.d.ts +2 -2
  67. package/lib/esm/GraphicalEditingScope.d.ts.map +1 -1
  68. package/lib/esm/GraphicalEditingScope.js +2 -2
  69. package/lib/esm/GraphicalEditingScope.js.map +1 -1
  70. package/lib/esm/IpcApp.d.ts.map +1 -1
  71. package/lib/esm/IpcApp.js +4 -4
  72. package/lib/esm/IpcApp.js.map +1 -1
  73. package/lib/esm/LocalhostIpcApp.js +2 -2
  74. package/lib/esm/LocalhostIpcApp.js.map +1 -1
  75. package/lib/esm/NativeApp.d.ts.map +1 -1
  76. package/lib/esm/NativeApp.js +4 -4
  77. package/lib/esm/NativeApp.js.map +1 -1
  78. package/lib/esm/core-frontend.d.ts +3 -0
  79. package/lib/esm/core-frontend.d.ts.map +1 -1
  80. package/lib/esm/core-frontend.js +3 -0
  81. package/lib/esm/core-frontend.js.map +1 -1
  82. package/lib/esm/imdl/ImdlGraphicsCreator.d.ts +19 -0
  83. package/lib/esm/imdl/ImdlGraphicsCreator.d.ts.map +1 -0
  84. package/lib/esm/imdl/ImdlGraphicsCreator.js +324 -0
  85. package/lib/esm/imdl/ImdlGraphicsCreator.js.map +1 -0
  86. package/lib/esm/imdl/ImdlModel.d.ts +181 -0
  87. package/lib/esm/imdl/ImdlModel.d.ts.map +1 -0
  88. package/lib/esm/imdl/ImdlModel.js +9 -0
  89. package/lib/esm/imdl/ImdlModel.js.map +1 -0
  90. package/lib/esm/imdl/ImdlParser.d.ts +40 -0
  91. package/lib/esm/imdl/ImdlParser.d.ts.map +1 -0
  92. package/lib/esm/imdl/ImdlParser.js +988 -0
  93. package/lib/esm/imdl/ImdlParser.js.map +1 -0
  94. package/lib/esm/imdl/ImdlSchema.d.ts +380 -0
  95. package/lib/esm/imdl/ImdlSchema.d.ts.map +1 -0
  96. package/lib/esm/imdl/ImdlSchema.js +9 -0
  97. package/lib/esm/imdl/ImdlSchema.js.map +1 -0
  98. package/lib/esm/render/primitives/AuxChannelTable.d.ts +4 -0
  99. package/lib/esm/render/primitives/AuxChannelTable.d.ts.map +1 -1
  100. package/lib/esm/render/primitives/AuxChannelTable.js +33 -0
  101. package/lib/esm/render/primitives/AuxChannelTable.js.map +1 -1
  102. package/lib/esm/render/primitives/VertexTable.d.ts +3 -3
  103. package/lib/esm/render/primitives/VertexTable.d.ts.map +1 -1
  104. package/lib/esm/render/primitives/VertexTable.js.map +1 -1
  105. package/lib/esm/render/primitives/VertexTableSplitter.d.ts +5 -1
  106. package/lib/esm/render/primitives/VertexTableSplitter.d.ts.map +1 -1
  107. package/lib/esm/render/primitives/VertexTableSplitter.js +13 -12
  108. package/lib/esm/render/primitives/VertexTableSplitter.js.map +1 -1
  109. package/lib/esm/tile/IModelTile.d.ts.map +1 -1
  110. package/lib/esm/tile/IModelTile.js +14 -15
  111. package/lib/esm/tile/IModelTile.js.map +1 -1
  112. package/lib/esm/tile/ImdlReader.d.ts +11 -420
  113. package/lib/esm/tile/ImdlReader.d.ts.map +1 -1
  114. package/lib/esm/tile/ImdlReader.js +67 -903
  115. package/lib/esm/tile/ImdlReader.js.map +1 -1
  116. package/lib/esm/tile/map/MapTile.d.ts +6 -1
  117. package/lib/esm/tile/map/MapTile.d.ts.map +1 -1
  118. package/lib/esm/tile/map/MapTile.js +8 -7
  119. package/lib/esm/tile/map/MapTile.js.map +1 -1
  120. package/lib/esm/tile/map/MapTileTree.d.ts.map +1 -1
  121. package/lib/esm/tile/map/MapTileTree.js +8 -2
  122. package/lib/esm/tile/map/MapTileTree.js.map +1 -1
  123. package/package.json +18 -18
@@ -0,0 +1,996 @@
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module Tiles
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.parseImdlDocument = exports.convertFeatureTable = exports.toMaterialArgs = exports.edgeParamsFromImdl = exports.toVertexTable = void 0;
11
+ const core_bentley_1 = require("@itwin/core-bentley");
12
+ const core_geometry_1 = require("@itwin/core-geometry");
13
+ const core_common_1 = require("@itwin/core-common");
14
+ const MeshPrimitives_1 = require("../render/primitives/mesh/MeshPrimitives");
15
+ const SurfaceParams_1 = require("../render/primitives/SurfaceParams");
16
+ const DisplayParams_1 = require("../render/primitives/DisplayParams");
17
+ const AuxChannelTable_1 = require("../render/primitives/AuxChannelTable");
18
+ const VertexTableSplitter_1 = require("../render/primitives/VertexTableSplitter");
19
+ const GraphicBranch_1 = require("../render/GraphicBranch");
20
+ const VertexTable_1 = require("../render/primitives/VertexTable");
21
+ /** Header preceding "glTF" data in iMdl tile. */
22
+ class GltfHeader extends core_common_1.TileHeader {
23
+ get isValid() { return core_common_1.TileFormat.Gltf === this.format; }
24
+ constructor(stream) {
25
+ super(stream);
26
+ this.scenePosition = 0;
27
+ this.sceneStrLength = 0;
28
+ this.binaryPosition = 0;
29
+ this.gltfLength = stream.readUint32();
30
+ this.sceneStrLength = stream.readUint32();
31
+ const value5 = stream.readUint32();
32
+ // Early versions of the reality data tile publisher incorrectly put version 2 into header - handle these old tiles
33
+ // validating the chunk type.
34
+ if (this.version === core_common_1.GltfVersions.Version2 && value5 === core_common_1.GltfVersions.Gltf1SceneFormat)
35
+ this.version = core_common_1.GltfVersions.Version1;
36
+ if (this.version === core_common_1.GltfVersions.Version1) {
37
+ const gltfSceneFormat = value5;
38
+ if (core_common_1.GltfVersions.Gltf1SceneFormat !== gltfSceneFormat) {
39
+ this.invalidate();
40
+ return;
41
+ }
42
+ this.scenePosition = stream.curPos;
43
+ this.binaryPosition = stream.curPos + this.sceneStrLength;
44
+ }
45
+ else if (this.version === core_common_1.GltfVersions.Version2) {
46
+ const sceneChunkType = value5;
47
+ this.scenePosition = stream.curPos;
48
+ stream.curPos = stream.curPos + this.sceneStrLength;
49
+ const binaryLength = stream.readUint32();
50
+ const binaryChunkType = stream.readUint32();
51
+ if (core_common_1.GltfV2ChunkTypes.JSON !== sceneChunkType || core_common_1.GltfV2ChunkTypes.Binary !== binaryChunkType || 0 === binaryLength) {
52
+ this.invalidate();
53
+ return;
54
+ }
55
+ this.binaryPosition = stream.curPos;
56
+ }
57
+ else {
58
+ this.invalidate();
59
+ }
60
+ }
61
+ }
62
+ const nodeIdRegex = /Node_(.*)/;
63
+ function extractNodeId(nodeName) {
64
+ const match = nodeName.match(nodeIdRegex);
65
+ (0, core_bentley_1.assert)(!!match && match.length === 2);
66
+ if (!match || match.length !== 2)
67
+ return 0;
68
+ const nodeId = Number.parseInt(match[1], 10);
69
+ (0, core_bentley_1.assert)(!Number.isNaN(nodeId));
70
+ return Number.isNaN(nodeId) ? 0 : nodeId;
71
+ }
72
+ class Texture extends core_common_1.RenderTexture {
73
+ constructor(type) {
74
+ super(type);
75
+ }
76
+ dispose() { }
77
+ get bytesUsed() { return 0; }
78
+ }
79
+ class NamedTexture extends Texture {
80
+ constructor(_name, type) {
81
+ super(type);
82
+ this._name = _name;
83
+ }
84
+ toImdl() {
85
+ return this._name;
86
+ }
87
+ }
88
+ class GradientTexture extends Texture {
89
+ constructor(_gradient) {
90
+ super(core_common_1.RenderTexture.Type.Normal);
91
+ this._gradient = _gradient;
92
+ }
93
+ toImdl() {
94
+ return this._gradient;
95
+ }
96
+ }
97
+ class Material extends core_common_1.RenderMaterial {
98
+ toImdl() {
99
+ const material = this.key ?? this.materialParams;
100
+ return { isAtlas: false, material };
101
+ }
102
+ // eslint-disable-next-line deprecation/deprecation
103
+ constructor(params, imdl) {
104
+ super(params);
105
+ this.materialParams = imdl ?? {
106
+ alpha: params.alpha,
107
+ diffuse: {
108
+ color: params.diffuseColor?.toJSON(),
109
+ weight: params.diffuse,
110
+ },
111
+ specular: {
112
+ color: params.specularColor?.toJSON(),
113
+ weight: params.specular,
114
+ exponent: params.specularExponent,
115
+ },
116
+ };
117
+ }
118
+ static create(args) {
119
+ // eslint-disable-next-line deprecation/deprecation
120
+ const params = new core_common_1.RenderMaterial.Params();
121
+ params.alpha = args.alpha;
122
+ if (args.diffuse) {
123
+ if (undefined !== args.diffuse.weight)
124
+ params.diffuse = args.diffuse?.weight;
125
+ if (args.diffuse?.color)
126
+ params.diffuseColor = args.diffuse.color instanceof core_common_1.ColorDef ? args.diffuse.color : core_common_1.RgbColor.fromJSON(args.diffuse.color).toColorDef();
127
+ }
128
+ if (args.specular) {
129
+ if (undefined !== args.specular.weight)
130
+ params.specular = args.specular.weight;
131
+ if (undefined !== args.specular.exponent)
132
+ params.specularExponent = args.specular.exponent;
133
+ if (args.specular.color)
134
+ params.specularColor = args.specular.color instanceof core_common_1.ColorDef ? args.specular.color : core_common_1.RgbColor.fromJSON(args.specular.color).toColorDef();
135
+ }
136
+ return new Material(params);
137
+ }
138
+ }
139
+ /** @internal */
140
+ function toVertexTable(imdl) {
141
+ return new VertexTable_1.VertexTable({
142
+ ...imdl,
143
+ uniformColor: undefined !== imdl.uniformColor ? core_common_1.ColorDef.fromJSON(imdl.uniformColor) : undefined,
144
+ qparams: core_common_1.QParams3d.fromJSON(imdl.qparams),
145
+ uvParams: imdl.uvParams ? core_common_1.QParams2d.fromJSON(imdl.uvParams) : undefined,
146
+ });
147
+ }
148
+ exports.toVertexTable = toVertexTable;
149
+ function fromVertexTable(table) {
150
+ return {
151
+ ...table,
152
+ uniformColor: table.uniformColor?.toJSON(),
153
+ qparams: table.qparams.toJSON(),
154
+ uvParams: table.uvParams?.toJSON(),
155
+ };
156
+ }
157
+ /** @internal */
158
+ function edgeParamsFromImdl(imdl) {
159
+ return {
160
+ ...imdl,
161
+ segments: imdl.segments ? {
162
+ ...imdl.segments,
163
+ indices: new VertexTable_1.VertexIndices(imdl.segments.indices),
164
+ } : undefined,
165
+ silhouettes: imdl.silhouettes ? {
166
+ ...imdl.silhouettes,
167
+ indices: new VertexTable_1.VertexIndices(imdl.silhouettes.indices),
168
+ } : undefined,
169
+ polylines: imdl.polylines ? {
170
+ ...imdl.polylines,
171
+ indices: new VertexTable_1.VertexIndices(imdl.polylines.indices),
172
+ prevIndices: new VertexTable_1.VertexIndices(imdl.polylines.prevIndices),
173
+ } : undefined,
174
+ indexed: imdl.indexed ? {
175
+ indices: new VertexTable_1.VertexIndices(imdl.indexed.indices),
176
+ edges: imdl.indexed.edges,
177
+ } : undefined,
178
+ };
179
+ }
180
+ exports.edgeParamsFromImdl = edgeParamsFromImdl;
181
+ function edgeParamsToImdl(params) {
182
+ return {
183
+ ...params,
184
+ segments: params.segments ? {
185
+ ...params.segments,
186
+ indices: params.segments.indices.data,
187
+ } : undefined,
188
+ silhouettes: params.silhouettes ? {
189
+ ...params.silhouettes,
190
+ indices: params.silhouettes.indices.data,
191
+ } : undefined,
192
+ polylines: params.polylines ? {
193
+ ...params.polylines,
194
+ indices: params.polylines.indices.data,
195
+ prevIndices: params.polylines.prevIndices.data,
196
+ } : undefined,
197
+ indexed: params.indexed ? {
198
+ indices: params.indexed.indices.data,
199
+ edges: params.indexed.edges,
200
+ } : undefined,
201
+ };
202
+ }
203
+ class ImdlParser {
204
+ get _stream() {
205
+ return this._options.stream;
206
+ }
207
+ constructor(doc, binaryData, options, featureTableInfo) {
208
+ this._patterns = new Map();
209
+ this._document = doc;
210
+ this._binaryData = binaryData;
211
+ this._options = options;
212
+ this._featureTableInfo = featureTableInfo;
213
+ }
214
+ parse() {
215
+ const featureTable = this.parseFeatureTable();
216
+ if (!featureTable)
217
+ return core_common_1.TileReadStatus.InvalidFeatureTable;
218
+ const rtcCenter = this._document.rtcCenter ? {
219
+ x: this._document.rtcCenter[0] ?? 0,
220
+ y: this._document.rtcCenter[1] ?? 0,
221
+ z: this._document.rtcCenter[2] ?? 0,
222
+ } : undefined;
223
+ const nodes = this.parseNodes(featureTable);
224
+ return {
225
+ featureTable,
226
+ nodes,
227
+ rtcCenter,
228
+ binaryData: this._binaryData,
229
+ json: this._document,
230
+ patterns: this._patterns,
231
+ };
232
+ }
233
+ parseFeatureTable() {
234
+ this._stream.curPos = this._featureTableInfo.startPos;
235
+ const header = core_common_1.FeatureTableHeader.readFrom(this._stream);
236
+ if (!header || 0 !== header.length % 4)
237
+ return undefined;
238
+ // NB: We make a copy of the sub-array because we don't want to pin the entire data array in memory.
239
+ const numUint32s = (header.length - core_common_1.FeatureTableHeader.sizeInBytes) / 4;
240
+ const packedFeatureArray = new Uint32Array(this._stream.nextUint32s(numUint32s));
241
+ if (this._stream.isPastTheEnd)
242
+ return undefined;
243
+ let featureTable;
244
+ if (this._featureTableInfo.multiModel) {
245
+ featureTable = {
246
+ multiModel: true,
247
+ data: packedFeatureArray,
248
+ numFeatures: header.count,
249
+ numSubCategories: header.numSubCategories,
250
+ };
251
+ }
252
+ else {
253
+ let animNodesArray;
254
+ const animationNodes = this._document.animationNodes;
255
+ if (undefined !== animationNodes) {
256
+ const bytesPerId = core_bentley_1.JsonUtils.asInt(animationNodes.bytesPerId);
257
+ const bufferViewId = core_bentley_1.JsonUtils.asString(animationNodes.bufferView);
258
+ const bufferViewJson = this._document.bufferViews[bufferViewId];
259
+ if (undefined !== bufferViewJson) {
260
+ const byteOffset = core_bentley_1.JsonUtils.asInt(bufferViewJson.byteOffset);
261
+ const byteLength = core_bentley_1.JsonUtils.asInt(bufferViewJson.byteLength);
262
+ const bytes = this._binaryData.subarray(byteOffset, byteOffset + byteLength);
263
+ switch (bytesPerId) {
264
+ case 1:
265
+ animNodesArray = new Uint8Array(bytes);
266
+ break;
267
+ case 2:
268
+ // NB: A *copy* of the subarray.
269
+ animNodesArray = Uint16Array.from(new Uint16Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 2));
270
+ break;
271
+ case 4:
272
+ // NB: A *copy* of the subarray.
273
+ animNodesArray = Uint32Array.from(new Uint32Array(bytes.buffer, bytes.byteOffset, bytes.byteLength / 4));
274
+ break;
275
+ }
276
+ }
277
+ }
278
+ featureTable = {
279
+ multiModel: false,
280
+ data: packedFeatureArray,
281
+ numFeatures: header.count,
282
+ animationNodeIds: animNodesArray,
283
+ };
284
+ }
285
+ this._stream.curPos = this._featureTableInfo.startPos + header.length;
286
+ return featureTable;
287
+ }
288
+ parseNodes(featureTable) {
289
+ const nodes = [];
290
+ const docNodes = this._document.nodes;
291
+ const docMeshes = this._document.meshes;
292
+ if (undefined === docNodes.Node_Root) {
293
+ // A veeeery early version of the tile format (prior to introduction of schedule animation support) just supplied a flat list of meshes.
294
+ // We shall never encounter such tiles again.
295
+ return nodes;
296
+ }
297
+ for (const nodeKey of Object.keys(docNodes)) {
298
+ const docNode = this._document.nodes[nodeKey];
299
+ (0, core_bentley_1.assert)(undefined !== docNode); // we're iterating the keys...
300
+ const docMesh = docMeshes[docNode];
301
+ const docPrimitives = docMesh?.primitives;
302
+ if (!docPrimitives)
303
+ continue;
304
+ const layerId = docMesh.layer;
305
+ if ("Node_Root" === nodeKey) {
306
+ if (this._options.timeline) {
307
+ // Split up the root node into transform nodes.
308
+ this.parseAnimationBranches(nodes, docMesh, featureTable, this._options.timeline);
309
+ }
310
+ else if (this._options.createUntransformedRootNode) {
311
+ // If transform nodes exist in the tile tree, then we need to create a branch for the root node so that elements not associated with
312
+ // any node in the schedule script can be grouped together.
313
+ nodes.push({
314
+ animationNodeId: GraphicBranch_1.AnimationNodeId.Untransformed,
315
+ primitives: this.parseNodePrimitives(docPrimitives),
316
+ });
317
+ }
318
+ else {
319
+ nodes.push({ primitives: this.parseNodePrimitives(docPrimitives) });
320
+ }
321
+ }
322
+ else if (undefined === layerId) {
323
+ nodes.push({
324
+ animationNodeId: extractNodeId(nodeKey),
325
+ animationId: `${this._options.batchModelId}_${nodeKey}`,
326
+ primitives: this.parseNodePrimitives(docPrimitives),
327
+ });
328
+ }
329
+ else {
330
+ nodes.push({
331
+ layerId,
332
+ primitives: this.parseNodePrimitives(docPrimitives),
333
+ });
334
+ }
335
+ }
336
+ return nodes;
337
+ }
338
+ parseAnimationBranches(output, docMesh, imdlFeatureTable, timeline) {
339
+ const docPrimitives = docMesh.primitives;
340
+ if (!docPrimitives)
341
+ return;
342
+ const nodesById = new Map();
343
+ const getNode = (nodeId) => {
344
+ let node = nodesById.get(nodeId);
345
+ if (!node) {
346
+ node = {
347
+ animationNodeId: nodeId,
348
+ animationId: `${this._options.batchModelId}_Node_${nodeId}`,
349
+ primitives: [],
350
+ };
351
+ nodesById.set(nodeId, node);
352
+ output.push(node);
353
+ }
354
+ return node;
355
+ };
356
+ // NB: The BatchType is irrelevant - just use Primary.
357
+ (0, core_bentley_1.assert)(undefined === imdlFeatureTable.animationNodeIds);
358
+ const featureTable = convertFeatureTable(imdlFeatureTable, this._options.batchModelId);
359
+ featureTable.populateAnimationNodeIds((feature) => timeline.getBatchIdForFeature(feature), timeline.maxBatchId);
360
+ imdlFeatureTable.animationNodeIds = featureTable.animationNodeIds;
361
+ const discreteNodeIds = timeline.discreteBatchIds;
362
+ const computeNodeId = (featureIndex) => {
363
+ const nodeId = featureTable.getAnimationNodeId(featureIndex);
364
+ return 0 !== nodeId && discreteNodeIds.has(nodeId) ? nodeId : 0;
365
+ };
366
+ const splitArgs = {
367
+ maxDimension: this._options.maxVertexTableSize,
368
+ computeNodeId,
369
+ featureTable,
370
+ };
371
+ const convertMaterial = (imdl) => {
372
+ if (!imdl)
373
+ return undefined;
374
+ else if (imdl.isAtlas)
375
+ return imdl;
376
+ const material = (typeof imdl.material === "string") ? this.materialFromJson(imdl.material) : Material.create(toMaterialArgs(imdl.material));
377
+ return material ? { isAtlas: false, material } : undefined;
378
+ };
379
+ for (const docPrimitive of docPrimitives) {
380
+ const primitive = this.parseNodePrimitive(docPrimitive);
381
+ if (!primitive)
382
+ continue;
383
+ switch (primitive.type) {
384
+ case "pattern":
385
+ // ###TODO animated area patterns
386
+ getNode(GraphicBranch_1.AnimationNodeId.Untransformed).primitives.push(primitive);
387
+ break;
388
+ case "mesh": {
389
+ const mesh = primitive.params;
390
+ const texMap = mesh.surface.textureMapping;
391
+ const params = {
392
+ vertices: toVertexTable(primitive.params.vertices),
393
+ surface: {
394
+ ...primitive.params.surface,
395
+ indices: new VertexTable_1.VertexIndices(primitive.params.surface.indices),
396
+ material: convertMaterial(mesh.surface.material),
397
+ textureMapping: texMap ? {
398
+ alwaysDisplayed: texMap.alwaysDisplayed,
399
+ // The texture type doesn't actually matter here.
400
+ texture: typeof texMap.texture === "string" ? new NamedTexture(texMap.texture, core_common_1.RenderTexture.Type.Normal) : new GradientTexture(texMap.texture),
401
+ } : undefined,
402
+ },
403
+ edges: primitive.params.edges ? edgeParamsFromImdl(primitive.params.edges) : undefined,
404
+ isPlanar: primitive.params.isPlanar,
405
+ auxChannels: primitive.params.auxChannels ? AuxChannelTable_1.AuxChannelTable.fromJSON(primitive.params.auxChannels) : undefined,
406
+ };
407
+ const split = (0, VertexTableSplitter_1.splitMeshParams)({
408
+ ...splitArgs,
409
+ params,
410
+ createMaterial: (args) => Material.create(args),
411
+ });
412
+ for (const [nodeId, p] of split) {
413
+ let material;
414
+ if (p.surface.material) {
415
+ if (p.surface.material.isAtlas) {
416
+ material = p.surface.material;
417
+ }
418
+ else {
419
+ (0, core_bentley_1.assert)(p.surface.material.material instanceof Material);
420
+ material = p.surface.material.material.toImdl();
421
+ }
422
+ }
423
+ (0, core_bentley_1.assert)(p.surface.textureMapping === undefined || p.surface.textureMapping.texture instanceof Texture);
424
+ getNode(nodeId).primitives.push({
425
+ type: "mesh",
426
+ params: {
427
+ vertices: fromVertexTable(p.vertices),
428
+ surface: {
429
+ ...p.surface,
430
+ indices: p.surface.indices.data,
431
+ material,
432
+ textureMapping: p.surface.textureMapping?.texture instanceof Texture ? {
433
+ texture: p.surface.textureMapping.texture.toImdl(),
434
+ alwaysDisplayed: p.surface.textureMapping.alwaysDisplayed,
435
+ } : undefined,
436
+ },
437
+ edges: p.edges ? edgeParamsToImdl(p.edges) : undefined,
438
+ isPlanar: p.isPlanar,
439
+ auxChannels: p.auxChannels?.toJSON(),
440
+ },
441
+ });
442
+ }
443
+ break;
444
+ }
445
+ case "point": {
446
+ const params = {
447
+ vertices: toVertexTable(primitive.params.vertices),
448
+ indices: new VertexTable_1.VertexIndices(primitive.params.indices),
449
+ weight: primitive.params.weight,
450
+ };
451
+ const split = (0, VertexTableSplitter_1.splitPointStringParams)({ ...splitArgs, params });
452
+ for (const [nodeId, p] of split) {
453
+ getNode(nodeId).primitives.push({
454
+ type: "point",
455
+ params: {
456
+ vertices: fromVertexTable(p.vertices),
457
+ indices: p.indices.data,
458
+ weight: p.weight,
459
+ },
460
+ });
461
+ }
462
+ break;
463
+ }
464
+ case "polyline": {
465
+ const params = {
466
+ ...primitive.params,
467
+ vertices: toVertexTable(primitive.params.vertices),
468
+ polyline: {
469
+ indices: new VertexTable_1.VertexIndices(primitive.params.polyline.indices),
470
+ prevIndices: new VertexTable_1.VertexIndices(primitive.params.polyline.prevIndices),
471
+ nextIndicesAndParams: primitive.params.polyline.nextIndicesAndParams,
472
+ },
473
+ };
474
+ const split = (0, VertexTableSplitter_1.splitPolylineParams)({ ...splitArgs, params });
475
+ for (const [nodeId, p] of split) {
476
+ getNode(nodeId).primitives.push({
477
+ type: "polyline",
478
+ params: {
479
+ ...p,
480
+ vertices: fromVertexTable(p.vertices),
481
+ polyline: {
482
+ indices: p.polyline.indices.data,
483
+ prevIndices: p.polyline.prevIndices.data,
484
+ nextIndicesAndParams: p.polyline.nextIndicesAndParams,
485
+ },
486
+ },
487
+ });
488
+ }
489
+ break;
490
+ }
491
+ }
492
+ }
493
+ }
494
+ parseTesselatedPolyline(json) {
495
+ const indices = this.findBuffer(json.indices);
496
+ const prevIndices = this.findBuffer(json.prevIndices);
497
+ const nextIndicesAndParams = this.findBuffer(json.nextIndicesAndParams);
498
+ return indices && prevIndices && nextIndicesAndParams ? { indices, prevIndices, nextIndicesAndParams } : undefined;
499
+ }
500
+ parseSegmentEdges(imdl) {
501
+ const indices = this.findBuffer(imdl.indices);
502
+ const endPointAndQuadIndices = this.findBuffer(imdl.endPointAndQuadIndices);
503
+ return indices && endPointAndQuadIndices ? { indices, endPointAndQuadIndices } : undefined;
504
+ }
505
+ parseSilhouetteEdges(imdl) {
506
+ const segments = this.parseSegmentEdges(imdl);
507
+ const normalPairs = this.findBuffer(imdl.normalPairs);
508
+ return segments && normalPairs ? { ...segments, normalPairs } : undefined;
509
+ }
510
+ parseIndexedEdges(imdl) {
511
+ const indices = this.findBuffer(imdl.indices);
512
+ const edgeTable = this.findBuffer(imdl.edges);
513
+ if (!indices || !edgeTable)
514
+ return undefined;
515
+ return {
516
+ indices,
517
+ edges: {
518
+ data: edgeTable,
519
+ width: imdl.width,
520
+ height: imdl.height,
521
+ silhouettePadding: imdl.silhouettePadding,
522
+ numSegments: imdl.numSegments,
523
+ },
524
+ };
525
+ }
526
+ parseEdges(imdl, displayParams) {
527
+ if (!imdl)
528
+ return undefined;
529
+ const segments = imdl.segments ? this.parseSegmentEdges(imdl.segments) : undefined;
530
+ const silhouettes = imdl.silhouettes ? this.parseSilhouetteEdges(imdl.silhouettes) : undefined;
531
+ const indexed = imdl.indexed ? this.parseIndexedEdges(imdl.indexed) : undefined;
532
+ const polylines = imdl.polylines ? this.parseTesselatedPolyline(imdl.polylines) : undefined;
533
+ if (!segments && !silhouettes && !indexed && !polylines)
534
+ return undefined;
535
+ return {
536
+ segments,
537
+ silhouettes,
538
+ polylines,
539
+ indexed,
540
+ weight: displayParams.width,
541
+ linePixels: displayParams.linePixels,
542
+ };
543
+ }
544
+ getPattern(name) {
545
+ let primitives = this._patterns.get(name);
546
+ if (!primitives) {
547
+ const symbol = this._document.patternSymbols[name];
548
+ primitives = symbol ? this.parsePrimitives(symbol.primitives) : [];
549
+ this._patterns.set(name, primitives);
550
+ }
551
+ return primitives.length > 0 ? primitives : undefined;
552
+ }
553
+ parseAreaPattern(json) {
554
+ const primitives = this.getPattern(json.symbolName);
555
+ if (!primitives || primitives.length === 0)
556
+ return undefined;
557
+ const xyOffsets = this.findBuffer(json.xyOffsets);
558
+ if (!xyOffsets)
559
+ return undefined;
560
+ return {
561
+ type: "pattern",
562
+ params: {
563
+ ...json,
564
+ xyOffsets: new Float32Array(xyOffsets.buffer, xyOffsets.byteOffset, xyOffsets.byteLength / 4),
565
+ },
566
+ };
567
+ }
568
+ parseNodePrimitives(docPrimitives) {
569
+ const primitives = [];
570
+ for (const docPrimitive of docPrimitives) {
571
+ const primitive = this.parseNodePrimitive(docPrimitive);
572
+ if (primitive)
573
+ primitives.push(primitive);
574
+ }
575
+ return primitives;
576
+ }
577
+ parseNodePrimitive(docPrimitive) {
578
+ return docPrimitive.type === "areaPattern" ? this.parseAreaPattern(docPrimitive) : this.parsePrimitive(docPrimitive);
579
+ }
580
+ parsePrimitives(docPrimitives) {
581
+ const primitives = [];
582
+ for (const docPrimitive of docPrimitives) {
583
+ const primitive = this.parsePrimitive(docPrimitive);
584
+ if (primitive)
585
+ primitives.push(primitive);
586
+ }
587
+ return primitives;
588
+ }
589
+ parsePrimitive(docPrimitive) {
590
+ let modifier = this.parseInstances(docPrimitive);
591
+ if (!modifier && docPrimitive.viewIndependentOrigin) {
592
+ const origin = core_geometry_1.Point3d.fromJSON(docPrimitive.viewIndependentOrigin);
593
+ modifier = {
594
+ type: "viewIndependentOrigin",
595
+ origin: { x: origin.x, y: origin.y, z: origin.z },
596
+ };
597
+ }
598
+ const materialName = docPrimitive.material ?? "";
599
+ const dpMaterial = materialName.length ? core_bentley_1.JsonUtils.asObject(this._document.materials[materialName]) : undefined;
600
+ const displayParams = dpMaterial ? this.parseDisplayParams(dpMaterial) : undefined;
601
+ if (!displayParams)
602
+ return undefined;
603
+ const vertices = this.parseVertexTable(docPrimitive);
604
+ if (!vertices)
605
+ return undefined;
606
+ let primitive;
607
+ const isPlanar = !this._options.is3d || core_bentley_1.JsonUtils.asBool(docPrimitive.isPlanar);
608
+ switch (docPrimitive.type) {
609
+ case MeshPrimitives_1.Mesh.PrimitiveType.Mesh: {
610
+ const surface = this.parseSurface(docPrimitive, displayParams);
611
+ if (surface) {
612
+ primitive = {
613
+ type: "mesh",
614
+ params: {
615
+ vertices,
616
+ surface,
617
+ isPlanar,
618
+ auxChannels: this.parseAuxChannelTable(docPrimitive),
619
+ edges: this.parseEdges(docPrimitive.edges, displayParams),
620
+ },
621
+ };
622
+ }
623
+ break;
624
+ }
625
+ case MeshPrimitives_1.Mesh.PrimitiveType.Polyline: {
626
+ const polyline = this.parseTesselatedPolyline(docPrimitive);
627
+ if (polyline) {
628
+ let type = core_common_1.PolylineTypeFlags.Normal;
629
+ if (DisplayParams_1.DisplayParams.RegionEdgeType.Outline === displayParams.regionEdgeType)
630
+ type = (!displayParams.gradient || displayParams.gradient.isOutlined) ? core_common_1.PolylineTypeFlags.Edge : core_common_1.PolylineTypeFlags.Outline;
631
+ primitive = {
632
+ type: "polyline",
633
+ params: {
634
+ vertices,
635
+ polyline,
636
+ isPlanar,
637
+ type,
638
+ weight: displayParams.width,
639
+ linePixels: displayParams.linePixels,
640
+ },
641
+ };
642
+ }
643
+ break;
644
+ }
645
+ case MeshPrimitives_1.Mesh.PrimitiveType.Point: {
646
+ const indices = this.findBuffer(docPrimitive.indices);
647
+ const weight = displayParams.width;
648
+ if (indices) {
649
+ primitive = {
650
+ type: "point",
651
+ params: { vertices, indices, weight },
652
+ };
653
+ }
654
+ break;
655
+ }
656
+ }
657
+ if (primitive)
658
+ primitive.modifier = modifier;
659
+ return primitive;
660
+ }
661
+ parseSurface(mesh, displayParams) {
662
+ const surf = mesh.surface;
663
+ if (!surf)
664
+ return undefined;
665
+ const indices = this.findBuffer(surf.indices);
666
+ if (!indices)
667
+ return undefined;
668
+ const type = surf.type;
669
+ if (!(0, SurfaceParams_1.isValidSurfaceType)(type))
670
+ return undefined;
671
+ const texture = displayParams.textureMapping?.texture;
672
+ let material;
673
+ const atlas = mesh.vertices.materialAtlas;
674
+ const numColors = mesh.vertices.numColors;
675
+ if (atlas && undefined !== numColors) {
676
+ material = {
677
+ isAtlas: true,
678
+ hasTranslucency: core_bentley_1.JsonUtils.asBool(atlas.hasTranslucency),
679
+ overridesAlpha: core_bentley_1.JsonUtils.asBool(atlas.overridesAlpha, false),
680
+ vertexTableOffset: core_bentley_1.JsonUtils.asInt(numColors),
681
+ numMaterials: core_bentley_1.JsonUtils.asInt(atlas.numMaterials),
682
+ };
683
+ }
684
+ else if (displayParams.material) {
685
+ (0, core_bentley_1.assert)(displayParams.material instanceof Material);
686
+ material = displayParams.material.toImdl();
687
+ }
688
+ let textureMapping;
689
+ if (texture) {
690
+ (0, core_bentley_1.assert)(texture instanceof Texture);
691
+ textureMapping = {
692
+ texture: texture.toImdl(),
693
+ alwaysDisplayed: core_bentley_1.JsonUtils.asBool(surf.alwaysDisplayTexture),
694
+ };
695
+ }
696
+ return {
697
+ type,
698
+ indices,
699
+ fillFlags: displayParams.fillFlags,
700
+ hasBakedLighting: false,
701
+ material,
702
+ textureMapping,
703
+ };
704
+ }
705
+ parseAuxChannelTable(primitive) {
706
+ const json = primitive.auxChannels;
707
+ if (undefined === json)
708
+ return undefined;
709
+ const bytes = this.findBuffer(core_bentley_1.JsonUtils.asString(json.bufferView));
710
+ if (undefined === bytes)
711
+ return undefined;
712
+ return {
713
+ data: bytes,
714
+ width: json.width,
715
+ height: json.height,
716
+ count: json.count,
717
+ numBytesPerVertex: json.numBytesPerVertex,
718
+ displacements: json.displacements,
719
+ normals: json.normals,
720
+ params: json.params,
721
+ };
722
+ }
723
+ parseVertexTable(primitive) {
724
+ const json = primitive.vertices;
725
+ if (!json)
726
+ return undefined;
727
+ const bytes = this.findBuffer(core_bentley_1.JsonUtils.asString(json.bufferView));
728
+ if (!bytes)
729
+ return undefined;
730
+ const uniformFeatureID = undefined !== json.featureID ? core_bentley_1.JsonUtils.asInt(json.featureID) : undefined;
731
+ const rangeMin = core_bentley_1.JsonUtils.asArray(json.params.decodedMin);
732
+ const rangeMax = core_bentley_1.JsonUtils.asArray(json.params.decodedMax);
733
+ if (undefined === rangeMin || undefined === rangeMax)
734
+ return undefined;
735
+ const qparams = core_common_1.QParams3d.fromRange(core_geometry_1.Range3d.create(core_geometry_1.Point3d.create(rangeMin[0], rangeMin[1], rangeMin[2]), core_geometry_1.Point3d.create(rangeMax[0], rangeMax[1], rangeMax[2])));
736
+ const uniformColor = undefined !== json.uniformColor ? core_common_1.ColorDef.fromJSON(json.uniformColor) : undefined;
737
+ let uvParams;
738
+ if (MeshPrimitives_1.Mesh.PrimitiveType.Mesh === primitive.type && primitive.surface && primitive.surface.uvParams) {
739
+ const uvMin = primitive.surface.uvParams.decodedMin;
740
+ const uvMax = primitive.surface.uvParams.decodedMax;
741
+ const uvRange = new core_geometry_1.Range2d(uvMin[0], uvMin[1], uvMax[0], uvMax[1]);
742
+ uvParams = core_common_1.QParams2d.fromRange(uvRange);
743
+ }
744
+ return {
745
+ data: bytes,
746
+ usesUnquantizedPositions: true === json.usesUnquantizedPositions,
747
+ qparams: qparams.toJSON(),
748
+ width: json.width,
749
+ height: json.height,
750
+ hasTranslucency: json.hasTranslucency,
751
+ uniformColor: uniformColor?.toJSON(),
752
+ featureIndexType: json.featureIndexType,
753
+ uniformFeatureID,
754
+ numVertices: json.count,
755
+ numRgbaPerVertex: json.numRgbaPerVertex,
756
+ uvParams: uvParams?.toJSON(),
757
+ };
758
+ }
759
+ parseInstances(primitive) {
760
+ const json = primitive.instances;
761
+ if (!json)
762
+ return undefined;
763
+ const count = core_bentley_1.JsonUtils.asInt(json.count, 0);
764
+ if (count <= 0)
765
+ return undefined;
766
+ const centerComponents = core_bentley_1.JsonUtils.asArray(json.transformCenter);
767
+ if (undefined === centerComponents || 3 !== centerComponents.length)
768
+ return undefined;
769
+ const transformCenter = core_geometry_1.Point3d.create(centerComponents[0], centerComponents[1], centerComponents[2]);
770
+ const featureIds = this.findBuffer(core_bentley_1.JsonUtils.asString(json.featureIds));
771
+ if (undefined === featureIds)
772
+ return undefined;
773
+ const transformBytes = this.findBuffer(core_bentley_1.JsonUtils.asString(json.transforms));
774
+ if (undefined === transformBytes)
775
+ return undefined;
776
+ // 1 transform = 3 rows of 4 floats = 12 floats per instance
777
+ const numFloats = transformBytes.byteLength / 4;
778
+ (0, core_bentley_1.assert)(Math.floor(numFloats) === numFloats);
779
+ (0, core_bentley_1.assert)(0 === numFloats % 12);
780
+ const transforms = new Float32Array(transformBytes.buffer, transformBytes.byteOffset, numFloats);
781
+ let symbologyOverrides;
782
+ if (undefined !== json.symbologyOverrides)
783
+ symbologyOverrides = this.findBuffer(core_bentley_1.JsonUtils.asString(json.symbologyOverrides));
784
+ return {
785
+ type: "instances",
786
+ count,
787
+ transforms,
788
+ transformCenter,
789
+ featureIds,
790
+ symbologyOverrides,
791
+ };
792
+ }
793
+ findBuffer(bufferViewId) {
794
+ if (typeof bufferViewId !== "string" || 0 === bufferViewId.length)
795
+ return undefined;
796
+ const bufferViewJson = this._document.bufferViews[bufferViewId];
797
+ if (undefined === bufferViewJson)
798
+ return undefined;
799
+ const byteOffset = core_bentley_1.JsonUtils.asInt(bufferViewJson.byteOffset);
800
+ const byteLength = core_bentley_1.JsonUtils.asInt(bufferViewJson.byteLength);
801
+ if (0 === byteLength)
802
+ return undefined;
803
+ return this._binaryData.subarray(byteOffset, byteOffset + byteLength);
804
+ }
805
+ colorDefFromMaterialJson(json) {
806
+ return undefined !== json ? core_common_1.ColorDef.from(json[0] * 255 + 0.5, json[1] * 255 + 0.5, json[2] * 255 + 0.5) : undefined;
807
+ }
808
+ materialFromJson(key) {
809
+ const materialJson = this._document.renderMaterials[key];
810
+ if (!materialJson)
811
+ return undefined;
812
+ // eslint-disable-next-line deprecation/deprecation
813
+ const materialParams = new core_common_1.RenderMaterial.Params(key);
814
+ materialParams.diffuseColor = this.colorDefFromMaterialJson(materialJson.diffuseColor);
815
+ if (materialJson.diffuse !== undefined)
816
+ materialParams.diffuse = core_bentley_1.JsonUtils.asDouble(materialJson.diffuse);
817
+ materialParams.specularColor = this.colorDefFromMaterialJson(materialJson.specularColor);
818
+ if (materialJson.specular !== undefined)
819
+ materialParams.specular = core_bentley_1.JsonUtils.asDouble(materialJson.specular);
820
+ materialParams.reflectColor = this.colorDefFromMaterialJson(materialJson.reflectColor);
821
+ if (materialJson.reflect !== undefined)
822
+ materialParams.reflect = core_bentley_1.JsonUtils.asDouble(materialJson.reflect);
823
+ if (materialJson.specularExponent !== undefined)
824
+ materialParams.specularExponent = materialJson.specularExponent;
825
+ if (undefined !== materialJson.transparency)
826
+ materialParams.alpha = 1.0 - materialJson.transparency;
827
+ materialParams.refract = core_bentley_1.JsonUtils.asDouble(materialJson.refract);
828
+ materialParams.shadows = core_bentley_1.JsonUtils.asBool(materialJson.shadows);
829
+ materialParams.ambient = core_bentley_1.JsonUtils.asDouble(materialJson.ambient);
830
+ if (undefined !== materialJson.textureMapping)
831
+ materialParams.textureMapping = this.textureMappingFromJson(materialJson.textureMapping.texture);
832
+ // eslint-disable-next-line deprecation/deprecation
833
+ return new Material(materialParams);
834
+ }
835
+ parseNamedTexture(namedTex, name) {
836
+ const textureType = core_bentley_1.JsonUtils.asBool(namedTex.isGlyph) ? core_common_1.RenderTexture.Type.Glyph :
837
+ (core_bentley_1.JsonUtils.asBool(namedTex.isTileSection) ? core_common_1.RenderTexture.Type.TileSection : core_common_1.RenderTexture.Type.Normal);
838
+ return new NamedTexture(name, textureType);
839
+ }
840
+ parseConstantLodProps(propsJson) {
841
+ if (undefined === propsJson)
842
+ return undefined;
843
+ return {
844
+ repetitions: core_bentley_1.JsonUtils.asDouble(propsJson.repetitions, 1.0),
845
+ offset: { x: propsJson.offset ? core_bentley_1.JsonUtils.asDouble(propsJson.offset[0]) : 0.0, y: propsJson.offset ? core_bentley_1.JsonUtils.asDouble(propsJson.offset[1]) : 0.0 },
846
+ minDistClamp: core_bentley_1.JsonUtils.asDouble(propsJson.minDistClamp, 1.0),
847
+ maxDistClamp: core_bentley_1.JsonUtils.asDouble(propsJson.maxDistClamp, 4096.0 * 1024.0 * 1024.0),
848
+ };
849
+ }
850
+ textureMappingFromJson(json) {
851
+ if (!json)
852
+ return undefined;
853
+ const name = core_bentley_1.JsonUtils.asString(json.name);
854
+ const namedTex = 0 !== name.length ? this._document.namedTextures[name] : undefined;
855
+ const texture = namedTex ? this.parseNamedTexture(namedTex, name) : undefined;
856
+ if (!texture)
857
+ return undefined;
858
+ const paramsJson = json.params;
859
+ const tf = paramsJson.transform;
860
+ const paramProps = {
861
+ textureMat2x3: new core_common_1.TextureMapping.Trans2x3(tf[0][0], tf[0][1], tf[0][2], tf[1][0], tf[1][1], tf[1][2]),
862
+ textureWeight: core_bentley_1.JsonUtils.asDouble(paramsJson.weight, 1.0),
863
+ mapMode: core_bentley_1.JsonUtils.asInt(paramsJson.mode),
864
+ worldMapping: core_bentley_1.JsonUtils.asBool(paramsJson.worldMapping),
865
+ useConstantLod: core_bentley_1.JsonUtils.asBool(paramsJson.useConstantLod),
866
+ constantLodProps: this.parseConstantLodProps(paramsJson.constantLodParams),
867
+ };
868
+ const textureMapping = new core_common_1.TextureMapping(texture, new core_common_1.TextureMapping.Params(paramProps));
869
+ const normalMapJson = json.normalMapParams;
870
+ if (normalMapJson) {
871
+ const normalTexName = core_bentley_1.JsonUtils.asString(normalMapJson.textureName);
872
+ const namedNormalTex = normalTexName.length > 0 ? this._document.namedTextures[normalTexName] : undefined;
873
+ const normalMap = namedNormalTex ? this.parseNamedTexture(namedNormalTex, normalTexName) : undefined;
874
+ if (normalMap) {
875
+ textureMapping.normalMapParams = {
876
+ normalMap,
877
+ greenUp: core_bentley_1.JsonUtils.asBool(normalMapJson.greenUp),
878
+ scale: core_bentley_1.JsonUtils.asDouble(normalMapJson.scale, 1),
879
+ useConstantLod: core_bentley_1.JsonUtils.asBool(normalMapJson.useConstantLod),
880
+ };
881
+ }
882
+ }
883
+ return textureMapping;
884
+ }
885
+ parseDisplayParams(json) {
886
+ const type = core_bentley_1.JsonUtils.asInt(json.type, DisplayParams_1.DisplayParams.Type.Mesh);
887
+ const lineColor = core_common_1.ColorDef.create(core_bentley_1.JsonUtils.asInt(json.lineColor));
888
+ const fillColor = core_common_1.ColorDef.create(core_bentley_1.JsonUtils.asInt(json.fillColor));
889
+ const width = core_bentley_1.JsonUtils.asInt(json.lineWidth);
890
+ const linePixels = core_bentley_1.JsonUtils.asInt(json.linePixels, core_common_1.LinePixels.Solid);
891
+ const fillFlags = core_bentley_1.JsonUtils.asInt(json.fillFlags, core_common_1.FillFlags.None);
892
+ const ignoreLighting = core_bentley_1.JsonUtils.asBool(json.ignoreLighting);
893
+ // Material will always contain its own texture if it has one
894
+ const materialKey = json.materialId;
895
+ const material = undefined !== materialKey ? this.materialFromJson(materialKey) : undefined;
896
+ // We will only attempt to include the texture if material is undefined
897
+ let textureMapping;
898
+ let gradient;
899
+ if (!material) {
900
+ const textureJson = json.texture;
901
+ textureMapping = undefined !== textureJson ? this.textureMappingFromJson(textureJson) : undefined;
902
+ if (undefined === textureMapping) {
903
+ const gradientProps = json.gradient;
904
+ gradient = undefined !== gradientProps ? core_common_1.Gradient.Symb.fromJSON(gradientProps) : undefined;
905
+ if (gradient) {
906
+ (0, core_bentley_1.assert)(undefined !== gradientProps);
907
+ const texture = new GradientTexture(gradientProps);
908
+ textureMapping = new core_common_1.TextureMapping(texture, new core_common_1.TextureMapping.Params({ textureMat2x3: new core_common_1.TextureMapping.Trans2x3(0, 1, 0, 1, 0, 0) }));
909
+ }
910
+ }
911
+ }
912
+ return new DisplayParams_1.DisplayParams(type, lineColor, fillColor, width, linePixels, fillFlags, material, gradient, ignoreLighting, textureMapping);
913
+ }
914
+ }
915
+ /** @internal */
916
+ function toMaterialArgs(mat) {
917
+ const args = { alpha: mat.alpha };
918
+ if (mat.diffuse) {
919
+ args.diffuse = {
920
+ weight: mat.diffuse.weight,
921
+ color: undefined !== mat.diffuse.color ? core_common_1.ColorDef.fromJSON(mat.diffuse.color) : undefined,
922
+ };
923
+ }
924
+ if (mat.specular) {
925
+ args.specular = {
926
+ weight: mat.specular.weight,
927
+ exponent: mat.specular.exponent,
928
+ color: undefined !== mat.specular.color ? core_common_1.ColorDef.fromJSON(mat.specular.color) : undefined,
929
+ };
930
+ }
931
+ return args;
932
+ }
933
+ exports.toMaterialArgs = toMaterialArgs;
934
+ /** @internal */
935
+ function convertFeatureTable(imdlFeatureTable, batchModelId) {
936
+ const table = imdlFeatureTable.multiModel
937
+ ? core_common_1.MultiModelPackedFeatureTable.create(imdlFeatureTable.data, batchModelId, imdlFeatureTable.numFeatures, core_common_1.BatchType.Primary, imdlFeatureTable.numSubCategories)
938
+ : new core_common_1.PackedFeatureTable(imdlFeatureTable.data, batchModelId, imdlFeatureTable.numFeatures, core_common_1.BatchType.Primary);
939
+ table.animationNodeIds = imdlFeatureTable.animationNodeIds;
940
+ return table;
941
+ }
942
+ exports.convertFeatureTable = convertFeatureTable;
943
+ /** @internal */
944
+ function parseImdlDocument(options) {
945
+ const stream = options.stream;
946
+ const imdlHeader = new core_common_1.ImdlHeader(stream);
947
+ if (!imdlHeader.isValid)
948
+ return core_common_1.TileReadStatus.InvalidHeader;
949
+ else if (!imdlHeader.isReadableVersion)
950
+ return core_common_1.TileReadStatus.NewerMajorVersion;
951
+ // Skip the feature table - we need to parse the JSON segment first to access its animationNodeIds.
952
+ const ftStartPos = stream.curPos;
953
+ const ftHeader = core_common_1.FeatureTableHeader.readFrom(stream);
954
+ if (!ftHeader)
955
+ return core_common_1.TileReadStatus.InvalidFeatureTable;
956
+ stream.curPos = ftStartPos + ftHeader.length;
957
+ // A glTF header follows the feature table
958
+ const gltfHeader = new GltfHeader(stream);
959
+ if (!gltfHeader.isValid)
960
+ return core_common_1.TileReadStatus.InvalidTileData;
961
+ stream.curPos = gltfHeader.scenePosition;
962
+ const sceneStrData = stream.nextBytes(gltfHeader.sceneStrLength);
963
+ const sceneStr = (0, core_bentley_1.utf8ToString)(sceneStrData);
964
+ if (!sceneStr)
965
+ return core_common_1.TileReadStatus.InvalidScene;
966
+ try {
967
+ const sceneValue = JSON.parse(sceneStr);
968
+ const imdlDoc = {
969
+ scene: core_bentley_1.JsonUtils.asString(sceneValue.scene),
970
+ scenes: core_bentley_1.JsonUtils.asArray(sceneValue.scenes),
971
+ animationNodes: core_bentley_1.JsonUtils.asObject(sceneValue.animationNodes),
972
+ bufferViews: core_bentley_1.JsonUtils.asObject(sceneValue.bufferViews) ?? {},
973
+ meshes: core_bentley_1.JsonUtils.asObject(sceneValue.meshes),
974
+ nodes: core_bentley_1.JsonUtils.asObject(sceneValue.nodes) ?? {},
975
+ materials: core_bentley_1.JsonUtils.asObject(sceneValue.materials) ?? {},
976
+ renderMaterials: core_bentley_1.JsonUtils.asObject(sceneValue.renderMaterials) ?? {},
977
+ namedTextures: core_bentley_1.JsonUtils.asObject(sceneValue.namedTextures) ?? {},
978
+ patternSymbols: core_bentley_1.JsonUtils.asObject(sceneValue.patternSymbols) ?? {},
979
+ rtcCenter: core_bentley_1.JsonUtils.asArray(sceneValue.rtcCenter),
980
+ };
981
+ if (!imdlDoc.meshes)
982
+ return core_common_1.TileReadStatus.InvalidTileData;
983
+ const binaryData = new Uint8Array(stream.arrayBuffer, gltfHeader.binaryPosition);
984
+ const featureTable = {
985
+ startPos: ftStartPos,
986
+ multiModel: 0 !== (imdlHeader.flags & core_common_1.ImdlFlags.MultiModelFeatureTable),
987
+ };
988
+ const parser = new ImdlParser(imdlDoc, binaryData, options, featureTable);
989
+ return parser.parse();
990
+ }
991
+ catch (_) {
992
+ return core_common_1.TileReadStatus.InvalidTileData;
993
+ }
994
+ }
995
+ exports.parseImdlDocument = parseImdlDocument;
996
+ //# sourceMappingURL=ImdlParser.js.map