SSAOPass.js 11.3 KB
Newer Older
1
/**
M
Mugen87 已提交
2
 * @author Mugen87 / https://github.com/Mugen87
T
Tentone 已提交
3
 */
M
Mugen87 已提交
4

T
Tentone 已提交
5 6
THREE.SSAOPass = function ( scene, camera, width, height ) {

M
Mugen87 已提交
7 8 9 10 11 12 13 14 15 16 17
	THREE.Pass.call( this );

	this.width = ( width !== undefined ) ? width : 512;
	this.height = ( height !== undefined ) ? height : 512;

	this.clear = true;

	this.camera = camera;
	this.scene = scene;

	this.kernelRadius = 8;
18
	this.kernelSize = 32;
M
Mugen87 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	this.kernel = [];
	this.noiseTexture = null;
	this.output = 0;

	this.minDistance = 0.005;
	this.maxDistance = 0.1;

	//

	this.generateSampleKernel();
	this.generateRandomKernelRotations();

	// beauty render target with depth buffer

	var depthTexture = new THREE.DepthTexture();
	depthTexture.type = THREE.UnsignedShortType;
	depthTexture.minFilter = THREE.NearestFilter;
	depthTexture.maxFilter = THREE.NearestFilter;

38
	this.beautyRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, {
M
Mugen87 已提交
39 40 41 42 43 44 45 46 47
		minFilter: THREE.LinearFilter,
		magFilter: THREE.LinearFilter,
		format: THREE.RGBAFormat,
		depthTexture: depthTexture,
		depthBuffer: true
	} );

	// normal render target

48
	this.normalRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, {
M
Mugen87 已提交
49 50 51 52 53 54 55
		minFilter: THREE.NearestFilter,
		magFilter: THREE.NearestFilter,
		format: THREE.RGBAFormat
	} );

	// ssao render target

56
	this.ssaoRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, {
M
Mugen87 已提交
57 58 59 60 61 62 63 64 65
		minFilter: THREE.LinearFilter,
		magFilter: THREE.LinearFilter,
		format: THREE.RGBAFormat
	} );

	this.blurRenderTarget = this.ssaoRenderTarget.clone();

	// ssao material

M
Mugen87 已提交
66
	if ( THREE.SSAOShader === undefined ) {
67

M
Mugen87 已提交
68
		console.error( 'THREE.SSAOPass: The pass relies on THREE.SSAOShader.' );
69

T
Tentone 已提交
70 71
	}

M
Mugen87 已提交
72 73 74 75 76 77 78
	this.ssaoMaterial = new THREE.ShaderMaterial( {
		defines: Object.assign( {}, THREE.SSAOShader.defines ),
		uniforms: THREE.UniformsUtils.clone( THREE.SSAOShader.uniforms ),
		vertexShader: THREE.SSAOShader.vertexShader,
		fragmentShader: THREE.SSAOShader.fragmentShader,
		blending: THREE.NoBlending
	} );
T
Tentone 已提交
79

M
Mugen87 已提交
80 81 82 83 84 85 86
	this.ssaoMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
	this.ssaoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
	this.ssaoMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
	this.ssaoMaterial.uniforms[ 'tNoise' ].value = this.noiseTexture;
	this.ssaoMaterial.uniforms[ 'kernel' ].value = this.kernel;
	this.ssaoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
	this.ssaoMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
87
	this.ssaoMaterial.uniforms[ 'resolution' ].value.set( this.width, this.height );
M
Mugen87 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
	this.ssaoMaterial.uniforms[ 'cameraProjectionMatrix' ].value.copy( this.camera.projectionMatrix );
	this.ssaoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.getInverse( this.camera.projectionMatrix );

	// normal material

	this.normalMaterial = new THREE.MeshNormalMaterial();
	this.normalMaterial.blending = THREE.NoBlending;

	// blur material

	this.blurMaterial = new THREE.ShaderMaterial( {
		defines: Object.assign( {}, THREE.SSAOBlurShader.defines ),
		uniforms: THREE.UniformsUtils.clone( THREE.SSAOBlurShader.uniforms ),
		vertexShader: THREE.SSAOBlurShader.vertexShader,
		fragmentShader: THREE.SSAOBlurShader.fragmentShader
	} );
	this.blurMaterial.uniforms[ 'tDiffuse' ].value = this.ssaoRenderTarget.texture;
