ManualMSAARenderPass.js 4.8 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, 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. blendSrcAlpha: THREE.OneFactor,
  35. blendDstAlpha: THREE.OneFactor,
  36. blendEquation: THREE.AddEquation,
  37. depthTest: false,
  38. depthWrite: false
  39. } );
  40. this.camera2 = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  41. this.scene2 = new THREE.Scene();
  42. this.quad2 = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), this.materialComposite );
  43. this.scene2.add( this.quad2 );
  44. };
  45. THREE.ManualMSAARenderPass.prototype = {
  46. constructor: THREE.ManualMSAARenderPass,
  47. dispose: function() {
  48. if ( this.sampleRenderTarget ) {
  49. this.sampleRenderTarget.dispose();
  50. this.sampleRenderTarget = null;
  51. }
  52. },
  53. setSize: function ( width, height ) {
  54. this.sampleRenderTarget.setSize( width, height );
  55. },
  56. render: function ( renderer, writeBuffer, readBuffer, delta ) {
  57. var camera = ( this.camera || this.scene.camera );
  58. var jitterOffsets = THREE.ManualMSAARenderPass.JitterVectors[ Math.max( 0, Math.min( this.sampleLevel, 5 ) ) ];
  59. if( jitterOffsets.length === 1 ) {
  60. renderer.render( this.scene, camera, writeBuffer, true );
  61. return;
  62. }
  63. if ( ! this.sampleRenderTarget ) {
  64. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  65. }
  66. var autoClear = renderer.autoClear;
  67. renderer.autoClear = false;
  68. this.compositeUniforms[ "scale" ].value = 1.0 / ( jitterOffsets.length );
  69. this.compositeUniforms[ "tForeground" ].value = this.sampleRenderTarget;
  70. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  71. for ( var i = 0; i < jitterOffsets.length; i ++ ) {
  72. // only jitters perspective cameras. TODO: add support for jittering orthogonal cameras
  73. var jitterOffset = jitterOffsets[i];
  74. if ( camera.setViewOffset ) {
  75. camera.setViewOffset( readBuffer.width, readBuffer.height,
  76. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  77. readBuffer.width, readBuffer.height );
  78. }
  79. renderer.render( this.scene, this.camera, this.sampleRenderTarget, true );
  80. renderer.render( this.scene2, this.camera2, writeBuffer, ( i === 0 ) );
  81. }
  82. // reset jitter to nothing. TODO: add support for orthogonal cameras
  83. if ( camera.setViewOffset ) camera.setViewOffset( undefined, undefined, undefined, undefined, undefined, undefined );
  84. renderer.autoClear = true;
  85. }
  86. };
  87. // These jitter vectors are specified in integers because it is easier.
  88. // I am assuming a [-8,8) integer grid, but it needs to be mapped onto [-0.5,0.5)
  89. // before being used, thus these integers need to be scaled by 1/16.
  90. //
  91. // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
  92. THREE.ManualMSAARenderPass.JitterVectors = [
  93. [
  94. [ 0, 0 ]
  95. ],
  96. [
  97. [ 4, 4 ], [ - 4, - 4 ]
  98. ],
  99. [
  100. [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ]
  101. ],
  102. [
  103. [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ],
  104. [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ]
  105. ],
  106. [
  107. [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ],
  108. [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ],
  109. [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ],
  110. [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ]
  111. ],
  112. [
  113. [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ],
  114. [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ],
  115. [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ],
  116. [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ],
  117. [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ],
  118. [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ],
  119. [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ],
  120. [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ]
  121. ]
  122. ];