depthCullingState.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /**
  2. * @internal
  3. **/
  4. export class DepthCullingState {
  5. /**
  6. * Initializes the state.
  7. * @param reset
  8. */
  9. constructor(reset = true) {
  10. this._isDepthTestDirty = false;
  11. this._isDepthMaskDirty = false;
  12. this._isDepthFuncDirty = false;
  13. this._isCullFaceDirty = false;
  14. this._isCullDirty = false;
  15. this._isZOffsetDirty = false;
  16. this._isFrontFaceDirty = false;
  17. if (reset) {
  18. this.reset();
  19. }
  20. }
  21. get isDirty() {
  22. return (this._isDepthFuncDirty ||
  23. this._isDepthTestDirty ||
  24. this._isDepthMaskDirty ||
  25. this._isCullFaceDirty ||
  26. this._isCullDirty ||
  27. this._isZOffsetDirty ||
  28. this._isFrontFaceDirty);
  29. }
  30. get zOffset() {
  31. return this._zOffset;
  32. }
  33. set zOffset(value) {
  34. if (this._zOffset === value) {
  35. return;
  36. }
  37. this._zOffset = value;
  38. this._isZOffsetDirty = true;
  39. }
  40. get zOffsetUnits() {
  41. return this._zOffsetUnits;
  42. }
  43. set zOffsetUnits(value) {
  44. if (this._zOffsetUnits === value) {
  45. return;
  46. }
  47. this._zOffsetUnits = value;
  48. this._isZOffsetDirty = true;
  49. }
  50. get cullFace() {
  51. return this._cullFace;
  52. }
  53. set cullFace(value) {
  54. if (this._cullFace === value) {
  55. return;
  56. }
  57. this._cullFace = value;
  58. this._isCullFaceDirty = true;
  59. }
  60. get cull() {
  61. return this._cull;
  62. }
  63. set cull(value) {
  64. if (this._cull === value) {
  65. return;
  66. }
  67. this._cull = value;
  68. this._isCullDirty = true;
  69. }
  70. get depthFunc() {
  71. return this._depthFunc;
  72. }
  73. set depthFunc(value) {
  74. if (this._depthFunc === value) {
  75. return;
  76. }
  77. this._depthFunc = value;
  78. this._isDepthFuncDirty = true;
  79. }
  80. get depthMask() {
  81. return this._depthMask;
  82. }
  83. set depthMask(value) {
  84. if (this._depthMask === value) {
  85. return;
  86. }
  87. this._depthMask = value;
  88. this._isDepthMaskDirty = true;
  89. }
  90. get depthTest() {
  91. return this._depthTest;
  92. }
  93. set depthTest(value) {
  94. if (this._depthTest === value) {
  95. return;
  96. }
  97. this._depthTest = value;
  98. this._isDepthTestDirty = true;
  99. }
  100. get frontFace() {
  101. return this._frontFace;
  102. }
  103. set frontFace(value) {
  104. if (this._frontFace === value) {
  105. return;
  106. }
  107. this._frontFace = value;
  108. this._isFrontFaceDirty = true;
  109. }
  110. reset() {
  111. this._depthMask = true;
  112. this._depthTest = true;
  113. this._depthFunc = null;
  114. this._cullFace = null;
  115. this._cull = null;
  116. this._zOffset = 0;
  117. this._zOffsetUnits = 0;
  118. this._frontFace = null;
  119. this._isDepthTestDirty = true;
  120. this._isDepthMaskDirty = true;
  121. this._isDepthFuncDirty = false;
  122. this._isCullFaceDirty = false;
  123. this._isCullDirty = false;
  124. this._isZOffsetDirty = true;
  125. this._isFrontFaceDirty = false;
  126. }
  127. apply(gl) {
  128. if (!this.isDirty) {
  129. return;
  130. }
  131. // Cull
  132. if (this._isCullDirty) {
  133. if (this.cull) {
  134. gl.enable(gl.CULL_FACE);
  135. }
  136. else {
  137. gl.disable(gl.CULL_FACE);
  138. }
  139. this._isCullDirty = false;
  140. }
  141. // Cull face
  142. if (this._isCullFaceDirty) {
  143. gl.cullFace(this.cullFace);
  144. this._isCullFaceDirty = false;
  145. }
  146. // Depth mask
  147. if (this._isDepthMaskDirty) {
  148. gl.depthMask(this.depthMask);
  149. this._isDepthMaskDirty = false;
  150. }
  151. // Depth test
  152. if (this._isDepthTestDirty) {
  153. if (this.depthTest) {
  154. gl.enable(gl.DEPTH_TEST);
  155. }
  156. else {
  157. gl.disable(gl.DEPTH_TEST);
  158. }
  159. this._isDepthTestDirty = false;
  160. }
  161. // Depth func
  162. if (this._isDepthFuncDirty) {
  163. gl.depthFunc(this.depthFunc);
  164. this._isDepthFuncDirty = false;
  165. }
  166. // zOffset
  167. if (this._isZOffsetDirty) {
  168. if (this.zOffset || this.zOffsetUnits) {
  169. gl.enable(gl.POLYGON_OFFSET_FILL);
  170. gl.polygonOffset(this.zOffset, this.zOffsetUnits);
  171. }
  172. else {
  173. gl.disable(gl.POLYGON_OFFSET_FILL);
  174. }
  175. this._isZOffsetDirty = false;
  176. }
  177. // Front face
  178. if (this._isFrontFaceDirty) {
  179. gl.frontFace(this.frontFace);
  180. this._isFrontFaceDirty = false;
  181. }
  182. }
  183. }
  184. //# sourceMappingURL=depthCullingState.js.map