threepipe
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {_testFinish, CanvasSnapshotPlugin, isWebpExportSupported, LoadingScreenPlugin, ThreeViewer} from 'threepipe'
  2. import {createSimpleButtons} from '../examples-utils/simple-bottom-buttons.js'
  3. const viewer = new ThreeViewer({
  4. canvas: document.getElementById('mcanvas') as HTMLCanvasElement,
  5. msaa: true,
  6. renderScale: 'auto',
  7. plugins: [LoadingScreenPlugin],
  8. })
  9. async function init() {
  10. const snapshotPlugin = viewer.addPluginSync(new CanvasSnapshotPlugin())
  11. await viewer.setEnvironmentMap('https://threejs.org/examples/textures/equirectangular/venice_sunset_1k.hdr')
  12. await viewer.load('https://threejs.org/examples/models/gltf/kira.glb', {
  13. autoCenter: true,
  14. autoScale: true,
  15. })
  16. createSimpleButtons({
  17. ['Download snapshot (jpeg)']: async(btn: HTMLButtonElement) => {
  18. btn.disabled = true
  19. await snapshotPlugin.downloadSnapshot('snapshot.jpeg', {
  20. mimeType: 'image/jpeg', // mime type of the image
  21. quality: 0.9, // quality of the image (0-1) only for jpeg and webp
  22. displayPixelRatio: 2, // render scale
  23. })
  24. btn.disabled = false
  25. },
  26. ['Download snapshot (1024x1024 size, png)']: async(btn: HTMLButtonElement) => {
  27. btn.disabled = true
  28. // save scale
  29. const scale = viewer.renderManager.renderScale
  30. // set fixed render size
  31. viewer.setRenderSize({width: 1024, height: 1024})
  32. // (optional) hide the canvas. (not needed if you have an overlay)
  33. await snapshotPlugin.downloadSnapshot('snapshot.png', {
  34. mimeType: 'image/png', // mime type of the image
  35. })
  36. // revert scale
  37. viewer.renderManager.renderScale = scale
  38. // revert render size to fill container
  39. viewer.setSize()
  40. btn.disabled = false
  41. },
  42. ['Download snapshot (crop rect, png)']: async(btn: HTMLButtonElement) => {
  43. btn.disabled = true
  44. await snapshotPlugin.downloadSnapshot('snapshot.png', {
  45. scale: 1, // scale the final image
  46. displayPixelRatio: 2, // render scale
  47. mimeType: 'image/png', // mime type of the image
  48. rect: { // region to take snapshot. Crop center of the canvas
  49. height: viewer.canvas.clientHeight / 2,
  50. width: viewer.canvas.clientWidth / 2,
  51. x: viewer.canvas.clientWidth / 4,
  52. y: viewer.canvas.clientHeight / 4,
  53. },
  54. })
  55. btn.disabled = false
  56. },
  57. ['Download snapshot (webp)']: async(btn: HTMLButtonElement) => {
  58. btn.disabled = true
  59. if (!isWebpExportSupported()) {
  60. alert('WebP export is not supported in this browser, try the latest version of chrome, firefox or edge.')
  61. btn.disabled = false
  62. return
  63. }
  64. await snapshotPlugin.downloadSnapshot('snapshot.webp', {
  65. mimeType: 'image/webp', // mime type of the image
  66. scale: 1, // scale the final image
  67. quality: 0.9, // quality of the image (0-1) only for jpeg and webp
  68. displayPixelRatio: 2, // render scale
  69. })
  70. btn.disabled = false
  71. },
  72. })
  73. }
  74. init().finally(_testFinish)