WebGLState.js 18 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, DoubleSide, BackSide } from '../../constants';
  5. import { Vector4 } from '../../math/Vector4';
  6. function WebGLState( gl, extensions, paramThreeToGL ) {
  7. function ColorBuffer() {
  8. var locked = false;
  9. var color = new Vector4();
  10. var currentColorMask = null;
  11. var currentColorClear = new Vector4();
  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( 0, 0, 0, 1 );
  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 ( stencilTest ) {
  125. enable( gl.STENCIL_TEST );
  126. } else {
  127. disable( gl.STENCIL_TEST );
  128. }
  129. },
  130. setMask: function ( stencilMask ) {
  131. if ( currentStencilMask !== stencilMask && ! locked ) {
  132. gl.stencilMask( stencilMask );
  133. currentStencilMask = stencilMask;
  134. }
  135. },
  136. setFunc: function ( stencilFunc, stencilRef, stencilMask ) {
  137. if ( currentStencilFunc !== stencilFunc ||
  138. currentStencilRef !== stencilRef ||
  139. currentStencilFuncMask !== stencilMask ) {
  140. gl.stencilFunc( stencilFunc, stencilRef, stencilMask );
  141. currentStencilFunc = stencilFunc;
  142. currentStencilRef = stencilRef;
  143. currentStencilFuncMask = stencilMask;
  144. }
  145. },
  146. setOp: function ( stencilFail, stencilZFail, stencilZPass ) {
  147. if ( currentStencilFail !== stencilFail ||
  148. currentStencilZFail !== stencilZFail ||
  149. currentStencilZPass !== stencilZPass ) {
  150. gl.stencilOp( stencilFail, stencilZFail, stencilZPass );
  151. currentStencilFail = stencilFail;
  152. currentStencilZFail = stencilZFail;
  153. currentStencilZPass = stencilZPass;
  154. }
  155. },
  156. setLocked: function ( lock ) {
  157. locked = lock;
  158. },
  159. setClear: function ( stencil ) {
  160. if ( currentStencilClear !== stencil ) {
  161. gl.clearStencil( stencil );
  162. currentStencilClear = stencil;
  163. }
  164. },
  165. reset: function () {
  166. locked = false;
  167. currentStencilMask = null;
  168. currentStencilFunc = null;
  169. currentStencilRef = null;
  170. currentStencilFuncMask = null;
  171. currentStencilFail = null;
  172. currentStencilZFail = null;
  173. currentStencilZPass = null;
  174. currentStencilClear = null;
  175. }
  176. };
  177. }
  178. //
  179. var colorBuffer = new ColorBuffer();
  180. var depthBuffer = new DepthBuffer();
  181. var stencilBuffer = new StencilBuffer();
  182. var maxVertexAttributes = gl.getParameter( gl.MAX_VERTEX_ATTRIBS );
  183. var newAttributes = new Uint8Array( maxVertexAttributes );
  184. var enabledAttributes = new Uint8Array( maxVertexAttributes );
  185. var attributeDivisors = new Uint8Array( maxVertexAttributes );
  186. var capabilities = {};
  187. var compressedTextureFormats = null;
  188. var currentBlending = null;
  189. var currentBlendEquation = null;
  190. var currentBlendSrc = null;
  191. var currentBlendDst = null;
  192. var currentBlendEquationAlpha = null;
  193. var currentBlendSrcAlpha = null;
  194. var currentBlendDstAlpha = null;
  195. var currentPremultipledAlpha = false;
  196. var currentFlipSided = null;
  197. var currentCullFace = null;
  198. var currentLineWidth = null;
  199. var currentPolygonOffsetFactor = null;
  200. var currentPolygonOffsetUnits = null;
  201. var currentScissorTest = null;
  202. var maxTextures = gl.getParameter( gl.MAX_TEXTURE_IMAGE_UNITS );
  203. var version = parseFloat( /^WebGL\ ([0-9])/.exec( gl.getParameter( gl.VERSION ) )[ 1 ] );
  204. var lineWidthAvailable = parseFloat( version ) >= 1.0;
  205. var currentTextureSlot = null;
  206. var currentBoundTextures = {};
  207. var currentScissor = new Vector4();
  208. var currentViewport = new Vector4();
  209. function createTexture( type, target, count ) {
  210. var data = new Uint8Array( 4 ); // 4 is required to match default unpack alignment of 4.
  211. var texture = gl.createTexture();
  212. gl.bindTexture( type, texture );
  213. gl.texParameteri( type, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  214. gl.texParameteri( type, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  215. for ( var i = 0; i < count; i ++ ) {
  216. gl.texImage2D( target + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  217. }
  218. return texture;
  219. }
  220. var emptyTextures = {};
  221. emptyTextures[ gl.TEXTURE_2D ] = createTexture( gl.TEXTURE_2D, gl.TEXTURE_2D, 1 );
  222. emptyTextures[ gl.TEXTURE_CUBE_MAP ] = createTexture( gl.TEXTURE_CUBE_MAP, gl.TEXTURE_CUBE_MAP_POSITIVE_X, 6 );
  223. //
  224. function init() {
  225. colorBuffer.setClear( 0, 0, 0, 1 );
  226. depthBuffer.setClear( 1 );
  227. stencilBuffer.setClear( 0 );
  228. enable( gl.DEPTH_TEST );
  229. setDepthFunc( LessEqualDepth );
  230. setFlipSided( false );
  231. setCullFace( CullFaceBack );
  232. enable( gl.CULL_FACE );
  233. enable( gl.BLEND );
  234. setBlending( NormalBlending );
  235. }
  236. function initAttributes() {
  237. for ( var i = 0, l = newAttributes.length; i < l; i ++ ) {
  238. newAttributes[ i ] = 0;
  239. }
  240. }
  241. function enableAttribute( attribute ) {
  242. newAttributes[ attribute ] = 1;
  243. if ( enabledAttributes[ attribute ] === 0 ) {
  244. gl.enableVertexAttribArray( attribute );
  245. enabledAttributes[ attribute ] = 1;
  246. }
  247. if ( attributeDivisors[ attribute ] !== 0 ) {
  248. var extension = extensions.get( 'ANGLE_instanced_arrays' );
  249. extension.vertexAttribDivisorANGLE( attribute, 0 );
  250. attributeDivisors[ attribute ] = 0;
  251. }
  252. }
  253. function enableAttributeAndDivisor( attribute, meshPerAttribute, extension ) {
  254. newAttributes[ attribute ] = 1;
  255. if ( enabledAttributes[ attribute ] === 0 ) {
  256. gl.enableVertexAttribArray( attribute );
  257. enabledAttributes[ attribute ] = 1;
  258. }
  259. if ( attributeDivisors[ attribute ] !== meshPerAttribute ) {
  260. extension.vertexAttribDivisorANGLE( attribute, meshPerAttribute );
  261. attributeDivisors[ attribute ] = meshPerAttribute;
  262. }
  263. }
  264. function disableUnusedAttributes() {
  265. for ( var i = 0, l = enabledAttributes.length; i !== l; ++ i ) {
  266. if ( enabledAttributes[ i ] !== newAttributes[ i ] ) {
  267. gl.disableVertexAttribArray( i );
  268. enabledAttributes[ i ] = 0;
  269. }
  270. }
  271. }
  272. function enable( id ) {
  273. if ( capabilities[ id ] !== true ) {
  274. gl.enable( id );
  275. capabilities[ id ] = true;
  276. }
  277. }
  278. function disable( id ) {
  279. if ( capabilities[ id ] !== false ) {
  280. gl.disable( id );
  281. capabilities[ id ] = false;
  282. }
  283. }
  284. function getCompressedTextureFormats() {
  285. if ( compressedTextureFormats === null ) {
  286. compressedTextureFormats = [];
  287. if ( extensions.get( 'WEBGL_compressed_texture_pvrtc' ) ||
  288. extensions.get( 'WEBGL_compressed_texture_s3tc' ) ||
  289. extensions.get( 'WEBGL_compressed_texture_etc1' ) ) {
  290. var formats = gl.getParameter( gl.COMPRESSED_TEXTURE_FORMATS );
  291. for ( var i = 0; i < formats.length; i ++ ) {
  292. compressedTextureFormats.push( formats[ i ] );
  293. }
  294. }
  295. }
  296. return compressedTextureFormats;
  297. }
  298. function setBlending( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, premultipliedAlpha ) {
  299. if ( blending !== NoBlending ) {
  300. enable( gl.BLEND );
  301. } else {
  302. disable( gl.BLEND );
  303. }
  304. if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) {
  305. if ( blending === AdditiveBlending ) {
  306. if ( premultipliedAlpha ) {
  307. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  308. gl.blendFuncSeparate( gl.ONE, gl.ONE, gl.ONE, gl.ONE );
  309. } else {
  310. gl.blendEquation( gl.FUNC_ADD );
  311. gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
  312. }
  313. } else if ( blending === SubtractiveBlending ) {
  314. if ( premultipliedAlpha ) {
  315. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  316. gl.blendFuncSeparate( gl.ZERO, gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ONE_MINUS_SRC_ALPHA );
  317. } else {
  318. gl.blendEquation( gl.FUNC_ADD );
  319. gl.blendFunc( gl.ZERO, gl.ONE_MINUS_SRC_COLOR );
  320. }
  321. } else if ( blending === MultiplyBlending ) {
  322. if ( premultipliedAlpha ) {
  323. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  324. gl.blendFuncSeparate( gl.ZERO, gl.SRC_COLOR, gl.ZERO, gl.SRC_ALPHA );
  325. } else {
  326. gl.blendEquation( gl.FUNC_ADD );
  327. gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
  328. }
  329. } else {
  330. if ( premultipliedAlpha ) {
  331. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  332. gl.blendFuncSeparate( gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  333. } else {
  334. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  335. gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  336. }
  337. }
  338. currentBlending = blending;
  339. currentPremultipledAlpha = premultipliedAlpha;
  340. }
  341. if ( blending === CustomBlending ) {
  342. blendEquationAlpha = blendEquationAlpha || blendEquation;
  343. blendSrcAlpha = blendSrcAlpha || blendSrc;
  344. blendDstAlpha = blendDstAlpha || blendDst;
  345. if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
  346. gl.blendEquationSeparate( paramThreeToGL( blendEquation ), paramThreeToGL( blendEquationAlpha ) );
  347. currentBlendEquation = blendEquation;
  348. currentBlendEquationAlpha = blendEquationAlpha;
  349. }
  350. if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
  351. gl.blendFuncSeparate( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ), paramThreeToGL( blendSrcAlpha ), paramThreeToGL( blendDstAlpha ) );
  352. currentBlendSrc = blendSrc;
  353. currentBlendDst = blendDst;
  354. currentBlendSrcAlpha = blendSrcAlpha;
  355. currentBlendDstAlpha = blendDstAlpha;
  356. }
  357. } else {
  358. currentBlendEquation = null;
  359. currentBlendSrc = null;
  360. currentBlendDst = null;
  361. currentBlendEquationAlpha = null;
  362. currentBlendSrcAlpha = null;
  363. currentBlendDstAlpha = null;
  364. }
  365. }
  366. function setMaterial( material ) {
  367. material.side === DoubleSide
  368. ? disable( gl.CULL_FACE )
  369. : enable( gl.CULL_FACE );
  370. setFlipSided( material.side === BackSide );
  371. material.transparent === true
  372. ? setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha, material.premultipliedAlpha )
  373. : setBlending( NoBlending );
  374. setDepthFunc( material.depthFunc );
  375. setDepthTest( material.depthTest );
  376. setDepthWrite( material.depthWrite );
  377. setColorWrite( material.colorWrite );
  378. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  379. }
  380. // TODO Deprecate
  381. function setColorWrite( colorWrite ) {
  382. colorBuffer.setMask( colorWrite );
  383. }
  384. function setDepthTest( depthTest ) {
  385. depthBuffer.setTest( depthTest );
  386. }
  387. function setDepthWrite( depthWrite ) {
  388. depthBuffer.setMask( depthWrite );
  389. }
  390. function setDepthFunc( depthFunc ) {
  391. depthBuffer.setFunc( depthFunc );
  392. }
  393. function setStencilTest( stencilTest ) {
  394. stencilBuffer.setTest( stencilTest );
  395. }
  396. function setStencilWrite( stencilWrite ) {
  397. stencilBuffer.setMask( stencilWrite );
  398. }
  399. function setStencilFunc( stencilFunc, stencilRef, stencilMask ) {
  400. stencilBuffer.setFunc( stencilFunc, stencilRef, stencilMask );
  401. }
  402. function setStencilOp( stencilFail, stencilZFail, stencilZPass ) {
  403. stencilBuffer.setOp( stencilFail, stencilZFail, stencilZPass );
  404. }
  405. //
  406. function setFlipSided( flipSided ) {
  407. if ( currentFlipSided !== flipSided ) {
  408. if ( flipSided ) {
  409. gl.frontFace( gl.CW );
  410. } else {
  411. gl.frontFace( gl.CCW );
  412. }
  413. currentFlipSided = flipSided;
  414. }
  415. }
  416. function setCullFace( cullFace ) {
  417. if ( cullFace !== CullFaceNone ) {
  418. enable( gl.CULL_FACE );
  419. if ( cullFace !== currentCullFace ) {
  420. if ( cullFace === CullFaceBack ) {
  421. gl.cullFace( gl.BACK );
  422. } else if ( cullFace === CullFaceFront ) {
  423. gl.cullFace( gl.FRONT );
  424. } else {
  425. gl.cullFace( gl.FRONT_AND_BACK );
  426. }
  427. }
  428. } else {
  429. disable( gl.CULL_FACE );
  430. }
  431. currentCullFace = cullFace;
  432. }
  433. function setLineWidth( width ) {
  434. if ( width !== currentLineWidth ) {
  435. if ( lineWidthAvailable ) gl.lineWidth( width );
  436. currentLineWidth = width;
  437. }
  438. }
  439. function setPolygonOffset( polygonOffset, factor, units ) {
  440. if ( polygonOffset ) {
  441. enable( gl.POLYGON_OFFSET_FILL );
  442. if ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) {
  443. gl.polygonOffset( factor, units );
  444. currentPolygonOffsetFactor = factor;
  445. currentPolygonOffsetUnits = units;
  446. }
  447. } else {
  448. disable( gl.POLYGON_OFFSET_FILL );
  449. }
  450. }
  451. function getScissorTest() {
  452. return currentScissorTest;
  453. }
  454. function setScissorTest( scissorTest ) {
  455. currentScissorTest = scissorTest;
  456. if ( scissorTest ) {
  457. enable( gl.SCISSOR_TEST );
  458. } else {
  459. disable( gl.SCISSOR_TEST );
  460. }
  461. }
  462. // texture
  463. function activeTexture( webglSlot ) {
  464. if ( webglSlot === undefined ) webglSlot = gl.TEXTURE0 + maxTextures - 1;
  465. if ( currentTextureSlot !== webglSlot ) {
  466. gl.activeTexture( webglSlot );
  467. currentTextureSlot = webglSlot;
  468. }
  469. }
  470. function bindTexture( webglType, webglTexture ) {
  471. if ( currentTextureSlot === null ) {
  472. activeTexture();
  473. }
  474. var boundTexture = currentBoundTextures[ currentTextureSlot ];
  475. if ( boundTexture === undefined ) {
  476. boundTexture = { type: undefined, texture: undefined };
  477. currentBoundTextures[ currentTextureSlot ] = boundTexture;
  478. }
  479. if ( boundTexture.type !== webglType || boundTexture.texture !== webglTexture ) {
  480. gl.bindTexture( webglType, webglTexture || emptyTextures[ webglType ] );
  481. boundTexture.type = webglType;
  482. boundTexture.texture = webglTexture;
  483. }
  484. }
  485. function compressedTexImage2D() {
  486. try {
  487. gl.compressedTexImage2D.apply( gl, arguments );
  488. } catch ( error ) {
  489. console.error( error );
  490. }
  491. }
  492. function texImage2D() {
  493. try {
  494. gl.texImage2D.apply( gl, arguments );
  495. } catch ( error ) {
  496. console.error( error );
  497. }
  498. }
  499. //
  500. function scissor( scissor ) {
  501. if ( currentScissor.equals( scissor ) === false ) {
  502. gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w );
  503. currentScissor.copy( scissor );
  504. }
  505. }
  506. function viewport( viewport ) {
  507. if ( currentViewport.equals( viewport ) === false ) {
  508. gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w );
  509. currentViewport.copy( viewport );
  510. }
  511. }
  512. //
  513. function reset() {
  514. for ( var i = 0; i < enabledAttributes.length; i ++ ) {
  515. if ( enabledAttributes[ i ] === 1 ) {
  516. gl.disableVertexAttribArray( i );
  517. enabledAttributes[ i ] = 0;
  518. }
  519. }
  520. capabilities = {};
  521. compressedTextureFormats = null;
  522. currentTextureSlot = null;
  523. currentBoundTextures = {};
  524. currentBlending = null;
  525. currentFlipSided = null;
  526. currentCullFace = null;
  527. colorBuffer.reset();
  528. depthBuffer.reset();
  529. stencilBuffer.reset();
  530. }
  531. return {
  532. buffers: {
  533. color: colorBuffer,
  534. depth: depthBuffer,
  535. stencil: stencilBuffer
  536. },
  537. init: init,
  538. initAttributes: initAttributes,
  539. enableAttribute: enableAttribute,
  540. enableAttributeAndDivisor: enableAttributeAndDivisor,
  541. disableUnusedAttributes: disableUnusedAttributes,
  542. enable: enable,
  543. disable: disable,
  544. getCompressedTextureFormats: getCompressedTextureFormats,
  545. setBlending: setBlending,
  546. setMaterial: setMaterial,
  547. setColorWrite: setColorWrite,
  548. setDepthTest: setDepthTest,
  549. setDepthWrite: setDepthWrite,
  550. setDepthFunc: setDepthFunc,
  551. setStencilTest: setStencilTest,
  552. setStencilWrite: setStencilWrite,
  553. setStencilFunc: setStencilFunc,
  554. setStencilOp: setStencilOp,
  555. setFlipSided: setFlipSided,
  556. setCullFace: setCullFace,
  557. setLineWidth: setLineWidth,
  558. setPolygonOffset: setPolygonOffset,
  559. getScissorTest: getScissorTest,
  560. setScissorTest: setScissorTest,
  561. activeTexture: activeTexture,
  562. bindTexture: bindTexture,
  563. compressedTexImage2D: compressedTexImage2D,
  564. texImage2D: texImage2D,
  565. scissor: scissor,
  566. viewport: viewport,
  567. reset: reset
  568. };
  569. }
  570. export { WebGLState };
粤ICP备19079148号