SSAARenderPass.js 5.4 KB

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