SSAARenderPass.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. *
  3. * Supersample Anti-Aliasing Render Pass
  4. *
  5. * This manual approach to SSAA re-renders the scene ones for each sample with camera jitter and accumulates the results.
  6. *
  7. * References: https://en.wikipedia.org/wiki/Supersampling
  8. *
  9. */
  10. THREE.SSAARenderPass = function ( scene, camera, clearColor, clearAlpha ) {
  11. THREE.Pass.call( this );
  12. this.scene = scene;
  13. this.camera = camera;
  14. this.sampleLevel = 4; // specified as n, where the number of samples is 2^n, so sampleLevel = 4, is 2^4 samples, 16.
  15. this.unbiased = true;
  16. // as we need to clear the buffer in this pass, clearColor must be set to something, defaults to black.
  17. this.clearColor = ( clearColor !== undefined ) ? clearColor : 0x000000;
  18. this.clearAlpha = ( clearAlpha !== undefined ) ? clearAlpha : 0;
  19. this._oldClearColor = new THREE.Color();
  20. if ( THREE.CopyShader === undefined ) console.error( 'THREE.SSAARenderPass relies on THREE.CopyShader' );
  21. var copyShader = THREE.CopyShader;
  22. this.copyUniforms = THREE.UniformsUtils.clone( copyShader.uniforms );
  23. this.copyMaterial = new THREE.ShaderMaterial( {
  24. uniforms: this.copyUniforms,
  25. vertexShader: copyShader.vertexShader,
  26. fragmentShader: copyShader.fragmentShader,
  27. premultipliedAlpha: true,
  28. transparent: true,
  29. blending: THREE.AdditiveBlending,
  30. depthTest: false,
  31. depthWrite: false
  32. } );
  33. this.fsQuad = new THREE.Pass.FullScreenQuad( this.copyMaterial );
  34. };
  35. THREE.SSAARenderPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  36. constructor: THREE.SSAARenderPass,
  37. dispose: function () {
  38. if ( this.sampleRenderTarget ) {
  39. this.sampleRenderTarget.dispose();
  40. this.sampleRenderTarget = null;
  41. }
  42. },
  43. setSize: function ( width, height ) {
  44. if ( this.sampleRenderTarget ) this.sampleRenderTarget.setSize( width, height );
  45. },
  46. render: function ( renderer, writeBuffer, readBuffer ) {
  47. if ( ! this.sampleRenderTarget ) {
  48. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat } );
  49. this.sampleRenderTarget.texture.name = 'SSAARenderPass.sample';
  50. }
  51. var jitterOffsets = THREE.SSAARenderPass.JitterVectors[ Math.max( 0, Math.min( this.sampleLevel, 5 ) ) ];
  52. var autoClear = renderer.autoClear;
  53. renderer.autoClear = false;
  54. renderer.getClearColor( this._oldClearColor );
  55. var oldClearAlpha = renderer.getClearAlpha();
  56. var baseSampleWeight = 1.0 / jitterOffsets.length;
  57. var roundingRange = 1 / 32;
  58. this.copyUniforms[ 'tDiffuse' ].value = this.sampleRenderTarget.texture;
  59. var width = readBuffer.width, height = readBuffer.height;
  60. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  61. for ( var i = 0; i < jitterOffsets.length; i ++ ) {
  62. var jitterOffset = jitterOffsets[ i ];
  63. if ( this.camera.setViewOffset ) {
  64. this.camera.setViewOffset( width, height,
  65. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  66. width, height );
  67. }
  68. var sampleWeight = baseSampleWeight;
  69. if ( this.unbiased ) {
  70. // the theory is that equal weights for each sample lead to an accumulation of rounding errors.
  71. // The following equation varies the sampleWeight per sample so that it is uniformly distributed
  72. // across a range of values whose rounding errors cancel each other out.
  73. var uniformCenteredDistribution = ( - 0.5 + ( i + 0.5 ) / jitterOffsets.length );
  74. sampleWeight += roundingRange * uniformCenteredDistribution;
  75. }
  76. this.copyUniforms[ 'opacity' ].value = sampleWeight;
  77. renderer.setClearColor( this.clearColor, this.clearAlpha );
  78. renderer.setRenderTarget( this.sampleRenderTarget );
  79. renderer.clear();
  80. renderer.render( this.scene, this.camera );
  81. renderer.setRenderTarget( this.renderToScreen ? null : writeBuffer );
  82. if ( i === 0 ) {
  83. renderer.setClearColor( 0x000000, 0.0 );
  84. renderer.clear();
  85. }
  86. this.fsQuad.render( renderer );
  87. }
  88. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  89. renderer.autoClear = autoClear;
  90. renderer.setClearColor( this._oldClearColor, oldClearAlpha );
  91. }
  92. } );
  93. // These jitter vectors are specified in integers because it is easier.
  94. // I am assuming a [-8,8) integer grid, but it needs to be mapped onto [-0.5,0.5)
  95. // before being used, thus these integers need to be scaled by 1/16.
  96. //
  97. // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
  98. THREE.SSAARenderPass.JitterVectors = [
  99. [
  100. [ 0, 0 ]
  101. ],
  102. [
  103. [ 4, 4 ], [ - 4, - 4 ]
  104. ],
  105. [
  106. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  107. ],
  108. [
  109. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  110. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  111. ],
  112. [
  113. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  114. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  115. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  116. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  117. ],
  118. [
  119. [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ],
  120. [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ],
  121. [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ],
  122. [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ],
  123. [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ],
  124. [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ],
  125. [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ],
  126. [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ]
  127. ]
  128. ];