threepipe
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

IAssetImporter.ts 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import {EventDispatcher, LoadingManager, Object3D} from 'three'
  2. import {IDisposable} from 'ts-browser-helpers'
  3. import {IAsset, IFile} from './IAsset'
  4. import {ILoader} from './IImporter'
  5. import {ICamera, IMaterial, IObject3D, ITexture} from '../core'
  6. import {ISerializedConfig, ISerializedViewerConfig} from '../viewer'
  7. import {GLTF} from 'three/examples/jsm/loaders/GLTFLoader.js'
  8. export interface RootSceneImportResult extends Object3D {
  9. readonly visible: true
  10. importedViewerConfig?: ISerializedViewerConfig
  11. userData: {
  12. rootSceneModelRoot?: true
  13. __importData?: any
  14. gltfExtras?: GLTF['userData']
  15. gltfAsset?: GLTF['asset']
  16. [key: string]: any
  17. }
  18. }
  19. export type ImportResultObject = IObject3D | ITexture | ICamera | ISerializedConfig | ISerializedViewerConfig | RootSceneImportResult | IMaterial
  20. export interface ImportResultExtras {
  21. constructor: any
  22. assetImporterProcessed?: boolean
  23. isObject3D?: boolean
  24. isCamera?: boolean
  25. isMaterial?: boolean
  26. isTexture?: boolean
  27. userData?: IImportResultUserData
  28. __rootPath?: string
  29. __rootBlob?: IFile
  30. // __disposed?: boolean
  31. [key: string]: any
  32. }
  33. export type ImportResult = ImportResultObject & ImportResultExtras
  34. export interface IImportResultUserData{
  35. rootPath?: string
  36. /**
  37. * extra arbitrary data saved by the importer that can be used by the plugins (like gltf material variants)
  38. */
  39. __importData?: any
  40. /**
  41. * This can be set to true in the importer to indicate that the source buffer should be loaded and cached in the userdata during processRaw
  42. */
  43. __needsSourceBuffer?: boolean
  44. /**
  45. * Cached source buffer for the asset (only cached when __needsSourceBuffer is set)
  46. */
  47. __sourceBuffer?: ArrayBuffer
  48. /**
  49. * Cached source blob for the asset
  50. */
  51. __sourceBlob?: IFile
  52. }
  53. export interface ProcessRawOptions {
  54. /**
  55. * default = true, toggle to control the processing of the raw objects in the proecssRaw method
  56. */
  57. processRaw?: boolean,
  58. /**
  59. * default = false. If true, the importer will reprocess the imported objects, even if they are already processed.
  60. */
  61. forceImporterReprocess?: boolean,
  62. /**
  63. * internal use
  64. */
  65. rootPath?: string,
  66. /**
  67. * default = undefined, only used for textures
  68. */
  69. generateMipmaps?: boolean|undefined,
  70. /**
  71. * If true, the importer will replace any three.js light instances with upgraded lights
  72. * default = true
  73. */
  74. replaceLights?: boolean, // default = true
  75. /**
  76. * If true, the importer will replace any three.js camera instances with upgraded cameras
  77. * default = true
  78. */
  79. replaceCameras?: boolean, // default = true
  80. /**
  81. * If true, the importer will replace any three.js material instances with upgraded materials
  82. * default = true
  83. */
  84. replaceMaterials?: boolean, // default = true
  85. /**
  86. * default = true, if true, the importer will automatically import the contents of zip files, if zip importer is registered.
  87. */
  88. autoImportZipContents?: boolean,
  89. /**
  90. * @internal
  91. * default = false, if set to true, it will test if the data textures are complete. [internal use]
  92. */
  93. _testDataTextureComplete?: boolean,
  94. /**
  95. * @deprecated use processRaw instead
  96. */
  97. processImported?: boolean, // same as processRaw
  98. [key: string]: any
  99. }
  100. export interface LoadFileOptions {
  101. /**
  102. * The file extension to use for the file. If not specified, the importer will try to determine the file extension from the file name/url.
  103. */
  104. fileExtension?: string,
  105. /**
  106. * The custom {@link ILoader} to use for the file. If not specified, the importer will try to determine the loader from the file extension.
  107. */
  108. fileHandler?: ILoader,
  109. /**
  110. * Query string to add to the url. Default = undefined
  111. */
  112. queryString?: string,
  113. /**
  114. * for internal use
  115. */
  116. rootPath?: string,
  117. }
  118. export interface ImportFilesOptions extends ProcessRawOptions, LoadFileOptions {
  119. /**
  120. * Allowed file extensions. If undefined, all files are allowed.
  121. */
  122. allowedExtensions?: string[]
  123. }
  124. export interface ImportAssetOptions extends ProcessRawOptions, LoadFileOptions {
  125. /**
  126. * Default = false. If true, the asset will be imported again on subsequent calls, even if it is already imported.
  127. */
  128. forceImport?: boolean,
  129. /**
  130. * If true or not specified, and any of the assets is disposed(only root objects are checked, not children), all assets will be imported in this call. If false, old assets will be returned.
  131. * Default = true.
  132. */
  133. reimportDisposed?: boolean,
  134. /**
  135. * Path override to use for the asset. This will be used in the importer as override to path inside the asset/cached asset.
  136. */
  137. pathOverride?: string,
  138. /**
  139. * Mime type to use when importing the file, if not specified, it will be determined from the file extension.
  140. */
  141. mimeType?: string,
  142. /**
  143. * Pass a custom file to use for the import. This will be used in the importer, and nothing will be fetched from the path
  144. */
  145. importedFile?: IFile,
  146. /**
  147. * Use {@link MeshLine}(an extension of three.js `Line2`) instead of default `Line` for lines. This allows changing line width(fat lines) and other properties.
  148. *
  149. * Note - Only for gltf, glb files or files loaded with {@link GLTFLoader2}. If this flag is not passed, the default value is the value of the static property `GLTFLoader2.UseMeshLines`.
  150. */
  151. useMeshLines?: boolean,
  152. }
  153. // export type IAssetImporterEventTypes = 'onLoad' | 'onProgress' | 'onStop' | 'onError' | 'onStart' | 'loaderCreate' | 'importFile' | 'importFiles' | 'processRaw' | 'processRawStart'
  154. export interface IAssetImporterEventMap {
  155. loaderCreate: {type: 'loaderCreate', loader: ILoader}
  156. importFile: {type: 'importFile', path: string, state: 'downloading'|'done'|'error'|'adding', progress?: number, loadedBytes?: number, totalBytes?: number, error?: any}
  157. importFiles: {type: 'importFiles', files: Map<string, IFile>, state: 'start'|'end'}
  158. processRaw: {type: 'processRaw', data: any, options: ProcessRawOptions, path?: string}
  159. processRawStart: {type: 'processRawStart', data: any, options: ProcessRawOptions, path?: string}
  160. /**
  161. * @deprecated use the {@link importFile} event instead
  162. */
  163. onLoad: {type: 'onLoad'}
  164. /**
  165. * @deprecated use the {@link importFile} event instead
  166. */
  167. onProgress: {type: 'onProgress', url: string, loaded: number, total: number}
  168. /**
  169. * @deprecated use the {@link importFile} event instead
  170. */
  171. onError: {type: 'onError', url: string}
  172. /**
  173. * @deprecated use the {@link importFile} event instead
  174. */
  175. onStart: {type: 'onStart', url: string, loaded: number, total: number}
  176. }
  177. export interface IAssetImporter<TE extends IAssetImporterEventMap = IAssetImporterEventMap> extends EventDispatcher<TE>, IDisposable {
  178. readonly loadingManager: LoadingManager
  179. readonly cachedAssets: IAsset[]
  180. /**
  181. * Import single or multiple assets(like in case of zip files) from a path(url) or an {@link IAsset}.
  182. * @param assetOrPath - The path or asset to import
  183. * @param options - Options for the import
  184. */
  185. import<T extends ImportResult = ImportResult>(assetOrPath?: IAsset | string, options?: ImportAssetOptions): Promise<(T|undefined)[]>;
  186. /**
  187. * Import a single asset from a path(url) or an {@link IAsset}.
  188. * @param asset
  189. * @param options
  190. */
  191. importSingle<T extends ImportResult = ImportResult>(asset?: IAsset | string, options?: ImportAssetOptions): Promise<T|undefined>;
  192. /**
  193. * Import multiple local files/blobs from a map of files.
  194. * @param files
  195. * @param options
  196. */
  197. importFiles(files: Map<string, IFile>, options?: ImportFilesOptions): Promise<Map<string, any[]> | undefined>;
  198. /**
  199. * Register a file to a specific path, so this file will be used when importing the path.
  200. * @param path
  201. * @param file
  202. */
  203. registerFile(path: string, file?: IFile): ILoader | undefined;
  204. /**
  205. * Unregister a file from a specific path.
  206. * @param path
  207. */
  208. unregisterFile(path: string): void;
  209. /**
  210. * Process the raw output from the loaders and return the updated/patched-objects.
  211. * @param res
  212. * @param options
  213. */
  214. processRaw(res: any, options: ProcessRawOptions): Promise<any[]>
  215. addURLModifier(modifier: (url: string) => string): void
  216. removeURLModifier(modifier: (url: string) => string): void
  217. }