threepipe
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

I3DMLoadPlugin.ts 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {
  2. BaseImporterPlugin,
  3. generateUUID,
  4. GLTF,
  5. GLTFLoader2,
  6. IAssetImporter,
  7. ILoader,
  8. ImportAddOptions,
  9. Importer,
  10. LoadingManager,
  11. ThreeViewer,
  12. } from 'threepipe'
  13. import type {I3DMResult} from '3d-tiles-renderer/src/three/loaders/I3DMLoader'
  14. import {I3DMLoader, I3DMScene, LoaderBase} from '3d-tiles-renderer'
  15. /**
  16. * Adds support for loading .i3dm files and data uris.
  17. * Instanced 3D Model (i3dm) file format is part of OGC 3D Tiles.
  18. * Specification - https://www.ogc.org/standards/3dtiles/
  19. */
  20. export class I3DMLoadPlugin extends BaseImporterPlugin {
  21. public static readonly PluginType = 'I3DMLoadPlugin'
  22. protected _importer = new Importer(I3DMLoader2, ['i3dm'], ['model/i3dm'], false)
  23. onAdded(viewer: ThreeViewer) {
  24. super.onAdded(viewer)
  25. this._importer.onCtor = (l, ai) => {
  26. if (l) l.ai = ai
  27. return l
  28. }
  29. }
  30. }
  31. // todo no need to extend GLTFLoader2 for just transform function, it can be called manually or rewritten
  32. export class I3DMLoader2 extends GLTFLoader2 implements ILoader<I3DMResult, I3DMScene> {
  33. loader
  34. ai?: IAssetImporter
  35. constructor(manager: LoadingManager) {
  36. super(manager)
  37. this.loader = new I3DMLoader(manager)
  38. }
  39. transform(res: I3DMResult, options: ImportAddOptions): I3DMScene {
  40. return super.transform(res, options) as any
  41. }
  42. parse(data: ArrayBuffer, _path: string, onLoad: (gltf: GLTF) => void, onError?: (event: ErrorEvent) => void, _url?: string) {
  43. if (!this.ai) {
  44. console.error('[I3DMLoader] load failed, IAssetImporter not set')
  45. }
  46. const tmpFile = generateUUID() + '.gltf'
  47. this.ai?.registerFile(tmpFile) // to set the gltf loader in manager
  48. ;(this.loader as LoaderBase).workingPath = _path
  49. this.loader.parse(data)
  50. .then(onLoad)
  51. .catch(onError)
  52. .finally(() => {
  53. if (tmpFile) this.ai?.unregisterFile(tmpFile)
  54. })
  55. // super.parse(data, path, onLoad, onError, url)
  56. }
  57. }