WebGLTextureUtils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {
  2. PlaneGeometry,
  3. ShaderMaterial,
  4. Uniform,
  5. Mesh,
  6. PerspectiveCamera,
  7. Scene,
  8. WebGLRenderer,
  9. CanvasTexture,
  10. SRGBColorSpace
  11. } from 'three';
  12. /** @module WebGLTextureUtils */
  13. let _renderer;
  14. let fullscreenQuadGeometry;
  15. let fullscreenQuadMaterial;
  16. let fullscreenQuad;
  17. /**
  18. * Returns an uncompressed version of the given compressed texture.
  19. *
  20. * This module can only be used with {@link WebGLRenderer}. When using {@link WebGPURenderer},
  21. * import the function from {@link WebGPUTextureUtils}.
  22. *
  23. * @param {CompressedTexture} texture - The compressed texture.
  24. * @param {number} [maxTextureSize=Infinity] - The maximum size of the uncompressed texture.
  25. * @param {?WebGLRenderer} [renderer=null] - A reference to a renderer.
  26. * @return {CanvasTexture} The uncompressed texture.
  27. */
  28. export function decompress( texture, maxTextureSize = Infinity, renderer = null ) {
  29. if ( ! fullscreenQuadGeometry ) fullscreenQuadGeometry = new PlaneGeometry( 2, 2, 1, 1 );
  30. if ( ! fullscreenQuadMaterial ) fullscreenQuadMaterial = new ShaderMaterial( {
  31. uniforms: { blitTexture: new Uniform( texture ) },
  32. vertexShader: `
  33. varying vec2 vUv;
  34. void main(){
  35. vUv = uv;
  36. gl_Position = vec4(position.xy * 1.0,0.,.999999);
  37. }`,
  38. fragmentShader: `
  39. uniform sampler2D blitTexture;
  40. varying vec2 vUv;
  41. void main(){
  42. gl_FragColor = vec4(vUv.xy, 0, 1);
  43. #ifdef IS_SRGB
  44. gl_FragColor = sRGBTransferOETF( texture2D( blitTexture, vUv) );
  45. #else
  46. gl_FragColor = texture2D( blitTexture, vUv);
  47. #endif
  48. }`
  49. } );
  50. fullscreenQuadMaterial.uniforms.blitTexture.value = texture;
  51. fullscreenQuadMaterial.defines.IS_SRGB = texture.colorSpace == SRGBColorSpace;
  52. fullscreenQuadMaterial.needsUpdate = true;
  53. if ( ! fullscreenQuad ) {
  54. fullscreenQuad = new Mesh( fullscreenQuadGeometry, fullscreenQuadMaterial );
  55. fullscreenQuad.frustumCulled = false;
  56. }
  57. const _camera = new PerspectiveCamera();
  58. const _scene = new Scene();
  59. _scene.add( fullscreenQuad );
  60. if ( renderer === null ) {
  61. renderer = _renderer = new WebGLRenderer( { antialias: false } );
  62. }
  63. const width = Math.min( texture.image.width, maxTextureSize );
  64. const height = Math.min( texture.image.height, maxTextureSize );
  65. renderer.setSize( width, height );
  66. renderer.clear();
  67. renderer.render( _scene, _camera );
  68. const canvas = document.createElement( 'canvas' );
  69. const context = canvas.getContext( '2d' );
  70. canvas.width = width;
  71. canvas.height = height;
  72. context.drawImage( renderer.domElement, 0, 0, width, height );
  73. const readableTexture = new CanvasTexture( canvas );
  74. readableTexture.minFilter = texture.minFilter;
  75. readableTexture.magFilter = texture.magFilter;
  76. readableTexture.wrapS = texture.wrapS;
  77. readableTexture.wrapT = texture.wrapT;
  78. readableTexture.colorSpace = texture.colorSpace;
  79. readableTexture.name = texture.name;
  80. if ( _renderer ) {
  81. _renderer.forceContextLoss();
  82. _renderer.dispose();
  83. _renderer = null;
  84. }
  85. return readableTexture;
  86. }
粤ICP备19079148号