ManualMSAARenderPass.js 4.4 KB

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