Browse Source

SSAOPass: Exclude point clouds and lines.

Mugen87 4 years ago
parent
commit
94339bcf88
2 changed files with 84 additions and 20 deletions
  1. 42 10
      examples/js/postprocessing/SSAOPass.js
  2. 42 10
      examples/jsm/postprocessing/SSAOPass.js

+ 42 - 10
examples/js/postprocessing/SSAOPass.js

@@ -1,5 +1,7 @@
 console.warn( "THREE.SSAOPass: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
 console.warn( "THREE.SSAOPass: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/#manual/en/introduction/Installation." );
 
 
+var _cache = new Map();
+
 THREE.SSAOPass = function ( scene, camera, width, height ) {
 THREE.SSAOPass = function ( scene, camera, width, height ) {
 
 
 	THREE.Pass.call( this );
 	THREE.Pass.call( this );
@@ -26,7 +28,7 @@ THREE.SSAOPass = function ( scene, camera, width, height ) {
 	this.generateSampleKernel();
 	this.generateSampleKernel();
 	this.generateRandomKernelRotations();
 	this.generateRandomKernelRotations();
 
 
-	// beauty render target with depth buffer
+	// beauty render target
 
 
 	var depthTexture = new THREE.DepthTexture();
 	var depthTexture = new THREE.DepthTexture();
 	depthTexture.type = THREE.UnsignedShortType;
 	depthTexture.type = THREE.UnsignedShortType;
@@ -36,17 +38,16 @@ THREE.SSAOPass = function ( scene, camera, width, height ) {
 	this.beautyRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, {
 	this.beautyRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, {
 		minFilter: THREE.LinearFilter,
 		minFilter: THREE.LinearFilter,
 		magFilter: THREE.LinearFilter,
 		magFilter: THREE.LinearFilter,
-		format: THREE.RGBAFormat,
-		depthTexture: depthTexture,
-		depthBuffer: true
+		format: THREE.RGBAFormat
 	} );
 	} );
 
 
-	// normal render target
+	// normal render target with depth buffer
 
 
 	this.normalRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, {
 	this.normalRenderTarget = new THREE.WebGLRenderTarget( this.width, this.height, {
 		minFilter: THREE.NearestFilter,
 		minFilter: THREE.NearestFilter,
 		magFilter: THREE.NearestFilter,
 		magFilter: THREE.NearestFilter,
-		format: THREE.RGBAFormat
+		format: THREE.RGBAFormat,
+		depthTexture: depthTexture
 	} );
 	} );
 
 
 	// ssao render target
 	// ssao render target
@@ -77,7 +78,7 @@ THREE.SSAOPass = function ( scene, camera, width, height ) {
 
 
 	this.ssaoMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
 	this.ssaoMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
 	this.ssaoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
 	this.ssaoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
-	this.ssaoMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
+	this.ssaoMaterial.uniforms[ 'tDepth' ].value = this.normalRenderTarget.depthTexture;
 	this.ssaoMaterial.uniforms[ 'tNoise' ].value = this.noiseTexture;
 	this.ssaoMaterial.uniforms[ 'tNoise' ].value = this.noiseTexture;
 	this.ssaoMaterial.uniforms[ 'kernel' ].value = this.kernel;
 	this.ssaoMaterial.uniforms[ 'kernel' ].value = this.kernel;
 	this.ssaoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
 	this.ssaoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
@@ -111,7 +112,7 @@ THREE.SSAOPass = function ( scene, camera, width, height ) {
 		fragmentShader: THREE.SSAODepthShader.fragmentShader,
 		fragmentShader: THREE.SSAODepthShader.fragmentShader,
 		blending: THREE.NoBlending
 		blending: THREE.NoBlending
 	} );
 	} );
-	this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
+	this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.normalRenderTarget.depthTexture;
 	this.depthRenderMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
 	this.depthRenderMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
 	this.depthRenderMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
 	this.depthRenderMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
 
 
