SSAARenderPass.js 6.1 KB

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