MSAAPass.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * @author bhouston / http://clara.io/
  3. *
  4. * NOTE: Accumulating a lot of samples with a 8-bit-per-channel RGB/RGBA buffer will
  5. * lead to discretization effects. For accurate sample accumulation use a floating
  6. * point buffer.
  7. *
  8. * It is also possible to reduce the discretization effects by having more buffers
  9. * and doing less combines. So render to 4 buffers and combine them 4 times for
  10. * 16 samples and you'll get 4x less discretization artifacts and then current
  11. * approach.
  12. *
  13. */
  14. THREE.MSAAPass = function ( scene, camera, params ) {
  15. this.scene = scene;
  16. this.camera = camera;
  17. this.sampleLevel = 4; // specified as n, where the number of samples is 2^n, so sampleLevel = 4, is 2^4 samples, 16.
  18. this.params = params || { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
  19. this.params.minFilter = THREE.NearestFilter;
  20. this.params.maxFilter = THREE.NearestFilter;
  21. console.log( 'this.params', this.params );
  22. this.enabled = true;
  23. this.needsSwap = true;
  24. if ( THREE.CompositeShader === undefined ) {
  25. console.error( "THREE.MSAAPass relies on THREE.CompositeShader" );
  26. }
  27. var compositeShader = THREE.CompositeShader;
  28. this.uniforms = THREE.UniformsUtils.clone( compositeShader.uniforms );
  29. this.materialComposite = new THREE.ShaderMaterial( {
  30. uniforms: this.uniforms,
  31. vertexShader: compositeShader.vertexShader,
  32. fragmentShader: compositeShader.fragmentShader,
  33. transparent: true,
  34. blending: THREE.CustomBlending,
  35. blendSrc: THREE.OneFactor,
  36. blendDst: THREE.OneFactor,
  37. blendEquation: THREE.AddEquation,
  38. depthTest: false,
  39. depthWrite: false
  40. } );
  41. this.camera2 = new THREE.OrthographicCamera( -1, 1, 1, -1, 0, 1 );
  42. this.scene2 = new THREE.Scene();
  43. this.quad2 = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), this.materialComposite );
  44. this.scene2.add( this.quad2 );
  45. };
  46. THREE.MSAAPass.prototype = {
  47. dispose: function() {
  48. if( this.sampleRenderTarget ) {
  49. this.sampleRenderTarget.dispose();
  50. this.sampleRenderTarget = null;
  51. }
  52. },
  53. render: function ( renderer, writeBuffer, readBuffer, delta ) {
  54. if( ! this.sampleRenderTarget ) {
  55. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params, "msaa.renderTarget0" );
  56. }
  57. var camera = ( this.camera || this.scene.camera );
  58. var jitterOffsets = THREE.MSAAPass.JitterVectors[ Math.max( 0, Math.min( this.sampleLevel, 5 ) ) ];
  59. this.uniforms[ "tForeground" ].value = this.sampleRenderTarget;
  60. this.uniforms[ "scale" ].value = 1.0 / jitterOffsets.length;
  61. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  62. for( var i = 0; i < jitterOffsets.length; i ++ ) {
  63. // only jitters perspective cameras. TODO: add support for jittering orthogonal cameras
  64. if( camera.setViewOffset ) camera.setViewOffset( readBuffer.width, readBuffer.height, jitterOffsets[i].x, jitterOffsets[i].y, readBuffer.width, readBuffer.height );
  65. renderer.render( this.scene, camera, this.sampleRenderTarget, true );
  66. // clear on the first render, accumulate the others
  67. var autoClear = renderer.autoClear;
  68. renderer.autoClear = false;
  69. renderer.render( this.scene2, this.camera2, writeBuffer, i === 0 );
  70. renderer.autoClear = true;
  71. }
  72. // reset jitter to nothing. TODO: add support for orthogonal cameras
  73. if( camera.setViewOffset ) camera.setViewOffset( undefined, undefined, undefined, undefined, undefined, undefined );
  74. }
  75. };
  76. THREE.MSAAPass.normalizedJitterOffsets = function( jitterVectors ) {
  77. var scaledJitterOffsets = [];
  78. for( var i = 0; i < jitterVectors.length; i ++ ) {
  79. scaledJitterOffsets.push( new THREE.Vector2( jitterVectors[i][0], jitterVectors[i][1] ).multiplyScalar( 1.0 / 16.0 ) );
  80. }
  81. return scaledJitterOffsets;
  82. },
  83. // These jitter vectors are specified in integers because it is easier.
  84. // I am assuming a [-8,8] integer grid, but it needs to be mapped onto [-0.5,0.5]
  85. // before being used, thus these integers need to be scaled by 1/16.
  86. //
  87. // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
  88. THREE.MSAAPass.JitterVectors = [
  89. THREE.MSAAPass.normalizedJitterOffsets( [
  90. [ 0, 0 ]
  91. ] ),
  92. THREE.MSAAPass.normalizedJitterOffsets( [
  93. [ 4, 4 ],
  94. [ -4, -4 ]
  95. ] ),
  96. THREE.MSAAPass.normalizedJitterOffsets( [
  97. [ -2, -6 ],
  98. [ 6, -2 ],
  99. [ -6, 2 ],
  100. [ 2, 6 ]
  101. ] ),
  102. THREE.MSAAPass.normalizedJitterOffsets( [
  103. [ 1, -3 ],
  104. [ -1, 3 ],
  105. [ 5, 1 ],
  106. [ -3, -5 ],
  107. [ -5, 5 ],
  108. [ -7, -1 ],
  109. [ 3, 7 ],
  110. [ 7, -7 ]
  111. ] ),
  112. THREE.MSAAPass.normalizedJitterOffsets( [
  113. [ 1, 1 ],
  114. [ -1, -3 ],
  115. [ -3, 2 ],
  116. [ 4, -1 ],
  117. [ -5, -2 ],
  118. [ 2, 5 ],
  119. [ 5, 3 ],
  120. [ 3, -5 ],
  121. [ -2, 6 ],
  122. [ 0, -7 ],
  123. [ -4, -6 ],
  124. [ -6, 4 ],
  125. [ -8, 0 ],
  126. [ 7, -4 ],
  127. [ 6, 7 ],
  128. [ -7, -8 ]
  129. ] ),
  130. THREE.MSAAPass.normalizedJitterOffsets( [
  131. [ -4, -7 ],
  132. [ -7, -5 ],
  133. [ -3, -5 ],
  134. [ -5, -4 ],
  135. [ -1, -4 ],
  136. [ -2, -2 ],
  137. [ -6, -1 ],
  138. [ -4, 0 ],
  139. [ -7, 1 ],
  140. [ -1, 2 ],
  141. [ -6, 3 ],
  142. [ -3, 3 ],
  143. [ -7, 6 ],
  144. [ -3, 6 ],
  145. [ -5, 7 ],
  146. [ -1, 7 ],
  147. [ 5, -7 ],
  148. [ 1, -6 ],
  149. [ 6, -5 ],
  150. [ 4, -4 ],
  151. [ 2, -3 ],
  152. [ 7, -2 ],
  153. [ 1, -1 ],
  154. [ 4, -1 ],
  155. [ 2, 1 ],
  156. [ 6, 2 ],
  157. [ 0, 4 ],
  158. [ 4, 4 ],
  159. [ 2, 5 ],
  160. [ 7, 5 ],
  161. [ 5, 6 ],
  162. [ 3, 7 ]
  163. ] )
  164. ];