WebGLUtils.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_ASTC_10x5_Format, RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_10x10_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, RGB_ETC1_Format, RGB_ETC2_Format, RGBA_ETC2_EAC_Format, RGBA_PVRTC_2BPPV1_Format, RGBA_PVRTC_4BPPV1_Format, RGB_PVRTC_2BPPV1_Format, RGB_PVRTC_4BPPV1_Format, RGBA_S3TC_DXT5_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT1_Format, RGB_S3TC_DXT1_Format, DepthFormat, DepthStencilFormat, LuminanceAlphaFormat, LuminanceFormat, RedFormat, RGBFormat, RGBAFormat, AlphaFormat, RedIntegerFormat, RGFormat, RGIntegerFormat, RGBAIntegerFormat, HalfFloatType, FloatType, UnsignedIntType, IntType, UnsignedShortType, ShortType, ByteType, UnsignedInt248Type, UnsignedShort5551Type, UnsignedShort4444Type, UnsignedByteType, RGBA_BPTC_Format, sRGBEncoding, _SRGBAFormat } from '../../constants.js';
  2. function WebGLUtils( gl, extensions, capabilities ) {
  3. const isWebGL2 = capabilities.isWebGL2;
  4. function convert( p, encoding = null ) {
  5. let extension;
  6. if ( p === UnsignedByteType ) return gl.UNSIGNED_BYTE;
  7. if ( p === UnsignedShort4444Type ) return gl.UNSIGNED_SHORT_4_4_4_4;
  8. if ( p === UnsignedShort5551Type ) return gl.UNSIGNED_SHORT_5_5_5_1;
  9. if ( p === ByteType ) return gl.BYTE;
  10. if ( p === ShortType ) return gl.SHORT;
  11. if ( p === UnsignedShortType ) return gl.UNSIGNED_SHORT;
  12. if ( p === IntType ) return gl.INT;
  13. if ( p === UnsignedIntType ) return gl.UNSIGNED_INT;
  14. if ( p === FloatType ) return gl.FLOAT;
  15. if ( p === HalfFloatType ) {
  16. if ( isWebGL2 ) return gl.HALF_FLOAT;
  17. extension = extensions.get( 'OES_texture_half_float' );
  18. if ( extension !== null ) {
  19. return extension.HALF_FLOAT_OES;
  20. } else {
  21. return null;
  22. }
  23. }
  24. if ( p === AlphaFormat ) return gl.ALPHA;
  25. if ( p === RGBAFormat ) return gl.RGBA;
  26. if ( p === LuminanceFormat ) return gl.LUMINANCE;
  27. if ( p === LuminanceAlphaFormat ) return gl.LUMINANCE_ALPHA;
  28. if ( p === DepthFormat ) return gl.DEPTH_COMPONENT;
  29. if ( p === DepthStencilFormat ) return gl.DEPTH_STENCIL;
  30. if ( p === RedFormat ) return gl.RED;
  31. if ( p === RGBFormat ) {
  32. console.warn( 'THREE.WebGLRenderer: THREE.RGBFormat has been removed. Use THREE.RGBAFormat instead. https://github.com/mrdoob/three.js/pull/23228' );
  33. return gl.RGBA;
  34. }
  35. // WebGL 1 sRGB fallback
  36. if ( p === _SRGBAFormat ) {
  37. extension = extensions.get( 'EXT_sRGB' );
  38. if ( extension !== null ) {
  39. return extension.SRGB_ALPHA_EXT;
  40. } else {
  41. return null;
  42. }
  43. }
  44. // WebGL2 formats.
  45. if ( p === RedIntegerFormat ) return gl.RED_INTEGER;
  46. if ( p === RGFormat ) return gl.RG;
  47. if ( p === RGIntegerFormat ) return gl.RG_INTEGER;
  48. if ( p === RGBAIntegerFormat ) return gl.RGBA_INTEGER;
  49. // S3TC
  50. if ( p === RGB_S3TC_DXT1_Format || p === RGBA_S3TC_DXT1_Format || p === RGBA_S3TC_DXT3_Format || p === RGBA_S3TC_DXT5_Format ) {
  51. if ( encoding === sRGBEncoding ) {
  52. extension = extensions.get( 'WEBGL_compressed_texture_s3tc_srgb' );
  53. if ( extension !== null ) {
  54. if ( p === RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_SRGB_S3TC_DXT1_EXT;
  55. if ( p === RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT;
  56. if ( p === RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
  57. if ( p === RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
  58. } else {
  59. return null;
  60. }
  61. } else {
  62. extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
  63. if ( extension !== null ) {
  64. if ( p === RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT;
  65. if ( p === RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  66. if ( p === RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  67. if ( p === RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  68. } else {
  69. return null;
  70. }
  71. }
  72. }
  73. // PVRTC
  74. if ( p === RGB_PVRTC_4BPPV1_Format || p === RGB_PVRTC_2BPPV1_Format || p === RGBA_PVRTC_4BPPV1_Format || p === RGBA_PVRTC_2BPPV1_Format ) {
  75. extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );
  76. if ( extension !== null ) {
  77. if ( p === RGB_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  78. if ( p === RGB_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  79. if ( p === RGBA_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  80. if ( p === RGBA_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  81. } else {
  82. return null;
  83. }
  84. }
  85. // ETC1
  86. if ( p === RGB_ETC1_Format ) {
  87. extension = extensions.get( 'WEBGL_compressed_texture_etc1' );
  88. if ( extension !== null ) {
  89. return extension.COMPRESSED_RGB_ETC1_WEBGL;
  90. } else {
  91. return null;
  92. }
  93. }
  94. // ETC2
  95. if ( p === RGB_ETC2_Format || p === RGBA_ETC2_EAC_Format ) {
  96. extension = extensions.get( 'WEBGL_compressed_texture_etc' );
  97. if ( extension !== null ) {
  98. if ( p === RGB_ETC2_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ETC2 : extension.COMPRESSED_RGB8_ETC2;
  99. if ( p === RGBA_ETC2_EAC_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ETC2_EAC : extension.COMPRESSED_RGBA8_ETC2_EAC;
  100. } else {
  101. return null;
  102. }
  103. }
  104. // ASTC
  105. if ( p === RGBA_ASTC_4x4_Format || p === RGBA_ASTC_5x4_Format || p === RGBA_ASTC_5x5_Format ||
  106. p === RGBA_ASTC_6x5_Format || p === RGBA_ASTC_6x6_Format || p === RGBA_ASTC_8x5_Format ||
  107. p === RGBA_ASTC_8x6_Format || p === RGBA_ASTC_8x8_Format || p === RGBA_ASTC_10x5_Format ||
  108. p === RGBA_ASTC_10x6_Format || p === RGBA_ASTC_10x8_Format || p === RGBA_ASTC_10x10_Format ||
  109. p === RGBA_ASTC_12x10_Format || p === RGBA_ASTC_12x12_Format ) {
  110. extension = extensions.get( 'WEBGL_compressed_texture_astc' );
  111. if ( extension !== null ) {
  112. if ( p === RGBA_ASTC_4x4_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR : extension.COMPRESSED_RGBA_ASTC_4x4_KHR;
  113. if ( p === RGBA_ASTC_5x4_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR : extension.COMPRESSED_RGBA_ASTC_5x4_KHR;
  114. if ( p === RGBA_ASTC_5x5_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR : extension.COMPRESSED_RGBA_ASTC_5x5_KHR;
  115. if ( p === RGBA_ASTC_6x5_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR : extension.COMPRESSED_RGBA_ASTC_6x5_KHR;
  116. if ( p === RGBA_ASTC_6x6_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR : extension.COMPRESSED_RGBA_ASTC_6x6_KHR;
  117. if ( p === RGBA_ASTC_8x5_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR : extension.COMPRESSED_RGBA_ASTC_8x5_KHR;
  118. if ( p === RGBA_ASTC_8x6_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR : extension.COMPRESSED_RGBA_ASTC_8x6_KHR;
  119. if ( p === RGBA_ASTC_8x8_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR : extension.COMPRESSED_RGBA_ASTC_8x8_KHR;
  120. if ( p === RGBA_ASTC_10x5_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR : extension.COMPRESSED_RGBA_ASTC_10x5_KHR;
  121. if ( p === RGBA_ASTC_10x6_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR : extension.COMPRESSED_RGBA_ASTC_10x6_KHR;
  122. if ( p === RGBA_ASTC_10x8_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR : extension.COMPRESSED_RGBA_ASTC_10x8_KHR;
  123. if ( p === RGBA_ASTC_10x10_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR : extension.COMPRESSED_RGBA_ASTC_10x10_KHR;
  124. if ( p === RGBA_ASTC_12x10_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR : extension.COMPRESSED_RGBA_ASTC_12x10_KHR;
  125. if ( p === RGBA_ASTC_12x12_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR : extension.COMPRESSED_RGBA_ASTC_12x12_KHR;
  126. } else {
  127. return null;
  128. }
  129. }
  130. // BPTC
  131. if ( p === RGBA_BPTC_Format ) {
  132. extension = extensions.get( 'EXT_texture_compression_bptc' );
  133. if ( extension !== null ) {
  134. if ( p === RGBA_BPTC_Format ) return ( encoding === sRGBEncoding ) ? extension.COMPRESSED_SRGB_ALPHA_BPTC_UNORM_EXT : extension.COMPRESSED_RGBA_BPTC_UNORM_EXT;
  135. } else {
  136. return null;
  137. }
  138. }
  139. //
  140. if ( p === UnsignedInt248Type ) {
  141. if ( isWebGL2 ) return gl.UNSIGNED_INT_24_8;
  142. extension = extensions.get( 'WEBGL_depth_texture' );
  143. if ( extension !== null ) {
  144. return extension.UNSIGNED_INT_24_8_WEBGL;
  145. } else {
  146. return null;
  147. }
  148. }
  149. }
  150. return { convert: convert };
  151. }
  152. export { WebGLUtils };