WebGLUtils.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @author thespite / http://www.twitter.com/thespite
  3. */
  4. import { MaxEquation, MinEquation, 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, 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, SrcAlphaSaturateFactor, OneMinusDstColorFactor, DstColorFactor, OneMinusDstAlphaFactor, DstAlphaFactor, OneMinusSrcAlphaFactor, SrcAlphaFactor, OneMinusSrcColorFactor, SrcColorFactor, OneFactor, ZeroFactor, ReverseSubtractEquation, SubtractEquation, AddEquation, DepthFormat, DepthStencilFormat, LuminanceAlphaFormat, LuminanceFormat, RedFormat, RGBAFormat, RGBFormat, AlphaFormat, HalfFloatType, FloatType, UnsignedIntType, IntType, UnsignedShortType, ShortType, ByteType, UnsignedInt248Type, UnsignedShort565Type, UnsignedShort5551Type, UnsignedShort4444Type, UnsignedByteType, LinearMipmapLinearFilter, LinearMipmapNearestFilter, LinearFilter, NearestMipmapLinearFilter, NearestMipmapNearestFilter, NearestFilter, MirroredRepeatWrapping, ClampToEdgeWrapping, RepeatWrapping } from '../../constants.js';
  5. function WebGLUtils( gl, extensions, capabilities ) {
  6. function convert( p ) {
  7. var extension;
  8. if ( p === RepeatWrapping ) return gl.REPEAT;
  9. if ( p === ClampToEdgeWrapping ) return gl.CLAMP_TO_EDGE;
  10. if ( p === MirroredRepeatWrapping ) return gl.MIRRORED_REPEAT;
  11. if ( p === NearestFilter ) return gl.NEAREST;
  12. if ( p === NearestMipmapNearestFilter ) return gl.NEAREST_MIPMAP_NEAREST;
  13. if ( p === NearestMipmapLinearFilter ) return gl.NEAREST_MIPMAP_LINEAR;
  14. if ( p === LinearFilter ) return gl.LINEAR;
  15. if ( p === LinearMipmapNearestFilter ) return gl.LINEAR_MIPMAP_NEAREST;
  16. if ( p === LinearMipmapLinearFilter ) return gl.LINEAR_MIPMAP_LINEAR;
  17. if ( p === UnsignedByteType ) return gl.UNSIGNED_BYTE;
  18. if ( p === UnsignedShort4444Type ) return gl.UNSIGNED_SHORT_4_4_4_4;
  19. if ( p === UnsignedShort5551Type ) return gl.UNSIGNED_SHORT_5_5_5_1;
  20. if ( p === UnsignedShort565Type ) return gl.UNSIGNED_SHORT_5_6_5;
  21. if ( p === ByteType ) return gl.BYTE;
  22. if ( p === ShortType ) return gl.SHORT;
  23. if ( p === UnsignedShortType ) return gl.UNSIGNED_SHORT;
  24. if ( p === IntType ) return gl.INT;
  25. if ( p === UnsignedIntType ) return gl.UNSIGNED_INT;
  26. if ( p === FloatType ) return gl.FLOAT;
  27. if ( p === HalfFloatType ) {
  28. if ( capabilities.isWebGL2 ) return gl.HALF_FLOAT;
  29. extension = extensions.get( 'OES_texture_half_float' );
  30. if ( extension !== null ) return extension.HALF_FLOAT_OES;
  31. }
  32. if ( p === AlphaFormat ) return gl.ALPHA;
  33. if ( p === RGBFormat ) return gl.RGB;
  34. if ( p === RGBAFormat ) return gl.RGBA;
  35. if ( p === LuminanceFormat ) return gl.LUMINANCE;
  36. if ( p === LuminanceAlphaFormat ) return gl.LUMINANCE_ALPHA;
  37. if ( p === DepthFormat ) return gl.DEPTH_COMPONENT;
  38. if ( p === DepthStencilFormat ) return gl.DEPTH_STENCIL;
  39. if ( p === RedFormat ) return gl.RED;
  40. if ( p === AddEquation ) return gl.FUNC_ADD;
  41. if ( p === SubtractEquation ) return gl.FUNC_SUBTRACT;
  42. if ( p === ReverseSubtractEquation ) return gl.FUNC_REVERSE_SUBTRACT;
  43. if ( p === ZeroFactor ) return gl.ZERO;
  44. if ( p === OneFactor ) return gl.ONE;
  45. if ( p === SrcColorFactor ) return gl.SRC_COLOR;
  46. if ( p === OneMinusSrcColorFactor ) return gl.ONE_MINUS_SRC_COLOR;
  47. if ( p === SrcAlphaFactor ) return gl.SRC_ALPHA;
  48. if ( p === OneMinusSrcAlphaFactor ) return gl.ONE_MINUS_SRC_ALPHA;
  49. if ( p === DstAlphaFactor ) return gl.DST_ALPHA;
  50. if ( p === OneMinusDstAlphaFactor ) return gl.ONE_MINUS_DST_ALPHA;
  51. if ( p === DstColorFactor ) return gl.DST_COLOR;
  52. if ( p === OneMinusDstColorFactor ) return gl.ONE_MINUS_DST_COLOR;
  53. if ( p === SrcAlphaSaturateFactor ) return gl.SRC_ALPHA_SATURATE;
  54. if ( p === RGB_S3TC_DXT1_Format || p === RGBA_S3TC_DXT1_Format ||
  55. p === RGBA_S3TC_DXT3_Format || p === RGBA_S3TC_DXT5_Format ) {
  56. extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
  57. if ( extension !== null ) {
  58. if ( p === RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT;
  59. if ( p === RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  60. if ( p === RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  61. if ( p === RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  62. }
  63. }
  64. if ( p === RGB_PVRTC_4BPPV1_Format || p === RGB_PVRTC_2BPPV1_Format ||
  65. p === RGBA_PVRTC_4BPPV1_Format || p === RGBA_PVRTC_2BPPV1_Format ) {
  66. extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );
  67. if ( extension !== null ) {
  68. if ( p === RGB_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  69. if ( p === RGB_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  70. if ( p === RGBA_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  71. if ( p === RGBA_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  72. }
  73. }
  74. if ( p === RGB_ETC1_Format ) {
  75. extension = extensions.get( 'WEBGL_compressed_texture_etc1' );
  76. if ( extension !== null ) return extension.COMPRESSED_RGB_ETC1_WEBGL;
  77. }
  78. if ( p === RGBA_ASTC_4x4_Format || p === RGBA_ASTC_5x4_Format || p === RGBA_ASTC_5x5_Format ||
  79. p === RGBA_ASTC_6x5_Format || p === RGBA_ASTC_6x6_Format || p === RGBA_ASTC_8x5_Format ||
  80. p === RGBA_ASTC_8x6_Format || p === RGBA_ASTC_8x8_Format || p === RGBA_ASTC_10x5_Format ||
  81. p === RGBA_ASTC_10x6_Format || p === RGBA_ASTC_10x8_Format || p === RGBA_ASTC_10x10_Format ||
  82. p === RGBA_ASTC_12x10_Format || p === RGBA_ASTC_12x12_Format ) {
  83. extension = extensions.get( 'WEBGL_compressed_texture_astc' );
  84. if ( extension !== null ) {
  85. return p;
  86. }
  87. }
  88. if ( p === MinEquation || p === MaxEquation ) {
  89. if ( capabilities.isWebGL2 ) {
  90. if ( p === MinEquation ) return gl.MIN;
  91. if ( p === MaxEquation ) return gl.MAX;
  92. }
  93. extension = extensions.get( 'EXT_blend_minmax' );
  94. if ( extension !== null ) {
  95. if ( p === MinEquation ) return extension.MIN_EXT;
  96. if ( p === MaxEquation ) return extension.MAX_EXT;
  97. }
  98. }
  99. if ( p === UnsignedInt248Type ) {
  100. if ( capabilities.isWebGL2 ) return gl.UNSIGNED_INT_24_8;
  101. extension = extensions.get( 'WEBGL_depth_texture' );
  102. if ( extension !== null ) return extension.UNSIGNED_INT_24_8_WEBGL;
  103. }
  104. return 0;
  105. }
  106. return { convert: convert };
  107. }
  108. export { WebGLUtils };