@@ -166,15 +167,17 @@ THREE.SSAOPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ),
 
 
 	render: function ( renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) {
 	render: function ( renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) {
 
 
-		// render beauty and depth
+		// render beauty
 
 
 		renderer.setRenderTarget( this.beautyRenderTarget );
 		renderer.setRenderTarget( this.beautyRenderTarget );
 		renderer.clear();
 		renderer.clear();
 		renderer.render( this.scene, this.camera );
 		renderer.render( this.scene, this.camera );
 
 
-		// render normals
+		// render normals and depth (honor only meshes, points and lines do not contribute to SSAO)
 
 
+		this.overrideVisibility();
 		this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
 		this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
+		this.restoreVisibility();
 
 
 		// render SSAO
 		// render SSAO
 
 
@@ -387,6 +390,35 @@ THREE.SSAOPass.prototype = Object.assign( Object.create( THREE.Pass.prototype ),
 		this.noiseTexture.wrapS = THREE.RepeatWrapping;
 		this.noiseTexture.wrapS = THREE.RepeatWrapping;
 		this.noiseTexture.wrapT = THREE.RepeatWrapping;
 		this.noiseTexture.wrapT = THREE.RepeatWrapping;
 
 
+	},
+
+	overrideVisibility: function () {
+
+		var scene = this.scene;
+
+		scene.traverse( function ( object ) {
+
+			_cache.set( object, object.visible );
+
+			if ( object.isPoints || object.isLine ) object.visible = false;
+
+		} );
+
+	},
+
+	restoreVisibility: function () {
+
+		var scene = this.scene;
+
+		scene.traverse( function ( object ) {
+
+			var visible = _cache.get( object );
+			object.visible = visible;
+
+		} );
+
+		_cache.clear();
+
 	}
 	}
 
 
 } );
 } );

+ 42 - 10
examples/jsm/postprocessing/SSAOPass.js

@@ -28,6 +28,8 @@ import { SSAOBlurShader } from "../shaders/SSAOShader.js";
 import { SSAODepthShader } from "../shaders/SSAOShader.js";
 import { SSAODepthShader } from "../shaders/SSAOShader.js";
 import { CopyShader } from "../shaders/CopyShader.js";
 import { CopyShader } from "../shaders/CopyShader.js";
 
 
