alphaCullingState.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @internal
  3. **/
  4. export class AlphaState {
  5. /**
  6. * Initializes the state.
  7. */
  8. constructor() {
  9. this._blendFunctionParameters = new Array(4);
  10. this._blendEquationParameters = new Array(2);
  11. this._blendConstants = new Array(4);
  12. this._isBlendConstantsDirty = false;
  13. this._alphaBlend = false;
  14. this._isAlphaBlendDirty = false;
  15. this._isBlendFunctionParametersDirty = false;
  16. this._isBlendEquationParametersDirty = false;
  17. this.reset();
  18. }
  19. get isDirty() {
  20. return this._isAlphaBlendDirty || this._isBlendFunctionParametersDirty || this._isBlendEquationParametersDirty;
  21. }
  22. get alphaBlend() {
  23. return this._alphaBlend;
  24. }
  25. set alphaBlend(value) {
  26. if (this._alphaBlend === value) {
  27. return;
  28. }
  29. this._alphaBlend = value;
  30. this._isAlphaBlendDirty = true;
  31. }
  32. setAlphaBlendConstants(r, g, b, a) {
  33. if (this._blendConstants[0] === r && this._blendConstants[1] === g && this._blendConstants[2] === b && this._blendConstants[3] === a) {
  34. return;
  35. }
  36. this._blendConstants[0] = r;
  37. this._blendConstants[1] = g;
  38. this._blendConstants[2] = b;
  39. this._blendConstants[3] = a;
  40. this._isBlendConstantsDirty = true;
  41. }
  42. setAlphaBlendFunctionParameters(value0, value1, value2, value3) {
  43. if (this._blendFunctionParameters[0] === value0 &&
  44. this._blendFunctionParameters[1] === value1 &&
  45. this._blendFunctionParameters[2] === value2 &&
  46. this._blendFunctionParameters[3] === value3) {
  47. return;
  48. }
  49. this._blendFunctionParameters[0] = value0;
  50. this._blendFunctionParameters[1] = value1;
  51. this._blendFunctionParameters[2] = value2;
  52. this._blendFunctionParameters[3] = value3;
  53. this._isBlendFunctionParametersDirty = true;
  54. }
  55. setAlphaEquationParameters(rgb, alpha) {
  56. if (this._blendEquationParameters[0] === rgb && this._blendEquationParameters[1] === alpha) {
  57. return;
  58. }
  59. this._blendEquationParameters[0] = rgb;
  60. this._blendEquationParameters[1] = alpha;
  61. this._isBlendEquationParametersDirty = true;
  62. }
  63. reset() {
  64. this._alphaBlend = false;
  65. this._blendFunctionParameters[0] = null;
  66. this._blendFunctionParameters[1] = null;
  67. this._blendFunctionParameters[2] = null;
  68. this._blendFunctionParameters[3] = null;
  69. this._blendEquationParameters[0] = null;
  70. this._blendEquationParameters[1] = null;
  71. this._blendConstants[0] = null;
  72. this._blendConstants[1] = null;
  73. this._blendConstants[2] = null;
  74. this._blendConstants[3] = null;
  75. this._isAlphaBlendDirty = true;
  76. this._isBlendFunctionParametersDirty = false;
  77. this._isBlendEquationParametersDirty = false;
  78. this._isBlendConstantsDirty = false;
  79. }
  80. apply(gl) {
  81. if (!this.isDirty) {
  82. return;
  83. }
  84. // Alpha blend
  85. if (this._isAlphaBlendDirty) {
  86. if (this._alphaBlend) {
  87. gl.enable(gl.BLEND);
  88. }
  89. else {
  90. gl.disable(gl.BLEND);
  91. }
  92. this._isAlphaBlendDirty = false;
  93. }
  94. // Alpha function
  95. if (this._isBlendFunctionParametersDirty) {
  96. gl.blendFuncSeparate(this._blendFunctionParameters[0], this._blendFunctionParameters[1], this._blendFunctionParameters[2], this._blendFunctionParameters[3]);
  97. this._isBlendFunctionParametersDirty = false;
  98. }
  99. // Alpha equation
  100. if (this._isBlendEquationParametersDirty) {
  101. gl.blendEquationSeparate(this._blendEquationParameters[0], this._blendEquationParameters[1]);
  102. this._isBlendEquationParametersDirty = false;
  103. }
  104. // Constants
  105. if (this._isBlendConstantsDirty) {
  106. gl.blendColor(this._blendConstants[0], this._blendConstants[1], this._blendConstants[2], this._blendConstants[3]);
  107. this._isBlendConstantsDirty = false;
  108. }
  109. }
  110. }
  111. //# sourceMappingURL=alphaCullingState.js.map