WebGLState.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  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 currentBoundFramebuffers = {};
  184. let currentProgram = null;
  185. let currentBlendingEnabled = false;
  186. let currentBlending = null;
  187. let currentBlendEquation = null;
  188. let currentBlendSrc = null;
  189. let currentBlendDst = null;
  190. let currentBlendEquationAlpha = null;
  191. let currentBlendSrcAlpha = null;
  192. let currentBlendDstAlpha = null;
  193. let currentPremultipledAlpha = false;
  194. let currentFlipSided = null;
  195. let currentCullFace = null;
  196. let currentLineWidth = null;
  197. let currentPolygonOffsetFactor = null;
  198. let currentPolygonOffsetUnits = null;
  199. const maxTextures = gl.getParameter( gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS );
  200. let lineWidthAvailable = false;
  201. let version = 0;
  202. const glVersion = gl.getParameter( gl.VERSION );
  203. if ( glVersion.indexOf( 'WebGL' ) !== - 1 ) {
  204. version = parseFloat( /^WebGL (\d)/.exec( glVersion )[ 1 ] );
  205. lineWidthAvailable = ( version >= 1.0 );
  206. } else if ( glVersion.indexOf( 'OpenGL ES' ) !== - 1 ) {
  207. version = parseFloat( /^OpenGL ES (\d)/.exec( glVersion )[ 1 ] );
  208. lineWidthAvailable = ( version >= 2.0 );
  209. }
  210. let currentTextureSlot = null;
  211. let currentBoundTextures = {};
  212. const scissorParam = gl.getParameter( gl.SCISSOR_BOX );
  213. const viewportParam = gl.getParameter( gl.VIEWPORT );
  214. const currentScissor = new Vector4().fromArray( scissorParam );
  215. const currentViewport = new Vector4().fromArray( viewportParam );
  216. function createTexture( type, target, count ) {
  217. const data = new Uint8Array( 4 ); // 4 is required to match default unpack alignment of 4.
  218. const texture = gl.createTexture();
  219. gl.bindTexture( type, texture );
  220. gl.texParameteri( type, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  221. gl.texParameteri( type, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  222. for ( let i = 0; i < count; i ++ ) {
  223. gl.texImage2D( target + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  224. }
  225. return texture;
  226. }
  227. const emptyTextures = {};
  228. emptyTextures[ gl.TEXTURE_2D ] = createTexture( gl.TEXTURE_2D, gl.TEXTURE_2D, 1 );
  229. emptyTextures[ gl.TEXTURE_CUBE_MAP ] = createTexture( gl.TEXTURE_CUBE_MAP, gl.TEXTURE_CUBE_MAP_POSITIVE_X, 6 );
  230. // init
  231. colorBuffer.setClear( 0, 0, 0, 1 );
  232. depthBuffer.setClear( 1 );
  233. stencilBuffer.setClear( 0 );
  234. enable( gl.DEPTH_TEST );
  235. depthBuffer.setFunc( LessEqualDepth );
  236. setFlipSided( false );
  237. setCullFace( CullFaceBack );
  238. enable( gl.CULL_FACE );
  239. setBlending( NoBlending );
  240. //
  241. function enable( id ) {
  242. if ( enabledCapabilities[ id ] !== true ) {
  243. gl.enable( id );
  244. enabledCapabilities[ id ] = true;
  245. }
  246. }
  247. function disable( id ) {
  248. if ( enabledCapabilities[ id ] !== false ) {
  249. gl.disable( id );
  250. enabledCapabilities[ id ] = false;
  251. }
  252. }
  253. function bindFramebuffer( target, framebuffer ) {
  254. if ( currentBoundFramebuffers[ target ] !== framebuffer ) {
  255. gl.bindFramebuffer( target, framebuffer );
  256. currentBoundFramebuffers[ target ] = framebuffer;
  257. if ( isWebGL2 ) {
  258. // gl.DRAW_FRAMEBUFFER is equivalent to gl.FRAMEBUFFER
  259. if ( target === gl.DRAW_FRAMEBUFFER ) {
  260. currentBoundFramebuffers[ gl.FRAMEBUFFER ] = framebuffer;
  261. }
  262. if ( target === gl.FRAMEBUFFER ) {
  263. currentBoundFramebuffers[ gl.DRAW_FRAMEBUFFER ] = framebuffer;
  264. }
  265. }
  266. return true;
  267. }
  268. return false;
  269. }
  270. function useProgram( program ) {
  271. if ( currentProgram !== program ) {
  272. gl.useProgram( program );
  273. currentProgram = program;
  274. return true;
  275. }
  276. return false;
  277. }
  278. const equationToGL = {
  279. [ AddEquation ]: gl.FUNC_ADD,
  280. [ SubtractEquation ]: gl.FUNC_SUBTRACT,
  281. [ ReverseSubtractEquation ]: gl.FUNC_REVERSE_SUBTRACT
  282. };
  283. if ( isWebGL2 ) {
  284. equationToGL[ MinEquation ] = gl.MIN;
  285. equationToGL[ MaxEquation ] = gl.MAX;
  286. } else {
  287. const extension = extensions.get( 'EXT_blend_minmax' );
  288. if ( extension !== null ) {
  289. equationToGL[ MinEquation ] = extension.MIN_EXT;
  290. equationToGL[ MaxEquation ] = extension.MAX_EXT;
  291. }
  292. }
  293. const factorToGL = {
  294. [ ZeroFactor ]: gl.ZERO,
  295. [ OneFactor ]: gl.ONE,
  296. [ SrcColorFactor ]: gl.SRC_COLOR,
  297. [ SrcAlphaFactor ]: gl.SRC_ALPHA,
  298. [ SrcAlphaSaturateFactor ]: gl.SRC_ALPHA_SATURATE,
  299. [ DstColorFactor ]: gl.DST_COLOR,
  300. [ DstAlphaFactor ]: gl.DST_ALPHA,
  301. [ OneMinusSrcColorFactor ]: gl.ONE_MINUS_SRC_COLOR,
  302. [ OneMinusSrcAlphaFactor ]: gl.ONE_MINUS_SRC_ALPHA,
  303. [ OneMinusDstColorFactor ]: gl.ONE_MINUS_DST_COLOR,
  304. [ OneMinusDstAlphaFactor ]: gl.ONE_MINUS_DST_ALPHA
  305. };
  306. function setBlending( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, premultipliedAlpha ) {
  307. if ( blending === NoBlending ) {
  308. if ( currentBlendingEnabled === true ) {
  309. disable( gl.BLEND );
  310. currentBlendingEnabled = false;
  311. }
  312. return;
  313. }
  314. if ( currentBlendingEnabled === false ) {
  315. enable( gl.BLEND );
  316. currentBlendingEnabled = true;
  317. }
  318. if ( blending !== CustomBlending ) {
  319. if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) {
  320. if ( currentBlendEquation !== AddEquation || currentBlendEquationAlpha !== AddEquation ) {
  321. gl.blendEquation( gl.FUNC_ADD );
  322. currentBlendEquation = AddEquation;
  323. currentBlendEquationAlpha = AddEquation;
  324. }
  325. if ( premultipliedAlpha ) {
  326. switch ( blending ) {
  327. case NormalBlending:
  328. gl.blendFuncSeparate( gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  329. break;
  330. case AdditiveBlending:
  331. gl.blendFunc( gl.ONE, gl.ONE );
  332. break;
  333. case SubtractiveBlending:
  334. gl.blendFuncSeparate( gl.ZERO, gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ONE_MINUS_SRC_ALPHA );
  335. break;
  336. case MultiplyBlending:
  337. gl.blendFuncSeparate( gl.ZERO, gl.SRC_COLOR, gl.ZERO, gl.SRC_ALPHA );
  338. break;
  339. default:
  340. console.error( 'THREE.WebGLState: Invalid blending: ', blending );
  341. break;
  342. }
  343. } else {
  344. switch ( blending ) {
  345. case NormalBlending:
  346. gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  347. break;
  348. case AdditiveBlending:
  349. gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
  350. break;
  351. case SubtractiveBlending:
  352. gl.blendFunc( gl.ZERO, gl.ONE_MINUS_SRC_COLOR );
  353. break;
  354. case MultiplyBlending:
  355. gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
  356. break;
  357. default:
  358. console.error( 'THREE.WebGLState: Invalid blending: ', blending );
  359. break;
  360. }
  361. }
  362. currentBlendSrc = null;
  363. currentBlendDst = null;
  364. currentBlendSrcAlpha = null;
  365. currentBlendDstAlpha = null;
  366. currentBlending = blending;
  367. currentPremultipledAlpha = premultipliedAlpha;
  368. }
  369. return;
  370. }
  371. // custom blending
  372. blendEquationAlpha = blendEquationAlpha || blendEquation;
  373. blendSrcAlpha = blendSrcAlpha || blendSrc;
  374. blendDstAlpha = blendDstAlpha || blendDst;
  375. if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
  376. gl.blendEquationSeparate( equationToGL[ blendEquation ], equationToGL[ blendEquationAlpha ] );
  377. currentBlendEquation = blendEquation;
  378. currentBlendEquationAlpha = blendEquationAlpha;
  379. }
  380. if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
  381. gl.blendFuncSeparate( factorToGL[ blendSrc ], factorToGL[ blendDst ], factorToGL[ blendSrcAlpha ], factorToGL[ blendDstAlpha ] );
  382. currentBlendSrc = blendSrc;
  383. currentBlendDst = blendDst;
  384. currentBlendSrcAlpha = blendSrcAlpha;
  385. currentBlendDstAlpha = blendDstAlpha;
  386. }
  387. currentBlending = blending;
  388. currentPremultipledAlpha = null;
  389. }
  390. function setMaterial( material, frontFaceCW ) {
  391. material.side === DoubleSide
  392. ? disable( gl.CULL_FACE )
  393. : enable( gl.CULL_FACE );
  394. let flipSided = ( material.side === BackSide );
  395. if ( frontFaceCW ) flipSided = ! flipSided;
  396. setFlipSided( flipSided );
  397. ( material.blending === NormalBlending && material.transparent === false )
  398. ? setBlending( NoBlending )
  399. : setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha, material.premultipliedAlpha );
  400. depthBuffer.setFunc( material.depthFunc );
  401. depthBuffer.setTest( material.depthTest );
  402. depthBuffer.setMask( material.depthWrite );
  403. colorBuffer.setMask( material.colorWrite );
  404. const stencilWrite = material.stencilWrite;
  405. stencilBuffer.setTest( stencilWrite );
  406. if ( stencilWrite ) {
  407. stencilBuffer.setMask( material.stencilWriteMask );
  408. stencilBuffer.setFunc( material.stencilFunc, material.stencilRef, material.stencilFuncMask );
  409. stencilBuffer.setOp( material.stencilFail, material.stencilZFail, material.stencilZPass );
  410. }
  411. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  412. material.alphaToCoverage === true
  413. ? enable( gl.SAMPLE_ALPHA_TO_COVERAGE )
  414. : disable( gl.SAMPLE_ALPHA_TO_COVERAGE );
  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. let 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 unbindTexture() {
  493. const boundTexture = currentBoundTextures[ currentTextureSlot ];
  494. if ( boundTexture !== undefined && boundTexture.type !== undefined ) {
  495. gl.bindTexture( boundTexture.type, null );
  496. boundTexture.type = undefined;
  497. boundTexture.texture = undefined;
  498. }
  499. }
  500. function compressedTexImage2D() {
  501. try {
  502. gl.compressedTexImage2D.apply( gl, arguments );
  503. } catch ( error ) {
  504. console.error( 'THREE.WebGLState:', error );
  505. }
  506. }
  507. function texSubImage2D() {
  508. try {
  509. gl.texSubImage2D.apply( gl, arguments );
  510. } catch ( error ) {
  511. console.error( 'THREE.WebGLState:', error );
  512. }
  513. }
  514. function texStorage2D() {
  515. try {
  516. gl.texStorage2D.apply( gl, arguments );
  517. } catch ( error ) {
  518. console.error( 'THREE.WebGLState:', error );
  519. }
  520. }
  521. function texImage2D() {
  522. try {
  523. gl.texImage2D.apply( gl, arguments );
  524. } catch ( error ) {
  525. console.error( 'THREE.WebGLState:', error );
  526. }
  527. }
  528. function texImage3D() {
  529. try {
  530. gl.texImage3D.apply( gl, arguments );
  531. } catch ( error ) {
  532. console.error( 'THREE.WebGLState:', error );
  533. }
  534. }
  535. //
  536. function scissor( scissor ) {
  537. if ( currentScissor.equals( scissor ) === false ) {
  538. gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w );
  539. currentScissor.copy( scissor );
  540. }
  541. }
  542. function viewport( viewport ) {
  543. if ( currentViewport.equals( viewport ) === false ) {
  544. gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w );
  545. currentViewport.copy( viewport );
  546. }
  547. }
  548. //
  549. function reset() {
  550. // reset state
  551. gl.disable( gl.BLEND );
  552. gl.disable( gl.CULL_FACE );
  553. gl.disable( gl.DEPTH_TEST );
  554. gl.disable( gl.POLYGON_OFFSET_FILL );
  555. gl.disable( gl.SCISSOR_TEST );
  556. gl.disable( gl.STENCIL_TEST );
  557. gl.disable( gl.SAMPLE_ALPHA_TO_COVERAGE );
  558. gl.blendEquation( gl.FUNC_ADD );
  559. gl.blendFunc( gl.ONE, gl.ZERO );
  560. gl.blendFuncSeparate( gl.ONE, gl.ZERO, gl.ONE, gl.ZERO );
  561. gl.colorMask( true, true, true, true );
  562. gl.clearColor( 0, 0, 0, 0 );
  563. gl.depthMask( true );
  564. gl.depthFunc( gl.LESS );
  565. gl.clearDepth( 1 );
  566. gl.stencilMask( 0xffffffff );
  567. gl.stencilFunc( gl.ALWAYS, 0, 0xffffffff );
  568. gl.stencilOp( gl.KEEP, gl.KEEP, gl.KEEP );
  569. gl.clearStencil( 0 );
  570. gl.cullFace( gl.BACK );
  571. gl.frontFace( gl.CCW );
  572. gl.polygonOffset( 0, 0 );
  573. gl.activeTexture( gl.TEXTURE0 );
  574. gl.bindFramebuffer( gl.FRAMEBUFFER, null );
  575. if ( isWebGL2 === true ) {
  576. gl.bindFramebuffer( gl.DRAW_FRAMEBUFFER, null );
  577. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, null );
  578. }
  579. gl.useProgram( null );
  580. gl.lineWidth( 1 );
  581. gl.scissor( 0, 0, gl.canvas.width, gl.canvas.height );
  582. gl.viewport( 0, 0, gl.canvas.width, gl.canvas.height );
  583. // reset internals
  584. enabledCapabilities = {};
  585. currentTextureSlot = null;
  586. currentBoundTextures = {};
  587. currentBoundFramebuffers = {};
  588. currentProgram = null;
  589. currentBlendingEnabled = false;
  590. currentBlending = null;
  591. currentBlendEquation = null;
  592. currentBlendSrc = null;
  593. currentBlendDst = null;
  594. currentBlendEquationAlpha = null;
  595. currentBlendSrcAlpha = null;
  596. currentBlendDstAlpha = null;
  597. currentPremultipledAlpha = false;
  598. currentFlipSided = null;
  599. currentCullFace = null;
  600. currentLineWidth = null;
  601. currentPolygonOffsetFactor = null;
  602. currentPolygonOffsetUnits = null;
  603. currentScissor.set( 0, 0, gl.canvas.width, gl.canvas.height );
  604. currentViewport.set( 0, 0, gl.canvas.width, gl.canvas.height );
  605. colorBuffer.reset();
  606. depthBuffer.reset();
  607. stencilBuffer.reset();
  608. }
  609. return {
  610. buffers: {
  611. color: colorBuffer,
  612. depth: depthBuffer,
  613. stencil: stencilBuffer
  614. },
  615. enable: enable,
  616. disable: disable,
  617. bindFramebuffer: bindFramebuffer,
  618. useProgram: useProgram,
  619. setBlending: setBlending,
  620. setMaterial: setMaterial,
  621. setFlipSided: setFlipSided,
  622. setCullFace: setCullFace,
  623. setLineWidth: setLineWidth,
  624. setPolygonOffset: setPolygonOffset,
  625. setScissorTest: setScissorTest,
  626. activeTexture: activeTexture,
  627. bindTexture: bindTexture,
  628. unbindTexture: unbindTexture,
  629. compressedTexImage2D: compressedTexImage2D,
  630. texImage2D: texImage2D,
  631. texImage3D: texImage3D,
  632. texStorage2D: texStorage2D,
  633. texSubImage2D: texSubImage2D,
  634. scissor: scissor,
  635. viewport: viewport,
  636. reset: reset
  637. };
  638. }
  639. export { WebGLState };