WebGLState.js 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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, ConstantColorFactor, OneMinusConstantColorFactor, ConstantAlphaFactor, OneMinusConstantAlphaFactor } from '../../constants.js';
  2. import { Color } from '../../math/Color.js';
  3. import { Vector4 } from '../../math/Vector4.js';
  4. function WebGLState( gl ) {
  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. switch ( depthFunc ) {
  59. case NeverDepth:
  60. gl.depthFunc( gl.NEVER );
  61. break;
  62. case AlwaysDepth:
  63. gl.depthFunc( gl.ALWAYS );
  64. break;
  65. case LessDepth:
  66. gl.depthFunc( gl.LESS );
  67. break;
  68. case LessEqualDepth:
  69. gl.depthFunc( gl.LEQUAL );
  70. break;
  71. case EqualDepth:
  72. gl.depthFunc( gl.EQUAL );
  73. break;
  74. case GreaterEqualDepth:
  75. gl.depthFunc( gl.GEQUAL );
  76. break;
  77. case GreaterDepth:
  78. gl.depthFunc( gl.GREATER );
  79. break;
  80. case NotEqualDepth:
  81. gl.depthFunc( gl.NOTEQUAL );
  82. break;
  83. default:
  84. gl.depthFunc( gl.LEQUAL );
  85. }
  86. currentDepthFunc = depthFunc;
  87. }
  88. },
  89. setLocked: function ( lock ) {
  90. locked = lock;
  91. },
  92. setClear: function ( depth ) {
  93. if ( currentDepthClear !== depth ) {
  94. gl.clearDepth( depth );
  95. currentDepthClear = depth;
  96. }
  97. },
  98. reset: function () {
  99. locked = false;
  100. currentDepthMask = null;
  101. currentDepthFunc = null;
  102. currentDepthClear = null;
  103. }
  104. };
  105. }
  106. function StencilBuffer() {
  107. let locked = false;
  108. let currentStencilMask = null;
  109. let currentStencilFunc = null;
  110. let currentStencilRef = null;
  111. let currentStencilFuncMask = null;
  112. let currentStencilFail = null;
  113. let currentStencilZFail = null;
  114. let currentStencilZPass = null;
  115. let currentStencilClear = null;
  116. return {
  117. setTest: function ( stencilTest ) {
  118. if ( ! locked ) {
  119. if ( stencilTest ) {
  120. enable( gl.STENCIL_TEST );
  121. } else {
  122. disable( gl.STENCIL_TEST );
  123. }
  124. }
  125. },
  126. setMask: function ( stencilMask ) {
  127. if ( currentStencilMask !== stencilMask && ! locked ) {
  128. gl.stencilMask( stencilMask );
  129. currentStencilMask = stencilMask;
  130. }
  131. },
  132. setFunc: function ( stencilFunc, stencilRef, stencilMask ) {
  133. if ( currentStencilFunc !== stencilFunc ||
  134. currentStencilRef !== stencilRef ||
  135. currentStencilFuncMask !== stencilMask ) {
  136. gl.stencilFunc( stencilFunc, stencilRef, stencilMask );
  137. currentStencilFunc = stencilFunc;
  138. currentStencilRef = stencilRef;
  139. currentStencilFuncMask = stencilMask;
  140. }
  141. },
  142. setOp: function ( stencilFail, stencilZFail, stencilZPass ) {
  143. if ( currentStencilFail !== stencilFail ||
  144. currentStencilZFail !== stencilZFail ||
  145. currentStencilZPass !== stencilZPass ) {
  146. gl.stencilOp( stencilFail, stencilZFail, stencilZPass );
  147. currentStencilFail = stencilFail;
  148. currentStencilZFail = stencilZFail;
  149. currentStencilZPass = stencilZPass;
  150. }
  151. },
  152. setLocked: function ( lock ) {
  153. locked = lock;
  154. },
  155. setClear: function ( stencil ) {
  156. if ( currentStencilClear !== stencil ) {
  157. gl.clearStencil( stencil );
  158. currentStencilClear = stencil;
  159. }
  160. },
  161. reset: function () {
  162. locked = false;
  163. currentStencilMask = null;
  164. currentStencilFunc = null;
  165. currentStencilRef = null;
  166. currentStencilFuncMask = null;
  167. currentStencilFail = null;
  168. currentStencilZFail = null;
  169. currentStencilZPass = null;
  170. currentStencilClear = null;
  171. }
  172. };
  173. }
  174. //
  175. const colorBuffer = new ColorBuffer();
  176. const depthBuffer = new DepthBuffer();
  177. const stencilBuffer = new StencilBuffer();
  178. const uboBindings = new WeakMap();
  179. const uboProgramMap = new WeakMap();
  180. let enabledCapabilities = {};
  181. let currentBoundFramebuffers = {};
  182. let currentDrawbuffers = new WeakMap();
  183. let defaultDrawbuffers = [];
  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 currentBlendColor = new Color( 0, 0, 0 );
  194. let currentBlendAlpha = 0;
  195. let currentPremultipledAlpha = false;
  196. let currentFlipSided = null;
  197. let currentCullFace = null;
  198. let currentLineWidth = null;
  199. let currentPolygonOffsetFactor = null;
  200. let currentPolygonOffsetUnits = null;
  201. const maxTextures = gl.getParameter( gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS );
  202. let lineWidthAvailable = false;
  203. let version = 0;
  204. const glVersion = gl.getParameter( gl.VERSION );
  205. if ( glVersion.indexOf( 'WebGL' ) !== - 1 ) {
  206. version = parseFloat( /^WebGL (\d)/.exec( glVersion )[ 1 ] );
  207. lineWidthAvailable = ( version >= 1.0 );
  208. } else if ( glVersion.indexOf( 'OpenGL ES' ) !== - 1 ) {
  209. version = parseFloat( /^OpenGL ES (\d)/.exec( glVersion )[ 1 ] );
  210. lineWidthAvailable = ( version >= 2.0 );
  211. }
  212. let currentTextureSlot = null;
  213. let currentBoundTextures = {};
  214. const scissorParam = gl.getParameter( gl.SCISSOR_BOX );
  215. const viewportParam = gl.getParameter( gl.VIEWPORT );
  216. const currentScissor = new Vector4().fromArray( scissorParam );
  217. const currentViewport = new Vector4().fromArray( viewportParam );
  218. function createTexture( type, target, count, dimensions ) {
  219. const data = new Uint8Array( 4 ); // 4 is required to match default unpack alignment of 4.
  220. const texture = gl.createTexture();
  221. gl.bindTexture( type, texture );
  222. gl.texParameteri( type, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  223. gl.texParameteri( type, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  224. for ( let i = 0; i < count; i ++ ) {
  225. if ( type === gl.TEXTURE_3D || type === gl.TEXTURE_2D_ARRAY ) {
  226. gl.texImage3D( target, 0, gl.RGBA, 1, 1, dimensions, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  227. } else {
  228. gl.texImage2D( target + i, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, data );
  229. }
  230. }
  231. return texture;
  232. }
  233. const emptyTextures = {};
  234. emptyTextures[ gl.TEXTURE_2D ] = createTexture( gl.TEXTURE_2D, gl.TEXTURE_2D, 1 );
  235. emptyTextures[ gl.TEXTURE_CUBE_MAP ] = createTexture( gl.TEXTURE_CUBE_MAP, gl.TEXTURE_CUBE_MAP_POSITIVE_X, 6 );
  236. emptyTextures[ gl.TEXTURE_2D_ARRAY ] = createTexture( gl.TEXTURE_2D_ARRAY, gl.TEXTURE_2D_ARRAY, 1, 1 );
  237. emptyTextures[ gl.TEXTURE_3D ] = createTexture( gl.TEXTURE_3D, gl.TEXTURE_3D, 1, 1 );
  238. // init
  239. colorBuffer.setClear( 0, 0, 0, 1 );
  240. depthBuffer.setClear( 1 );
  241. stencilBuffer.setClear( 0 );
  242. enable( gl.DEPTH_TEST );
  243. depthBuffer.setFunc( LessEqualDepth );
  244. setFlipSided( false );
  245. setCullFace( CullFaceBack );
  246. enable( gl.CULL_FACE );
  247. setBlending( NoBlending );
  248. //
  249. function enable( id ) {
  250. if ( enabledCapabilities[ id ] !== true ) {
  251. gl.enable( id );
  252. enabledCapabilities[ id ] = true;
  253. }
  254. }
  255. function disable( id ) {
  256. if ( enabledCapabilities[ id ] !== false ) {
  257. gl.disable( id );
  258. enabledCapabilities[ id ] = false;
  259. }
  260. }
  261. function bindFramebuffer( target, framebuffer ) {
  262. if ( currentBoundFramebuffers[ target ] !== framebuffer ) {
  263. gl.bindFramebuffer( target, framebuffer );
  264. currentBoundFramebuffers[ target ] = framebuffer;
  265. // gl.DRAW_FRAMEBUFFER is equivalent to gl.FRAMEBUFFER
  266. if ( target === gl.DRAW_FRAMEBUFFER ) {
  267. currentBoundFramebuffers[ gl.FRAMEBUFFER ] = framebuffer;
  268. }
  269. if ( target === gl.FRAMEBUFFER ) {
  270. currentBoundFramebuffers[ gl.DRAW_FRAMEBUFFER ] = framebuffer;
  271. }
  272. return true;
  273. }
  274. return false;
  275. }
  276. function drawBuffers( renderTarget, framebuffer ) {
  277. let drawBuffers = defaultDrawbuffers;
  278. let needsUpdate = false;
  279. if ( renderTarget ) {
  280. drawBuffers = currentDrawbuffers.get( framebuffer );
  281. if ( drawBuffers === undefined ) {
  282. drawBuffers = [];
  283. currentDrawbuffers.set( framebuffer, drawBuffers );
  284. }
  285. const textures = renderTarget.textures;
  286. if ( drawBuffers.length !== textures.length || drawBuffers[ 0 ] !== gl.COLOR_ATTACHMENT0 ) {
  287. for ( let i = 0, il = textures.length; i < il; i ++ ) {
  288. drawBuffers[ i ] = gl.COLOR_ATTACHMENT0 + i;
  289. }
  290. drawBuffers.length = textures.length;
  291. needsUpdate = true;
  292. }
  293. } else {
  294. if ( drawBuffers[ 0 ] !== gl.BACK ) {
  295. drawBuffers[ 0 ] = gl.BACK;
  296. needsUpdate = true;
  297. }
  298. }
  299. if ( needsUpdate ) {
  300. gl.drawBuffers( drawBuffers );
  301. }
  302. }
  303. function useProgram( program ) {
  304. if ( currentProgram !== program ) {
  305. gl.useProgram( program );
  306. currentProgram = program;
  307. return true;
  308. }
  309. return false;
  310. }
  311. const equationToGL = {
  312. [ AddEquation ]: gl.FUNC_ADD,
  313. [ SubtractEquation ]: gl.FUNC_SUBTRACT,
  314. [ ReverseSubtractEquation ]: gl.FUNC_REVERSE_SUBTRACT
  315. };
  316. equationToGL[ MinEquation ] = gl.MIN;
  317. equationToGL[ MaxEquation ] = gl.MAX;
  318. const factorToGL = {
  319. [ ZeroFactor ]: gl.ZERO,
  320. [ OneFactor ]: gl.ONE,
  321. [ SrcColorFactor ]: gl.SRC_COLOR,
  322. [ SrcAlphaFactor ]: gl.SRC_ALPHA,
  323. [ SrcAlphaSaturateFactor ]: gl.SRC_ALPHA_SATURATE,
  324. [ DstColorFactor ]: gl.DST_COLOR,
  325. [ DstAlphaFactor ]: gl.DST_ALPHA,
  326. [ OneMinusSrcColorFactor ]: gl.ONE_MINUS_SRC_COLOR,
  327. [ OneMinusSrcAlphaFactor ]: gl.ONE_MINUS_SRC_ALPHA,
  328. [ OneMinusDstColorFactor ]: gl.ONE_MINUS_DST_COLOR,
  329. [ OneMinusDstAlphaFactor ]: gl.ONE_MINUS_DST_ALPHA,
  330. [ ConstantColorFactor ]: gl.CONSTANT_COLOR,
  331. [ OneMinusConstantColorFactor ]: gl.ONE_MINUS_CONSTANT_COLOR,
  332. [ ConstantAlphaFactor ]: gl.CONSTANT_ALPHA,
  333. [ OneMinusConstantAlphaFactor ]: gl.ONE_MINUS_CONSTANT_ALPHA
  334. };
  335. function setBlending( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, blendColor, blendAlpha, premultipliedAlpha ) {
  336. if ( blending === NoBlending ) {
  337. if ( currentBlendingEnabled === true ) {
  338. disable( gl.BLEND );
  339. currentBlendingEnabled = false;
  340. }
  341. return;
  342. }
  343. if ( currentBlendingEnabled === false ) {
  344. enable( gl.BLEND );
  345. currentBlendingEnabled = true;
  346. }
  347. if ( blending !== CustomBlending ) {
  348. if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) {
  349. if ( currentBlendEquation !== AddEquation || currentBlendEquationAlpha !== AddEquation ) {
  350. gl.blendEquation( gl.FUNC_ADD );
  351. currentBlendEquation = AddEquation;
  352. currentBlendEquationAlpha = AddEquation;
  353. }
  354. if ( premultipliedAlpha ) {
  355. switch ( blending ) {
  356. case NormalBlending:
  357. gl.blendFuncSeparate( gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  358. break;
  359. case AdditiveBlending:
  360. gl.blendFunc( gl.ONE, gl.ONE );
  361. break;
  362. case SubtractiveBlending:
  363. gl.blendFuncSeparate( gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ZERO, gl.ONE );
  364. break;
  365. case MultiplyBlending:
  366. gl.blendFuncSeparate( gl.ZERO, gl.SRC_COLOR, gl.ZERO, gl.SRC_ALPHA );
  367. break;
  368. default:
  369. console.error( 'THREE.WebGLState: Invalid blending: ', blending );
  370. break;
  371. }
  372. } else {
  373. switch ( blending ) {
  374. case NormalBlending:
  375. gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  376. break;
  377. case AdditiveBlending:
  378. gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
  379. break;
  380. case SubtractiveBlending:
  381. gl.blendFuncSeparate( gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ZERO, gl.ONE );
  382. break;
  383. case MultiplyBlending:
  384. gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
  385. break;
  386. default:
  387. console.error( 'THREE.WebGLState: Invalid blending: ', blending );
  388. break;
  389. }
  390. }
  391. currentBlendSrc = null;
  392. currentBlendDst = null;
  393. currentBlendSrcAlpha = null;
  394. currentBlendDstAlpha = null;
  395. currentBlendColor.set( 0, 0, 0 );
  396. currentBlendAlpha = 0;
  397. currentBlending = blending;
  398. currentPremultipledAlpha = premultipliedAlpha;
  399. }
  400. return;
  401. }
  402. // custom blending
  403. blendEquationAlpha = blendEquationAlpha || blendEquation;
  404. blendSrcAlpha = blendSrcAlpha || blendSrc;
  405. blendDstAlpha = blendDstAlpha || blendDst;
  406. if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
  407. gl.blendEquationSeparate( equationToGL[ blendEquation ], equationToGL[ blendEquationAlpha ] );
  408. currentBlendEquation = blendEquation;
  409. currentBlendEquationAlpha = blendEquationAlpha;
  410. }
  411. if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
  412. gl.blendFuncSeparate( factorToGL[ blendSrc ], factorToGL[ blendDst ], factorToGL[ blendSrcAlpha ], factorToGL[ blendDstAlpha ] );
  413. currentBlendSrc = blendSrc;
  414. currentBlendDst = blendDst;
  415. currentBlendSrcAlpha = blendSrcAlpha;
  416. currentBlendDstAlpha = blendDstAlpha;
  417. }
  418. if ( blendColor.equals( currentBlendColor ) === false || blendAlpha !== currentBlendAlpha ) {
  419. gl.blendColor( blendColor.r, blendColor.g, blendColor.b, blendAlpha );
  420. currentBlendColor.copy( blendColor );
  421. currentBlendAlpha = blendAlpha;
  422. }
  423. currentBlending = blending;
  424. currentPremultipledAlpha = false;
  425. }
  426. function setMaterial( material, frontFaceCW ) {
  427. material.side === DoubleSide
  428. ? disable( gl.CULL_FACE )
  429. : enable( gl.CULL_FACE );
  430. let flipSided = ( material.side === BackSide );
  431. if ( frontFaceCW ) flipSided = ! flipSided;
  432. setFlipSided( flipSided );
  433. ( material.blending === NormalBlending && material.transparent === false )
  434. ? setBlending( NoBlending )
  435. : setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha, material.blendColor, material.blendAlpha, material.premultipliedAlpha );
  436. depthBuffer.setFunc( material.depthFunc );
  437. depthBuffer.setTest( material.depthTest );
  438. depthBuffer.setMask( material.depthWrite );
  439. colorBuffer.setMask( material.colorWrite );
  440. const stencilWrite = material.stencilWrite;
  441. stencilBuffer.setTest( stencilWrite );
  442. if ( stencilWrite ) {
  443. stencilBuffer.setMask( material.stencilWriteMask );
  444. stencilBuffer.setFunc( material.stencilFunc, material.stencilRef, material.stencilFuncMask );
  445. stencilBuffer.setOp( material.stencilFail, material.stencilZFail, material.stencilZPass );
  446. }
  447. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  448. material.alphaToCoverage === true
  449. ? enable( gl.SAMPLE_ALPHA_TO_COVERAGE )
  450. : disable( gl.SAMPLE_ALPHA_TO_COVERAGE );
  451. }
  452. //
  453. function setFlipSided( flipSided ) {
  454. if ( currentFlipSided !== flipSided ) {
  455. if ( flipSided ) {
  456. gl.frontFace( gl.CW );
  457. } else {
  458. gl.frontFace( gl.CCW );
  459. }
  460. currentFlipSided = flipSided;
  461. }
  462. }
  463. function setCullFace( cullFace ) {
  464. if ( cullFace !== CullFaceNone ) {
  465. enable( gl.CULL_FACE );
  466. if ( cullFace !== currentCullFace ) {
  467. if ( cullFace === CullFaceBack ) {
  468. gl.cullFace( gl.BACK );
  469. } else if ( cullFace === CullFaceFront ) {
  470. gl.cullFace( gl.FRONT );
  471. } else {
  472. gl.cullFace( gl.FRONT_AND_BACK );
  473. }
  474. }
  475. } else {
  476. disable( gl.CULL_FACE );
  477. }
  478. currentCullFace = cullFace;
  479. }
  480. function setLineWidth( width ) {
  481. if ( width !== currentLineWidth ) {
  482. if ( lineWidthAvailable ) gl.lineWidth( width );
  483. currentLineWidth = width;
  484. }
  485. }
  486. function setPolygonOffset( polygonOffset, factor, units ) {
  487. if ( polygonOffset ) {
  488. enable( gl.POLYGON_OFFSET_FILL );
  489. if ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) {
  490. gl.polygonOffset( factor, units );
  491. currentPolygonOffsetFactor = factor;
  492. currentPolygonOffsetUnits = units;
  493. }
  494. } else {
  495. disable( gl.POLYGON_OFFSET_FILL );
  496. }
  497. }
  498. function setScissorTest( scissorTest ) {
  499. if ( scissorTest ) {
  500. enable( gl.SCISSOR_TEST );
  501. } else {
  502. disable( gl.SCISSOR_TEST );
  503. }
  504. }
  505. // texture
  506. function activeTexture( webglSlot ) {
  507. if ( webglSlot === undefined ) webglSlot = gl.TEXTURE0 + maxTextures - 1;
  508. if ( currentTextureSlot !== webglSlot ) {
  509. gl.activeTexture( webglSlot );
  510. currentTextureSlot = webglSlot;
  511. }
  512. }
  513. function bindTexture( webglType, webglTexture, webglSlot ) {
  514. if ( webglSlot === undefined ) {
  515. if ( currentTextureSlot === null ) {
  516. webglSlot = gl.TEXTURE0 + maxTextures - 1;
  517. } else {
  518. webglSlot = currentTextureSlot;
  519. }
  520. }
  521. let boundTexture = currentBoundTextures[ webglSlot ];
  522. if ( boundTexture === undefined ) {
  523. boundTexture = { type: undefined, texture: undefined };
  524. currentBoundTextures[ webglSlot ] = boundTexture;
  525. }
  526. if ( boundTexture.type !== webglType || boundTexture.texture !== webglTexture ) {
  527. if ( currentTextureSlot !== webglSlot ) {
  528. gl.activeTexture( webglSlot );
  529. currentTextureSlot = webglSlot;
  530. }
  531. gl.bindTexture( webglType, webglTexture || emptyTextures[ webglType ] );
  532. boundTexture.type = webglType;
  533. boundTexture.texture = webglTexture;
  534. }
  535. }
  536. function unbindTexture() {
  537. const boundTexture = currentBoundTextures[ currentTextureSlot ];
  538. if ( boundTexture !== undefined && boundTexture.type !== undefined ) {
  539. gl.bindTexture( boundTexture.type, null );
  540. boundTexture.type = undefined;
  541. boundTexture.texture = undefined;
  542. }
  543. }
  544. function compressedTexImage2D() {
  545. try {
  546. gl.compressedTexImage2D.apply( gl, arguments );
  547. } catch ( error ) {
  548. console.error( 'THREE.WebGLState:', error );
  549. }
  550. }
  551. function compressedTexImage3D() {
  552. try {
  553. gl.compressedTexImage3D.apply( gl, arguments );
  554. } catch ( error ) {
  555. console.error( 'THREE.WebGLState:', error );
  556. }
  557. }
  558. function texSubImage2D() {
  559. try {
  560. gl.texSubImage2D.apply( gl, arguments );
  561. } catch ( error ) {
  562. console.error( 'THREE.WebGLState:', error );
  563. }
  564. }
  565. function texSubImage3D() {
  566. try {
  567. gl.texSubImage3D.apply( gl, arguments );
  568. } catch ( error ) {
  569. console.error( 'THREE.WebGLState:', error );
  570. }
  571. }
  572. function compressedTexSubImage2D() {
  573. try {
  574. gl.compressedTexSubImage2D.apply( gl, arguments );
  575. } catch ( error ) {
  576. console.error( 'THREE.WebGLState:', error );
  577. }
  578. }
  579. function compressedTexSubImage3D() {
  580. try {
  581. gl.compressedTexSubImage3D.apply( gl, arguments );
  582. } catch ( error ) {
  583. console.error( 'THREE.WebGLState:', error );
  584. }
  585. }
  586. function texStorage2D() {
  587. try {
  588. gl.texStorage2D.apply( gl, arguments );
  589. } catch ( error ) {
  590. console.error( 'THREE.WebGLState:', error );
  591. }
  592. }
  593. function texStorage3D() {
  594. try {
  595. gl.texStorage3D.apply( gl, arguments );
  596. } catch ( error ) {
  597. console.error( 'THREE.WebGLState:', error );
  598. }
  599. }
  600. function texImage2D() {
  601. try {
  602. gl.texImage2D.apply( gl, arguments );
  603. } catch ( error ) {
  604. console.error( 'THREE.WebGLState:', error );
  605. }
  606. }
  607. function texImage3D() {
  608. try {
  609. gl.texImage3D.apply( gl, arguments );
  610. } catch ( error ) {
  611. console.error( 'THREE.WebGLState:', error );
  612. }
  613. }
  614. //
  615. function scissor( scissor ) {
  616. if ( currentScissor.equals( scissor ) === false ) {
  617. gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w );
  618. currentScissor.copy( scissor );
  619. }
  620. }
  621. function viewport( viewport ) {
  622. if ( currentViewport.equals( viewport ) === false ) {
  623. gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w );
  624. currentViewport.copy( viewport );
  625. }
  626. }
  627. function updateUBOMapping( uniformsGroup, program ) {
  628. let mapping = uboProgramMap.get( program );
  629. if ( mapping === undefined ) {
  630. mapping = new WeakMap();
  631. uboProgramMap.set( program, mapping );
  632. }
  633. let blockIndex = mapping.get( uniformsGroup );
  634. if ( blockIndex === undefined ) {
  635. blockIndex = gl.getUniformBlockIndex( program, uniformsGroup.name );
  636. mapping.set( uniformsGroup, blockIndex );
  637. }
  638. }
  639. function uniformBlockBinding( uniformsGroup, program ) {
  640. const mapping = uboProgramMap.get( program );
  641. const blockIndex = mapping.get( uniformsGroup );
  642. if ( uboBindings.get( program ) !== blockIndex ) {
  643. // bind shader specific block index to global block point
  644. gl.uniformBlockBinding( program, blockIndex, uniformsGroup.__bindingPointIndex );
  645. uboBindings.set( program, blockIndex );
  646. }
  647. }
  648. //
  649. function reset() {
  650. // reset state
  651. gl.disable( gl.BLEND );
  652. gl.disable( gl.CULL_FACE );
  653. gl.disable( gl.DEPTH_TEST );
  654. gl.disable( gl.POLYGON_OFFSET_FILL );
  655. gl.disable( gl.SCISSOR_TEST );
  656. gl.disable( gl.STENCIL_TEST );
  657. gl.disable( gl.SAMPLE_ALPHA_TO_COVERAGE );
  658. gl.blendEquation( gl.FUNC_ADD );
  659. gl.blendFunc( gl.ONE, gl.ZERO );
  660. gl.blendFuncSeparate( gl.ONE, gl.ZERO, gl.ONE, gl.ZERO );
  661. gl.blendColor( 0, 0, 0, 0 );
  662. gl.colorMask( true, true, true, true );
  663. gl.clearColor( 0, 0, 0, 0 );
  664. gl.depthMask( true );
  665. gl.depthFunc( gl.LESS );
  666. gl.clearDepth( 1 );
  667. gl.stencilMask( 0xffffffff );
  668. gl.stencilFunc( gl.ALWAYS, 0, 0xffffffff );
  669. gl.stencilOp( gl.KEEP, gl.KEEP, gl.KEEP );
  670. gl.clearStencil( 0 );
  671. gl.cullFace( gl.BACK );
  672. gl.frontFace( gl.CCW );
  673. gl.polygonOffset( 0, 0 );
  674. gl.activeTexture( gl.TEXTURE0 );
  675. gl.bindFramebuffer( gl.FRAMEBUFFER, null );
  676. gl.bindFramebuffer( gl.DRAW_FRAMEBUFFER, null );
  677. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, null );
  678. gl.useProgram( null );
  679. gl.lineWidth( 1 );
  680. gl.scissor( 0, 0, gl.canvas.width, gl.canvas.height );
  681. gl.viewport( 0, 0, gl.canvas.width, gl.canvas.height );
  682. // reset internals
  683. enabledCapabilities = {};
  684. currentTextureSlot = null;
  685. currentBoundTextures = {};
  686. currentBoundFramebuffers = {};
  687. currentDrawbuffers = new WeakMap();
  688. defaultDrawbuffers = [];
  689. currentProgram = null;
  690. currentBlendingEnabled = false;
  691. currentBlending = null;
  692. currentBlendEquation = null;
  693. currentBlendSrc = null;
  694. currentBlendDst = null;
  695. currentBlendEquationAlpha = null;
  696. currentBlendSrcAlpha = null;
  697. currentBlendDstAlpha = null;
  698. currentBlendColor = new Color( 0, 0, 0 );
  699. currentBlendAlpha = 0;
  700. currentPremultipledAlpha = false;
  701. currentFlipSided = null;
  702. currentCullFace = null;
  703. currentLineWidth = null;
  704. currentPolygonOffsetFactor = null;
  705. currentPolygonOffsetUnits = null;
  706. currentScissor.set( 0, 0, gl.canvas.width, gl.canvas.height );
  707. currentViewport.set( 0, 0, gl.canvas.width, gl.canvas.height );
  708. colorBuffer.reset();
  709. depthBuffer.reset();
  710. stencilBuffer.reset();
  711. }
  712. return {
  713. buffers: {
  714. color: colorBuffer,
  715. depth: depthBuffer,
  716. stencil: stencilBuffer
  717. },
  718. enable: enable,
  719. disable: disable,
  720. bindFramebuffer: bindFramebuffer,
  721. drawBuffers: drawBuffers,
  722. useProgram: useProgram,
  723. setBlending: setBlending,
  724. setMaterial: setMaterial,
  725. setFlipSided: setFlipSided,
  726. setCullFace: setCullFace,
  727. setLineWidth: setLineWidth,
  728. setPolygonOffset: setPolygonOffset,
  729. setScissorTest: setScissorTest,
  730. activeTexture: activeTexture,
  731. bindTexture: bindTexture,
  732. unbindTexture: unbindTexture,
  733. compressedTexImage2D: compressedTexImage2D,
  734. compressedTexImage3D: compressedTexImage3D,
  735. texImage2D: texImage2D,
  736. texImage3D: texImage3D,
  737. updateUBOMapping: updateUBOMapping,
  738. uniformBlockBinding: uniformBlockBinding,
  739. texStorage2D: texStorage2D,
  740. texStorage3D: texStorage3D,
  741. texSubImage2D: texSubImage2D,
  742. texSubImage3D: texSubImage3D,
  743. compressedTexSubImage2D: compressedTexSubImage2D,
  744. compressedTexSubImage3D: compressedTexSubImage3D,
  745. scissor: scissor,
  746. viewport: viewport,
  747. reset: reset
  748. };
  749. }
  750. export { WebGLState };