WebGLState.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.WebGLState = function ( gl, paramThreeToGL ) {
  5. var newAttributes = new Uint8Array( 16 );
  6. var enabledAttributes = new Uint8Array( 16 );
  7. var currentBlending = null;
  8. var currentBlendEquation = null;
  9. var currentBlendSrc = null;
  10. var currentBlendDst = null;
  11. var currentBlendEquationAlpha = null;
  12. var currentBlendSrcAlpha = null;
  13. var currentBlendDstAlpha = null;
  14. var currentDepthTest = null;
  15. var currentDepthWrite = null;
  16. var currentDoubleSided = null;
  17. var currentFlipSided = null;
  18. var currentLineWidth = null;
  19. var currentPolygonOffset = null;
  20. var currentPolygonOffsetFactor = null;
  21. var currentPolygonOffsetUnits = null;
  22. this.initAttributes = function () {
  23. for ( var i = 0, l = newAttributes.length; i < l; i ++ ) {
  24. newAttributes[ i ] = 0;
  25. }
  26. };
  27. this.enableAttribute = function ( attribute ) {
  28. newAttributes[ attribute ] = 1;
  29. if ( enabledAttributes[ attribute ] === 0 ) {
  30. gl.enableVertexAttribArray( attribute );
  31. enabledAttributes[ attribute ] = 1;
  32. }
  33. };
  34. this.disableUnusedAttributes = function () {
  35. for ( var i = 0, l = enabledAttributes.length; i < l; i ++ ) {
  36. if ( enabledAttributes[ i ] !== newAttributes[ i ] ) {
  37. gl.disableVertexAttribArray( i );
  38. enabledAttributes[ i ] = 0;
  39. }
  40. }
  41. };
  42. this.setBlending = function ( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha ) {
  43. if ( blending !== currentBlending ) {
  44. if ( blending === THREE.NoBlending ) {
  45. gl.disable( gl.BLEND );
  46. } else if ( blending === THREE.AdditiveBlending ) {
  47. gl.enable( gl.BLEND );
  48. gl.blendEquation( gl.FUNC_ADD );
  49. gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
  50. } else if ( blending === THREE.SubtractiveBlending ) {
  51. // TODO: Find blendFuncSeparate() combination
  52. gl.enable( gl.BLEND );
  53. gl.blendEquation( gl.FUNC_ADD );
  54. gl.blendFunc( gl.ZERO, gl.ONE_MINUS_SRC_COLOR );
  55. } else if ( blending === THREE.MultiplyBlending ) {
  56. // TODO: Find blendFuncSeparate() combination
  57. gl.enable( gl.BLEND );
  58. gl.blendEquation( gl.FUNC_ADD );
  59. gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
  60. } else if ( blending === THREE.CustomBlending ) {
  61. gl.enable( gl.BLEND );
  62. } else {
  63. gl.enable( gl.BLEND );
  64. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  65. gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  66. }
  67. currentBlending = blending;
  68. }
  69. if ( blending === THREE.CustomBlending ) {
  70. blendEquationAlpha = blendEquationAlpha || blendEquation;
  71. blendSrcAlpha = blendSrcAlpha || blendSrc;
  72. blendDstAlpha = blendDstAlpha || blendDst;
  73. if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
  74. gl.blendEquationSeparate( paramThreeToGL( blendEquation ), paramThreeToGL( blendEquationAlpha ) );
  75. currentBlendEquation = blendEquation;
  76. currentBlendEquationAlpha = blendEquationAlpha;
  77. }
  78. if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
  79. gl.blendFuncSeparate( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ), paramThreeToGL( blendSrcAlpha ), paramThreeToGL( blendDstAlpha ) );
  80. currentBlendSrc = blendSrc;
  81. currentBlendDst = blendDst;
  82. currentBlendSrcAlpha = blendSrcAlpha;
  83. currentBlendDstAlpha = blendDstAlpha;
  84. }
  85. } else {
  86. currentBlendEquation = null;
  87. currentBlendSrc = null;
  88. currentBlendDst = null;
  89. currentBlendEquationAlpha = null;
  90. currentBlendSrcAlpha = null;
  91. currentBlendDstAlpha = null;
  92. }
  93. };
  94. this.setDepthTest = function ( depthTest ) {
  95. if ( currentDepthTest !== depthTest ) {
  96. if ( depthTest ) {
  97. gl.enable( gl.DEPTH_TEST );
  98. } else {
  99. gl.disable( gl.DEPTH_TEST );
  100. }
  101. currentDepthTest = depthTest;
  102. }
  103. };
  104. this.setDepthWrite = function ( depthWrite ) {
  105. if ( currentDepthWrite !== depthWrite ) {
  106. gl.depthMask( depthWrite );
  107. currentDepthWrite = depthWrite;
  108. }
  109. };
  110. this.setDoubleSided = function ( doubleSided ) {
  111. if ( currentDoubleSided !== doubleSided ) {
  112. if ( doubleSided ) {
  113. gl.disable( gl.CULL_FACE );
  114. } else {
  115. gl.enable( gl.CULL_FACE );
  116. }
  117. currentDoubleSided = doubleSided;
  118. }
  119. };
  120. this.setFlipSided = function ( flipSided ) {
  121. if ( currentFlipSided !== flipSided ) {
  122. if ( flipSided ) {
  123. gl.frontFace( gl.CW );
  124. } else {
  125. gl.frontFace( gl.CCW );
  126. }
  127. currentFlipSided = flipSided;
  128. }
  129. };
  130. this.setLineWidth = function ( width ) {
  131. if ( width !== currentLineWidth ) {
  132. gl.lineWidth( width );
  133. currentLineWidth = width;
  134. }
  135. };
  136. this.setPolygonOffset = function ( polygonoffset, factor, units ) {
  137. if ( currentPolygonOffset !== polygonoffset ) {
  138. if ( polygonoffset ) {
  139. gl.enable( gl.POLYGON_OFFSET_FILL );
  140. } else {
  141. gl.disable( gl.POLYGON_OFFSET_FILL );
  142. }
  143. currentPolygonOffset = polygonoffset;
  144. }
  145. if ( polygonoffset && ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) ) {
  146. gl.polygonOffset( factor, units );
  147. currentPolygonOffsetFactor = factor;
  148. currentPolygonOffsetUnits = units;
  149. }
  150. };
  151. this.reset = function () {
  152. for ( var i = 0; i < enabledAttributes.length; i ++ ) {
  153. enabledAttributes[ i ] = 0;
  154. }
  155. currentBlending = null;
  156. currentDepthTest = null;
  157. currentDepthWrite = null;
  158. currentDoubleSided = null;
  159. currentFlipSided = null;
  160. };
  161. };