WebGLState.js 21 KB

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