+var _cache = new Map();
+
 var SSAOPass = function ( scene, camera, width, height ) {
 var SSAOPass = function ( scene, camera, width, height ) {
 
 
 	Pass.call( this );
 	Pass.call( this );
@@ -54,7 +56,7 @@ var SSAOPass = function ( scene, camera, width, height ) {
 	this.generateSampleKernel();
 	this.generateSampleKernel();
 	this.generateRandomKernelRotations();
 	this.generateRandomKernelRotations();
 
 
-	// beauty render target with depth buffer
+	// beauty render target
 
 
 	var depthTexture = new DepthTexture();
 	var depthTexture = new DepthTexture();
 	depthTexture.type = UnsignedShortType;
 	depthTexture.type = UnsignedShortType;
@@ -64,17 +66,16 @@ var SSAOPass = function ( scene, camera, width, height ) {
 	this.beautyRenderTarget = new WebGLRenderTarget( this.width, this.height, {
 	this.beautyRenderTarget = new WebGLRenderTarget( this.width, this.height, {
 		minFilter: LinearFilter,
 		minFilter: LinearFilter,
 		magFilter: LinearFilter,
 		magFilter: LinearFilter,
-		format: RGBAFormat,
-		depthTexture: depthTexture,
-		depthBuffer: true
+		format: RGBAFormat
 	} );
 	} );
 
 
-	// normal render target
+	// normal render target with depth buffer
 
 
 	this.normalRenderTarget = new WebGLRenderTarget( this.width, this.height, {
 	this.normalRenderTarget = new WebGLRenderTarget( this.width, this.height, {
 		minFilter: NearestFilter,
 		minFilter: NearestFilter,
 		magFilter: NearestFilter,
 		magFilter: NearestFilter,
-		format: RGBAFormat
+		format: RGBAFormat,
+		depthTexture: depthTexture
 	} );
 	} );
 
 
 	// ssao render target
 	// ssao render target
@@ -105,7 +106,7 @@ var SSAOPass = function ( scene, camera, width, height ) {
 
 
 	this.ssaoMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
 	this.ssaoMaterial.uniforms[ 'tDiffuse' ].value = this.beautyRenderTarget.texture;
 	this.ssaoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
 	this.ssaoMaterial.uniforms[ 'tNormal' ].value = this.normalRenderTarget.texture;
-	this.ssaoMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
+	this.ssaoMaterial.uniforms[ 'tDepth' ].value = this.normalRenderTarget.depthTexture;
 	this.ssaoMaterial.uniforms[ 'tNoise' ].value = this.noiseTexture;
 	this.ssaoMaterial.uniforms[ 'tNoise' ].value = this.noiseTexture;
 	this.ssaoMaterial.uniforms[ 'kernel' ].value = this.kernel;
 	this.ssaoMaterial.uniforms[ 'kernel' ].value = this.kernel;
 	this.ssaoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
 	this.ssaoMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
@@ -139,7 +140,7 @@ var SSAOPass = function ( scene, camera, width, height ) {
 		fragmentShader: SSAODepthShader.fragmentShader,
 		fragmentShader: SSAODepthShader.fragmentShader,
 		blending: NoBlending
 		blending: NoBlending
 	} );
 	} );
-	this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.beautyRenderTarget.depthTexture;
+	this.depthRenderMaterial.uniforms[ 'tDepth' ].value = this.normalRenderTarget.depthTexture;
 	this.depthRenderMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
 	this.depthRenderMaterial.uniforms[ 'cameraNear' ].value = this.camera.near;
 	this.depthRenderMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
 	this.depthRenderMaterial.uniforms[ 'cameraFar' ].value = this.camera.far;
 
 
@@ -194,15 +195,17 @@ SSAOPass.prototype = Object.assign( Object.create( Pass.prototype ), {
 
 
 	render: function ( renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) {
 	render: function ( renderer, writeBuffer /*, readBuffer, deltaTime, maskActive */ ) {
 
 
-		// render beauty and depth
+		// render beauty
 
 
 		renderer.setRenderTarget( this.beautyRenderTarget );
 		renderer.setRenderTarget( this.beautyRenderTarget );
 		renderer.clear();
 		renderer.clear();
 		renderer.render( this.scene, this.camera );
 		renderer.render( this.scene, this.camera );
 
 
-		// render normals
+		// render normals and depth (honor only meshes, points and lines do not contribute to SSAO)
 
 
+		this.overrideVisibility();
 		this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
 		this.renderOverride( renderer, this.normalMaterial, this.normalRenderTarget, 0x7777ff, 1.0 );
+		this.restoreVisibility();
 
 
 		// render SSAO
 		// render SSAO
 
 
@@ -415,6 +418,35 @@ SSAOPass.prototype = Object.assign( Object.create( Pass.prototype ), {
 		this.noiseTexture.wrapS = RepeatWrapping;
 		this.noiseTexture.wrapS = RepeatWrapping;
 		this.noiseTexture.wrapT = RepeatWrapping;
 		this.noiseTexture.wrapT = RepeatWrapping;
 
 
+	},
+
+	overrideVisibility: function () {
+
+		var scene = this.scene;
+
+		scene.traverse( function ( object ) {
+
+			_cache.set( object, object.visible );
+
+			if ( object.isPoints || object.isLine ) object.visible = false;
+
+		} );
+
+	},
+
+	restoreVisibility: function () {
+
+		var scene = this.scene;
+
+		scene.traverse( function ( object ) {
+
+			var visible = _cache.get( object );
+			object.visible = visible;
+
+		} );
+
+		_cache.clear();
+
 	}
 	}
 
 
 } );
 } );