import { Transform } from "@itwin/core-geometry" ; import { DecorateContext , GraphicBranch , GraphicType , IModelApp , readGltfGraphics , RenderGraphic , Tool , } from "@itwin/core-frontend" ; class GltfDecoration { private readonly _graphic : RenderGraphic ; private readonly _tooltip : string ; private readonly _pickableId ? : string ; public constructor ( graphic , tooltip , pickableId ? ) { this . _graphic = graphic ; this . _tooltip = tooltip ; this . _pickableId = pickableId ; } public readonly useCachedDecorations = true ; public decorate ( context ) { if ( context . viewport . view . isSpatialView ( ) ) context . addDecoration ( GraphicType . Scene , this . _graphic ) ; } public testDecorationHit ( id ) { return undefined !== this . _pickableId && id === this . _pickableId ; } public async getDecorationToolTip ( ) { return this . _tooltip ; } } /** Opens a file picker from which the user can select a glTF or glb file. Creates a decoration graphic from the glTF and
 * installs a decorator to display it at the center of the active viewport's iModel's project extents.
 */ export class GltfDecorationTool extends Tool { public static override toolId = "AddGltfDecoration" ; public static override get minArgs ( ) { return 1 ; } public static override get maxArgs ( ) { return 1 ; } public override async parseAndRun ( ... args ) { return this . run ( args [ 0 ] ) ; } public override async run ( url ? ) { if ( "string" !== typeof url ) return false ; const iModel = IModelApp . viewManager . selectedView ?. iModel ; if ( ! iModel ) return false ; try { const response = await fetch ( url ) ; const buffer = await response . arrayBuffer ( ) ; const id = iModel . transientIds . next ; let graphic = await readGltfGraphics ( { gltf : new Uint8Array ( buffer ) , iModel , baseUrl : url , pickableOptions : { id , modelId : iModel . transientIds . next , } , } ) ; if ( ! graphic ) return false ; const branch = new GraphicBranch ( ) ; branch . add ( graphic ) ; const transform = Transform . createTranslation ( iModel . projectExtents . center ) ; graphic = IModelApp . renderSystem . createGraphicBranch ( branch , transform ) ; const graphicOwner = IModelApp . renderSystem . createGraphicOwner ( graphic ) ; const decorator = new GltfDecoration ( graphicOwner , url , id ) ; IModelApp . viewManager . addDecorator ( decorator ) ; iModel . onClose . addOnce ( ( ) => { graphicOwner . disposeGraphic ( ) ; IModelApp . viewManager . dropDecorator ( decorator ) ; } ) ; return true ; } catch ( _ ) { return false ; } } }