TAARenderPass.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. console.warn( "THREE.TAARenderPass: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
  2. /**
  3. *
  4. * Temporal Anti-Aliasing Render Pass
  5. *
  6. * 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.
  7. *
  8. * References:
  9. *
  10. * TODO: Add support for motion vector pas so that accumulation of samples across frames can occur on dynamics scenes.
  11. *
  12. */
  13. THREE.TAARenderPass = function ( scene, camera, clearColor, clearAlpha ) {
  14. if ( THREE.SSAARenderPass === undefined ) {
  15. console.error( "THREE.TAARenderPass relies on THREE.SSAARenderPass" );
  16. }
  17. THREE.SSAARenderPass.call( this, scene, camera, clearColor, clearAlpha );
  18. this.sampleLevel = 0;
  19. this.accumulate = false;
  20. };
  21. THREE.TAARenderPass.JitterVectors = THREE.SSAARenderPass.JitterVectors;
  22. THREE.TAARenderPass.prototype = Object.assign( Object.create( THREE.SSAARenderPass.prototype ), {
  23. constructor: THREE.TAARenderPass,
  24. render: function ( renderer, writeBuffer, readBuffer, deltaTime ) {
  25. if ( ! this.accumulate ) {
  26. THREE.SSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, deltaTime );
  27. this.accumulateIndex = - 1;
  28. return;
  29. }
  30. var jitterOffsets = THREE.TAARenderPass.JitterVectors[ 5 ];
  31. if ( ! this.sampleRenderTarget ) {
  32. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  33. this.sampleRenderTarget.texture.name = "TAARenderPass.sample";
  34. }
  35. if ( ! this.holdRenderTarget ) {
  36. this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  37. this.holdRenderTarget.texture.name = "TAARenderPass.hold";
  38. }
  39. if ( this.accumulate && this.accumulateIndex === - 1 ) {
  40. THREE.SSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, deltaTime );
  41. this.accumulateIndex = 0;
  42. }
  43. var autoClear = renderer.autoClear;
  44. renderer.autoClear = false;
  45. var sampleWeight = 1.0 / ( jitterOffsets.length );
  46. if ( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  47. this.copyUniforms[ "opacity" ].value = sampleWeight;
  48. this.copyUniforms[ "tDiffuse" ].value = writeBuffer.texture;
  49. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  50. var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  51. for ( var i = 0; i < numSamplesPerFrame; i ++ ) {
  52. var j = this.accumulateIndex;
  53. var jitterOffset = jitterOffsets[ j ];
  54. if ( this.camera.setViewOffset ) {
  55. this.camera.setViewOffset( readBuffer.width, readBuffer.height,
  56. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  57. readBuffer.width, readBuffer.height );
  58. }
  59. renderer.setRenderTarget( writeBuffer );
  60. renderer.clear();
  61. renderer.render( this.scene, this.camera );
  62. renderer.setRenderTarget( this.sampleRenderTarget );
  63. if ( this.accumulateIndex === 0 ) renderer.clear();
  64. this.fsQuad.render( renderer );
  65. this.accumulateIndex ++;
  66. if ( this.accumulateIndex >= jitterOffsets.length ) break;
  67. }
  68. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  69. }
  70. var accumulationWeight = this.accumulateIndex * sampleWeight;
  71. if ( accumulationWeight > 0 ) {
  72. this.copyUniforms[ "opacity" ].value = 1.0;
  73. this.copyUniforms[ "tDiffuse" ].value = this.sampleRenderTarget.texture;
  74. renderer.setRenderTarget( writeBuffer );
  75. renderer.clear();
  76. this.fsQuad.render( renderer );
  77. }
  78. if ( accumulationWeight < 1.0 ) {
  79. this.copyUniforms[ "opacity" ].value = 1.0 - accumulationWeight;
  80. this.copyUniforms[ "tDiffuse" ].value = this.holdRenderTarget.texture;
  81. renderer.setRenderTarget( writeBuffer );
  82. if ( accumulationWeight === 0 ) renderer.clear();
  83. this.fsQuad.render( renderer );
  84. }
  85. renderer.autoClear = autoClear;
  86. }
  87. } );