Browse Source

WebGPURenderer: Add bloom emissive/selective examples (#28913)

* Add background names

* Fix RenderTarget dispose and MRT

* add emissive bloom

* update

* remove support to `array` and added `getNode()`

* added bloom selective
sunag 1 year ago
parent
commit
460efde132

+ 2 - 0
examples/files.json

@@ -375,6 +375,8 @@
 		"webgpu_postprocessing_anamorphic",
 		"webgpu_postprocessing_anamorphic",
 		"webgpu_postprocessing_ao",
 		"webgpu_postprocessing_ao",
 		"webgpu_postprocessing_bloom",
 		"webgpu_postprocessing_bloom",
+		"webgpu_postprocessing_bloom_emissive",
+		"webgpu_postprocessing_bloom_selective",
 		"webgpu_postprocessing_dof",
 		"webgpu_postprocessing_dof",
 		"webgpu_postprocessing_pixel",
 		"webgpu_postprocessing_pixel",
 		"webgpu_postprocessing_fxaa",
 		"webgpu_postprocessing_fxaa",

BIN
examples/screenshots/webgpu_postprocessing_bloom_emissive.jpg


BIN
examples/screenshots/webgpu_postprocessing_bloom_selective.jpg


+ 142 - 0
examples/webgpu_postprocessing_bloom_emissive.html

@@ -0,0 +1,142 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgpu - bloom emissive</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<link type="text/css" rel="stylesheet" href="main.css">
+	</head>
+	<body>
+
+		<div id="info">
+			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - bloom emissive
+		</div>
+
+		<script type="importmap">
+			{
+				"imports": {
+					"three": "../build/three.webgpu.js",
+					"three/tsl": "../build/three.webgpu.js",
+					"three/addons/": "./jsm/"
+				}
+			}
+		</script>
+
+		<script type="module">
+
+			import * as THREE from 'three';
+			import { pass, mrt, output, emissive } from 'three/tsl';
+
+			import { RGBELoader } from 'three/addons/loaders/RGBELoader.js';
+
+			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
+			import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
+
+			import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
+
+			let camera, scene, renderer;
+			let postProcessing;
+
+			init();
+
+			function init() {
+
+				const container = document.createElement( 'div' );
+				document.body.appendChild( container );
+
+				//
+
+				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.25, 20 );
+				camera.position.set( - 1.8, 0.6, 2.7 );
+
+				scene = new THREE.Scene();
+
+				new RGBELoader()
+					.setPath( 'textures/equirectangular/' )
+					.load( 'moonless_golf_1k.hdr', function ( texture ) {
+
+						texture.mapping = THREE.EquirectangularReflectionMapping;
+
+						scene.background = texture;
+						scene.environment = texture;
+
+						// model
+
+						const loader = new GLTFLoader().setPath( 'models/gltf/DamagedHelmet/glTF/' );
+						loader.load( 'DamagedHelmet.gltf', function ( gltf ) {
+
+							scene.add( gltf.scene );
+
+						} );
+
+					} );
+
+				//
+
+				renderer = new THREE.WebGPURenderer();
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				renderer.setAnimationLoop( render );
+				renderer.toneMapping = THREE.ACESFilmicToneMapping;
+				container.appendChild( renderer.domElement );
+
+				//
+
+				const scenePass = pass( scene, camera );
+				scenePass.setMRT( mrt( {
+					output,
+					emissive
+				} ) );
+
+				const outputPass = scenePass.getTextureNode();
+				const emissivePass = scenePass.getTextureNode( 'emissive' );
+
+				const bloomPass = emissivePass.bloom( 2.5, .5 );
+
+				postProcessing = new THREE.PostProcessing( renderer );
+				postProcessing.outputColorTransform = false;
+				postProcessing.outputNode = outputPass.add( bloomPass ).renderOutput();
+
+				//
+
+				const controls = new OrbitControls( camera, renderer.domElement );
+				controls.minDistance = 2;
+				controls.maxDistance = 10;
+				controls.target.set( 0, 0, - 0.2 );
+
+				window.addEventListener( 'resize', onWindowResize );
+
+				//
+
+				const gui = new GUI();
+
+				const bloomFolder = gui.addFolder( 'bloom' );
+				bloomFolder.add( bloomPass.strength, 'value', 0.0, 5.0 ).name( 'strength' );
+				bloomFolder.add( bloomPass.radius, 'value', 0.0, 1.0 ).name( 'radius' );
+
+				const toneMappingFolder = gui.addFolder( 'tone mapping' );
+				toneMappingFolder.add( renderer, 'toneMappingExposure', 0.1, 2 ).name( 'exposure' );
+
+			}
+
+			function onWindowResize() {
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			//
+
+			function render() {
+
+				postProcessing.render();
+
+			}
+
+		</script>
+
+	</body>
+</html>

+ 162 - 0
examples/webgpu_postprocessing_bloom_selective.html

@@ -0,0 +1,162 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgpu - bloom selective</title>
+		<meta charset="utf-8">
+		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
+		<link type="text/css" rel="stylesheet" href="main.css">
+	</head>
+	<body>
+
+		<div id="info">
+			<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - bloom selective
+		</div>
+
+		<script type="importmap">
+			{
+				"imports": {
+					"three": "../build/three.webgpu.js",
+					"three/tsl": "../build/three.webgpu.js",
+					"three/addons/": "./jsm/"
+				}
+			}
+		</script>
+
+		<script type="module">
+
+			import * as THREE from 'three';
+			import { pass, mrt, output, float, uniform } from 'three/tsl';
+
+			import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
+
+			import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
+
+			// scene
+
+			const scene = new THREE.Scene();
+
+			const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 200 );
+			camera.position.set( 0, 0, 20 );
+			camera.lookAt( 0, 0, 0 );
+
+			const geometry = new THREE.IcosahedronGeometry( 1, 15 );
+
+			for ( let i = 0; i < 50; i ++ ) {
+
+				const color = new THREE.Color();
+				color.setHSL( Math.random(), 0.7, Math.random() * 0.2 + 0.05 );
+
+				const bloomIntensity = Math.random() > 0.5 ? 1 : 0;
+
+				const material = new THREE.MeshBasicNodeMaterial( { color: color } );
+				material.mrtNode = mrt( {
+					bloomIntensity: uniform( bloomIntensity )
+				} );
+
+				const sphere = new THREE.Mesh( geometry, material );
+				sphere.position.x = Math.random() * 10 - 5;
+				sphere.position.y = Math.random() * 10 - 5;
+				sphere.position.z = Math.random() * 10 - 5;
+				sphere.position.normalize().multiplyScalar( Math.random() * 4.0 + 2.0 );
+				sphere.scale.setScalar( Math.random() * Math.random() + 0.5 );
+				scene.add( sphere );
+
+			}
+
+			// renderer
+
+			const renderer = new THREE.WebGPURenderer();
+			renderer.setPixelRatio( window.devicePixelRatio );
+			renderer.setSize( window.innerWidth, window.innerHeight );
+			renderer.setAnimationLoop( animate );
+			renderer.toneMapping = THREE.NeutralToneMapping;
+			document.body.appendChild( renderer.domElement );
+
+			// post processing
+
+			const scenePass = pass( scene, camera );
+			scenePass.setMRT( mrt( {
+				output,
+				bloomIntensity: float( 0 ) // default bloom intensity
+			} ) );
+
+			const outputPass = scenePass.getTextureNode();
+			const bloomIntensityPass = scenePass.getTextureNode( 'bloomIntensity' );
+
+			const bloomPass = outputPass.mul( bloomIntensityPass ).bloom();
+
+			const postProcessing = new THREE.PostProcessing( renderer );
+			postProcessing.outputColorTransform = false;
+			postProcessing.outputNode = outputPass.add( bloomPass ).renderOutput();
+
+			// controls
+
+			const controls = new OrbitControls( camera, renderer.domElement );
+			controls.maxPolarAngle = Math.PI * 0.5;
+			controls.minDistance = 1;
+			controls.maxDistance = 100;
+
+			// raycaster
+
+			const raycaster = new THREE.Raycaster();
+			const mouse = new THREE.Vector2();
+
+			window.addEventListener( 'pointerdown', ( event ) => {
+
+				mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
+				mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
+
+				raycaster.setFromCamera( mouse, camera );
+
+				const intersects = raycaster.intersectObjects( scene.children, false );
+
+				if ( intersects.length > 0 ) {
+
+					const material = intersects[ 0 ].object.material;
+
+					const bloomIntensity = material.mrtNode.getNode( 'bloomIntensity' );
+					bloomIntensity.value = bloomIntensity.value === 0 ? 1 : 0;
+
+				}
+
+			} );
+
+			// gui
+
+			const gui = new GUI();
+
+			const bloomFolder = gui.addFolder( 'bloom' );
+			bloomFolder.add( bloomPass.threshold, 'value', 0.0, 1.0 ).name( 'threshold' );
+			bloomFolder.add( bloomPass.strength, 'value', 0.0, 3 ).name( 'strength' );
+			bloomFolder.add( bloomPass.radius, 'value', 0.0, 1.0 ).name( 'radius' );
+
+			const toneMappingFolder = gui.addFolder( 'tone mapping' );
+			toneMappingFolder.add( renderer, 'toneMappingExposure', 0.1, 3 ).name( 'exposure' );
+
+			// events
+
+			window.onresize = function () {
+
+				const width = window.innerWidth;
+				const height = window.innerHeight;
+
+				camera.aspect = width / height;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( width, height );
+
+			};
+
+			// animate
+
+			function animate() {
+
+				postProcessing.render();
+
+			}
+
+		</script>
+
+	</body>
+
+</html>

+ 1 - 0
test/e2e/puppeteer.js

@@ -119,6 +119,7 @@ const exceptionList = [
 	'webgpu_sandbox',
 	'webgpu_sandbox',
 	'webgpu_sprites',
 	'webgpu_sprites',
 	'webgpu_video_panorama',
 	'webgpu_video_panorama',
+	'webgpu_postprocessing_bloom_emissive',
 
 
 	// Awaiting for WebGPU Backend support in Puppeteer
 	// Awaiting for WebGPU Backend support in Puppeteer
 	'webgpu_storage_buffer',
 	'webgpu_storage_buffer',