babylon.stencilState.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. module BABYLON.Internals {
  2. export class _StencilState {
  3. private _isStencilTestDirty = false;
  4. private _isStencilMaskDirty = false;
  5. private _isStencilFuncDirty = false;
  6. private _isStencilOpDirty = false;
  7. private _stencilTest: boolean;
  8. private _stencilMask: number;
  9. private _stencilFunc: number;
  10. private _stencilFuncRef: number;
  11. private _stencilFuncMask: number;
  12. private _stencilOpStencilFail: number;
  13. private _stencilOpDepthFail: number;
  14. private _stencilOpStencilDepthPass: number;
  15. public get isDirty(): boolean {
  16. return this._isStencilTestDirty || this._isStencilMaskDirty || this._isStencilFuncDirty || this._isStencilOpDirty;
  17. }
  18. public get stencilFunc(): number {
  19. return this._stencilFunc;
  20. }
  21. public set stencilFunc(value: number) {
  22. if (this._stencilFunc === value) {
  23. return;
  24. }
  25. this._stencilFunc = value;
  26. this._isStencilFuncDirty = true;
  27. }
  28. public get stencilFuncRef(): number {
  29. return this._stencilFuncRef;
  30. }
  31. public set stencilFuncRef(value: number) {
  32. if (this._stencilFuncRef === value) {
  33. return;
  34. }
  35. this._stencilFuncRef = value;
  36. this._isStencilFuncDirty = true;
  37. }
  38. public get stencilFuncMask(): number {
  39. return this._stencilFuncMask;
  40. }
  41. public set stencilFuncMask(value: number) {
  42. if (this._stencilFuncMask === value) {
  43. return;
  44. }
  45. this._stencilFuncMask = value;
  46. this._isStencilFuncDirty = true;
  47. }
  48. public get stencilOpStencilFail(): number {
  49. return this._stencilOpStencilFail;
  50. }
  51. public set stencilOpStencilFail(value: number) {
  52. if (this._stencilOpStencilFail === value) {
  53. return;
  54. }
  55. this._stencilOpStencilFail = value;
  56. this._isStencilOpDirty = true;
  57. }
  58. public get stencilOpDepthFail(): number {
  59. return this._stencilOpDepthFail;
  60. }
  61. public set stencilOpDepthFail(value: number) {
  62. if (this._stencilOpDepthFail === value) {
  63. return;
  64. }
  65. this._stencilOpDepthFail = value;
  66. this._isStencilOpDirty = true;
  67. }
  68. public get stencilOpStencilDepthPass(): number {
  69. return this._stencilOpStencilDepthPass;
  70. }
  71. public set stencilOpStencilDepthPass(value: number) {
  72. if (this._stencilOpStencilDepthPass === value) {
  73. return;
  74. }
  75. this._stencilOpStencilDepthPass = value;
  76. this._isStencilOpDirty = true;
  77. }
  78. public get stencilMask(): number {
  79. return this._stencilMask;
  80. }
  81. public set stencilMask(value: number) {
  82. if (this._stencilMask === value) {
  83. return;
  84. }
  85. this._stencilMask = value;
  86. this._isStencilMaskDirty = true;
  87. }
  88. public get stencilTest(): boolean {
  89. return this._stencilTest;
  90. }
  91. public set stencilTest(value: boolean) {
  92. if (this._stencilTest === value) {
  93. return;
  94. }
  95. this._stencilTest = value;
  96. this._isStencilTestDirty = true;
  97. }
  98. public constructor() {
  99. this.reset();
  100. }
  101. public reset() {
  102. this._stencilTest = false;
  103. this._stencilMask = 0xFF;
  104. this._stencilFunc = Engine.ALWAYS;
  105. this._stencilFuncRef = 1;
  106. this._stencilFuncMask = 0xFF;
  107. this._stencilOpStencilFail = Engine.KEEP;
  108. this._stencilOpDepthFail = Engine.KEEP;
  109. this._stencilOpStencilDepthPass = Engine.REPLACE;
  110. this._isStencilTestDirty = true;
  111. this._isStencilMaskDirty = true;
  112. this._isStencilFuncDirty = true;
  113. this._isStencilOpDirty = true;
  114. }
  115. public apply(gl: WebGLRenderingContext) {
  116. if (!this.isDirty) {
  117. return;
  118. }
  119. // Stencil test
  120. if (this._isStencilTestDirty) {
  121. if (this.stencilTest) {
  122. gl.enable(gl.STENCIL_TEST);
  123. } else {
  124. gl.disable(gl.STENCIL_TEST);
  125. }
  126. this._isStencilTestDirty = false;
  127. }
  128. // Stencil mask
  129. if (this._isStencilMaskDirty) {
  130. gl.stencilMask(this.stencilMask);
  131. this._isStencilMaskDirty = false;
  132. }
  133. // Stencil func
  134. if (this._isStencilFuncDirty) {
  135. gl.stencilFunc(this.stencilFunc, this.stencilFuncRef, this.stencilFuncMask);
  136. this._isStencilFuncDirty = false;
  137. }
  138. // Stencil op
  139. if (this._isStencilOpDirty) {
  140. gl.stencilOp(this.stencilOpStencilFail, this.stencilOpDepthFail, this.stencilOpStencilDepthPass);
  141. this._isStencilOpDirty = false;
  142. }
  143. }
  144. }
  145. }