DepthTexture2D.js

  1. 'use strict';
  2. const Texture2D = require('./Texture2D');
  3. const MAG_FILTERS = {
  4. NEAREST: true,
  5. LINEAR: true
  6. };
  7. const MIN_FILTERS = {
  8. NEAREST: true,
  9. LINEAR: true
  10. };
  11. const WRAP_MODES = {
  12. REPEAT: true,
  13. CLAMP_TO_EDGE: true,
  14. MIRRORED_REPEAT: true
  15. };
  16. const DEPTH_TYPES = {
  17. UNSIGNED_BYTE: true,
  18. UNSIGNED_SHORT: true,
  19. UNSIGNED_INT: true
  20. };
  21. const FORMATS = {
  22. DEPTH_COMPONENT: true,
  23. DEPTH_STENCIL: true
  24. };
  25. /**
  26. * The default type for depth textures.
  27. * @private
  28. * @constant {string}
  29. */
  30. const DEFAULT_TYPE = 'UNSIGNED_INT';
  31. /**
  32. * The default format for depth textures.
  33. * @private
  34. * @constant {string}
  35. */
  36. const DEFAULT_FORMAT = 'DEPTH_COMPONENT';
  37. /**
  38. * The default wrap mode for depth textures.
  39. * @private
  40. * @constant {string}
  41. */
  42. const DEFAULT_WRAP = 'CLAMP_TO_EDGE';
  43. /**
  44. * The default min / mag filter for depth textures.
  45. * @private
  46. * @constant {string}
  47. */
  48. const DEFAULT_FILTER = 'LINEAR';
  49. /**
  50. * A texture class to represent a 2D depth texture.
  51. * @augments Texture2D
  52. */
  53. class DepthTexture2D extends Texture2D {
  54. /**
  55. * Instantiates a DepthTexture2D object.
  56. *
  57. * @param {Object} spec - The specification arguments.
  58. * @param {Uint8Array|Uint16Array|Uint32Array} spec.src - The data to buffer.
  59. * @param {number} spec.width - The width of the texture.
  60. * @param {number} spec.height - The height of the texture.
  61. * @param {string} spec.wrap - The wrapping type over both S and T dimension.
  62. * @param {string} spec.wrapS - The wrapping type over the S dimension.
  63. * @param {string} spec.wrapT - The wrapping type over the T dimension.
  64. * @param {string} spec.filter - The min / mag filter used during scaling.
  65. * @param {string} spec.minFilter - The minification filter used during scaling.
  66. * @param {string} spec.magFilter - The magnification filter used during scaling.
  67. * @param {string} spec.format - The texture pixel format.
  68. * @param {string} spec.type - The texture pixel component type.
  69. */
  70. constructor(spec = {}) {
  71. // get specific params
  72. spec.wrapS = spec.wrapS || spec.wrap;
  73. spec.wrapT = spec.wrapT || spec.wrap;
  74. spec.minFilter = spec.minFilter || spec.filter;
  75. spec.magFilter = spec.magFilter || spec.filter;
  76. // set texture params
  77. spec.wrapS = WRAP_MODES[spec.wrapS] ? spec.wrapS : DEFAULT_WRAP;
  78. spec.wrapT = WRAP_MODES[spec.wrapT] ? spec.wrapT : DEFAULT_WRAP;
  79. spec.minFilter = MIN_FILTERS[spec.minFilter] ? spec.minFilter : DEFAULT_FILTER;
  80. spec.magFilter = MAG_FILTERS[spec.magFilter] ? spec.magFilter : DEFAULT_FILTER;
  81. // set mip-mapping and format
  82. spec.mipMap = false; // disable mip-mapping
  83. spec.invertY = false; // no need to invert-y
  84. spec.premultiplyAlpha = false; // no alpha to pre-multiply
  85. spec.format = FORMATS[spec.format] ? spec.format : DEFAULT_FORMAT;
  86. // check if stencil-depth, or just depth
  87. if (spec.format === 'DEPTH_STENCIL') {
  88. spec.type = 'UNSIGNED_INT_24_8_WEBGL';
  89. } else {
  90. spec.type = DEPTH_TYPES[spec.type] ? spec.type : DEFAULT_TYPE;
  91. }
  92. // call base constructor
  93. super(spec);
  94. }
  95. }
  96. module.exports = DepthTexture2D;