ManualMSAARenderPass.js 4.1 KB

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