TAARenderPass.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. *
  3. * Temporal Anti-Aliasing Render Pass
  4. *
  5. * @author bhouston / http://clara.io/
  6. *
  7. * When there is no motion in the scene, the TAA render pass accumulates jittered camera samples across frames to create a high quality anti-aliased result.
  8. *
  9. * References:
  10. *
  11. * TODO: Add support for motion vector pas so that accumulation of samples across frames can occur on dynamics scenes.
  12. *
  13. */
  14. THREE.TAARenderPass = function ( scene, camera, params ) {
  15. if ( THREE.ManualMSAARenderPass === undefined ) {
  16. console.error( "THREE.TAARenderPass relies on THREE.ManualMSAARenderPass" );
  17. }
  18. THREE.ManualMSAARenderPass.call( this, scene, camera, params );
  19. this.sampleLevel = 1;
  20. this.accumulate = false;
  21. if ( THREE.CompositeShader === undefined ) {
  22. console.error( "THREE.TAARenderPass relies on THREE.CompositeShader" );
  23. }
  24. var accumulateShader = THREE.CompositeShader;
  25. this.accumulateUniforms = THREE.UniformsUtils.clone( accumulateShader.uniforms );
  26. this.materialAccumulate = new THREE.ShaderMaterial( {
  27. uniforms: this.accumulateUniforms,
  28. vertexShader: accumulateShader.vertexShader,
  29. fragmentShader: accumulateShader.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.camera3 = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  39. this.scene3 = new THREE.Scene();
  40. this.quad3 = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), this.materialAccumulate );
  41. this.scene3.add( this.quad3 );
  42. };
  43. THREE.TAARenderPass.prototype = Object.create( THREE.ManualMSAARenderPass.prototype );
  44. THREE.TAARenderPass.prototype.constructor = THREE.TAARenderPass;
  45. THREE.TAARenderPass.JitterVectors = THREE.ManualMSAARenderPass.JitterVectors;
  46. THREE.TAARenderPass.prototype.render = function ( renderer, writeBuffer, readBuffer, delta ) {
  47. if( ! this.accumulate ) {
  48. THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta );
  49. this.accumulateIndex = -1;
  50. return;
  51. }
  52. var jitterOffsets = THREE.TAARenderPass.JitterVectors[ 4 ];
  53. var camera = ( this.camera || this.scene.camera );
  54. if ( ! this.sampleRenderTarget ) {
  55. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  56. }
  57. if ( ! this.holdRenderTarget ) {
  58. this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  59. }
  60. if( this.accumulate && this.accumulateIndex === -1 ) {
  61. THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, delta );
  62. this.accumulateIndex = 0;
  63. }
  64. var sampleWeight = 1.0 / ( jitterOffsets.length );
  65. if( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  66. var autoClear = renderer.autoClear;
  67. renderer.autoClear = false;
  68. this.accumulateUniforms[ "scale" ].value = sampleWeight;
  69. this.accumulateUniforms[ "tForeground" ].value = writeBuffer;
  70. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  71. var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  72. for ( var i = 0; i < numSamplesPerFrame; i ++ ) {
  73. var j = this.accumulateIndex;
  74. // only jitters perspective cameras. TODO: add support for jittering orthogonal cameras
  75. var jitterOffset = jitterOffsets[j];
  76. if ( camera.setViewOffset ) {
  77. camera.setViewOffset( readBuffer.width, readBuffer.height,
  78. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  79. readBuffer.width, readBuffer.height );
  80. }
  81. renderer.render( this.scene, this.camera, writeBuffer, true );
  82. renderer.render( this.scene3, this.camera3, this.sampleRenderTarget, ( this.accumulateIndex == 0 ) );
  83. this.accumulateIndex ++;
  84. if( this.accumulateIndex >= jitterOffsets.length ) break;
  85. }
  86. // reset jitter to nothing. TODO: add support for orthogonal cameras
  87. if ( camera.setViewOffset ) camera.setViewOffset( undefined, undefined, undefined, undefined, undefined, undefined );
  88. renderer.autoClear = true;
  89. }
  90. // TODO: Add smooth transition from holdRenderTarget to sampleRenderTarget either while the sampleRenderTarget is being created or once it is done.
  91. // This should be possible with BlendShader I think.
  92. this.accumulateUniforms[ "scale" ].value = 1.0;
  93. this.accumulateUniforms[ "tForeground" ].value = ( this.accumulateIndex < jitterOffsets.length ) ? this.holdRenderTarget : this.sampleRenderTarget;
  94. renderer.render( this.scene3, this.camera3, writeBuffer );
  95. }