TAARenderPass.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }
  35. if ( ! this.holdRenderTarget ) {
  36. this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  37. }
  38. if( this.accumulate && this.accumulateIndex === -1 ) {
  39. THREE.SSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, delta );
  40. this.accumulateIndex = 0;
  41. }
  42. var autoClear = renderer.autoClear;
  43. renderer.autoClear = false;
  44. var sampleWeight = 1.0 / ( jitterOffsets.length );
  45. if( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  46. this.copyUniforms[ "opacity" ].value = sampleWeight;
  47. this.copyUniforms[ "tDiffuse" ].value = writeBuffer.texture;
  48. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  49. var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  50. for ( var i = 0; i < numSamplesPerFrame; i ++ ) {
  51. var j = this.accumulateIndex;
  52. var jitterOffset = jitterOffsets[j];
  53. if ( this.camera.setViewOffset ) {
  54. this.camera.setViewOffset( readBuffer.width, readBuffer.height,
  55. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  56. readBuffer.width, readBuffer.height );
  57. }
  58. renderer.render( this.scene, this.camera, writeBuffer, true );
  59. renderer.render( this.scene2, this.camera2, this.sampleRenderTarget, ( this.accumulateIndex === 0 ) );
  60. this.accumulateIndex ++;
  61. if( this.accumulateIndex >= jitterOffsets.length ) break;
  62. }
  63. if ( this.camera.clearViewOffset ) this.camera.clearViewOffset();
  64. }
  65. var accumulationWeight = this.accumulateIndex * sampleWeight;
  66. if( accumulationWeight > 0 ) {
  67. this.copyUniforms[ "opacity" ].value = 1.0;
  68. this.copyUniforms[ "tDiffuse" ].value = this.sampleRenderTarget.texture;
  69. renderer.render( this.scene2, this.camera2, writeBuffer, true );
  70. }
  71. if( accumulationWeight < 1.0 ) {
  72. this.copyUniforms[ "opacity" ].value = 1.0 - accumulationWeight;
  73. this.copyUniforms[ "tDiffuse" ].value = this.holdRenderTarget.texture;
  74. renderer.render( this.scene2, this.camera2, writeBuffer, ( accumulationWeight === 0 ) );
  75. }
  76. renderer.autoClear = autoClear;
  77. }
  78. });