MSAAPass.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /**
  2. * @author bhouston / http://clara.io/
  3. */
  4. THREE.MSAAPass = function ( scene, camera, params, clearColor, clearAlpha ) {
  5. this.scene = scene;
  6. this.camera = camera;
  7. this.currentSampleLevel = 4;
  8. this.params = params || { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
  9. this.params.minFilter = THREE.NearestFilter;
  10. this.params.maxFilter = THREE.NearestFilter;
  11. this.clearColor = clearColor;
  12. this.clearAlpha = ( clearAlpha !== undefined ) ? clearAlpha : 1;
  13. this.oldClearColor = new THREE.Color();
  14. this.oldClearAlpha = 1;
  15. this.enabled = true;
  16. this.clear = false;
  17. this.needsSwap = true;
  18. var msaaShader = THREE.MSAA4Shader;
  19. this.uniforms = THREE.UniformsUtils.clone( msaaShader.uniforms );
  20. this.materialMSAA = new THREE.ShaderMaterial({
  21. shaderID: msaaShader.shaderID,
  22. uniforms: this.uniforms,
  23. vertexShader: msaaShader.vertexShader,
  24. fragmentShader: msaaShader.fragmentShader,
  25. transparent: true,
  26. blending: THREE.CustomBlending,
  27. blendSrc: THREE.OneFactor,
  28. blendDst: THREE.OneFactor,
  29. blendEquation: THREE.AddEquation,
  30. depthTest: false,
  31. depthWrite: false
  32. });
  33. this.camera2 = new THREE.OrthographicCamera( -1, 1, 1, -1, 0, 1 );
  34. this.scene2 = new THREE.Scene();
  35. this.quad2 = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), null );
  36. this.scene2.add( this.quad2 );
  37. this.devicePixelRatio = 1;
  38. };
  39. THREE.MSAAPass.prototype = {
  40. dispose: function() {
  41. if( this.renderTargets ) {
  42. for( var i = 0; i < this.renderTargets.length; i ++ ) {
  43. this.renderTargets[i].dispose();
  44. }
  45. this.renderTargets = null;
  46. }
  47. },
  48. render: function ( renderer, writeBuffer, readBuffer, delta ) {
  49. if( ! this.renderTargets ) {
  50. this.renderTargets = [];
  51. for( var i = 0; i < 2; i ++ ) {
  52. this.renderTargets.push( new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params, "msaa.renderTarget" + i ) );
  53. }
  54. }
  55. var camera = ( this.camera || this.scene.camera );
  56. var currentSampleOffsets = THREE.MSAAPass.JitterVectors[ Math.max( 0, Math.min( this.currentSampleLevel, 5 ) ) ];
  57. if( ! currentSampleOffsets ) {
  58. renderer.render( this.scene, camera, this.renderTargets[0], true );
  59. this.uniforms[ "tBackground" ].value = readBuffer;
  60. this.uniforms[ "scale" ].value = 1.0;
  61. for( var k = 0; k < this.renderTargets.length; k ++ ) {
  62. this.uniforms[ "tSample" + k ].value = this.renderTargets[0];
  63. }
  64. this.quad2.material = this.materialMSAA;
  65. renderer.render( this.scene2, this.camera2, writeBuffer, true );
  66. return;
  67. }
  68. this.scene.overrideMaterial = null;
  69. this.oldClearColor.copy( renderer.getClearColor() );
  70. this.oldClearAlpha = renderer.getClearAlpha();
  71. renderer.setClearColor( new THREE.Color( 0, 0, 0 ), 0 );
  72. for( var j = 0; j < currentSampleOffsets.length; j += this.renderTargets.length ) {
  73. this.uniforms[ "tBackground" ].value = readBuffer;
  74. this.uniforms[ "scale" ].value = 1.0 / currentSampleOffsets.length;
  75. for( var k = 0; k < this.renderTargets.length; k ++ ) {
  76. this.uniforms[ "tSample" + k ].value = this.renderTargets[k];
  77. }
  78. this.quad2.material = this.materialMSAA;
  79. for( var k = 0; k < Math.min( currentSampleOffsets.length - j, this.renderTargets.length ); k ++ ) {
  80. var i = j + k;
  81. if( camera.setViewOffset ) {
  82. camera.setViewOffset( readBuffer.width, readBuffer.height, currentSampleOffsets[i].x, currentSampleOffsets[i].y, readBuffer.width, readBuffer.height );
  83. }
  84. renderer.render( this.scene, camera, this.renderTargets[k], true );
  85. }
  86. renderer.render( this.scene2, this.camera2, writeBuffer, j === 0 );
  87. }
  88. camera.fullWidth = undefined;
  89. camera.fullHeight = undefined;
  90. camera.x = undefined;
  91. camera.y = undefined;
  92. camera.width = undefined;
  93. camera.height = undefined;
  94. camera.updateProjectionMatrix();
  95. renderer.setClearColor( this.oldClearColor, this.oldClearAlpha );
  96. }
  97. };
  98. THREE.MSAAPass.normalizedJitterVectors = function() {
  99. var xfrm = new THREE.Matrix4().makeScale( 1 / 16.0, 1/ 16.0, 0 );
  100. return function( jitterVectors ) {
  101. var vectors2 = [];
  102. for( var i = 0; i < jitterVectors.length; i ++ ) {
  103. vectors2.push( new THREE.Vector3( jitterVectors[i][0], jitterVectors[i][0] ).applyMatrix4( xfrm ) );
  104. }
  105. return vectors2;
  106. }
  107. }(),
  108. THREE.MSAAPass.JitterVectors = [
  109. THREE.MSAAPass.normalizedJitterVectors( [
  110. [ 0, 0 ]
  111. ] ),
  112. THREE.MSAAPass.normalizedJitterVectors( [
  113. [ 4, 4 ],
  114. [ -4, -4 ]
  115. ] ),
  116. THREE.MSAAPass.normalizedJitterVectors( [
  117. [ -2, -6 ],
  118. [ 6, -2 ],
  119. [ -6, 2 ],
  120. [ 2, 6 ]
  121. ] ),
  122. THREE.MSAAPass.normalizedJitterVectors( [
  123. [ 1, -3 ],
  124. [ -1, 3 ],
  125. [ 5, 1 ],
  126. [ -3, -5 ],
  127. [ -5, 5 ],
  128. [ -7, -1 ],
  129. [ 3, 7 ],
  130. [ 7, -7 ]
  131. ] ),
  132. THREE.MSAAPass.normalizedJitterVectors( [
  133. [ 1, 1 ],
  134. [ -1, -3 ],
  135. [ -3, 2 ],
  136. [ 4, -1 ],
  137. [ -5, -2 ],
  138. [ 2, 5 ],
  139. [ 5, 3 ],
  140. [ 3, -5 ],
  141. [ -2, 6 ],
  142. [ 0, -7 ],
  143. [ -4, -6 ],
  144. [ -6, 4 ],
  145. [ -8, 0 ],
  146. [ 7, -4 ],
  147. [ 6, 7 ],
  148. [ -7, -8 ]
  149. ] ),
  150. THREE.MSAAPass.normalizedJitterVectors( [
  151. [ -4, -7 ],
  152. [ -7, -5 ],
  153. [ -3, -5 ],
  154. [ -5, -4 ],
  155. [ -1, -4 ],
  156. [ -2, -2 ],
  157. [ -6, -1 ],
  158. [ -4, 0 ],
  159. [ -7, 1 ],
  160. [ -1, 2 ],
  161. [ -6, 3 ],
  162. [ -3, 3 ],
  163. [ -7, 6 ],
  164. [ -3, 6 ],
  165. [ -5, 7 ],
  166. [ -1, 7 ],
  167. [ 5, -7 ],
  168. [ 1, -6 ],
  169. [ 6, -5 ],
  170. [ 4, -4 ],
  171. [ 2, -3 ],
  172. [ 7, -2 ],
  173. [ 1, -1 ],
  174. [ 4, -1 ],
  175. [ 2, 1 ],
  176. [ 6, 2 ],
  177. [ 0, 4 ],
  178. [ 4, 4 ],
  179. [ 2, 5 ],
  180. [ 7, 5 ],
  181. [ 5, 6 ],
  182. [ 3, 7 ]
  183. ] )
  184. ];