TAARenderPass.js 3.9 KB

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