WebGPUTextureUtils.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {
  2. QuadMesh,
  3. NodeMaterial,
  4. WebGPURenderer,
  5. CanvasTexture
  6. } from 'three';
  7. import { texture, uv } from 'three/tsl';
  8. /** @module WebGPUTextureUtils */
  9. let _renderer;
  10. const _quadMesh = /*@__PURE__*/ new QuadMesh();
  11. /**
  12. * Returns an uncompressed version of the given compressed texture.
  13. *
  14. * This module can only be used with {@link WebGPURenderer}. When using {@link WebGLRenderer},
  15. * import the function from {@link WebGLTextureUtils}.
  16. *
  17. * @async
  18. * @param {CompressedTexture} blitTexture - The compressed texture.
  19. * @param {number} [maxTextureSize=Infinity] - The maximum size of the uncompressed texture.
  20. * @param {?WebGPURenderer} [renderer=null] - A reference to a renderer.
  21. * @return {Promise<CanvasTexture>} A Promise that resolved with the uncompressed texture.
  22. */
  23. export async function decompress( blitTexture, maxTextureSize = Infinity, renderer = null ) {
  24. if ( renderer === null ) {
  25. renderer = _renderer = new WebGPURenderer();
  26. await renderer.init();
  27. }
  28. const material = new NodeMaterial();
  29. material.fragmentNode = texture( blitTexture, uv().flipY() );
  30. const width = Math.min( blitTexture.image.width, maxTextureSize );
  31. const height = Math.min( blitTexture.image.height, maxTextureSize );
  32. const currentOutputColorSpace = renderer.outputColorSpace;
  33. renderer.setSize( width, height );
  34. renderer.outputColorSpace = blitTexture.colorSpace;
  35. _quadMesh.material = material;
  36. _quadMesh.render( renderer );
  37. renderer.outputColorSpace = currentOutputColorSpace;
  38. const canvas = document.createElement( 'canvas' );
  39. const context = canvas.getContext( '2d' );
  40. canvas.width = width;
  41. canvas.height = height;
  42. context.drawImage( renderer.domElement, 0, 0, width, height );
  43. const readableTexture = new CanvasTexture( canvas );
  44. readableTexture.minFilter = blitTexture.minFilter;
  45. readableTexture.magFilter = blitTexture.magFilter;
  46. readableTexture.wrapS = blitTexture.wrapS;
  47. readableTexture.wrapT = blitTexture.wrapT;
  48. readableTexture.colorSpace = blitTexture.colorSpace;
  49. readableTexture.name = blitTexture.name;
  50. if ( _renderer !== null ) {
  51. _renderer.dispose();
  52. _renderer = null;
  53. }
  54. return readableTexture;
  55. }
粤ICP备19079148号