105
	this.blurMaterial.uniforms[ 'resolution' ].value.set( this.width, this.height );
T
Tentone 已提交
106

M
Mugen87 已提交
107
	// material for rendering the depth
T
Tentone 已提交
108

M
Mugen87 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	this.depthRenderMaterial = new THREE.ShaderMaterial( {
		defines: Object.assign( {}, THREE.SSAODepthShader.defines ),
		uniforms: THREE.UniformsUtils.clone( THREE.SSAODepthShader.uniforms ),
		vertexShader: THREE.SSAODepthShader.vertexShader,
		fragmentShader: THREE.SSAODepthShader.fragmentShader,
		blending: THREE.NoBlending
	} );
	this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
	this.depthRenderMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
	this.depthRenderMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;

	// material for rendering the content of a render target

	this.copyMaterial = new THREE.ShaderMaterial( {
		uniforms: THREE.UniformsUtils.clone( THREE.CopyShader.uniforms ),
		vertexShader: THREE.CopyShader.vertexShader,
		fragmentShader: THREE.CopyShader.fragmentShader,
		transparent: true,
		depthTest: false,
		depthWrite: false,
		blendSrc: THREE.DstColorFactor,
		blendDst: THREE.ZeroFactor,
		blendEquation: THREE.AddEquation,
		blendSrcAlpha: THREE.DstAlphaFactor,
		blendDstAlpha: THREE.ZeroFactor,
		blendEquationAlpha: THREE.AddEquation
	} );
T
Tentone 已提交
136

137
	this.fsQuad = new THREE.Pass.FullScreenQuad( null );
M
Mugen87 已提交
138 139 140

	this.originalClearColor = new THREE.Color();

M
Mugen87 已提交
141
};
T
Tentone 已提交
142

