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.

script.ts 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {
  2. _testFinish,
  3. DepthBufferPlugin,
  4. downloadBlob,
  5. isWebpExportSupported,
  6. LoadingScreenPlugin,
  7. ThreeViewer,
  8. UnsignedByteType,
  9. WebGLRenderTarget,
  10. } from 'threepipe'
  11. import {createSimpleButtons} from '../examples-utils/simple-bottom-buttons.js'
  12. const viewer = new ThreeViewer({
  13. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  14. rgbm: false, // this will make the composer target not use RGBM encoding, and use HalfFloat. Otherwise, its UnsignedByteType
  15. plugins: [LoadingScreenPlugin],
  16. })
  17. async function init() {
  18. const depthPlugin = viewer.addPluginSync(DepthBufferPlugin, UnsignedByteType)
  19. await viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr')
  20. await viewer.load('https://threejs.org/examples/models/gltf/kira.glb', {
  21. autoCenter: true,
  22. autoScale: true,
  23. })
  24. const composerBuffer = viewer.renderManager.composerTarget as WebGLRenderTarget
  25. const depthBuffer = depthPlugin.target!
  26. async function downloadTarget(target: WebGLRenderTarget, type = '') {
  27. const blob = await viewer.export(target, {exportExt: type})
  28. if (blob) downloadBlob(blob, target.texture.name + '.' + blob.ext)
  29. else alert('Unable to get screenshot')
  30. }
  31. createSimpleButtons({
  32. ['Download composer (EXR)']: async(btn: HTMLButtonElement) => {
  33. btn.disabled = true
  34. await downloadTarget(composerBuffer) // default for Float and HalfFloat buffers is EXR.
  35. btn.disabled = false
  36. },
  37. // UnsignedByte targets can only be exported in webp, png, jpeg format.
  38. ['Download depth (PNG)']: async(btn: HTMLButtonElement) => {
  39. btn.disabled = true
  40. await downloadTarget(depthBuffer) // default for UnsignedByte buffers is PNG.
  41. btn.disabled = false
  42. },
  43. ['Download depth (JPEG)']: async(btn: HTMLButtonElement) => {
  44. btn.disabled = true
  45. await downloadTarget(depthBuffer, 'jpeg')
  46. btn.disabled = false
  47. },
  48. // HalfFloat targets can also be exported in webp, png, jpeg format, but they will be clamped
  49. ['Download composer (WEBP)']: async(btn: HTMLButtonElement) => {
  50. btn.disabled = true
  51. if (!isWebpExportSupported()) {
  52. alert('WebP export is not supported in this browser, try the latest version of chrome, firefox or edge.')
  53. btn.disabled = false
  54. return
  55. }
  56. await downloadTarget(composerBuffer, 'webp')
  57. btn.disabled = false
  58. },
  59. })
  60. }
  61. init().finally(_testFinish)