threepipe
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

exporting-files.md 6.5KB

1 год назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. ---
  2. prev:
  3. text: 'Loading Files'
  4. link: './loading-files'
  5. next:
  6. text: 'Render Pipeline'
  7. link: './render-pipeline'
  8. ---
  9. # Exporting files
  10. Threepipe has support for exporting various asset type with AssetManager, as well as support to export viewer and plugin configuration, arbitrary objects etc using the [serialization](#serialization) system.
  11. [viewer.export()](https://threepipe.org/docs/classes/ThreeViewer.html#export) is a high-level wrapper for exporting scene objects, materials, textures, render targets, viewer/scene configuration and plugin configurations.
  12. AssetManager internally uses [AssetExporter](https://threepipe.org/docs/classes/AssetExporter.html) to export files.
  13. AssetExporter includes some basic exporters for glb, exr, textures,
  14. and materials and a system to register exporters for different file types with plugins or custom exporters.
  15. ## Exporting 3D models
  16. Export the root scene as glb
  17. ```typescript
  18. const blob = await viewer.exportScene({
  19. viewerConfig: true, // default = true. export all viewer and plugin configuration. if false only the model root object is exported.
  20. })
  21. // download the file
  22. downloadBlob(blob, 'scene.glb')
  23. ```
  24. Export a single object from the scene as glb
  25. ```typescript
  26. const object = viewer.scene.getObjectByName('objectName');
  27. const glb: Blob = await viewer.export(object, {
  28. exportExt: 'glb', // default = glb for models
  29. embedUrlImages: true, // default = false. embed images in glb even when url is available.
  30. })
  31. // download the file
  32. downloadBlob(glb, 'object.glb')
  33. ```
  34. Check the example [glb-export](https://threepipe.org/examples/#glb-export/) to see a demo.
  35. ## Exporting Materials
  36. Export a material
  37. ```typescript
  38. const material = viewer.assetManager.materialManager.findMaterialsByName('materialName')[0];
  39. // or
  40. // const material = viewer.scene.getObjectByName('objectName').material;
  41. const blob = await viewer.export(material)
  42. // download the file
  43. downloadBlob(blob, 'material.' + blob.ext)
  44. ```
  45. Check the example [pmat-material-export](https://threepipe.org/examples/#pmat-material-export/) to see a demo.
  46. ## Exporting Canvas Images
  47. Canvas Screenshot/snapshot can be exported as png, jpeg or webp(if supported by the browser)
  48. ```typescript
  49. const blob = await viewer.getScreenshotBlob({mimeType: 'image/' + type, quality: 0.85})
  50. // or to get data url:
  51. // const dataUrl = await viewer.getScreenshotDataUrl({mimeType: 'image/' + type, quality: 0.85})
  52. // download the file
  53. downloadBlob(blob, 'screenshot.' + blob.ext)
  54. ```
  55. Check the example [image-snapshot-export](https://threepipe.org/examples/#image-snapshot-export/) to see a demo.
  56. See also: [CanvasSnapshotPlugin](../plugin/CanvasSnapshotPlugin).
  57. ## Exporting Viewer Config (vjson)
  58. The viewer configuration can be exported to JSON using `viewer.exportConfig` or `viewer.export(viewer)`. This would export a JSON object with all the viewer, scene and all plugin configuration but no 3D data.
  59. ::: tip
  60. Plugins can exclude themselves from being included in vjson by setting property `serializeWithViewer` to `false`
  61. :::
  62. We use the extension `.vjson` to easily identify viewer configuration files and use them as presets/starter scenes.
  63. ```typescript
  64. // get a blob directly
  65. const blob = viewer.export(viewer);
  66. // get a json object
  67. const json = viewer.exportConfig();
  68. // get a json object that will later be embedded in a binary file (like glb)
  69. const json2 = viewer.exportConfig(true);
  70. ```
  71. ## Exporting Plugin
  72. Any plugin that supports serialization(most of them), can be exported independently to JSON using `viewer.export` or `viewer.exportPluginConfig`.
  73. ::: note
  74. Don't use `plugin.toJSON` directly, use `viewer.export` instead as that will make sure the resources(like textures) are embedded with proper context.
  75. :::
  76. ```typescript
  77. const plugin = viewer.addPluginSync(SSAOPlugin)
  78. const blob = viewer.export(plugin);
  79. downloadBlob(blob, plugin.name + '.' + blob.ext); // json
  80. ```
  81. The exported JSON config can then be imported by `viewer.load` or `viewer.importPluginConfig`.
  82. ## Exporting Textures
  83. Textures can be exported to JSON using `viewer.export` or `AssetExporter`
  84. ```typescript
  85. const texture = await viewer.load('https://example.com/file.jpeg')
  86. const blob = await viewer.export(texture)
  87. downloadBlob(blob, texture.name + '.' + blob.ext)
  88. ```
  89. Render target textures can be exported with `viewer.renderManager.exportRenderTarget` or `viewer.export`,
  90. read about [Exporting Render Targets](#exporting-render-targets) below.
  91. TODO: add examples for texture export
  92. Textures and Uint8 Data Textures can be exported as a data url or copied to a new canvas
  93. ```typescript
  94. // get a base64 data url
  95. const dataUrl = textureToDataUrl(texture, 4096, false, 'image/png') // texture or data texture, max-size, flipY, mimeType
  96. // or copy to a new canvas
  97. const canvas = textureToCanvas(texture, 4096) // texture or data texture, max-size
  98. ```
  99. Data Textures of type Half float and Float can be exported with `viewer.export`
  100. ```typescript
  101. const dataTex = await viewer.load('https://example.com/file.hdr')
  102. const blob = await viewer.export(dataTexture, {exportExt: 'exr'})
  103. ```
  104. Check the example [hdr-to-exr](https://threepipe.org/examples/#hdr-to-exr/) to see a demo of HDR to EXR conversion.
  105. TODO: add support to export unsigned byte textures as png, jpeg, webp
  106. ## Exporting Images/Textures
  107. Exporting Textures as Images with image of types ImageBitmap, HTMLImageElement,
  108. HTMLOrSVGImageElement, CanvasImageSource, HTMLCanvasElement,
  109. OffscreenCanvas can be exported to png data urls with [imageBitmapToBase64](https://repalash.com/ts-browser-helpers/functions/imageBitmapToBase64.html) function.
  110. ```typescript
  111. const texture = await viewer.load('https://example.com/file.jpeg')
  112. const dataUrl = await imageBitmapToBase64(texture.image, 'image/png', 0.85);
  113. ```
  114. TODO: add support for texture export as images in AssetExporter
  115. ## Exporting Render Targets
  116. Unsigned byte render targets can be exported as png, jpeg or webp(if supported by the browser)
  117. ```typescript
  118. const depthPlugin = viewer.addPluginSync(DepthBufferPlugin, UnsignedByteType)
  119. // wait for the first render
  120. const blob = await viewer.export(depthPlugin.target!, {exportExt: 'png'})
  121. if (blob) downloadBlob(blob, target.texture.name + '.' + blob.ext)
  122. ```
  123. Half float and float render targets can be exported as exr
  124. ```typescript
  125. const depthPlugin = viewer.addPluginSync(DepthBufferPlugin, HalfFloatType)
  126. // wait for the first render
  127. const blob = await viewer.export(depthPlugin.target!, {exportExt: 'exr'})
  128. if (blob) downloadBlob(blob, target.texture.name + '.' + blob.ext)
  129. ```
  130. ::: tip
  131. `exportExt` is determined automatically if not specified.
  132. :::