-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(tile-converter): preprocess stage for I3SConverter (#2520)
- Loading branch information
Showing
11 changed files
with
335 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
modules/tile-converter/src/i3s-converter/helpers/preprocess-3d-tiles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import {Tiles3DTileContent, Tiles3DTileJSONPostprocessed} from '@loaders.gl/3d-tiles'; | ||
import {GltfPrimitiveModeString, PreprocessData} from '../types'; | ||
import {GLTF, GLTFLoader} from '@loaders.gl/gltf'; | ||
import {parse} from '@loaders.gl/core'; | ||
|
||
/** | ||
* glTF primitive modes | ||
* @see https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#_mesh_primitive_mode | ||
*/ | ||
export const GLTF_PRIMITIVE_MODES = [ | ||
GltfPrimitiveModeString.POINTS, // 0 | ||
GltfPrimitiveModeString.LINES, // 1 | ||
GltfPrimitiveModeString.LINE_LOOP, // 2 | ||
GltfPrimitiveModeString.LINE_STRIP, // 3 | ||
GltfPrimitiveModeString.TRIANGLES, // 4 | ||
GltfPrimitiveModeString.TRIANGLE_STRIP, // 5 | ||
GltfPrimitiveModeString.TRIANGLE_FAN // 6 | ||
]; | ||
|
||
/** | ||
* Analyze tile content. This function is used during preprocess stage of | ||
* conversion | ||
* @param tile - 3DTiles tile JSON metadata | ||
* @param tileContent - 3DTiles tile content ArrayBuffer | ||
* @returns | ||
*/ | ||
export const analyzeTileContent = async ( | ||
tile: Tiles3DTileJSONPostprocessed, | ||
tileContent: Tiles3DTileContent | null | ||
): Promise<PreprocessData> => { | ||
const result: PreprocessData = { | ||
meshTopologyTypes: new Set() | ||
}; | ||
if (!tileContent?.gltfArrayBuffer) { | ||
return result; | ||
} | ||
|
||
const gltfData = await parse(tileContent.gltfArrayBuffer, GLTFLoader, { | ||
gltf: {normalize: false, loadBuffers: false, loadImages: false, decompressMeshes: false} | ||
}); | ||
const gltf = gltfData.json; | ||
|
||
if (!gltf) { | ||
return result; | ||
} | ||
const meshTypes = getMeshTypesFromGltf(gltf); | ||
result.meshTopologyTypes = meshTypes; | ||
return result; | ||
}; | ||
|
||
/** | ||
* Get mesh topology types that the glb content has | ||
* @param gltfJson - JSON part of GLB content | ||
* @returns array of mesh types found | ||
*/ | ||
const getMeshTypesFromGltf = (gltfJson: GLTF): Set<GltfPrimitiveModeString> => { | ||
const result: Set<GltfPrimitiveModeString> = new Set(); | ||
for (const mesh of gltfJson.meshes || []) { | ||
for (const primitive of mesh.primitives) { | ||
let {mode} = primitive; | ||
if (typeof mode !== 'number') { | ||
mode = 4; // Default is 4 - TRIANGLES | ||
} | ||
result.add(GLTF_PRIMITIVE_MODES[mode]); | ||
} | ||
} | ||
return result; | ||
}; | ||
|
||
/** | ||
* Merge object2 into object1 | ||
* @param object1 | ||
* @param object2 | ||
* @returns nothing | ||
*/ | ||
export const mergePreprocessData = (object1: PreprocessData, object2: PreprocessData): void => { | ||
// Merge topology mesh types info | ||
for (const type of object2.meshTopologyTypes) { | ||
object1.meshTopologyTypes.add(type); | ||
} | ||
}; |
51 changes: 51 additions & 0 deletions
51
modules/tile-converter/src/i3s-converter/helpers/tileset-traversal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import {Tiles3DTileJSONPostprocessed} from '@loaders.gl/3d-tiles'; | ||
import {NodeIndexDocument} from './node-index-document'; | ||
import {Matrix4} from '@math.gl/core'; | ||
|
||
/** Traversal props for the conversion stage */ | ||
export type TraversalConversionProps = { | ||
/** Transformation matrix for the specific tile */ | ||
transform: Matrix4; | ||
/** Parent nodes of the converted tile. Multiple nodes can be if one tile is converted to multiple nodes*/ | ||
parentNodes: NodeIndexDocument[]; | ||
}; | ||
|
||
/** | ||
* Travesal of 3DTile tiles tree with making specific actions with each tile | ||
* @param tile - 3DTiles tile JSON metadata | ||
* @param traversalProps - traversal props used to pass data through recursive calls | ||
* @param processTile - callback to make some actions with the current tile | ||
* @param postprocessTile - callback to make some action after processing of the current tile and all the subtree | ||
* @param maxDepth - max recursive calls number the travesal function will do. If not set, the traversal function will | ||
* go through all the tree. | ||
* This value is used to limit the convertion with only partial number of levels of the tileset | ||
* @param level - counter to keep recursive calls number of the tiles tree. This value used to be able to break | ||
* traversal at the some level of the tree | ||
* @returns void | ||
*/ | ||
export const traverseDatasetWith = async <TProps>( | ||
tile: Tiles3DTileJSONPostprocessed, | ||
traversalProps: TProps, | ||
processTile: (tile: Tiles3DTileJSONPostprocessed, traversalProps: TProps) => Promise<TProps>, | ||
postprocessTile?: (processResults: TProps[], currentTraversalProps: TProps) => Promise<void>, | ||
maxDepth?: number, | ||
level = 0 | ||
): Promise<void> => { | ||
if (maxDepth && level > maxDepth) { | ||
return; | ||
} | ||
const processResults: TProps[] = []; | ||
const newTraversalProps: TProps = await processTile(tile, traversalProps); | ||
processResults.push(newTraversalProps); | ||
for (const childTile of tile.children) { | ||
await traverseDatasetWith( | ||
childTile, | ||
newTraversalProps, | ||
processTile, | ||
postprocessTile, | ||
maxDepth, | ||
level + 1 | ||
); | ||
} | ||
postprocessTile && (await postprocessTile(processResults, traversalProps)); | ||
}; |
Oops, something went wrong.