TAARenderPass.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. blendSrcAlpha: THREE.OneFactor,
  35. blendDstAlpha: THREE.OneFactor,
  36. blendEquation: THREE.AddEquation,
  37. depthTest: false,
  38. depthWrite: false
  39. } );
  40. this.camera3 = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  41. this.scene3 = new THREE.Scene();
  42. this.quad3 = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), this.materialAccumulate );
  43. this.scene3.add( this.quad3 );
  44. };
  45. THREE.TAARenderPass.prototype = Object.create( THREE.ManualMSAARenderPass.prototype );
  46. THREE.TAARenderPass.prototype.constructor = THREE.TAARenderPass;
  47. THREE.TAARenderPass.JitterVectors = THREE.ManualMSAARenderPass.JitterVectors;
  48. THREE.TAARenderPass.prototype.render = function ( renderer, writeBuffer, readBuffer, delta ) {
  49. if( ! this.accumulate ) {
  50. THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta );
  51. this.accumulateIndex = -1;
  52. return;
  53. }
  54. var jitterOffsets = THREE.TAARenderPass.JitterVectors[ 4 ];
  55. var camera = ( this.camera || this.scene.camera );
  56. if ( ! this.sampleRenderTarget ) {
  57. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  58. }
  59. if ( ! this.holdRenderTarget ) {
  60. this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  61. }
  62. if( this.accumulate && this.accumulateIndex === -1 ) {
  63. THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, delta );
  64. this.accumulateIndex = 0;
  65. }
  66. var sampleWeight = 1.0 / ( jitterOffsets.length );
  67. if( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  68. var autoClear = renderer.autoClear;
  69. renderer.autoClear = false;
  70. this.accumulateUniforms[ "scale" ].value = sampleWeight;
  71. this.accumulateUniforms[ "tForeground" ].value = writeBuffer;
  72. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  73. var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  74. for ( var i = 0; i < numSamplesPerFrame; i ++ ) {
  75. var j = this.accumulateIndex;
  76. // only jitters perspective cameras. TODO: add support for jittering orthogonal cameras
  77. var jitterOffset = jitterOffsets[j];
  78. if ( camera.setViewOffset ) {
  79. camera.setViewOffset( readBuffer.width, readBuffer.height,
  80. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  81. readBuffer.width, readBuffer.height );
  82. }
  83. renderer.render( this.scene, this.camera, writeBuffer, true );
  84. renderer.render( this.scene3, this.camera3, this.sampleRenderTarget, ( this.accumulateIndex == 0 ) );
  85. this.accumulateIndex ++;
  86. if( this.accumulateIndex >= jitterOffsets.length ) break;
  87. }
  88. // reset jitter to nothing. TODO: add support for orthogonal cameras
  89. if ( camera.setViewOffset ) camera.setViewOffset( undefined, undefined, undefined, undefined, undefined, undefined );
  90. renderer.autoClear = true;
  91. }
  92. // TODO: Add smooth transition from holdRenderTarget to sampleRenderTarget either while the sampleRenderTarget is being created or once it is done.
  93. // This should be possible with BlendShader I think.
  94. this.accumulateUniforms[ "scale" ].value = 1.0;
  95. this.accumulateUniforms[ "tForeground" ].value = ( this.accumulateIndex < jitterOffsets.length ) ? this.holdRenderTarget : this.sampleRenderTarget;
  96. renderer.render( this.scene3, this.camera3, writeBuffer );
  97. }