babylon.depthCullingState.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. module BABYLON.Internals {
  2. export class _DepthCullingState {
  3. private _isDepthTestDirty = false;
  4. private _isDepthMaskDirty = false;
  5. private _isDepthFuncDirty = false;
  6. private _isCullFaceDirty = false;
  7. private _isCullDirty = false;
  8. private _isZOffsetDirty = false;
  9. private _depthTest: boolean;
  10. private _depthMask: boolean;
  11. private _depthFunc: number;
  12. private _cull: boolean;
  13. private _cullFace: number;
  14. private _zOffset: number;
  15. /**
  16. * Initializes the state.
  17. */
  18. public constructor() {
  19. this.reset();
  20. }
  21. public get isDirty(): boolean {
  22. return this._isDepthFuncDirty || this._isDepthTestDirty || this._isDepthMaskDirty || this._isCullFaceDirty || this._isCullDirty || this._isZOffsetDirty;
  23. }
  24. public get zOffset(): number {
  25. return this._zOffset;
  26. }
  27. public set zOffset(value: number) {
  28. if (this._zOffset === value) {
  29. return;
  30. }
  31. this._zOffset = value;
  32. this._isZOffsetDirty = true;
  33. }
  34. public get cullFace(): number {
  35. return this._cullFace;
  36. }
  37. public set cullFace(value: number) {
  38. if (this._cullFace === value) {
  39. return;
  40. }
  41. this._cullFace = value;
  42. this._isCullFaceDirty = true;
  43. }
  44. public get cull() {
  45. return this._cull;
  46. }
  47. public set cull(value: boolean) {
  48. if (this._cull === value) {
  49. return;
  50. }
  51. this._cull = value;
  52. this._isCullDirty = true;
  53. }
  54. public get depthFunc(): number {
  55. return this._depthFunc;
  56. }
  57. public set depthFunc(value: number) {
  58. if (this._depthFunc === value) {
  59. return;
  60. }
  61. this._depthFunc = value;
  62. this._isDepthFuncDirty = true;
  63. }
  64. public get depthMask(): boolean {
  65. return this._depthMask;
  66. }
  67. public set depthMask(value: boolean) {
  68. if (this._depthMask === value) {
  69. return;
  70. }
  71. this._depthMask = value;
  72. this._isDepthMaskDirty = true;
  73. }
  74. public get depthTest(): boolean {
  75. return this._depthTest;
  76. }
  77. public set depthTest(value: boolean) {
  78. if (this._depthTest === value) {
  79. return;
  80. }
  81. this._depthTest = value;
  82. this._isDepthTestDirty = true;
  83. }
  84. public reset() {
  85. this._depthMask = true;
  86. this._depthTest = true;
  87. this._depthFunc = null;
  88. this._cullFace = null;
  89. this._cull = null;
  90. this._zOffset = 0;
  91. this._isDepthTestDirty = true;
  92. this._isDepthMaskDirty = true;
  93. this._isDepthFuncDirty = false;
  94. this._isCullFaceDirty = false;
  95. this._isCullDirty = false;
  96. this._isZOffsetDirty = false;
  97. }
  98. public apply(gl: WebGLRenderingContext) {
  99. if (!this.isDirty) {
  100. return;
  101. }
  102. // Cull
  103. if (this._isCullDirty) {
  104. if (this.cull) {
  105. gl.enable(gl.CULL_FACE);
  106. } else {
  107. gl.disable(gl.CULL_FACE);
  108. }
  109. this._isCullDirty = false;
  110. }
  111. // Cull face
  112. if (this._isCullFaceDirty) {
  113. gl.cullFace(this.cullFace);
  114. this._isCullFaceDirty = false;
  115. }
  116. // Depth mask
  117. if (this._isDepthMaskDirty) {
  118. gl.depthMask(this.depthMask);
  119. this._isDepthMaskDirty = false;
  120. }
  121. // Depth test
  122. if (this._isDepthTestDirty) {
  123. if (this.depthTest) {
  124. gl.enable(gl.DEPTH_TEST);
  125. } else {
  126. gl.disable(gl.DEPTH_TEST);
  127. }
  128. this._isDepthTestDirty = false;
  129. }
  130. // Depth func
  131. if (this._isDepthFuncDirty) {
  132. gl.depthFunc(this.depthFunc);
  133. this._isDepthFuncDirty = false;
  134. }
  135. // zOffset
  136. if (this._isZOffsetDirty) {
  137. if (this.zOffset) {
  138. gl.enable(gl.POLYGON_OFFSET_FILL);
  139. gl.polygonOffset(this.zOffset, 0);
  140. } else {
  141. gl.disable(gl.POLYGON_OFFSET_FILL);
  142. }
  143. this._isZOffsetDirty = false;
  144. }
  145. }
  146. }
  147. }