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