TextureHelper.js 5.1 KB

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