TextureHelperGPU.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import {
  2. NodeMaterial,
  3. BoxGeometry,
  4. BufferAttribute,
  5. Mesh,
  6. PlaneGeometry,
  7. DoubleSide,
  8. Vector3,
  9. } from 'three';
  10. import { texture as textureNode, cubeTexture, texture3D, float, vec4, attribute } from 'three/tsl';
  11. import { mergeGeometries } from '../utils/BufferGeometryUtils.js';
  12. /**
  13. * A helper that can be used to display any type of texture for
  14. * debugging purposes. Depending on the type of texture (2D, 3D, Array),
  15. * the helper becomes a plane or box mesh.
  16. *
  17. * This helper can only be used with {@link WebGPURenderer}.
  18. * When using {@link WebGLRenderer}, import from `TextureHelper.js`.
  19. *
  20. * @private
  21. * @augments Mesh
  22. */
  23. class TextureHelper extends Mesh {
  24. /**
  25. * Constructs a new texture helper.
  26. *
  27. * @param {Texture} texture - The texture to visualize.
  28. * @param {number} [width=1] - The helper's width.
  29. * @param {number} [height=1] - The helper's height.
  30. * @param {number} [depth=1] - The helper's depth.
  31. */
  32. constructor( texture, width = 1, height = 1, depth = 1 ) {
  33. const material = new NodeMaterial();
  34. material.side = DoubleSide;
  35. material.transparent = true;
  36. material.name = 'TextureHelper';
  37. let colorNode;
  38. const uvw = attribute( 'uvw' );
  39. if ( texture.isCubeTexture ) {
  40. colorNode = cubeTexture( texture ).sample( uvw );
  41. } else if ( texture.isData3DTexture || texture.isCompressed3DTexture ) {
  42. colorNode = texture3D( texture ).sample( uvw );
  43. } else if ( texture.isDataArrayTexture || texture.isCompressedArrayTexture ) {
  44. colorNode = textureNode( texture ).sample( uvw.xy ).depth( uvw.z );
  45. } else {
  46. colorNode = textureNode( texture );
  47. }
  48. const alphaNode = float( getAlpha( texture ) );
  49. material.colorNode = vec4( colorNode.rgb, alphaNode );
  50. const geometry = texture.isCubeTexture
  51. ? createCubeGeometry( width, height, depth )
  52. : createSliceGeometry( texture, width, height, depth );
  53. super( geometry, material );
  54. /**
  55. * The texture to visualize.
  56. *
  57. * @type {Texture}
  58. */
  59. this.texture = texture;
  60. this.type = 'TextureHelper';
  61. }
  62. /**
  63. * Frees the GPU-related resources allocated by this instance. Call this
  64. * method whenever this instance is no longer used in your app.
  65. */
  66. dispose() {
  67. this.geometry.dispose();
  68. this.material.dispose();
  69. }
  70. }
  71. function getImageCount( texture ) {
  72. if ( texture.isCubeTexture ) {
  73. return 6;
  74. } else if ( texture.isDataArrayTexture || texture.isCompressedArrayTexture ) {
  75. return texture.image.depth;
  76. } else if ( texture.isData3DTexture || texture.isCompressed3DTexture ) {
  77. return texture.image.depth;
  78. } else {
  79. return 1;
  80. }
  81. }
  82. function getAlpha( texture ) {
  83. if ( texture.isCubeTexture ) {
  84. return 1;
  85. } else if ( texture.isDataArrayTexture || texture.isCompressedArrayTexture ) {
  86. return Math.max( 1 / texture.image.depth, 0.25 );
  87. } else if ( texture.isData3DTexture || texture.isCompressed3DTexture ) {
  88. return Math.max( 1 / texture.image.depth, 0.25 );
  89. } else {
  90. return 1;
  91. }
  92. }
  93. function createCubeGeometry( width, height, depth ) {
  94. const geometry = new BoxGeometry( width, height, depth );
  95. const position = geometry.attributes.position;
  96. const uv = geometry.attributes.uv;
  97. const uvw = new BufferAttribute( new Float32Array( uv.count * 3 ), 3 );
  98. const _direction = new Vector3();
  99. for ( let j = 0, jl = uv.count; j < jl; ++ j ) {
  100. _direction.fromBufferAttribute( position, j ).normalize();
  101. const u = _direction.x;
  102. const v = _direction.y;
  103. const w = _direction.z;
  104. uvw.setXYZ( j, u, v, w );
  105. }
  106. geometry.deleteAttribute( 'uv' );
  107. geometry.setAttribute( 'uvw', uvw );
  108. return geometry;
  109. }
  110. function createSliceGeometry( texture, width, height, depth ) {
  111. const sliceCount = getImageCount( texture );
  112. const geometries = [];
  113. for ( let i = 0; i < sliceCount; ++ i ) {
  114. const geometry = new PlaneGeometry( width, height );
  115. if ( sliceCount > 1 ) {
  116. geometry.translate( 0, 0, depth * ( i / ( sliceCount - 1 ) - 0.5 ) );
  117. }
  118. const uv = geometry.attributes.uv;
  119. const uvw = new BufferAttribute( new Float32Array( uv.count * 3 ), 3 );
  120. for ( let j = 0, jl = uv.count; j < jl; ++ j ) {
  121. const u = uv.getX( j );
  122. const v = texture.flipY ? uv.getY( j ) : 1 - uv.getY( j );
  123. const w = sliceCount === 1
  124. ? 1
  125. : texture.isDataArrayTexture || texture.isCompressedArrayTexture
  126. ? i
  127. : i / ( sliceCount - 1 );
  128. uvw.setXYZ( j, u, v, w );
  129. }
  130. geometry.deleteAttribute( 'uv' );
  131. geometry.setAttribute( 'uvw', uvw );
  132. geometries.push( geometry );
  133. }
  134. return mergeGeometries( geometries );
  135. }
  136. export { TextureHelper };
粤ICP备19079148号