SSAARenderPass.js 6.0 KB

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