WebGLState.js 19 KB

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