TAARenderPass.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.SSAARenderPass === undefined ) {
  16. console.error( "THREE.TAARenderPass relies on THREE.SSAARenderPass" );
  17. }
  18. THREE.SSAARenderPass.call( this, scene, camera, params );
  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, delta ) {
  26. if ( ! this.accumulate ) {
  27. THREE.SSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta );
  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, delta );
  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.render( this.scene, this.camera, writeBuffer, true );
  61. renderer.render( this.scene2, this.camera2, this.sampleRenderTarget, ( this.accumulateIndex === 0 ) );
  62. this.accumulateIndex ++;
  63. if ( this.accumulateIndex >= jitterOffsets.length ) break;
  64. }
  65. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  66. }
  67. var accumulationWeight = this.accumulateIndex * sampleWeight;
  68. if ( accumulationWeight > 0 ) {
  69. this.copyUniforms[ "opacity" ].value = 1.0;
  70. this.copyUniforms[ "tDiffuse" ].value = this.sampleRenderTarget.texture;
  71. renderer.render( this.scene2, this.camera2, writeBuffer, true );
  72. }
  73. if ( accumulationWeight < 1.0 ) {
  74. this.copyUniforms[ "opacity" ].value = 1.0 - accumulationWeight;
  75. this.copyUniforms[ "tDiffuse" ].value = this.holdRenderTarget.texture;
  76. renderer.render( this.scene2, this.camera2, writeBuffer, ( accumulationWeight === 0 ) );
  77. }
  78. renderer.autoClear = autoClear;
  79. }
  80. } );