SSAARenderPass.js 5.9 KB

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