2
0

TAARenderPass.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, clearColor, clearAlpha ) {
  15. if ( THREE.SSAARenderPass === undefined ) {
  16. console.error( "THREE.TAARenderPass relies on THREE.SSAARenderPass" );
  17. }
  18. THREE.SSAARenderPass.call( this, scene, camera, clearColor, clearAlpha );
  19. this.sampleLevel = 0;
  20. this.accumulate = false;
  21. };
  22. THREE.TAARenderPass.JitterVectors = THREE.SSAARenderPass.JitterVectors;
  23. THREE.TAARenderPass.prototype = Object.assign( Object.create( THREE.SSAARenderPass.prototype ), {
  24. constructor: THREE.TAARenderPass,
  25. render: function ( renderer, writeBuffer, readBuffer, deltaTime ) {
  26. if ( ! this.accumulate ) {
  27. THREE.SSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, deltaTime );
  28. this.accumulateIndex = - 1;
  29. return;
  30. }
  31. var jitterOffsets = THREE.TAARenderPass.JitterVectors[ 5 ];
  32. if ( ! this.sampleRenderTarget ) {
  33. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  34. this.sampleRenderTarget.texture.name = "TAARenderPass.sample";
  35. }
  36. if ( ! this.holdRenderTarget ) {
  37. this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  38. this.holdRenderTarget.texture.name = "TAARenderPass.hold";
  39. }
  40. if ( this.accumulate && this.accumulateIndex === - 1 ) {
  41. THREE.SSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, deltaTime );
  42. this.accumulateIndex = 0;
  43. }
  44. var autoClear = renderer.autoClear;
  45. renderer.autoClear = false;
  46. var sampleWeight = 1.0 / ( jitterOffsets.length );
  47. if ( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  48. this.copyUniforms[ "opacity" ].value = sampleWeight;
  49. this.copyUniforms[ "tDiffuse" ].value = writeBuffer.texture;
  50. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  51. var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  52. for ( var i = 0; i < numSamplesPerFrame; i ++ ) {
  53. var j = this.accumulateIndex;
  54. var jitterOffset = jitterOffsets[ j ];
  55. if ( this.camera.setViewOffset ) {
  56. this.camera.setViewOffset( readBuffer.width, readBuffer.height,
  57. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  58. readBuffer.width, readBuffer.height );
  59. }
  60. renderer.setRenderTarget( writeBuffer );
  61. renderer.clear();
  62. renderer.render( this.scene, this.camera );
  63. renderer.setRenderTarget( this.sampleRenderTarget );
  64. if ( this.accumulateIndex === 0 ) renderer.clear();
  65. this.fsQuad.render( renderer );
  66. this.accumulateIndex ++;
  67. if ( this.accumulateIndex >= jitterOffsets.length ) break;
  68. }
  69. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  70. }
  71. var accumulationWeight = this.accumulateIndex * sampleWeight;
  72. if ( accumulationWeight > 0 ) {
  73. this.copyUniforms[ "opacity" ].value = 1.0;
  74. this.copyUniforms[ "tDiffuse" ].value = this.sampleRenderTarget.texture;
  75. renderer.setRenderTarget( writeBuffer );
  76. renderer.clear();
  77. this.fsQuad.render( renderer );
  78. }
  79. if ( accumulationWeight < 1.0 ) {
  80. this.copyUniforms[ "opacity" ].value = 1.0 - accumulationWeight;
  81. this.copyUniforms[ "tDiffuse" ].value = this.holdRenderTarget.texture;
  82. renderer.setRenderTarget( writeBuffer );
  83. if ( accumulationWeight === 0 ) renderer.clear();
  84. this.fsQuad.render( renderer );
  85. }
  86. renderer.autoClear = autoClear;
  87. }
  88. } );