WebGLState.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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 {
  140. gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
  141. gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
  142. }
  143. currentBlending = blending;
  144. }
  145. if ( blending === THREE.CustomBlending ) {
  146. blendEquationAlpha = blendEquationAlpha || blendEquation;
  147. blendSrcAlpha = blendSrcAlpha || blendSrc;
  148. blendDstAlpha = blendDstAlpha || blendDst;
  149. if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
  150. gl.blendEquationSeparate( paramThreeToGL( blendEquation ), paramThreeToGL( blendEquationAlpha ) );
  151. currentBlendEquation = blendEquation;
  152. currentBlendEquationAlpha = blendEquationAlpha;
  153. }
  154. if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
  155. gl.blendFuncSeparate( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ), paramThreeToGL( blendSrcAlpha ), paramThreeToGL( blendDstAlpha ) );
  156. currentBlendSrc = blendSrc;
  157. currentBlendDst = blendDst;
  158. currentBlendSrcAlpha = blendSrcAlpha;
  159. currentBlendDstAlpha = blendDstAlpha;
  160. }
  161. } else {
  162. currentBlendEquation = null;
  163. currentBlendSrc = null;
  164. currentBlendDst = null;
  165. currentBlendEquationAlpha = null;
  166. currentBlendSrcAlpha = null;
  167. currentBlendDstAlpha = null;
  168. }
  169. };
  170. this.setDepthFunc = function ( depthFunc ) {
  171. if ( currentDepthFunc !== depthFunc ) {
  172. if ( depthFunc ) {
  173. switch ( depthFunc ) {
  174. case THREE.NeverDepth:
  175. gl.depthFunc( gl.NEVER );
  176. break;
  177. case THREE.AlwaysDepth:
  178. gl.depthFunc( gl.ALWAYS );
  179. break;
  180. case THREE.LessDepth:
  181. gl.depthFunc( gl.LESS );
  182. break;
  183. case THREE.LessEqualDepth:
  184. gl.depthFunc( gl.LEQUAL );
  185. break;
  186. case THREE.EqualDepth:
  187. gl.depthFunc( gl.EQUAL );
  188. break;
  189. case THREE.GreaterEqualDepth:
  190. gl.depthFunc( gl.GEQUAL );
  191. break;
  192. case THREE.GreaterDepth:
  193. gl.depthFunc( gl.GREATER );
  194. break;
  195. case THREE.NotEqualDepth:
  196. gl.depthFunc( gl.NOTEQUAL );
  197. break;
  198. default:
  199. gl.depthFunc( gl.LEQUAL );
  200. }
  201. } else {
  202. gl.depthFunc( gl.LEQUAL );
  203. }
  204. currentDepthFunc = depthFunc;
  205. }
  206. };
  207. this.setDepthTest = function ( depthTest ) {
  208. if ( depthTest ) {
  209. this.enable( gl.DEPTH_TEST );
  210. } else {
  211. this.disable( gl.DEPTH_TEST );
  212. }
  213. };
  214. this.setDepthWrite = function ( depthWrite ) {
  215. // TODO: Rename to setDepthMask
  216. if ( currentDepthWrite !== depthWrite ) {
  217. gl.depthMask( depthWrite );
  218. currentDepthWrite = depthWrite;
  219. }
  220. };
  221. this.setColorWrite = function ( colorWrite ) {
  222. // TODO: Rename to setColorMask
  223. if ( currentColorWrite !== colorWrite ) {
  224. gl.colorMask( colorWrite, colorWrite, colorWrite, colorWrite );
  225. currentColorWrite = colorWrite;
  226. }
  227. };
  228. this.setStencilFunc = function ( stencilFunc, stencilRef, stencilMask ) {
  229. if ( currentStencilFunc !== stencilFunc ||
  230. currentStencilRef !== stencilRef ||
  231. currentStencilMask !== stencilMask ) {
  232. gl.stencilFunc( stencilFunc, stencilRef, stencilMask );
  233. currentStencilFunc = stencilFunc;
  234. currentStencilRef = stencilRef;
  235. currentStencilMask = stencilMask;
  236. }
  237. };
  238. this.setStencilOp = function ( stencilFail, stencilZFail, stencilZPass ) {
  239. if ( currentStencilFail !== stencilFail ||
  240. currentStencilZFail !== stencilZFail ||
  241. currentStencilZPass !== stencilZPass ) {
  242. gl.stencilOp( stencilFail, stencilZFail, stencilZPass );
  243. currentStencilFail = stencilFail;
  244. currentStencilZFail = stencilZFail;
  245. currentStencilZPass = stencilZPass;
  246. }
  247. };
  248. this.setStencilTest = function ( stencilTest ) {
  249. if ( stencilTest ) {
  250. this.enable( gl.STENCIL_TEST );
  251. } else {
  252. this.disable( gl.STENCIL_TEST );
  253. }
  254. };
  255. this.setStencilWrite = function ( stencilWrite ) {
  256. // TODO: Rename to setStencilMask
  257. if ( currentStencilWrite !== stencilWrite ) {
  258. gl.stencilMask( stencilWrite );
  259. currentStencilWrite = stencilWrite;
  260. }
  261. };
  262. this.setFlipSided = function ( flipSided ) {
  263. if ( currentFlipSided !== flipSided ) {
  264. if ( flipSided ) {
  265. gl.frontFace( gl.CW );
  266. } else {
  267. gl.frontFace( gl.CCW );
  268. }
  269. currentFlipSided = flipSided;
  270. }
  271. };
  272. this.setLineWidth = function ( width ) {
  273. if ( width !== currentLineWidth ) {
  274. gl.lineWidth( width );
  275. currentLineWidth = width;
  276. }
  277. };
  278. this.setPolygonOffset = function ( polygonOffset, factor, units ) {
  279. if ( polygonOffset ) {
  280. this.enable( gl.POLYGON_OFFSET_FILL );
  281. } else {
  282. this.disable( gl.POLYGON_OFFSET_FILL );
  283. }
  284. if ( polygonOffset && ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) ) {
  285. gl.polygonOffset( factor, units );
  286. currentPolygonOffsetFactor = factor;
  287. currentPolygonOffsetUnits = units;
  288. }
  289. };
  290. this.getScissorTest = function () {
  291. return currentScissorTest;
  292. };
  293. this.setScissorTest = function ( scissorTest ) {
  294. currentScissorTest = scissorTest;
  295. if ( scissorTest ) {
  296. this.enable( gl.SCISSOR_TEST );
  297. } else {
  298. this.disable( gl.SCISSOR_TEST );
  299. }
  300. };
  301. // texture
  302. this.activeTexture = function ( webglSlot ) {
  303. if ( webglSlot === undefined ) webglSlot = gl.TEXTURE0 + maxTextures - 1;
  304. if ( currentTextureSlot !== webglSlot ) {
  305. gl.activeTexture( webglSlot );
  306. currentTextureSlot = webglSlot;
  307. }
  308. };
  309. this.bindTexture = function ( webglType, webglTexture ) {
  310. if ( currentTextureSlot === undefined ) {
  311. _this.activeTexture();
  312. }
  313. var boundTexture = currentBoundTextures[ currentTextureSlot ];
  314. if ( boundTexture === undefined ) {
  315. boundTexture = { type: undefined, texture: undefined };
  316. currentBoundTextures[ currentTextureSlot ] = boundTexture;
  317. }
  318. if ( boundTexture.type !== webglType || boundTexture.texture !== webglTexture ) {
  319. gl.bindTexture( webglType, webglTexture || emptyTexture );
  320. boundTexture.type = webglType;
  321. boundTexture.texture = webglTexture;
  322. }
  323. };
  324. this.compressedTexImage2D = function () {
  325. try {
  326. gl.compressedTexImage2D.apply( gl, arguments );
  327. } catch ( error ) {
  328. console.error( error );
  329. }
  330. };
  331. this.texImage2D = function () {
  332. try {
  333. gl.texImage2D.apply( gl, arguments );
  334. } catch ( error ) {
  335. console.error( error );
  336. }
  337. };
  338. // clear values
  339. this.clearColor = function ( r, g, b, a ) {
  340. color.set( r, g, b, a );
  341. if ( currentClearColor.equals( color ) === false ) {
  342. gl.clearColor( r, g, b, a );
  343. currentClearColor.copy( color );
  344. }
  345. };
  346. this.clearDepth = function ( depth ) {
  347. if ( currentClearDepth !== depth ) {
  348. gl.clearDepth( depth );
  349. currentClearDepth = depth;
  350. }
  351. };
  352. this.clearStencil = function ( stencil ) {
  353. if ( currentClearStencil !== stencil ) {
  354. gl.clearStencil( stencil );
  355. currentClearStencil = stencil;
  356. }
  357. };
  358. //
  359. this.scissor = function ( scissor ) {
  360. if ( currentScissor.equals( scissor ) === false ) {
  361. gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w );
  362. currentScissor.copy( scissor );
  363. }
  364. };
  365. this.viewport = function ( viewport ) {
  366. if ( currentViewport.equals( viewport ) === false ) {
  367. gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w );
  368. currentViewport.copy( viewport );
  369. }
  370. };
  371. //
  372. this.reset = function () {
  373. for ( var i = 0; i < enabledAttributes.length; i ++ ) {
  374. if ( enabledAttributes[ i ] === 1 ) {
  375. gl.disableVertexAttribArray( i );
  376. enabledAttributes[ i ] = 0;
  377. }
  378. }
  379. capabilities = {};
  380. compressedTextureFormats = null;
  381. currentBlending = null;
  382. currentColorWrite = null;
  383. currentDepthWrite = null;
  384. currentStencilWrite = null;
  385. currentFlipSided = null;
  386. };
  387. };
粤ICP备19079148号