WebGLState.js 18 KB

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