ManualMSAARenderPass.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, params ) {
  13. this.scene = scene;
  14. this.camera = camera;
  15. this.sampleLevel = 4; // specified as n, where the number of samples is 2^n, so sampleLevel = 4, is 2^4 samples, 16.
  16. this.params = params || { minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, format: THREE.RGBAFormat };
  17. this.params.minFilter = THREE.NearestFilter;
  18. this.params.maxFilter = THREE.NearestFilter;
  19. this.enabled = true;
  20. this.needsSwap = true;
  21. if ( THREE.CompositeShader === undefined ) {
  22. console.error( "THREE.ManualMSAARenderPass relies on THREE.CompositeShader" );
  23. }
  24. var compositeShader = THREE.CompositeShader;
  25. this.compositeUniforms = THREE.UniformsUtils.clone( compositeShader.uniforms );
  26. this.materialComposite = new THREE.ShaderMaterial( {
  27. uniforms: this.compositeUniforms,
  28. vertexShader: compositeShader.vertexShader,
  29. fragmentShader: compositeShader.fragmentShader,
  30. transparent: true,
  31. blending: THREE.CustomBlending,
  32. blendSrc: THREE.OneFactor,
  33. blendDst: THREE.OneFactor,
  34. blendEquation: THREE.AddEquation,
  35. depthTest: false,
  36. depthWrite: false
  37. } );
  38. this.camera2 = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  39. this.scene2 = new THREE.Scene();
  40. this.quad2 = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), this.materialComposite );
  41. this.scene2.add( this.quad2 );
  42. };
  43. THREE.ManualMSAARenderPass.prototype = {
  44. constructor: THREE.ManualMSAARenderPass,
  45. dispose: function() {
  46. if ( this.sampleRenderTarget ) {
  47. this.sampleRenderTarget.dispose();
  48. this.sampleRenderTarget = null;
  49. }
  50. },
  51. setSize: function ( width, height ) {
  52. this.sampleRenderTarget.setSize( width, height );
  53. },
  54. render: function ( renderer, writeBuffer, readBuffer, delta ) {
  55. var camera = ( this.camera || this.scene.camera );
  56. var jitterOffsets = THREE.ManualMSAARenderPass.JitterVectors[ Math.max( 0, Math.min( this.sampleLevel, 4 ) ) ];
  57. if( jitterOffsets.length === 1 ) {
  58. renderer.render( this.scene, camera, writeBuffer, true );
  59. return;
  60. }
  61. if ( ! this.sampleRenderTarget ) {
  62. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  63. }
  64. var autoClear = renderer.autoClear;
  65. renderer.autoClear = false;
  66. this.compositeUniforms[ "scale" ].value = 1.0 / ( jitterOffsets.length );
  67. this.compositeUniforms[ "tForeground" ].value = this.sampleRenderTarget;
  68. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  69. for ( var i = 0; i < jitterOffsets.length; i ++ ) {
  70. // only jitters perspective cameras. TODO: add support for jittering orthogonal cameras
  71. var jitterOffset = jitterOffsets[i];
  72. if ( camera.setViewOffset ) {
  73. camera.setViewOffset( readBuffer.width, readBuffer.height,
  74. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  75. readBuffer.width, readBuffer.height );
  76. }
  77. renderer.render( this.scene, this.camera, this.sampleRenderTarget, true );
  78. renderer.render( this.scene2, this.camera2, writeBuffer, ( i === 0 ) );
  79. }
  80. // reset jitter to nothing. TODO: add support for orthogonal cameras
  81. if ( camera.setViewOffset ) camera.setViewOffset( undefined, undefined, undefined, undefined, undefined, undefined );
  82. renderer.autoClear = true;
  83. }
  84. };
  85. // These jitter vectors are specified in integers because it is easier.
  86. // I am assuming a [-8,8) integer grid, but it needs to be mapped onto [-0.5,0.5)
  87. // before being used, thus these integers need to be scaled by 1/16.
  88. //
  89. // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
  90. THREE.ManualMSAARenderPass.JitterVectors = [
  91. [
  92. [ 0, 0 ]
  93. ],
  94. [
  95. [ 4, 4 ], [ - 4, - 4 ]
  96. ],
  97. [
  98. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  99. ],
  100. [
  101. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  102. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  103. ],
  104. [
  105. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  106. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  107. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  108. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  109. ]
  110. ];