M
Mugen87 已提交
143
THREE.SSAOPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ), {
T
Tentone 已提交
144

M
Mugen87 已提交
145
	constructor: THREE.SSAOPass,
M
Mugen87 已提交
146

M
Mugen87 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
	dispose: function () {

		// dispose render targets

		this.beautyRenderTarget.dispose();
		this.normalRenderTarget.dispose();
		this.ssaoRenderTarget.dispose();
		this.blurRenderTarget.dispose();

		// dispose geometry

		this.quad.geometry.dispose();

		// dispose materials

		this.normalMaterial.dispose();
		this.blurMaterial.dispose();
		this.copyMaterial.dispose();
		this.depthRenderMaterial.dispose();

	},

169
	render: function ( renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) {
T
Tentone 已提交
170

M
Mugen87 已提交
171
		// render beauty and depth
M
Mugen87 已提交
172

173 174 175
		renderer.setRenderTarget( this.beautyRenderTarget );
		renderer.clear();
		renderer.render( this.scene, this.camera );
M
Mugen87 已提交
176

M
Mugen87 已提交
177
		// render normals
M
Mugen87 已提交
178

M
Mugen87 已提交
179
		this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
M
Mugen87 已提交
180

M
Mugen87 已提交
181
		// render SSAO
T
Tentone 已提交
182

M
Mugen87 已提交
183 184 185 186
		this.ssaoMaterial.uniforms[ 'kernelRadius' ].value = this.kernelRadius;
		this.ssaoMaterial.uniforms[ 'minDistance' ].value = this.minDistance;
		this.ssaoMaterial.uniforms[ 'maxDistance' ].value = this.maxDistance;
		this.renderPass( renderer, this.ssaoMaterial, this.ssaoRenderTarget );
M
Mugen87 已提交
187

M
Mugen87 已提交
188
		// render blur
M
Mugen87 已提交
189

M
Mugen87 已提交
190
		this.renderPass( renderer, this.blurMaterial, this.blurRenderTarget );
M
Mugen87 已提交
191

M
Mugen87 已提交
192
		// output result to screen
M
Mugen87 已提交
193

M
Mugen87 已提交
194
		switch ( this.output ) {
T
Tentone 已提交
195

M
Mugen87 已提交
196
			case THREE.SSAOPass.OUTPUT.SSAO:
M
Mugen87 已提交
197

M
Mugen87 已提交
198 199
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.ssaoRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
200
				this.renderPass( renderer, this.copyMaterial, null );
M
Mugen87 已提交
201

M
Mugen87 已提交
202
				break;
M
Mugen87 已提交
203

M
Mugen87 已提交
204
			case THREE.SSAOPass.OUTPUT.Blur:
M
Mugen87 已提交
205

M
Mugen87 已提交
206 207
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.blurRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
208
				this.renderPass( renderer, this.copyMaterial, null );
T
Tentone 已提交
209

M
Mugen87 已提交
210
				break;
M
Mugen87 已提交
211

M
Mugen87 已提交
212
			case THREE.SSAOPass.OUTPUT.Beauty:
M
Mugen87 已提交
213

M
Mugen87 已提交
214 215
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
216
				this.renderPass( renderer, this.copyMaterial, null );
M
Mugen87 已提交
217

M
Mugen87 已提交
218
				break;
M
Mugen87 已提交
219

M
Mugen87 已提交
220
			case THREE.SSAOPass.OUTPUT.Depth:
T
Tentone 已提交
221

M
Mugen87 已提交
222
				this.renderPass( renderer, this.depthRenderMaterial, null );
M
Mugen87 已提交
223

M
Mugen87 已提交
224
				break;
T
Tentone 已提交
225

M
Mugen87 已提交
226
			case THREE.SSAOPass.OUTPUT.Normal:
T
Tentone 已提交
227

M
Mugen87 已提交
228 229
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
230
				this.renderPass( renderer, this.copyMaterial, null );
T
Tentone 已提交
231

M
Mugen87 已提交
232
				break;
M
Mugen87 已提交
233

M
Mugen87 已提交
234
			case THREE.SSAOPass.OUTPUT.Default:
M
Mugen87 已提交
235

M
Mugen87 已提交
236 237
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
238
				this.renderPass( renderer, this.copyMaterial, null );
239

M
Mugen87 已提交
240 241 242
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.blurRenderTarget.texture;
				this.copyMaterial.blending = THREE.CustomBlending;
				this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
243

M
Mugen87 已提交
244
				break;
T
Tentone 已提交
245

M
Mugen87 已提交
246 247
			default:
				console.warn( 'THREE.SSAOPass: Unknown output type.' );
T
Tentone 已提交
248

M
Mugen87 已提交
249
		}
M
Mugen87 已提交
250

M
Mugen87 已提交
251
	},
252

M
Mugen87 已提交
253
	renderPass: function ( renderer, passMaterial, renderTarget, clearColor, clearAlpha ) {
254

M
Mugen87 已提交
255
		// save original state
M
Mugen87 已提交
256
		this.originalClearColor.copy( renderer.getClearColor() );
M
Mugen87 已提交
257 258
		var originalClearAlpha = renderer.getClearAlpha();
		var originalAutoClear = renderer.autoClear;
259

260 261
		renderer.setRenderTarget( renderTarget );

M
Mugen87 已提交
262 263
		// setup pass state
		renderer.autoClear = false;
264
		if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
265

M
Mugen87 已提交
266 267
			renderer.setClearColor( clearColor );
			renderer.setClearAlpha( clearAlpha || 0.0 );
268
			renderer.clear();
269

M
Mugen87 已提交
270
		}
271

272 273
		this.fsQuad.material = passMaterial;
		this.fsQuad.render( renderer );
T
Tentone 已提交
274

M
Mugen87 已提交
275 276
		// restore original state
		renderer.autoClear = originalAutoClear;
M
Mugen87 已提交
277
		renderer.setClearColor( this.originalClearColor );
M
Mugen87 已提交
278 279 280 281 282 283
		renderer.setClearAlpha( originalClearAlpha );

	},

	renderOverride: function ( renderer, overrideMaterial, renderTarget, clearColor, clearAlpha ) {

M
Mugen87 已提交
284
		this.originalClearColor.copy( renderer.getClearColor() );
M
Mugen87 已提交
285 286 287
		var originalClearAlpha = renderer.getClearAlpha();
		var originalAutoClear = renderer.autoClear;

288
		renderer.setRenderTarget( renderTarget );
M
Mugen87 已提交
289 290 291 292 293
		renderer.autoClear = false;

		clearColor = overrideMaterial.clearColor || clearColor;
		clearAlpha = overrideMaterial.clearAlpha || clearAlpha;

294
		if ( ( clearColor !== undefined ) && ( clearColor !== null ) ) {
M
Mugen87 已提交
295 296 297

			renderer.setClearColor( clearColor );
			renderer.setClearAlpha( clearAlpha || 0.0 );
298
			renderer.clear();
M
Mugen87 已提交
299 300 301 302

		}

		this.scene.overrideMaterial = overrideMaterial;
303
		renderer.render( this.scene, this.camera );
M
Mugen87 已提交
304 305 306 307 308
		this.scene.overrideMaterial = null;

		// restore original state

		renderer.autoClear = originalAutoClear;
M
Mugen87 已提交
309
		renderer.setClearColor( this.originalClearColor );
M
Mugen87 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
		renderer.setClearAlpha( originalClearAlpha );

	},

	setSize: function ( width, height ) {

		this.width = width;
		this.height = height;

		this.beautyRenderTarget.setSize( width, height );
		this.ssaoRenderTarget.setSize( width, height );
		this.normalRenderTarget.setSize( width, height );
		this.blurRenderTarget.setSize( width, height );

		this.ssaoMaterial.uniforms[ 'resolution' ].value.set( width, height );
		this.ssaoMaterial.uniforms[ 'cameraProjectionMatrix' ].value.copy( this.camera.projectionMatrix );
		this.ssaoMaterial.uniforms[ 'cameraInverseProjectionMatrix' ].value.getInverse( this.camera.projectionMatrix );

		this.blurMaterial.uniforms[ 'resolution' ].value.set( width, height );

	},

	generateSampleKernel: function () {

		var kernelSize = this.kernelSize;
		var kernel = this.kernel;

		for ( var i = 0; i < kernelSize; i ++ ) {

			var sample = new THREE.Vector3();
			sample.x = ( Math.random() * 2 ) - 1;
			sample.y = ( Math.random() * 2 ) - 1;
			sample.z = Math.random();

			sample.normalize();

			var scale = i / kernelSize;
			scale = THREE.Math.lerp( 0.1, 1, scale * scale );
			sample.multiplyScalar( scale );

			kernel.push( sample );

		}

	},

	generateRandomKernelRotations: function () {

		var width = 4, height = 4;

		if ( SimplexNoise === undefined ) {

			console.error( 'THREE.SSAOPass: The pass relies on THREE.SimplexNoise.' );

		}

		var simplex = new SimplexNoise();

		var size = width * height;
369
		var data = new Float32Array( size * 4 );
M
Mugen87 已提交
370 371 372

		for ( var i = 0; i < size; i ++ ) {

373 374
			var stride = i * 4;

M
Mugen87 已提交
375 376 377 378
			var x = ( Math.random() * 2 ) - 1;
			var y = ( Math.random() * 2 ) - 1;
			var z = 0;

379 380 381 382 383 384
			var noise = simplex.noise3d( x, y, z );

			data[ stride ] = noise;
			data[ stride + 1 ] = noise;
			data[ stride + 2 ] = noise;
			data[ stride + 3 ] = 1;
M
Mugen87 已提交
385 386 387

		}

388
		this.noiseTexture = new THREE.DataTexture( data, width, height, THREE.RGBAFormat, THREE.FloatType );
M
Mugen87 已提交
389 390 391 392 393
		this.noiseTexture.wrapS = THREE.RepeatWrapping;
		this.noiseTexture.wrapT = THREE.RepeatWrapping;
		this.noiseTexture.needsUpdate = true;

	}
T
Tentone 已提交
394

M
Mugen87 已提交
395
} );
T
Tentone 已提交
396

M
Mugen87 已提交
397 398 399 400 401 402 403
THREE.SSAOPass.OUTPUT = {
	'Default': 0,
	'SSAO': 1,
	'Blur': 2,
	'Beauty': 3,
	'Depth': 4,
	'Normal': 5
T
Tentone 已提交
404
};