TAARenderPass.js 3.7 KB
Newer Older
1
/**
2 3 4 5 6 7 8 9 10 11 12
 *
 * Temporal Anti-Aliasing Render Pass
 *
 * @author bhouston / http://clara.io/
 *
 * 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.
 *
 * References:
 *
 * TODO: Add support for motion vector pas so that accumulation of samples across frames can occur on dynamics scenes.
 *
13 14 15 16 17 18 19 20 21 22 23
 */

THREE.TAARenderPass = function ( scene, camera, params ) {

	if ( THREE.ManualMSAARenderPass === undefined ) {

		console.error( "THREE.TAARenderPass relies on THREE.ManualMSAARenderPass" );

	}
	THREE.ManualMSAARenderPass.call( this, scene, camera, params );

24
	this.sampleLevel = 0;
25
	this.accumulate = false;
26
	
27 28 29 30 31 32 33 34 35 36 37 38
};

THREE.TAARenderPass.prototype = Object.create( THREE.ManualMSAARenderPass.prototype );
THREE.TAARenderPass.prototype.constructor = THREE.TAARenderPass;
THREE.TAARenderPass.JitterVectors = THREE.ManualMSAARenderPass.JitterVectors;

THREE.TAARenderPass.prototype.render = function ( renderer, writeBuffer, readBuffer, delta ) {

	if( ! this.accumulate ) {

			THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, writeBuffer, readBuffer, delta );

39
			this.accumulateIndex = -1;
40 41 42 43
			return;

	}

44
	var jitterOffsets = THREE.TAARenderPass.JitterVectors[ 5 ];
45 46 47 48 49 50 51 52 53

	var camera = ( this.camera || this.scene.camera );

	if ( ! this.sampleRenderTarget ) {

		this.sampleRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );

	}

54 55 56 57 58 59 60 61 62 63 64 65 66
	if ( ! this.holdRenderTarget ) {

		this.holdRenderTarget = new THREE.WebGLRenderTarget( readBuffer.width, readBuffer.height, this.params );

	}

	if( this.accumulate && this.accumulateIndex === -1 ) {

			THREE.ManualMSAARenderPass.prototype.render.call( this, renderer, this.holdRenderTarget, readBuffer, delta );

			this.accumulateIndex = 0;

	}
67

68 69 70
	var autoClear = renderer.autoClear;
	renderer.autoClear = false;

B
Ben Houston 已提交
71 72
	var sampleWeight = 1.0 / ( jitterOffsets.length );

73
	if( this.accumulateIndex >= 0 && this.accumulateIndex < jitterOffsets.length ) {
74

75 76
		this.compositeUniforms[ "scale" ].value = sampleWeight;
		this.compositeUniforms[ "tForeground" ].value = writeBuffer;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

		// render the scene multiple times, each slightly jitter offset from the last and accumulate the results.
		var numSamplesPerFrame = Math.pow( 2, this.sampleLevel );
		for ( var i = 0; i < numSamplesPerFrame; i ++ ) {

			var j = this.accumulateIndex;
			// only jitters perspective cameras.	TODO: add support for jittering orthogonal cameras
			var jitterOffset = jitterOffsets[j];
			if ( camera.setViewOffset ) {
				camera.setViewOffset( readBuffer.width, readBuffer.height,
					jitterOffset[ 0 ] * 0.0625, jitterOffset[ 1 ] * 0.0625,   // 0.0625 = 1 / 16
					readBuffer.width, readBuffer.height );
			}

			renderer.render( this.scene, this.camera, writeBuffer, true );

93
			renderer.render( this.scene2, this.camera2, this.sampleRenderTarget, ( this.accumulateIndex == 0 ) );
94 95 96 97 98 99 100 101

			this.accumulateIndex ++;
			if( this.accumulateIndex >= jitterOffsets.length ) break;
		}

		// reset jitter to nothing.	TODO: add support for orthogonal cameras
		if ( camera.setViewOffset ) camera.setViewOffset( undefined, undefined, undefined, undefined, undefined, undefined );

102 103 104 105 106
	}

	var accumulationWeight = this.accumulateIndex * sampleWeight;

	if( accumulationWeight > 0 ) {
107 108 109
		this.compositeUniforms[ "scale" ].value = 1.0;
		this.compositeUniforms[ "tForeground" ].value = this.sampleRenderTarget;
		renderer.render( this.scene2, this.camera2, writeBuffer, true );
110
	}
111

112
	if( accumulationWeight < 1.0 ) {
113 114 115
		this.compositeUniforms[ "scale" ].value = 1.0 - accumulationWeight;
		this.compositeUniforms[ "tForeground" ].value = this.holdRenderTarget;
		renderer.render( this.scene2, this.camera2, writeBuffer, ( accumulationWeight === 0 ) );
116 117
	}

118
	renderer.autoClear = autoClear;
119

120 121

}