TAARenderPass.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 = 1;
  20. this.accumulate = false;
  21. if ( THREE.CompositeShader === undefined ) {
  22. console.error( "THREE.TAARenderPass relies on THREE.CompositeShader" );
  23. }
  24. var accumulateShader = THREE.CompositeShader;
  25. this.accumulateUniforms = THREE.UniformsUtils.clone( accumulateShader.uniforms );
  26. this.materialAccumulate = new THREE.ShaderMaterial( {
  27. uniforms: this.accumulateUniforms,
  28. vertexShader: accumulateShader.vertexShader,
  29. fragmentShader: accumulateShader.fragmentShader,
  30. transparent: true,
  31. blending: THREE.CustomBlending,
  32. blendSrc: THREE.OneFactor,
  33. blendDst: THREE.OneFactor,
  34. blendSrcAlpha: THREE.OneFactor,
  35. blendDstAlpha: THREE.OneFactor,
  36. blendEquation: THREE.AddEquation,
  37. depthTest: false,
  38. depthWrite: false
  39. } );
  40. this.scene3 = new THREE.Scene();
  41. this.quad3 = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), this.materialAccumulate );
  42. this.scene3.add( this.quad3 );
  43. };
  44. THREE.TAARenderPass.prototype = Object.create( THREE.ManualMSAARenderPass.prototype );
  45. THREE.TAARenderPass.prototype.constructor = THREE.TAARenderPass;
  46. THREE.TAARenderPass.JitterVectors = THREE.ManualMSAARenderPass.JitterVectors;
  47. THREE.TAARenderPass.prototype.render = function ( renderer, writeBuffer, readBuffer, delta ) {
  48. if( ! this.accumulate ) {
  49. THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta );
  50. this.accumulateIndex = -1;
  51. return;
  52. }
  53. var jitterOffsets = THREE.TAARenderPass.JitterVectors[ 4 ];
  54. var camera = ( this.camera || this.scene.camera );
  55. if ( ! this.sampleRenderTarget ) {
  56. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  57. }
  58. if ( ! this.holdRenderTarget ) {
  59. this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  60. }
  61. if( this.accumulate && this.accumulateIndex === -1 ) {
  62. THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, delta );
  63. this.accumulateIndex = 0;
  64. }
  65. var autoClear = renderer.autoClear;
  66. renderer.autoClear = false;
  67. var sampleWeight = 1.0 / ( jitterOffsets.length );
  68. if( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
  69. this.accumulateUniforms[ "scale" ].value = sampleWeight;
  70. this.accumulateUniforms[ "tForeground" ].value = writeBuffer;
  71. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  72. var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  73. for ( var i = 0; i < numSamplesPerFrame; i ++ ) {
  74. var j = this.accumulateIndex;
  75. // only jitters perspective cameras. TODO: add support for jittering orthogonal cameras
  76. var jitterOffset = jitterOffsets[j];
  77. if ( camera.setViewOffset ) {
  78. camera.setViewOffset( readBuffer.width, readBuffer.height,
  79. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  80. readBuffer.width, readBuffer.height );
  81. }
  82. renderer.render( this.scene, this.camera, writeBuffer, true );
  83. renderer.render( this.scene3, this.camera2, this.sampleRenderTarget, ( this.accumulateIndex == 0 ) );
  84. this.accumulateIndex ++;
  85. if( this.accumulateIndex >= jitterOffsets.length ) break;
  86. }
  87. // reset jitter to nothing. TODO: add support for orthogonal cameras
  88. if ( camera.setViewOffset ) camera.setViewOffset( undefined, undefined, undefined, undefined, undefined, undefined );
  89. }
  90. var accumulationWeight = this.accumulateIndex * sampleWeight;
  91. if( accumulationWeight > 0 ) {
  92. this.accumulateUniforms[ "scale" ].value = 1.0;
  93. this.accumulateUniforms[ "tForeground" ].value = this.sampleRenderTarget;
  94. renderer.render( this.scene3, this.camera2, writeBuffer, true );
  95. }
  96. if( accumulationWeight < 1.0 ) {
  97. this.accumulateUniforms[ "scale" ].value = 1.0 - accumulationWeight;
  98. this.accumulateUniforms[ "tForeground" ].value = this.holdRenderTarget;
  99. renderer.render( this.scene3, this.camera2, writeBuffer, ( accumulationWeight === 0 ) );
  100. }
  101. renderer.autoClear = autoClear;
  102. }