TAARenderPass.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 = 0;
  20. this.accumulate = false;
  21. };
  22. THREE.TAARenderPass.prototype = Object.create( THREE.ManualMSAARenderPass.prototype );
  23. THREE.TAARenderPass.prototype.constructor = THREE.TAARenderPass;
  24. THREE.TAARenderPass.JitterVectors = THREE.ManualMSAARenderPass.JitterVectors;
  25. THREE.TAARenderPass.prototype.render = function ( renderer, writeBuffer, readBuffer, delta ) {
  26. if( ! this.accumulate ) {
  27. THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta );
  28. this.accumulateIndex = -1;
  29. return;
  30. }
  31. var jitterOffsets = THREE.TAARenderPass.JitterVectors[ 5 ];
  32. var camera = ( this.camera || this.scene.camera );
  33. if ( ! this.sampleRenderTarget ) {
  34. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  35. }
  36. if ( ! this.holdRenderTarget ) {
  37. this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  38. }
  39. if( this.accumulate && this.accumulateIndex === -1 ) {
  40. THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, delta );
  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.compositeUniforms[ "scale" ].value = sampleWeight;
  48. this.compositeUniforms[ "tForeground" ].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. // only jitters perspective cameras. TODO: add support for jittering orthogonal cameras
  54. var jitterOffset = jitterOffsets[j];
  55. if ( camera.setViewOffset ) {
  56. 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. // reset jitter to nothing. TODO: add support for orthogonal cameras
  66. if ( camera.setViewOffset ) camera.setViewOffset( undefined, undefined, undefined, undefined, undefined, undefined );
  67. }
  68. var accumulationWeight = this.accumulateIndex * sampleWeight;
  69. if( accumulationWeight > 0 ) {
  70. this.compositeUniforms[ "scale" ].value = 1.0;
  71. this.compositeUniforms[ "tForeground" ].value = this.sampleRenderTarget.texture;
  72. renderer.render( this.scene2, this.camera2, writeBuffer, true );
  73. }
  74. if( accumulationWeight < 1.0 ) {
  75. this.compositeUniforms[ "scale" ].value = 1.0 - accumulationWeight;
  76. this.compositeUniforms[ "tForeground" ].value = this.holdRenderTarget.texture;
  77. renderer.render( this.scene2, this.camera2, writeBuffer, ( accumulationWeight === 0 ) );
  78. }
  79. renderer.autoClear = autoClear;
  80. }