stencilStateComposer.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @internal
  3. **/
  4. export class StencilStateComposer {
  5. get isDirty() {
  6. return this._isStencilTestDirty || this._isStencilMaskDirty || this._isStencilFuncDirty || this._isStencilOpDirty;
  7. }
  8. get func() {
  9. return this._func;
  10. }
  11. set func(value) {
  12. if (this._func === value) {
  13. return;
  14. }
  15. this._func = value;
  16. this._isStencilFuncDirty = true;
  17. }
  18. get funcRef() {
  19. return this._funcRef;
  20. }
  21. set funcRef(value) {
  22. if (this._funcRef === value) {
  23. return;
  24. }
  25. this._funcRef = value;
  26. this._isStencilFuncDirty = true;
  27. }
  28. get funcMask() {
  29. return this._funcMask;
  30. }
  31. set funcMask(value) {
  32. if (this._funcMask === value) {
  33. return;
  34. }
  35. this._funcMask = value;
  36. this._isStencilFuncDirty = true;
  37. }
  38. get opStencilFail() {
  39. return this._opStencilFail;
  40. }
  41. set opStencilFail(value) {
  42. if (this._opStencilFail === value) {
  43. return;
  44. }
  45. this._opStencilFail = value;
  46. this._isStencilOpDirty = true;
  47. }
  48. get opDepthFail() {
  49. return this._opDepthFail;
  50. }
  51. set opDepthFail(value) {
  52. if (this._opDepthFail === value) {
  53. return;
  54. }
  55. this._opDepthFail = value;
  56. this._isStencilOpDirty = true;
  57. }
  58. get opStencilDepthPass() {
  59. return this._opStencilDepthPass;
  60. }
  61. set opStencilDepthPass(value) {
  62. if (this._opStencilDepthPass === value) {
  63. return;
  64. }
  65. this._opStencilDepthPass = value;
  66. this._isStencilOpDirty = true;
  67. }
  68. get mask() {
  69. return this._mask;
  70. }
  71. set mask(value) {
  72. if (this._mask === value) {
  73. return;
  74. }
  75. this._mask = value;
  76. this._isStencilMaskDirty = true;
  77. }
  78. get enabled() {
  79. return this._enabled;
  80. }
  81. set enabled(value) {
  82. if (this._enabled === value) {
  83. return;
  84. }
  85. this._enabled = value;
  86. this._isStencilTestDirty = true;
  87. }
  88. constructor(reset = true) {
  89. this._isStencilTestDirty = false;
  90. this._isStencilMaskDirty = false;
  91. this._isStencilFuncDirty = false;
  92. this._isStencilOpDirty = false;
  93. this.useStencilGlobalOnly = false;
  94. if (reset) {
  95. this.reset();
  96. }
  97. }
  98. reset() {
  99. this.stencilMaterial = undefined;
  100. this.stencilGlobal?.reset();
  101. this._isStencilTestDirty = true;
  102. this._isStencilMaskDirty = true;
  103. this._isStencilFuncDirty = true;
  104. this._isStencilOpDirty = true;
  105. }
  106. apply(gl) {
  107. if (!gl) {
  108. return;
  109. }
  110. const stencilMaterialEnabled = !this.useStencilGlobalOnly && !!this.stencilMaterial?.enabled;
  111. this.enabled = stencilMaterialEnabled ? this.stencilMaterial.enabled : this.stencilGlobal.enabled;
  112. this.func = stencilMaterialEnabled ? this.stencilMaterial.func : this.stencilGlobal.func;
  113. this.funcRef = stencilMaterialEnabled ? this.stencilMaterial.funcRef : this.stencilGlobal.funcRef;
  114. this.funcMask = stencilMaterialEnabled ? this.stencilMaterial.funcMask : this.stencilGlobal.funcMask;
  115. this.opStencilFail = stencilMaterialEnabled ? this.stencilMaterial.opStencilFail : this.stencilGlobal.opStencilFail;
  116. this.opDepthFail = stencilMaterialEnabled ? this.stencilMaterial.opDepthFail : this.stencilGlobal.opDepthFail;
  117. this.opStencilDepthPass = stencilMaterialEnabled ? this.stencilMaterial.opStencilDepthPass : this.stencilGlobal.opStencilDepthPass;
  118. this.mask = stencilMaterialEnabled ? this.stencilMaterial.mask : this.stencilGlobal.mask;
  119. if (!this.isDirty) {
  120. return;
  121. }
  122. // Stencil test
  123. if (this._isStencilTestDirty) {
  124. if (this.enabled) {
  125. gl.enable(gl.STENCIL_TEST);
  126. }
  127. else {
  128. gl.disable(gl.STENCIL_TEST);
  129. }
  130. this._isStencilTestDirty = false;
  131. }
  132. // Stencil mask
  133. if (this._isStencilMaskDirty) {
  134. gl.stencilMask(this.mask);
  135. this._isStencilMaskDirty = false;
  136. }
  137. // Stencil func
  138. if (this._isStencilFuncDirty) {
  139. gl.stencilFunc(this.func, this.funcRef, this.funcMask);
  140. this._isStencilFuncDirty = false;
  141. }
  142. // Stencil op
  143. if (this._isStencilOpDirty) {
  144. gl.stencilOp(this.opStencilFail, this.opDepthFail, this.opStencilDepthPass);
  145. this._isStencilOpDirty = false;
  146. }
  147. }
  148. }
  149. //# sourceMappingURL=stencilStateComposer.js.map