SSAOPass.js 11.4 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 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
	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;
	this.kernelSize = 64;
	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

M
Mugen87 已提交
137
	//
T
Tentone 已提交
138

M
Mugen87 已提交
139 140 141 142
	this.quadCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
	this.quadScene = new THREE.Scene();
	this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), null );
	this.quadScene.add( this.quad );
M
Mugen87 已提交
143

M
Mugen87 已提交
144 145 146 147
	//

	this.originalClearColor = new THREE.Color();

M
Mugen87 已提交
148
};
T
Tentone 已提交
149

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

M
Mugen87 已提交
152
	constructor: THREE.SSAOPass,
M
Mugen87 已提交
153

M
Mugen87 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
	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();

	},

M
Mugen87 已提交
176
	render: function ( renderer, writeBuffer /*, readBuffer, delta, maskActive */ ) {
T
Tentone 已提交
177

M
Mugen87 已提交
178
		// render beauty and depth
M
Mugen87 已提交
179

M
Mugen87 已提交
180
		renderer.render( this.scene, this.camera, this.beautyRenderTarget, true );
M
Mugen87 已提交
181

M
Mugen87 已提交
182
		// render normals
M
Mugen87 已提交
183

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

M
Mugen87 已提交
186
		// render SSAO
T
Tentone 已提交
187

M
Mugen87 已提交
188 189 190 191
		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 已提交
192

M
Mugen87 已提交
193
		// render blur
M
Mugen87 已提交
194

M
Mugen87 已提交
195
		this.renderPass( renderer, this.blurMaterial, this.blurRenderTarget );
M
Mugen87 已提交
196

M
Mugen87 已提交
197
		// output result to screen
M
Mugen87 已提交
198

M
Mugen87 已提交
199
		switch ( this.output ) {
T
Tentone 已提交
200

M
Mugen87 已提交
201
			case THREE.SSAOPass.OUTPUT.SSAO:
M
Mugen87 已提交
202

M
Mugen87 已提交
203 204
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.ssaoRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
205
				this.renderPass( renderer, this.copyMaterial, null );
M
Mugen87 已提交
206

M
Mugen87 已提交
207
				break;
M
Mugen87 已提交
208

M
Mugen87 已提交
209
			case THREE.SSAOPass.OUTPUT.Blur:
M
Mugen87 已提交
210

M
Mugen87 已提交
211 212
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.blurRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
213
				this.renderPass( renderer, this.copyMaterial, null );
T
Tentone 已提交
214

M
Mugen87 已提交
215
				break;
M
Mugen87 已提交
216

M
Mugen87 已提交
217
			case THREE.SSAOPass.OUTPUT.Beauty:
M
Mugen87 已提交
218

M
Mugen87 已提交
219 220
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
221
				this.renderPass( renderer, this.copyMaterial, null );
M
Mugen87 已提交
222

M
Mugen87 已提交
223
				break;
M
Mugen87 已提交
224

M
Mugen87 已提交
225
			case THREE.SSAOPass.OUTPUT.Depth:
T
Tentone 已提交
226

M
Mugen87 已提交
227
				this.renderPass( renderer, this.depthRenderMaterial, null );
M
Mugen87 已提交
228

M
Mugen87 已提交
229
				break;
T
Tentone 已提交
230

M
Mugen87 已提交
231
			case THREE.SSAOPass.OUTPUT.Normal:
T
Tentone 已提交
232

M
Mugen87 已提交
233 234
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.normalRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
235
				this.renderPass( renderer, this.copyMaterial, null );
T
Tentone 已提交
236

M
Mugen87 已提交
237
				break;
M
Mugen87 已提交
238

M
Mugen87 已提交
239
			case THREE.SSAOPass.OUTPUT.Default:
M
Mugen87 已提交
240

M
Mugen87 已提交
241 242
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
				this.copyMaterial.blending = THREE.NoBlending;
M
Mugen87 已提交
243
				this.renderPass( renderer, this.copyMaterial, null );
244

M
Mugen87 已提交
245 246 247
				this.copyMaterial.uniforms[ 'tDiffuse' ].value = this.blurRenderTarget.texture;
				this.copyMaterial.blending = THREE.CustomBlending;
				this.renderPass( renderer, this.copyMaterial, this.renderToScreen ? null : writeBuffer );
248

M
Mugen87 已提交
249
				break;
T
Tentone 已提交
250

M
Mugen87 已提交
251 252
			default:
				console.warn( 'THREE.SSAOPass: Unknown output type.' );
T
Tentone 已提交
253

M
Mugen87 已提交
254
		}
M
Mugen87 已提交
255

M
Mugen87 已提交
256
	},
257

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

M
Mugen87 已提交
260
		// save original state
M
Mugen87 已提交
261
		this.originalClearColor.copy( renderer.getClearColor() );
M
Mugen87 已提交
262 263
		var originalClearAlpha = renderer.getClearAlpha();
		var originalAutoClear = renderer.autoClear;
264

M
Mugen87 已提交
265 266 267 268
		// setup pass state
		renderer.autoClear = false;
		var clearNeeded = ( clearColor !== undefined ) && ( clearColor !== null );
		if ( clearNeeded ) {
269

M
Mugen87 已提交
270 271
			renderer.setClearColor( clearColor );
			renderer.setClearAlpha( clearAlpha || 0.0 );
272

M
Mugen87 已提交
273
		}
274

M
Mugen87 已提交
275 276
		this.quad.material = passMaterial;
		renderer.render( this.quadScene, this.quadCamera, renderTarget, clearNeeded );
T
Tentone 已提交
277

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

	},

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

M
Mugen87 已提交
287
		this.originalClearColor.copy( renderer.getClearColor() );
M
Mugen87 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
		var originalClearAlpha = renderer.getClearAlpha();
		var originalAutoClear = renderer.autoClear;

		renderer.autoClear = false;

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

		var clearNeeded = ( clearColor !== undefined ) && ( clearColor !== null );

		if ( clearNeeded ) {

			renderer.setClearColor( clearColor );
			renderer.setClearAlpha( clearAlpha || 0.0 );

		}

		this.scene.overrideMaterial = overrideMaterial;
		renderer.render( this.scene, this.camera, renderTarget, clearNeeded );
		this.scene.overrideMaterial = null;

		// restore original state

		renderer.autoClear = originalAutoClear;
M
Mugen87 已提交
312
		renderer.setClearColor( this.originalClearColor );
M
Mugen87 已提交
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 369 370 371
		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;
372
		var data = new Float32Array( size * 4 );
M
Mugen87 已提交
373 374 375

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

376 377
			var stride = i * 4;

M
Mugen87 已提交
378 379 380 381
			var x = ( Math.random() * 2 ) - 1;
			var y = ( Math.random() * 2 ) - 1;
			var z = 0;

382 383 384 385 386 387
			var noise = simplex.noise3d( x, y, z );

			data[ stride ] = noise;
			data[ stride + 1 ] = noise;
			data[ stride + 2 ] = noise;
			data[ stride + 3 ] = 1;
M
Mugen87 已提交
388 389 390

		}

391
		this.noiseTexture = new THREE.DataTexture( data, width, height, THREE.RGBA, THREE.FloatType );
M
Mugen87 已提交
392 393 394 395 396
		this.noiseTexture.wrapS = THREE.RepeatWrapping;
		this.noiseTexture.wrapT = THREE.RepeatWrapping;
		this.noiseTexture.needsUpdate = true;

	}
T
Tentone 已提交
397

M
Mugen87 已提交
398
} );
T
Tentone 已提交
399

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