SSAARenderPass.js 5.5 KB

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