threepipe
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CMPTLoadPlugin.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import {
  2. AnyOptions,
  3. BaseImporterPlugin,
  4. FileLoader, generateUUID,
  5. Group,
  6. IAssetImporter,
  7. ILoader,
  8. Importer,
  9. Loader,
  10. LoaderUtils,
  11. LoadingManager,
  12. ThreeViewer,
  13. } from 'threepipe'
  14. import type {CMPTResult} from '3d-tiles-renderer/src/three/loaders/CMPTLoader'
  15. import {CMPTLoader, LoaderBase} from '3d-tiles-renderer'
  16. /**
  17. * Adds support for loading .cmpt files and data uris.
  18. * Composite (cmpt) file format is part of OGC 3D Tiles.
  19. * Specification - https://www.ogc.org/standards/3dtiles/
  20. */
  21. export class CMPTLoadPlugin extends BaseImporterPlugin {
  22. public static readonly PluginType = 'CMPTLoadPlugin'
  23. protected _importer = new Importer(CMPTLoader2, ['cmpt'], ['model/cmpt'], false)
  24. onAdded(viewer: ThreeViewer) {
  25. super.onAdded(viewer)
  26. this._importer.onCtor = (l, ai) => {
  27. if (l) l.ai = ai
  28. return l
  29. }
  30. }
  31. }
  32. export class CMPTLoader2 extends Loader implements ILoader<CMPTResult, Group> {
  33. loader
  34. ai?: IAssetImporter
  35. constructor(manager: LoadingManager) {
  36. super(manager)
  37. this.loader = new CMPTLoader(manager)
  38. }
  39. load(url: string, onLoad: (data: unknown) => void, onProgress?: (event: ProgressEvent) => void, onError?: (err: unknown) => void) {
  40. // eslint-disable-next-line @typescript-eslint/no-this-alias
  41. const scope = this
  42. let resourcePath
  43. if (this.resourcePath !== '') {
  44. resourcePath = this.resourcePath
  45. } else if (this.path !== '') {
  46. resourcePath = this.path
  47. } else {
  48. resourcePath = LoaderUtils.extractUrlBase(url)
  49. }
  50. // Tells the LoadingManager to track an extra item, which resolves after
  51. // the model is fully loaded. This means the count of items loaded will
  52. // be incorrect, but ensures manager.onLoad() does not fire early.
  53. this.manager.itemStart(url)
  54. // eslint-disable-next-line @typescript-eslint/naming-convention
  55. const _onError = function(e: any) {
  56. if (onError) {
  57. onError(e)
  58. } else {
  59. console.error(e)
  60. }
  61. scope.manager.itemError(url)
  62. scope.manager.itemEnd(url)
  63. }
  64. const loader = new FileLoader(this.manager)
  65. loader.setPath(this.path)
  66. loader.setResponseType('arraybuffer')
  67. loader.setRequestHeader(this.requestHeader)
  68. loader.setWithCredentials(this.withCredentials)
  69. loader.load(url, function(data) {
  70. try {
  71. scope.parse(data as any, resourcePath, function(gltf) {
  72. onLoad(gltf)
  73. scope.manager.itemEnd(url)
  74. }, _onError, url)
  75. } catch (e) {
  76. _onError(e)
  77. }
  78. }, onProgress, _onError)
  79. }
  80. transform(res: CMPTResult, _options: AnyOptions): Group {
  81. return res.scene
  82. }
  83. parse(data: ArrayBuffer, _path: string, onLoad: (res: CMPTResult) => void, onError?: (event: ErrorEvent) => void, _url?: string) {
  84. if (!this.ai) {
  85. console.error('[CMPTLoader] load failed, IAssetImporter not set')
  86. }
  87. // todo register draco for inside pnts files
  88. const tmpFile = generateUUID()
  89. this.ai?.registerFile(tmpFile + '.gltf') // to set the gltf loader in manager
  90. this.ai?.registerFile(tmpFile + '.drc') // to set the draco loader in manager
  91. ;(this.loader as LoaderBase).workingPath = _path
  92. this.loader.parse(data)
  93. .then(onLoad)
  94. .catch(onError)
  95. .finally(() => {
  96. if (tmpFile && this.ai) {
  97. this.ai.unregisterFile(tmpFile + '.gltf')
  98. this.ai.unregisterFile(tmpFile + '.drc')
  99. }
  100. })
  101. // super.parse(data, path, onLoad, onError, url)
  102. }
  103. }