WebGLState.js 13 KB

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