babylon.alphaCullingState.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. module BABYLON.Internals {
  2. export class _AlphaState {
  3. private _isAlphaBlendDirty = false;
  4. private _isBlendFunctionParametersDirty = false;
  5. private _alphaBlend = false;
  6. private _blendFunctionParameters = new Array<number>(4);
  7. public get isDirty(): boolean {
  8. return this._isAlphaBlendDirty || this._isBlendFunctionParametersDirty;
  9. }
  10. public get alphaBlend(): boolean {
  11. return this._alphaBlend;
  12. }
  13. public set alphaBlend(value: boolean) {
  14. if (this._alphaBlend === value) {
  15. return;
  16. }
  17. this._alphaBlend = value;
  18. this._isAlphaBlendDirty = true;
  19. }
  20. public setAlphaBlendFunctionParameters(value0: number, value1: number, value2: number, value3: number): void {
  21. if (
  22. this._blendFunctionParameters[0] === value0 &&
  23. this._blendFunctionParameters[1] === value1 &&
  24. this._blendFunctionParameters[2] === value2 &&
  25. this._blendFunctionParameters[3] === value3
  26. ) {
  27. return;
  28. }
  29. this._blendFunctionParameters[0] = value0;
  30. this._blendFunctionParameters[1] = value1;
  31. this._blendFunctionParameters[2] = value2;
  32. this._blendFunctionParameters[3] = value3;
  33. this._isBlendFunctionParametersDirty = true;
  34. }
  35. public reset() {
  36. this._alphaBlend = false;
  37. this._blendFunctionParameters[0] = null;
  38. this._blendFunctionParameters[1] = null;
  39. this._blendFunctionParameters[2] = null;
  40. this._blendFunctionParameters[3] = null;
  41. this._isAlphaBlendDirty = true;
  42. this._isBlendFunctionParametersDirty = false;
  43. }
  44. public apply(gl: WebGLRenderingContext) {
  45. if (!this.isDirty) {
  46. return;
  47. }
  48. // Alpha blend
  49. if (this._isAlphaBlendDirty) {
  50. if (this._alphaBlend) {
  51. gl.enable(gl.BLEND);
  52. } else {
  53. gl.disable(gl.BLEND);
  54. }
  55. this._isAlphaBlendDirty = false;
  56. }
  57. // Alpha function
  58. if (this._isBlendFunctionParametersDirty) {
  59. gl.blendFuncSeparate(this._blendFunctionParameters[0], this._blendFunctionParameters[1], this._blendFunctionParameters[2], this._blendFunctionParameters[3]);
  60. this._isBlendFunctionParametersDirty = false;
  61. }
  62. }
  63. }
  64. }