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.

loading-files.md 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. ---
  2. prev:
  3. text: 'Features'
  4. link: './features'
  5. next:
  6. text: 'Exporting Files'
  7. link: './exporting-files'
  8. ---
  9. # Loading files
  10. ThreePipe uses the [AssetManager](https://threepipe.org/docs/classes/AssetManager.html) to load files.
  11. The AssetManager has support for loading files from URLs, local files and data URLs.
  12. The AssetManager also adds support for loading files from a zip archive. The zip files are automatically unzipped, and the files are loaded from the zip archive.
  13. [`viewer.load()`](https://threepipe.org/docs/classes/ThreeViewer.html#load) is a simple helper for loading files from the AssetManager. It accepts urls, local `File`/`Blob`, data URLs, zip files, `IAsset`.
  14. It automatically adds the loaded object to the scene(if possible) and returns a promise that resolves to the loaded object/texture/material/etc, the materials are also automatically registered to the material manager.
  15. ```typescript
  16. const object = await viewer.load<IObject3D>('https://example.com/file.glb')
  17. ```
  18. ::: details AssetManager
  19. AssetManager internally uses [AssetImporter](https://threepipe.org/docs/classes/AssetImporter.html), which provides an API for managing three.js [LoadingManager](https://threejs.org/docs/#api/en/loaders/LoadingManager) and adding and registering loaders for different file types.
  20. If the purpose is not to add files to the scene then [`viewer.assetManager.importer.import()`](https://threepipe.org/docs/classes/AssetImporter.html#import) method can be used to import files from the `AssetImporter`. [`viewer.assetManager.loadImported()`](https://threepipe.org/docs/classes/AssetManager.html#loadImported)
  21. can then be called to load the imported files after any processing.
  22. The `viewer.load()`, `viewer.assetManager.addAsset()`
  23. and `viewer.assetManager.addAssetSingle()` methods perform combination of `import` and `loadImported`.
  24. :::
  25. ::: tip Caching
  26. The `AssetManager` automatically caches any loaded files in the browser's `CacheStorage`. This can be controlled by passing a custom storage or `false` to the `assetManager.storage` option in the viewer constructor.
  27. ```typescript
  28. const viewer = new ThreeViewer({
  29. assetManager: {
  30. storage: await caches.open('my-cache-storage'), // custom storage
  31. // storage: false // or disable caching
  32. }
  33. })
  34. ```
  35. Caching should be disabled if the server sets proper [cache-control](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control) headers, as the browser will automatically cache the files.
  36. Note that using a different query parameter in the URL will bypass the cache and load the file again.
  37. :::
  38. ## 3D models
  39. The 3d models are added to `viewer.scene.modelRoot` on `viewer.load` unless some option is specified.
  40. ```typescript
  41. const objectGlb = await viewer.load<IObject3D>('https://example.com/file.glb')
  42. const objectFbx = await viewer.load<IObject3D>('https://example.com/file.fbx')
  43. const objectObj = await viewer.load<IObject3D>('https://example.com/file.obj') // .mtl referenced in obj is automatically loaded
  44. // ... load any 3d model file as an object
  45. ```
  46. Here, we are casting to [IObject3D](https://threepipe.org/docs/interfaces/IObject3D.html) type
  47. to get the proper type and autocomplete for the object.
  48. `IObject3D` inherits [Object3D](https://threejs.org/docs/#api/en/core/Object3D) from three.js and adds some additional properties.
  49. For JavaScript, the type can be omitted.
  50. ```javascript
  51. const objectGlb = await viewer.load('https://example.com/file.glb')
  52. ```
  53. When loading models, several options can be passed to automatically process the model first time, like `autoScale`, `autoCenter`, `addToRoot` etc. Check [AddObjectOptions](https://threepipe.org/docs/interfaces/AddObjectOptions.html) and [ImportAddOptions](https://threepipe.org/docs/interfaces/ImportAddOptions.html) for more details.
  54. ::: tip
  55. Loaders for some file types are already added, add more loaders using plugins or by registering custom loaders.
  56. ```typescript
  57. viewer.addPluginSync(Rhino3dmLoadPlugin)
  58. ```
  59. Check the [model-viewer](https://threepipe.org/examples/#model-viewer/) example for a list of plugins that can be used.
  60. :::
  61. ## Materials
  62. The materials downloaded as `PMAT`/`BMAT`/`JSON` etc. from threepipe,
  63. webgi or the editor can be loaded
  64. and registered with the [MaterialManager](https://threepipe.org/docs/classes/MaterialManager)
  65. using the `viewer.load` method.
  66. Custom material types can also be registered by plugins(like `DMAT` for diamonds), which can also be loaded automatically using the `viewer.load` method.
  67. ```typescript
  68. const pMaterial = await viewer.load<PhysicalMaterial>('https://example.com/file.pmat')
  69. const bMaterial = await viewer.load<UnlitMaterial>('https://example.com/file.bmat')
  70. // ... load any material file as a material
  71. ```
  72. Casting to [PhysicalMaterial](https://threepipe.org/docs/classes/PhysicalMaterial) or [UnlitMaterial](https://threepipe.org/docs/classes/UnlitMaterial) is optional but recommended to get the proper type and autocomplete for the material.
  73. To assign the material on any object, set it to `object.material`
  74. ```typescript
  75. // find a loaded mesh in the scene
  76. const object = viewer.scene.getObjectByName('objectName');
  77. // assign the material
  78. object.material = pMaterial;
  79. ```
  80. To copy the properties without changing the material reference, use `material.copy()` or `material.setValues()` methods.
  81. ```typescript
  82. object.material.copy(pMaterial);
  83. // or use material manager to apply to multiple materials.
  84. viewer.assetManager.materialManager.applyMaterial(pMaterial, 'METAL') // apply props to all materials/objects with the name METAL
  85. ```
  86. TODO: add examples for material load and copy
  87. ## Images/Textures
  88. Images can be loaded using the `viewer.load` method.
  89. There is built-in support for loading all image formats supported by the browser (webp, png, jpeg, jpg, svg, ico, avif) and hdr, exr, hdr.png formats for all browsers.
  90. More formats like ktx2, ktx, etc. can be added using plugins.
  91. ```typescript
  92. const texture = await viewer.load<ITexture>('https://example.com/file.png')
  93. // ... load any image file as a texture
  94. ```
  95. Casting to [ITexture](https://threepipe.org/docs/interfaces/ITexture.html) is optional
  96. but recommended to get the proper type and autocomplete for the texture.
  97. It inherits from three.js [Texture](https://threejs.org/docs/#api/en/textures/Texture) and adds some additional properties.
  98. To assign the texture on any material, set it to `material.map`
  99. ```typescript
  100. // find a loaded mesh in the scene
  101. const object = viewer.scene.getObjectByName('objectName');
  102. const material = object.material as PhysicalMaterial;
  103. // assign the texture
  104. material.map = texture;
  105. material.setDirty() // to let the viewer know that the material has changed and needs to re-render the scene. This will also trigger fade effect if FrameFadePlugin is added.
  106. ```
  107. Check out the image load example to see it in action or to check the JS equivalent code: https://threepipe.org/examples/#image-load/
  108. ## Zip files
  109. .zip files are automatically unzipped and the files are sent to re-load recursively when loaded with `viewer.load`.
  110. Any level of zip hierarchy is flattened.
  111. Loading files like .gltf with references to assets inside the zip file,
  112. any relative references are also automatically resolved.
  113. This is supported for file types like gltf, glb, obj,
  114. etc. which support references to external files and has `root` set to `true in [IImporter](https://threepipe.org/docs/interfaces/IImporter.html).
  115. ```typescript
  116. const objectGltf = await viewer.load<IObject3D>('https://example.com/model.gltf.zip')
  117. ```
  118. If we know that the zip file contains a single gltf with all the assets, we can cast the result to [IObject3D](https://threepipe.org/docs/interfaces/IObject3D.html) type.
  119. To load multiple assets from zip files like multiple textures or materials, use `viewer.assetManager.addAsset` method which returns a promise of array of loaded assets.
  120. ```typescript
  121. const textures = await viewer.assetManager.addAsset<ITexture[]>('https://example.com/textures.zip')
  122. const materials = await viewer.assetManager.addAsset<IMaterial[]>('https://example.com/materials.zip')
  123. ```
  124. The auto import of zip contents can be disabled to get the files and blobs in the zip
  125. ```typescript
  126. const zip = await viewer.load<any>('https://example.com/file.zip', {autoImportZipContents: false})
  127. ```
  128. TODO - add example for loading zip files.
  129. ## txt, json files
  130. Text and JSON files can be loaded using the `viewer.load` method and return strings and objects respectively.
  131. ```typescript
  132. const text = await viewer.load<string>('https://example.com/file.txt')
  133. const json = await viewer.load<any>('https://example.com/file.json')
  134. ```
  135. ## Data URLs
  136. Data URLs can be loaded using the `viewer.load` method. The correct mime-type is required to be set in the data URL for finding the correct importer.
  137. ```typescript
  138. const dataUrl = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA' // ... some data url
  139. const texture = await viewer.load<ITexture>(dataUrl)
  140. ```
  141. ## Local files, File and Blob
  142. Local files can be loaded using the `viewer.load` method by passing a [IAsset](https://threepipe.org/docs/interfaces/IAsset) object with [File](https://developer.mozilla.org/en-US/docs/Web/API/File) or [Blob](https://developer.mozilla.org/en-US/docs/Web/API/Blob) object.
  143. ```typescript
  144. const file: File|Blob = fileObject // create a new file, blob or get from input element
  145. const res = await viewer.load(file)
  146. const res2 = await viewer.load<IObject>({
  147. // a path/name is required to determine the proper importer by extension. `file.name` can also be used if available
  148. path: 'file.glb',
  149. file
  150. })
  151. ```
  152. The same can be done for any file type.
  153. To load a `Map` of files(like when multiple files are dragged and dropped on the webpage) with internal references to other files, use `viewer.assetManager.importer.importFiles` method. Check the source for [DropzonePlugin](../plugin/DropzonePlugin) for an example.
  154. ## File/Blob with URL
  155. Files with references can be loaded with path, by setting the path as the name of the `File` object, or by specifying the `path` parameter.
  156. ```typescript
  157. const url = 'https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf'
  158. const blob = await fetch(url).then(res => res.blob())
  159. const file = new File([blob], url) // Set the file name to the URL, so that internal textures can be resolved correctly from the base path
  160. const result = await viewer.load(file, {
  161. autoCenter: true,
  162. autoScale: true,
  163. })
  164. ```
  165. Check the complete example - [file-load](https://threepipe.org/examples/#file-load/)
  166. ## Background, Environment maps
  167. The background and environment maps can be set using the `viewer.setBackgroundMap` and `viewer.setEnvironmentMap` methods respectively. These accept both loaded textures from `viewer.load` and direct URLs. Files can be of any image format including hdr, exr.
  168. ```typescript
  169. await viewer.setEnvironmentMap('https://example.com/file.hdr')
  170. await viewer.setBackgroundMap('https://example.com/file.png')
  171. ```
  172. The same texture can be set to both by setting `setBackground` or `setEnvironment` to true in the options:
  173. ```typescript
  174. await viewer.setEnvironmentMap('https://example.com/file.hdr', {setBackground: true})
  175. ```
  176. Check the HDR Load example to see it in action: https://threepipe.org/examples/#hdr-load/
  177. ## SVG strings
  178. SVG strings can be converted to data urls using the [svgUrl](https://repalash.com/ts-browser-helpers/functions/svgUrl.html) string template function
  179. ```typescript
  180. const svgDataUrl = svgUrl`<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"> ... </svg>`;
  181. const texture = await viewer.load<ITexture>(dataUrl)
  182. ```
  183. ## Custom file types
  184. Custom file importers/loaders can be registered to the `AssetImporter` using the `addImporter` method.
  185. ```typescript
  186. class CustomLoader extends FileLoader implements ILoader{
  187. constructor(manager?: LoadingManager) {
  188. super(manager);
  189. }
  190. load(url: string, onLoad: (data: any) => void, onProgress?: (event: ProgressEvent) => void, onError?: (event: ErrorEvent) => void): Mesh {
  191. this.setResponseType('json')
  192. return super.load(url, (json: any)=>{
  193. const mat = new PhysicalMaterial(json)
  194. onLoad?.(mat)
  195. }, onProgress, onError)
  196. }
  197. }
  198. viewer.assetManager.importer.addImporter(new Importer(CustomLoader, ['ext'], ['mime/type'], false))
  199. // load the file
  200. const mat = await viewer.load<PhysicalMaterial>('https://example.com/file.ext')
  201. ```