2
0

TAARenderPass.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * @author bhouston / http://clara.io/ *
  3. */
  4. THREE.TAARenderPass = function ( scene, camera, params ) {
  5. if ( THREE.ManualMSAARenderPass === undefined ) {
  6. console.error( "THREE.TAARenderPass relies on THREE.ManualMSAARenderPass" );
  7. }
  8. THREE.ManualMSAARenderPass.call( this, scene, camera, params );
  9. this.sampleLevel = 1;
  10. this.accumulate = false;
  11. if ( THREE.CompositeShader === undefined ) {
  12. console.error( "THREE.TAARenderPass relies on THREE.CompositeShader" );
  13. }
  14. var accumulateShader = THREE.CompositeShader;
  15. this.accumulateUniforms = THREE.UniformsUtils.clone( accumulateShader.uniforms );
  16. this.materialAccumulate = new THREE.ShaderMaterial( {
  17. uniforms: this.accumulateUniforms,
  18. vertexShader: accumulateShader.vertexShader,
  19. fragmentShader: accumulateShader.fragmentShader,
  20. transparent: true,
  21. blending: THREE.CustomBlending,
  22. blendSrc: THREE.OneFactor,
  23. blendDst: THREE.OneMinusSrcAlphaFactor,
  24. blendEquation: THREE.AddEquation,
  25. depthTest: false,
  26. depthWrite: false
  27. } );
  28. this.camera3 = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
  29. this.scene3 = new THREE.Scene();
  30. this.quad3 = new THREE.Mesh( new THREE.PlaneGeometry( 2, 2 ), this.materialAccumulate );
  31. this.scene3.add( this.quad3 );
  32. };
  33. THREE.TAARenderPass.prototype = Object.create( THREE.ManualMSAARenderPass.prototype );
  34. THREE.TAARenderPass.prototype.constructor = THREE.TAARenderPass;
  35. THREE.TAARenderPass.JitterVectors = THREE.ManualMSAARenderPass.JitterVectors;
  36. THREE.TAARenderPass.prototype.render = function ( renderer, writeBuffer, readBuffer, delta ) {
  37. if( ! this.accumulate ) {
  38. THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta );
  39. this.accumulateIndex = 0;
  40. return;
  41. }
  42. var jitterOffsets = THREE.TAARenderPass.JitterVectors[ 4 ];
  43. var camera = ( this.camera || this.scene.camera );
  44. if ( ! this.sampleRenderTarget ) {
  45. this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );
  46. }
  47. if( this.accumulateIndex < jitterOffsets.length ) {
  48. var autoClear = renderer.autoClear;
  49. renderer.autoClear = false;
  50. this.accumulateUniforms[ "scale" ].value = 1.0 / ( jitterOffsets.length );
  51. this.accumulateUniforms[ "tForeground" ].value = writeBuffer;
  52. // render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
  53. var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
  54. for ( var i = 0; i < numSamplesPerFrame; i ++ ) {
  55. var j = this.accumulateIndex;
  56. // only jitters perspective cameras. TODO: add support for jittering orthogonal cameras
  57. var jitterOffset = jitterOffsets[j];
  58. if ( camera.setViewOffset ) {
  59. camera.setViewOffset( readBuffer.width, readBuffer.height,
  60. jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625, // 0.0625 = 1 / 16
  61. readBuffer.width, readBuffer.height );
  62. }
  63. renderer.render( this.scene, this.camera, writeBuffer, true );
  64. this.accumulateUniforms[ "scale" ].value = 1.0 / ( this.accumulateIndex + 1 );
  65. renderer.render( this.scene3, this.camera3, this.sampleRenderTarget, ( this.accumulateIndex == 0 ) );
  66. this.accumulateIndex ++;
  67. if( this.accumulateIndex >= jitterOffsets.length ) break;
  68. }
  69. // reset jitter to nothing. TODO: add support for orthogonal cameras
  70. if ( camera.setViewOffset ) camera.setViewOffset( undefined, undefined, undefined, undefined, undefined, undefined );
  71. renderer.autoClear = true;
  72. }
  73. this.accumulateUniforms[ "scale" ].value = 1.0;
  74. this.accumulateUniforms[ "tForeground" ].value = this.sampleRenderTarget;
  75. renderer.render( this.scene3, this.camera3, writeBuffer );
  76. }