WebGLState.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.WebGLState = function ( gl, extensions, paramThreeToGL ) {
  5. var _this = this;
  6. var color = new THREE.Vector4();
  7. var maxVertexAttributes = gl.getParameter( gl.MAX_VERTEX_ATTRIBS );
  8. var newAttributes = new Uint8Array( maxVertexAttributes );
  9. var enabledAttributes = new Uint8Array( maxVertexAttributes );
  10. var attributeDivisors = new Uint8Array( maxVertexAttributes );
  11. var capabilities = {};
  12. var compressedTextureFormats = null;
  13. var currentBlending = null;
  14. var currentBlendEquation = null;
  15. var currentBlendSrc = null;
  16. var currentBlendDst = null;
  17. var currentBlendEquationAlpha = null;
  18. var currentBlendSrcAlpha = null;
  19. var currentBlendDstAlpha = null;
  20. var currentPremultipledAlpha = false;
  21. var currentDepthFunc = null;
  22. var currentDepthWrite = null;
  23. var currentColorWrite = null;
  24. var currentStencilWrite = null;
  25. var currentStencilFunc = null;
  26. var currentStencilRef = null;
  27. var currentStencilMask = null;
  28. var currentStencilFail = null;
  29. var currentStencilZFail = null;
  30. var currentStencilZPass = null;
  31. var currentFlipSided = null;
  32. var currentCullFace = null;
  33. var currentLineWidth = null;
  34. var currentPolygonOffsetFactor = null;
  35. var currentPolygonOffsetUnits = null;
  36. var currentScissorTest = null;
  37. var maxTextures = gl.getParameter( gl.MAX_TEXTURE_IMAGE_UNITS );
  38. var currentTextureSlot = undefined;
  39. var currentBoundTextures = {};
  40. var currentClearColor = new THREE.Vector4();
  41. var currentClearDepth = null;
  42. var currentClearStencil = null;
  43. var currentScissor = new THREE.Vector4();
  44. var currentViewport = new THREE.Vector4();
  45. this.init = function () {
  46. this.clearColor( 0, 0, 0, 1 );
  47. this.clearDepth( 1 );
  48. this.clearStencil( 0 );
  49. this.enable( gl.DEPTH_TEST );
  50. this.setDepthFunc( THREE.LessEqualDepth );
  51. this.setFlipSided( false );
  52. this.setCullFace( THREE.CullFaceBack );
  53. this.enable( gl.CULL_FACE );
  54. this.enable( gl.BLEND );
  55. this.setBlending( THREE.NormalBlending );
  56. };
  57. this.initAttributes = function () {
  58. for ( var i = 0, l = newAttributes.length; i < l; i ++ ) {
  59. newAttributes[ i ] = 0;
  60. }
  61. };
  62. this.enableAttribute = function ( attribute ) {
  63. newAttributes[ attribute ] = 1;
  64. if ( enabledAttributes[ attribute ] === 0 ) {
  65. gl.enableVertexAttribArray( attribute );
  66. enabledAttributes[ attribute ] = 1;
  67. }
  68. if ( attributeDivisors[ attribute ] !== 0 ) {
  69. var extension = extensions.get( 'ANGLE_instanced_arrays' );
  70. extension.vertexAttribDivisorANGLE( attribute, 0 );
  71. attributeDivisors[ attribute ] = 0;
  72. }
  73. };
  74. this.enableAttributeAndDivisor = function ( attribute, meshPerAttribute, extension ) {
  75. newAttributes[ attribute ] = 1;
  76. if ( enabledAttributes[ attribute ] === 0 ) {
  77. gl.enableVertexAttribArray( attribute );
  78. enabledAttributes[ attribute ] = 1;
  79. }
  80. if ( attributeDivisors[ attribute ] !== meshPerAttribute ) {
  81. extension.vertexAttribDivisorANGLE( attribute, meshPerAttribute );
  82. attributeDivisors[ attribute ] = meshPerAttribute;
  83. }
  84. };
  85. this.disableUnusedAttributes = function () {
  86. for ( var i = 0, l = enabledAttributes.length; i !== l; ++ i ) {
  87. if ( enabledAttributes[ i ] !== newAttributes[ i ] ) {
  88. gl.disableVertexAttribArray( i );
  89. enabledAttributes[ i ] = 0;
  90. }
  91. }
  92. };
  93. this.enable = function ( id ) {
  94. if ( capabilities[ id ] !== true ) {
  95. gl.enable( id );
  96. capabilities[ id ] = true;
  97. }
  98. };
  99. this.disable = function ( id ) {
  100. if ( capabilities[ id ] !== false ) {
  101. gl.disable( id );
  102. capabilities[ id ] = false;
  103. }
  104. };
  105. this.getCompressedTextureFormats = function () {
  106. if ( compressedTextureFormats === null ) {
  107. compressedTextureFormats = [];
  108. if ( extensions.get( 'WEBGL_compressed_texture_pvrtc' ) ||
  109. extensions.get( 'WEBGL_compressed_texture_s3tc' ) ||
  110. extensions.get( 'WEBGL_compressed_texture_etc1' ) ) {
  111. var formats = gl.getParameter( gl.COMPRESSED_TEXTURE_FORMATS );
  112. for ( var i = 0; i < formats.length; i ++ ) {
  113. compressedTextureFormats.push( formats[ i ] );
  114. }
  115. }
  116. }
  117. return compressedTextureFormats;
  118. };
  119. this.setBlending = function ( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, premultipliedAlpha ) {
  120. if ( blending !== THREE.NoBlending ) {
  121. this.enable( gl.BLEND );
  122. } else {
  123. this.disable( gl.BLEND );
  124. currentBlending = blending; // no blending, that is
  125. return;
  126. }
  127. if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) {
  128. if ( blending === THREE.AdditiveBlending ) {
  129. if ( premultipliedAlpha ) {
  130. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  131. gl.blendFuncSeparate( gl.ONE, gl.ONE, gl.ONE, gl.ONE );
  132. } else {
  133. gl.blendEquation( gl.FUNC_ADD );
  134. gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
  135. }
  136. } else if ( blending === THREE.SubtractiveBlending ) {
  137. if ( premultipliedAlpha ) {
  138. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  139. gl.blendFuncSeparate( gl.ZERO, gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ONE_MINUS_SRC_ALPHA );
  140. } else {
  141. gl.blendEquation( gl.FUNC_ADD );
  142. gl.blendFunc( gl.ZERO, gl.ONE_MINUS_SRC_COLOR );
  143. }
  144. } else if ( blending === THREE.MultiplyBlending ) {
  145. if ( premultipliedAlpha ) {
  146. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  147. gl.blendFuncSeparate( gl.ZERO, gl.ZERO, gl.SRC_COLOR, gl.SRC_ALPHA );
  148. } else {
  149. gl.blendEquation( gl.FUNC_ADD );
  150. gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
  151. }
  152. } else {
  153. if ( premultipliedAlpha ) {
  154. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  155. gl.blendFuncSeparate( gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  156. } else {
  157. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  158. gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  159. }
  160. }
  161. currentBlending = blending;
  162. currentPremultipledAlpha = premultipliedAlpha;
  163. }
  164. if ( blending === THREE.CustomBlending ) {
  165. blendEquationAlpha = blendEquationAlpha || blendEquation;
  166. blendSrcAlpha = blendSrcAlpha || blendSrc;
  167. blendDstAlpha = blendDstAlpha || blendDst;
  168. if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
  169. gl.blendEquationSeparate( paramThreeToGL( blendEquation ), paramThreeToGL( blendEquationAlpha ) );
  170. currentBlendEquation = blendEquation;
  171. currentBlendEquationAlpha = blendEquationAlpha;
  172. }
  173. if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
  174. gl.blendFuncSeparate( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ), paramThreeToGL( blendSrcAlpha ), paramThreeToGL( blendDstAlpha ) );
  175. currentBlendSrc = blendSrc;
  176. currentBlendDst = blendDst;
  177. currentBlendSrcAlpha = blendSrcAlpha;
  178. currentBlendDstAlpha = blendDstAlpha;
  179. }
  180. } else {
  181. currentBlendEquation = null;
  182. currentBlendSrc = null;
  183. currentBlendDst = null;
  184. currentBlendEquationAlpha = null;
  185. currentBlendSrcAlpha = null;
  186. currentBlendDstAlpha = null;
  187. }
  188. };
  189. this.setDepthFunc = function ( depthFunc ) {
  190. if ( currentDepthFunc !== depthFunc ) {
  191. if ( depthFunc ) {
  192. switch ( depthFunc ) {
  193. case THREE.NeverDepth:
  194. gl.depthFunc( gl.NEVER );
  195. break;
  196. case THREE.AlwaysDepth:
  197. gl.depthFunc( gl.ALWAYS );
  198. break;
  199. case THREE.LessDepth:
  200. gl.depthFunc( gl.LESS );
  201. break;
  202. case THREE.LessEqualDepth:
  203. gl.depthFunc( gl.LEQUAL );
  204. break;
  205. case THREE.EqualDepth:
  206. gl.depthFunc( gl.EQUAL );
  207. break;
  208. case THREE.GreaterEqualDepth:
  209. gl.depthFunc( gl.GEQUAL );
  210. break;
  211. case THREE.GreaterDepth:
  212. gl.depthFunc( gl.GREATER );
  213. break;
  214. case THREE.NotEqualDepth:
  215. gl.depthFunc( gl.NOTEQUAL );
  216. break;
  217. default:
  218. gl.depthFunc( gl.LEQUAL );
  219. }
  220. } else {
  221. gl.depthFunc( gl.LEQUAL );
  222. }
  223. currentDepthFunc = depthFunc;
  224. }
  225. };
  226. this.setDepthTest = function ( depthTest ) {
  227. if ( depthTest ) {
  228. this.enable( gl.DEPTH_TEST );
  229. } else {
  230. this.disable( gl.DEPTH_TEST );
  231. }
  232. };
  233. this.setDepthWrite = function ( depthWrite ) {
  234. // TODO: Rename to setDepthMask
  235. if ( currentDepthWrite !== depthWrite ) {
  236. gl.depthMask( depthWrite );
  237. currentDepthWrite = depthWrite;
  238. }
  239. };
  240. this.setColorWrite = function ( colorWrite ) {
  241. // TODO: Rename to setColorMask
  242. if ( currentColorWrite !== colorWrite ) {
  243. gl.colorMask( colorWrite, colorWrite, colorWrite, colorWrite );
  244. currentColorWrite = colorWrite;
  245. }
  246. };
  247. this.setStencilFunc = function ( stencilFunc, stencilRef, stencilMask ) {
  248. if ( currentStencilFunc !== stencilFunc ||
  249. currentStencilRef !== stencilRef ||
  250. currentStencilMask !== stencilMask ) {
  251. gl.stencilFunc( stencilFunc, stencilRef, stencilMask );
  252. currentStencilFunc = stencilFunc;
  253. currentStencilRef = stencilRef;
  254. currentStencilMask = stencilMask;
  255. }
  256. };
  257. this.setStencilOp = function ( stencilFail, stencilZFail, stencilZPass ) {
  258. if ( currentStencilFail !== stencilFail ||
  259. currentStencilZFail !== stencilZFail ||
  260. currentStencilZPass !== stencilZPass ) {
  261. gl.stencilOp( stencilFail, stencilZFail, stencilZPass );
  262. currentStencilFail = stencilFail;
  263. currentStencilZFail = stencilZFail;
  264. currentStencilZPass = stencilZPass;
  265. }
  266. };
  267. this.setStencilTest = function ( stencilTest ) {
  268. if ( stencilTest ) {
  269. this.enable( gl.STENCIL_TEST );
  270. } else {
  271. this.disable( gl.STENCIL_TEST );
  272. }
  273. };
  274. this.setStencilWrite = function ( stencilWrite ) {
  275. // TODO: Rename to setStencilMask
  276. if ( currentStencilWrite !== stencilWrite ) {
  277. gl.stencilMask( stencilWrite );
  278. currentStencilWrite = stencilWrite;
  279. }
  280. };
  281. this.setFlipSided = function ( flipSided ) {
  282. if ( currentFlipSided !== flipSided ) {
  283. if ( flipSided ) {
  284. gl.frontFace( gl.CW );
  285. } else {
  286. gl.frontFace( gl.CCW );
  287. }
  288. currentFlipSided = flipSided;
  289. }
  290. };
  291. this.setCullFace = function ( cullFace ) {
  292. if ( cullFace !== THREE.CullFaceNone ) {
  293. this.enable( gl.CULL_FACE );
  294. if ( cullFace !== currentCullFace ) {
  295. if ( cullFace === THREE.CullFaceBack ) {
  296. gl.cullFace( gl.BACK );
  297. } else if ( cullFace === THREE.CullFaceFront ) {
  298. gl.cullFace( gl.FRONT );
  299. } else {
  300. gl.cullFace( gl.FRONT_AND_BACK );
  301. }
  302. }
  303. } else {
  304. this.disable( gl.CULL_FACE );
  305. }
  306. currentCullFace = cullFace;
  307. };
  308. this.setLineWidth = function ( width ) {
  309. if ( width !== currentLineWidth ) {
  310. gl.lineWidth( width );
  311. currentLineWidth = width;
  312. }
  313. };
  314. this.setPolygonOffset = function ( polygonOffset, factor, units ) {
  315. if ( polygonOffset ) {
  316. this.enable( gl.POLYGON_OFFSET_FILL );
  317. if ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) {
  318. gl.polygonOffset( factor, units );
  319. currentPolygonOffsetFactor = factor;
  320. currentPolygonOffsetUnits = units;
  321. }
  322. } else {
  323. this.disable( gl.POLYGON_OFFSET_FILL );
  324. }
  325. };
  326. this.getScissorTest = function () {
  327. return currentScissorTest;
  328. };
  329. this.setScissorTest = function ( scissorTest ) {
  330. currentScissorTest = scissorTest;
  331. if ( scissorTest ) {
  332. this.enable( gl.SCISSOR_TEST );
  333. } else {
  334. this.disable( gl.SCISSOR_TEST );
  335. }
  336. };
  337. // texture
  338. this.activeTexture = function ( webglSlot ) {
  339. if ( webglSlot === undefined ) webglSlot = gl.TEXTURE0 + maxTextures - 1;
  340. if ( currentTextureSlot !== webglSlot ) {
  341. gl.activeTexture( webglSlot );
  342. currentTextureSlot = webglSlot;
  343. }
  344. };
  345. this.bindTexture = function ( webglType, webglTexture ) {
  346. if ( currentTextureSlot === undefined ) {
  347. _this.activeTexture();
  348. }
  349. var boundTexture = currentBoundTextures[ currentTextureSlot ];
  350. if ( boundTexture === undefined ) {
  351. boundTexture = { type: undefined, texture: undefined };
  352. currentBoundTextures[ currentTextureSlot ] = boundTexture;
  353. }
  354. if ( boundTexture.type !== webglType || boundTexture.texture !== webglTexture ) {
  355. gl.bindTexture( webglType, webglTexture );
  356. boundTexture.type = webglType;
  357. boundTexture.texture = webglTexture;
  358. }
  359. };
  360. this.compressedTexImage2D = function () {
  361. try {
  362. gl.compressedTexImage2D.apply( gl, arguments );
  363. } catch ( error ) {
  364. console.error( error );
  365. }
  366. };
  367. this.texImage2D = function () {
  368. try {
  369. gl.texImage2D.apply( gl, arguments );
  370. } catch ( error ) {
  371. console.error( error );
  372. }
  373. };
  374. // clear values
  375. this.clearColor = function ( r, g, b, a ) {
  376. color.set( r, g, b, a );
  377. if ( currentClearColor.equals( color ) === false ) {
  378. gl.clearColor( r, g, b, a );
  379. currentClearColor.copy( color );
  380. }
  381. };
  382. this.clearDepth = function ( depth ) {
  383. if ( currentClearDepth !== depth ) {
  384. gl.clearDepth( depth );
  385. currentClearDepth = depth;
  386. }
  387. };
  388. this.clearStencil = function ( stencil ) {
  389. if ( currentClearStencil !== stencil ) {
  390. gl.clearStencil( stencil );
  391. currentClearStencil = stencil;
  392. }
  393. };
  394. //
  395. this.scissor = function ( scissor ) {
  396. if ( currentScissor.equals( scissor ) === false ) {
  397. gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w );
  398. currentScissor.copy( scissor );
  399. }
  400. };
  401. this.viewport = function ( viewport ) {
  402. if ( currentViewport.equals( viewport ) === false ) {
  403. gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w );
  404. currentViewport.copy( viewport );
  405. }
  406. };
  407. //
  408. this.reset = function () {
  409. for ( var i = 0; i < enabledAttributes.length; i ++ ) {
  410. if ( enabledAttributes[ i ] === 1 ) {
  411. gl.disableVertexAttribArray( i );
  412. enabledAttributes[ i ] = 0;
  413. }
  414. }
  415. capabilities = {};
  416. compressedTextureFormats = null;
  417. currentTextureSlot = undefined;
  418. currentBoundTextures = {};
  419. currentBlending = null;
  420. currentColorWrite = null;
  421. currentDepthWrite = null;
  422. currentStencilWrite = null;
  423. currentFlipSided = null;
  424. currentCullFace = null;
  425. };
  426. };