ManualMSAARenderPass.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /**
  2. *
  3. * Manual Multi-Sample Anti-Aliasing Render Pass
  4. *
  5. * @author bhouston / http://clara.io/
  6. *
  7. * This manual approach to MSAA re-renders the scene ones for each sample with camera jitter and accumulates the results.
  8. *
  9. * References: https://en.wikipedia.org/wiki/Multisample_anti-aliasing
  10. *
  11. */
  12. THREE.ManualMSAARenderPass = function ( scene, camera, clearColor, clearAlpha ) {
  13. THREE.Pass.call( this );
  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;
  18. // as we need to clear the buffer in this pass, clearColor must be set to something, defaults to black.
  19. this.clearColor = ( clearColor !== undefined ) ? clearColor : 0x000000;
  20. this.clearAlpha = ( clearAlpha !== undefined ) ? clearAlpha : 1;
  21. if ( THREE.CopyShader === undefined ) console.error( "THREE.ManualMSAARenderPass relies on THREE.CopyShader" );
  22. var 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.camera2 = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  35. this.scene2 = new THREE.Scene();
  36. this.quad2 = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), this.copyMaterial );
  37. this.scene2.add( this.quad2 );
  38. };
  39. THREE.ManualMSAARenderPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
  40. constructor: THREE.ManualMSAARenderPass,
  41. dispose: function() {
  42. if ( this.sampleRenderTarget ) {
  43. this.sampleRenderTarget.dispose();
  44. this.sampleRenderTarget = null;
  45. }
  46. },
  47. setSize: function ( width, height ) {
  48. if ( this.sampleRenderTarget ) this.sampleRenderTarget.setSize( width, height );
  49. },
  50. render: function ( renderer, writeBuffer, readBuffer, delta, maskActive ) {
  51. if ( ! this.sampleRenderTarget ) {
  52. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height,
  53. { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat } );
  54. }
  55. var jitterOffsets = THREE.ManualMSAARenderPass.JitterVectors[ Math.max( 0, Math.min( this.sampleLevel, 5 ) ) ];
  56. var autoClear = renderer.autoClear;
  57. renderer.autoClear = false;
  58. var oldClearColorHex = renderer.getClearColor().getHex(), oldClearAlpha = renderer.getClearAlpha();
  59. var baseSampleWeight = 1.0 / jitterOffsets.length;
  60. var roundingRange = 1 / 32;
  61. this.copyUniforms[ "tDiffuse" ].value = this.sampleRenderTarget.texture;
  62. var width = readBuffer.width, height = readBuffer.height;
  63. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  64. for ( var i = 0; i < jitterOffsets.length; i ++ ) {
  65. var jitterOffset = jitterOffsets[i];
  66. if ( this.camera.setViewOffset ) {
  67. this.camera.setViewOffset( width, height,
  68. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  69. width, height );
  70. }
  71. var sampleWeight = baseSampleWeight;
  72. if( this.unbiased ) {
  73. // the theory is that equal weights for each sample lead to an accumulation of rounding errors.
  74. // The following equation varies the sampleWeight per sample so that it is uniformly distributed
  75. // across a range of values whose rounding errors cancel each other out.
  76. var uniformCenteredDistribution = ( -0.5 + ( i + 0.5 ) / jitterOffsets.length );
  77. sampleWeight += roundingRange * uniformCenteredDistribution;
  78. }
  79. this.copyUniforms[ "opacity" ].value = sampleWeight;
  80. renderer.setClearColor( this.clearColor, this.clearAlpha );
  81. renderer.render( this.scene, this.camera, this.sampleRenderTarget, true );
  82. if (i === 0) {
  83. renderer.setClearColor( 0x000000, 0.0 );
  84. }
  85. renderer.render( this.scene2, this.camera2, this.renderToScreen ? null : writeBuffer, (i === 0) );
  86. }
  87. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  88. renderer.autoClear = autoClear;
  89. renderer.setClearColor( oldClearColorHex, oldClearAlpha );
  90. }
  91. } );
  92. // These jitter vectors are specified in integers because it is easier.
  93. // I am assuming a [-8,8) integer grid, but it needs to be mapped onto [-0.5,0.5)
  94. // before being used, thus these integers need to be scaled by 1/16.
  95. //
  96. // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
  97. THREE.ManualMSAARenderPass.JitterVectors = [
  98. [
  99. [ 0, 0 ]
  100. ],
  101. [
  102. [ 4, 4 ], [ - 4, - 4 ]
  103. ],
  104. [
  105. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  106. ],
  107. [
  108. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  109. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  110. ],
  111. [
  112. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  113. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  114. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  115. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  116. ],
  117. [
  118. [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ],
  119. [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ],
  120. [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ],
  121. [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ],
  122. [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ],
  123. [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ],
  124. [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ],
  125. [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ]
  126. ]
  127. ];