WebGLState.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import { NotEqualDepth, GreaterDepth, GreaterEqualDepth, EqualDepth, LessEqualDepth, LessDepth, AlwaysDepth, NeverDepth, CullFaceFront, CullFaceBack, CullFaceNone, CustomBlending, MultiplyBlending, SubtractiveBlending, AdditiveBlending, NoBlending, NormalBlending, AddEquation, DoubleSide, BackSide } from '../../constants.js';
  5. import { Vector4 } from '../../math/Vector4.js';
  6. function WebGLState( gl, extensions, utils, capabilities ) {
  7. function ColorBuffer() {
  8. var locked = false;
  9. var color = new Vector4();
  10. var currentColorMask = null;
  11. var currentColorClear = new Vector4( 0, 0, 0, 0 );
  12. return {
  13. setMask: function ( colorMask ) {
  14. if ( currentColorMask !== colorMask && ! locked ) {
  15. gl.colorMask( colorMask, colorMask, colorMask, colorMask );
  16. currentColorMask = colorMask;
  17. }
  18. },
  19. setLocked: function ( lock ) {
  20. locked = lock;
  21. },
  22. setClear: function ( r, g, b, a, premultipliedAlpha ) {
  23. if ( premultipliedAlpha === true ) {
  24. r *= a; g *= a; b *= a;
  25. }
  26. color.set( r, g, b, a );
  27. if ( currentColorClear.equals( color ) === false ) {
  28. gl.clearColor( r, g, b, a );
  29. currentColorClear.copy( color );
  30. }
  31. },
  32. reset: function () {
  33. locked = false;
  34. currentColorMask = null;
  35. currentColorClear.set( - 1, 0, 0, 0 ); // set to invalid state
  36. }
  37. };
  38. }
  39. function DepthBuffer() {
  40. var locked = false;
  41. var currentDepthMask = null;
  42. var currentDepthFunc = null;
  43. var currentDepthClear = null;
  44. return {
  45. setTest: function ( depthTest ) {
  46. if ( depthTest ) {
  47. enable( gl.DEPTH_TEST );
  48. } else {
  49. disable( gl.DEPTH_TEST );
  50. }
  51. },
  52. setMask: function ( depthMask ) {
  53. if ( currentDepthMask !== depthMask && ! locked ) {
  54. gl.depthMask( depthMask );
  55. currentDepthMask = depthMask;
  56. }
  57. },
  58. setFunc: function ( depthFunc ) {
  59. if ( currentDepthFunc !== depthFunc ) {
  60. if ( depthFunc ) {
  61. switch ( depthFunc ) {
  62. case NeverDepth:
  63. gl.depthFunc( gl.NEVER );
  64. break;
  65. case AlwaysDepth:
  66. gl.depthFunc( gl.ALWAYS );
  67. break;
  68. case LessDepth:
  69. gl.depthFunc( gl.LESS );
  70. break;
  71. case LessEqualDepth:
  72. gl.depthFunc( gl.LEQUAL );
  73. break;
  74. case EqualDepth:
  75. gl.depthFunc( gl.EQUAL );
  76. break;
  77. case GreaterEqualDepth:
  78. gl.depthFunc( gl.GEQUAL );
  79. break;
  80. case GreaterDepth:
  81. gl.depthFunc( gl.GREATER );
  82. break;
  83. case NotEqualDepth:
  84. gl.depthFunc( gl.NOTEQUAL );
  85. break;
  86. default:
  87. gl.depthFunc( gl.LEQUAL );
  88. }
  89. } else {
  90. gl.depthFunc( gl.LEQUAL );
  91. }
  92. currentDepthFunc = depthFunc;
  93. }
  94. },
  95. setLocked: function ( lock ) {
  96. locked = lock;
  97. },
  98. setClear: function ( depth ) {
  99. if ( currentDepthClear !== depth ) {
  100. gl.clearDepth( depth );
  101. currentDepthClear = depth;
  102. }
  103. },
  104. reset: function () {
  105. locked = false;
  106. currentDepthMask = null;
  107. currentDepthFunc = null;
  108. currentDepthClear = null;
  109. }
  110. };
  111. }
  112. function StencilBuffer() {
  113. var locked = false;
  114. var currentStencilMask = null;
  115. var currentStencilFunc = null;
  116. var currentStencilRef = null;
  117. var currentStencilFuncMask = null;
  118. var currentStencilFail = null;
  119. var currentStencilZFail = null;
  120. var currentStencilZPass = null;
  121. var currentStencilClear = null;
  122. return {
  123. setTest: function ( stencilTest ) {
  124. if ( ! locked ) {
  125. if ( stencilTest ) {
  126. enable( gl.STENCIL_TEST );
  127. } else {
  128. disable( gl.STENCIL_TEST );
  129. }
  130. }
  131. },
  132. setMask: function ( stencilMask ) {
  133. if ( currentStencilMask !== stencilMask && ! locked ) {
  134. gl.stencilMask( stencilMask );
  135. currentStencilMask = stencilMask;
  136. }
  137. },
  138. setFunc: function ( stencilFunc, stencilRef, stencilMask ) {
  139. if ( currentStencilFunc !== stencilFunc ||
  140. currentStencilRef !== stencilRef ||
  141. currentStencilFuncMask !== stencilMask ) {
  142. gl.stencilFunc( stencilFunc, stencilRef, stencilMask );
  143. currentStencilFunc = stencilFunc;
  144. currentStencilRef = stencilRef;
  145. currentStencilFuncMask = stencilMask;
  146. }
  147. },
  148. setOp: function ( stencilFail, stencilZFail, stencilZPass ) {
  149. if ( currentStencilFail !== stencilFail ||
  150. currentStencilZFail !== stencilZFail ||
  151. currentStencilZPass !== stencilZPass ) {
  152. gl.stencilOp( stencilFail, stencilZFail, stencilZPass );
  153. currentStencilFail = stencilFail;
  154. currentStencilZFail = stencilZFail;
  155. currentStencilZPass = stencilZPass;
  156. }
  157. },
  158. setLocked: function ( lock ) {
  159. locked = lock;
  160. },
  161. setClear: function ( stencil ) {
  162. if ( currentStencilClear !== stencil ) {
  163. gl.clearStencil( stencil );
  164. currentStencilClear = stencil;
  165. }
  166. },
  167. reset: function () {
  168. locked = false;
  169. currentStencilMask = null;
  170. currentStencilFunc = null;
  171. currentStencilRef = null;
  172. currentStencilFuncMask = null;
  173. currentStencilFail = null;
  174. currentStencilZFail = null;
  175. currentStencilZPass = null;
  176. currentStencilClear = null;
  177. }
  178. };
  179. }
  180. //
  181. var colorBuffer = new ColorBuffer();
  182. var depthBuffer = new DepthBuffer();
  183. var stencilBuffer = new StencilBuffer();
  184. var maxVertexAttributes = gl.getParameter( gl.MAX_VERTEX_ATTRIBS );
  185. var newAttributes = new Uint8Array( maxVertexAttributes );
  186. var enabledAttributes = new Uint8Array( maxVertexAttributes );
  187. var attributeDivisors = new Uint8Array( maxVertexAttributes );
  188. var enabledCapabilities = {};
  189. var compressedTextureFormats = null;
  190. var currentProgram = null;
  191. var currentBlendingEnabled = null;
  192. var currentBlending = null;
  193. var currentBlendEquation = null;
  194. var currentBlendSrc = null;
  195. var currentBlendDst = null;
  196. var currentBlendEquationAlpha = null;
  197. var currentBlendSrcAlpha = null;
  198. var currentBlendDstAlpha = null;
  199. var currentPremultipledAlpha = false;
  200. var currentFlipSided = null;
  201. var currentCullFace = null;
  202. var currentLineWidth = null;
  203. var currentPolygonOffsetFactor = null;
  204. var currentPolygonOffsetUnits = null;
  205. var maxTextures = gl.getParameter( gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS );
  206. var lineWidthAvailable = false;
  207. var version = 0;
  208. var glVersion = gl.getParameter( gl.VERSION );
  209. if ( glVersion.indexOf( 'WebGL' ) !== - 1 ) {
  210. version = parseFloat( /^WebGL\ ([0-9])/.exec( glVersion )[ 1 ] );
  211. lineWidthAvailable = ( version >= 1.0 );
  212. } else if ( glVersion.indexOf( 'OpenGL ES' ) !== - 1 ) {
  213. version = parseFloat( /^OpenGL\ ES\ ([0-9])/.exec( glVersion )[ 1 ] );
  214. lineWidthAvailable = ( version >= 2.0 );
  215. }
  216. var currentTextureSlot = null;
  217. var currentBoundTextures = {};
  218. var currentScissor = new Vector4();
  219. var currentViewport = new Vector4();
  220. function createTexture( type, target, count ) {
  221. var data = new Uint8Array( 4 ); // 4 is required to match default unpack alignment of 4.
  222. var texture = gl.createTexture();
  223. gl.bindTexture( type, texture );
  224. gl.texParameteri( type, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  225. gl.texParameteri( type, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  226. for ( var i = 0; i < count; i ++ ) {
  227. gl.texImage2D( target + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  228. }
  229. return texture;
  230. }
  231. var emptyTextures = {};
  232. emptyTextures[ gl.TEXTURE_2D ] = createTexture( gl.TEXTURE_2D, gl.TEXTURE_2D, 1 );
  233. emptyTextures[ gl.TEXTURE_CUBE_MAP ] = createTexture( gl.TEXTURE_CUBE_MAP, gl.TEXTURE_CUBE_MAP_POSITIVE_X, 6 );
  234. // init
  235. colorBuffer.setClear( 0, 0, 0, 1 );
  236. depthBuffer.setClear( 1 );
  237. stencilBuffer.setClear( 0 );
  238. enable( gl.DEPTH_TEST );
  239. depthBuffer.setFunc( LessEqualDepth );
  240. setFlipSided( false );
  241. setCullFace( CullFaceBack );
  242. enable( gl.CULL_FACE );
  243. setBlending( NoBlending );
  244. //
  245. function initAttributes() {
  246. for ( var i = 0, l = newAttributes.length; i < l; i ++ ) {
  247. newAttributes[ i ] = 0;
  248. }
  249. }
  250. function enableAttribute( attribute ) {
  251. enableAttributeAndDivisor( attribute, 0 );
  252. }
  253. function enableAttributeAndDivisor( attribute, meshPerAttribute ) {
  254. newAttributes[ attribute ] = 1;
  255. if ( enabledAttributes[ attribute ] === 0 ) {
  256. gl.enableVertexAttribArray( attribute );
  257. enabledAttributes[ attribute ] = 1;
  258. }
  259. if ( attributeDivisors[ attribute ] !== meshPerAttribute ) {
  260. var extension = capabilities.isWebGL2 ? gl : extensions.get( 'ANGLE_instanced_arrays' );
  261. extension[ capabilities.isWebGL2 ? 'vertexAttribDivisor' : 'vertexAttribDivisorANGLE' ]( attribute, meshPerAttribute );
  262. attributeDivisors[ attribute ] = meshPerAttribute;
  263. }
  264. }
  265. function disableUnusedAttributes() {
  266. for ( var i = 0, l = enabledAttributes.length; i !== l; ++ i ) {
  267. if ( enabledAttributes[ i ] !== newAttributes[ i ] ) {
  268. gl.disableVertexAttribArray( i );
  269. enabledAttributes[ i ] = 0;
  270. }
  271. }
  272. }
  273. function enable( id ) {
  274. if ( enabledCapabilities[ id ] !== true ) {
  275. gl.enable( id );
  276. enabledCapabilities[ id ] = true;
  277. }
  278. }
  279. function disable( id ) {
  280. if ( enabledCapabilities[ id ] !== false ) {
  281. gl.disable( id );
  282. enabledCapabilities[ id ] = false;
  283. }
  284. }
  285. function getCompressedTextureFormats() {
  286. if ( compressedTextureFormats === null ) {
  287. compressedTextureFormats = [];
  288. if ( extensions.get( 'WEBGL_compressed_texture_pvrtc' ) ||
  289. extensions.get( 'WEBGL_compressed_texture_s3tc' ) ||
  290. extensions.get( 'WEBGL_compressed_texture_etc1' ) ||
  291. extensions.get( 'WEBGL_compressed_texture_astc' ) ) {
  292. var formats = gl.getParameter( gl.COMPRESSED_TEXTURE_FORMATS );
  293. for ( var i = 0; i < formats.length; i ++ ) {
  294. compressedTextureFormats.push( formats[ i ] );
  295. }
  296. }
  297. }
  298. return compressedTextureFormats;
  299. }
  300. function useProgram( program ) {
  301. if ( currentProgram !== program ) {
  302. gl.useProgram( program );
  303. currentProgram = program;
  304. return true;
  305. }
  306. return false;
  307. }
  308. function setBlending( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, premultipliedAlpha ) {
  309. if ( blending === NoBlending ) {
  310. if ( currentBlendingEnabled ) {
  311. disable( gl.BLEND );
  312. currentBlendingEnabled = false;
  313. }
  314. return;
  315. }
  316. if ( ! currentBlendingEnabled ) {
  317. enable( gl.BLEND );
  318. currentBlendingEnabled = true;
  319. }
  320. if ( blending !== CustomBlending ) {
  321. if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) {
  322. if ( currentBlendEquation !== AddEquation || currentBlendEquationAlpha !== AddEquation ) {
  323. gl.blendEquation( gl.FUNC_ADD );
  324. currentBlendEquation = AddEquation;
  325. currentBlendEquationAlpha = AddEquation;
  326. }
  327. if ( premultipliedAlpha ) {
  328. switch ( blending ) {
  329. case NormalBlending:
  330. gl.blendFuncSeparate( gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  331. break;
  332. case AdditiveBlending:
  333. gl.blendFunc( gl.ONE, gl.ONE );
  334. break;
  335. case SubtractiveBlending:
  336. gl.blendFuncSeparate( gl.ZERO, gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ONE_MINUS_SRC_ALPHA );
  337. break;
  338. case MultiplyBlending:
  339. gl.blendFuncSeparate( gl.ZERO, gl.SRC_COLOR, gl.ZERO, gl.SRC_ALPHA );
  340. break;
  341. default:
  342. console.error( 'THREE.WebGLState: Invalid blending: ', blending );
  343. break;
  344. }
  345. } else {
  346. switch ( blending ) {
  347. case NormalBlending:
  348. gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  349. break;
  350. case AdditiveBlending:
  351. gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
  352. break;
  353. case SubtractiveBlending:
  354. gl.blendFunc( gl.ZERO, gl.ONE_MINUS_SRC_COLOR );
  355. break;
  356. case MultiplyBlending:
  357. gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
  358. break;
  359. default:
  360. console.error( 'THREE.WebGLState: Invalid blending: ', blending );
  361. break;
  362. }
  363. }
  364. currentBlendSrc = null;
  365. currentBlendDst = null;
  366. currentBlendSrcAlpha = null;
  367. currentBlendDstAlpha = null;
  368. currentBlending = blending;
  369. currentPremultipledAlpha = premultipliedAlpha;
  370. }
  371. return;
  372. }
  373. // custom blending
  374. blendEquationAlpha = blendEquationAlpha || blendEquation;
  375. blendSrcAlpha = blendSrcAlpha || blendSrc;
  376. blendDstAlpha = blendDstAlpha || blendDst;
  377. if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
  378. gl.blendEquationSeparate( utils.convert( blendEquation ), utils.convert( blendEquationAlpha ) );
  379. currentBlendEquation = blendEquation;
  380. currentBlendEquationAlpha = blendEquationAlpha;
  381. }
  382. if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
  383. gl.blendFuncSeparate( utils.convert( blendSrc ), utils.convert( blendDst ), utils.convert( blendSrcAlpha ), utils.convert( blendDstAlpha ) );
  384. currentBlendSrc = blendSrc;
  385. currentBlendDst = blendDst;
  386. currentBlendSrcAlpha = blendSrcAlpha;
  387. currentBlendDstAlpha = blendDstAlpha;
  388. }
  389. currentBlending = blending;
  390. currentPremultipledAlpha = null;
  391. }
  392. function setMaterial( material, frontFaceCW ) {
  393. material.side === DoubleSide
  394. ? disable( gl.CULL_FACE )
  395. : enable( gl.CULL_FACE );
  396. var flipSided = ( material.side === BackSide );
  397. if ( frontFaceCW ) flipSided = ! flipSided;
  398. setFlipSided( flipSided );
  399. ( material.blending === NormalBlending && material.transparent === false )
  400. ? setBlending( NoBlending )
  401. : setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha, material.premultipliedAlpha );
  402. depthBuffer.setFunc( material.depthFunc );
  403. depthBuffer.setTest( material.depthTest );
  404. depthBuffer.setMask( material.depthWrite );
  405. colorBuffer.setMask( material.colorWrite );
  406. var stencilWrite = material.stencilWrite;
  407. stencilBuffer.setTest( stencilWrite );
  408. if ( stencilWrite ) {
  409. stencilBuffer.setMask( material.stencilWriteMask );
  410. stencilBuffer.setFunc( material.stencilFunc, material.stencilRef, material.stencilFuncMask );
  411. stencilBuffer.setOp( material.stencilFail, material.stencilZFail, material.stencilZPass );
  412. }
  413. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  414. }
  415. //
  416. function setFlipSided( flipSided ) {
  417. if ( currentFlipSided !== flipSided ) {
  418. if ( flipSided ) {
  419. gl.frontFace( gl.CW );
  420. } else {
  421. gl.frontFace( gl.CCW );
  422. }
  423. currentFlipSided = flipSided;
  424. }
  425. }
  426. function setCullFace( cullFace ) {
  427. if ( cullFace !== CullFaceNone ) {
  428. enable( gl.CULL_FACE );
  429. if ( cullFace !== currentCullFace ) {
  430. if ( cullFace === CullFaceBack ) {
  431. gl.cullFace( gl.BACK );
  432. } else if ( cullFace === CullFaceFront ) {
  433. gl.cullFace( gl.FRONT );
  434. } else {
  435. gl.cullFace( gl.FRONT_AND_BACK );
  436. }
  437. }
  438. } else {
  439. disable( gl.CULL_FACE );
  440. }
  441. currentCullFace = cullFace;
  442. }
  443. function setLineWidth( width ) {
  444. if ( width !== currentLineWidth ) {
  445. if ( lineWidthAvailable ) gl.lineWidth( width );
  446. currentLineWidth = width;
  447. }
  448. }
  449. function setPolygonOffset( polygonOffset, factor, units ) {
  450. if ( polygonOffset ) {
  451. enable( gl.POLYGON_OFFSET_FILL );
  452. if ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) {
  453. gl.polygonOffset( factor, units );
  454. currentPolygonOffsetFactor = factor;
  455. currentPolygonOffsetUnits = units;
  456. }
  457. } else {
  458. disable( gl.POLYGON_OFFSET_FILL );
  459. }
  460. }
  461. function setScissorTest( scissorTest ) {
  462. if ( scissorTest ) {
  463. enable( gl.SCISSOR_TEST );
  464. } else {
  465. disable( gl.SCISSOR_TEST );
  466. }
  467. }
  468. // texture
  469. function activeTexture( webglSlot ) {
  470. if ( webglSlot === undefined ) webglSlot = gl.TEXTURE0 + maxTextures - 1;
  471. if ( currentTextureSlot !== webglSlot ) {
  472. gl.activeTexture( webglSlot );
  473. currentTextureSlot = webglSlot;
  474. }
  475. }
  476. function bindTexture( webglType, webglTexture ) {
  477. if ( currentTextureSlot === null ) {
  478. activeTexture();
  479. }
  480. var boundTexture = currentBoundTextures[ currentTextureSlot ];
  481. if ( boundTexture === undefined ) {
  482. boundTexture = { type: undefined, texture: undefined };
  483. currentBoundTextures[ currentTextureSlot ] = boundTexture;
  484. }
  485. if ( boundTexture.type !== webglType || boundTexture.texture !== webglTexture ) {
  486. gl.bindTexture( webglType, webglTexture || emptyTextures[ webglType ] );
  487. boundTexture.type = webglType;
  488. boundTexture.texture = webglTexture;
  489. }
  490. }
  491. function compressedTexImage2D() {
  492. try {
  493. gl.compressedTexImage2D.apply( gl, arguments );
  494. } catch ( error ) {
  495. console.error( 'THREE.WebGLState:', error );
  496. }
  497. }
  498. function texImage2D() {
  499. try {
  500. gl.texImage2D.apply( gl, arguments );
  501. } catch ( error ) {
  502. console.error( 'THREE.WebGLState:', error );
  503. }
  504. }
  505. function texImage3D() {
  506. try {
  507. gl.texImage3D.apply( gl, arguments );
  508. } catch ( error ) {
  509. console.error( 'THREE.WebGLState:', error );
  510. }
  511. }
  512. //
  513. function scissor( scissor ) {
  514. if ( currentScissor.equals( scissor ) === false ) {
  515. gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w );
  516. currentScissor.copy( scissor );
  517. }
  518. }
  519. function viewport( viewport ) {
  520. if ( currentViewport.equals( viewport ) === false ) {
  521. gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w );
  522. currentViewport.copy( viewport );
  523. }
  524. }
  525. //
  526. function reset() {
  527. for ( var i = 0; i < enabledAttributes.length; i ++ ) {
  528. if ( enabledAttributes[ i ] === 1 ) {
  529. gl.disableVertexAttribArray( i );
  530. enabledAttributes[ i ] = 0;
  531. }
  532. }
  533. enabledCapabilities = {};
  534. compressedTextureFormats = null;
  535. currentTextureSlot = null;
  536. currentBoundTextures = {};
  537. currentProgram = null;
  538. currentBlending = null;
  539. currentFlipSided = null;
  540. currentCullFace = null;
  541. colorBuffer.reset();
  542. depthBuffer.reset();
  543. stencilBuffer.reset();
  544. }
  545. return {
  546. buffers: {
  547. color: colorBuffer,
  548. depth: depthBuffer,
  549. stencil: stencilBuffer
  550. },
  551. initAttributes: initAttributes,
  552. enableAttribute: enableAttribute,
  553. enableAttributeAndDivisor: enableAttributeAndDivisor,
  554. disableUnusedAttributes: disableUnusedAttributes,
  555. enable: enable,
  556. disable: disable,
  557. getCompressedTextureFormats: getCompressedTextureFormats,
  558. useProgram: useProgram,
  559. setBlending: setBlending,
  560. setMaterial: setMaterial,
  561. setFlipSided: setFlipSided,
  562. setCullFace: setCullFace,
  563. setLineWidth: setLineWidth,
  564. setPolygonOffset: setPolygonOffset,
  565. setScissorTest: setScissorTest,
  566. activeTexture: activeTexture,
  567. bindTexture: bindTexture,
  568. compressedTexImage2D: compressedTexImage2D,
  569. texImage2D: texImage2D,
  570. texImage3D: texImage3D,
  571. scissor: scissor,
  572. viewport: viewport,
  573. reset: reset
  574. };
  575. }
  576. export { WebGLState };