Explorar o código

Examples: Add radial blur demo. (#32297)

Michael Herzog hai 1 mes
pai
achega
4345fbc1f2

+ 2 - 1
examples/files.json

@@ -414,13 +414,14 @@
 		"webgpu_postprocessing_difference",
 		"webgpu_postprocessing_dof",
 		"webgpu_postprocessing_dof_basic",
-		"webgpu_postprocessing_pixel",
 		"webgpu_postprocessing_fxaa",
 		"webgpu_postprocessing_lensflare",
 		"webgpu_postprocessing_masking",
 		"webgpu_postprocessing_ca",
 		"webgpu_postprocessing_motion_blur",
 		"webgpu_postprocessing_outline",
+		"webgpu_postprocessing_pixel",
+		"webgpu_postprocessing_radial_blur",
 		"webgpu_postprocessing_smaa",
 		"webgpu_postprocessing_sobel",
 		"webgpu_postprocessing_ssaa",

+ 68 - 0
examples/jsm/tsl/display/radialBlur.js

@@ -0,0 +1,68 @@
+import { interleavedGradientNoise, Fn, vec2, vec4, mix, uv, Loop, premultiplyAlpha, unpremultiplyAlpha, int, float, nodeObject, convertToTexture, screenCoordinate } from 'three/tsl';
+
+/**
+ * This TSL function blurs an image in a circular pattern, radiating from a configurable center point in screen space.
+ *
+ * Radial blurs can be used for different kind of effects like producing simple faked lighting effects also known as
+ * "light shafts". The major limitation of this specific usage is the center point can only be defined in 2D so the
+ * effect does not honor the depth of 3D objects. Consequently, it is not intended for physically correct lit scenes.
+ *
+ * @tsl
+ * @function
+ * @param {Node<vec4>} textureNode - The texture node that should be blurred.
+ * @param {Object} [options={}] - Additional options for the radial blur effect.
+ * @param {Node<vec2>} [options.center=vec2(0.5, 0.5)] - The center of the light in screen uvs.
+ * @param {Node<int>} [options.weight=float(0.9)] - Base weight factor for each sample in the range `[0,1]`.
+ * @param {Node<int>} [options.decay=float(0.95)] - Decreases the weight factor so each iteration adds less to the sum. Must be in the range `[0,1]`.
+ * If you increase the sample count, you have to increase this option as well to avoid a darking effect.
+ * @param {Node<int>} [options.count=int(32)] - The number if iterations. Should be in the range `[16,64]`.
+ * @param {Node<int>} [options.exposure=float(5)] - Exposure control of the blur.
+ * @return {Node<vec4>} The blurred texture node.
+ */
+export const radialBlur = /*#__PURE__*/ Fn( ( [ textureNode, options = {} ] ) => {
+
+	textureNode = convertToTexture( textureNode );
+
+	const center = nodeObject( options.center ) || vec2( 0.5, 0.5 );
+	const weight = nodeObject( options.weight ) || float( 0.9 );
+	const decay = nodeObject( options.decay ) || float( 0.95 );
+	const count = nodeObject( options.count ) || int( 32 );
+	const exposure = nodeObject( options.exposure ) || float( 5 );
+	const premultipliedAlpha = options.premultipliedAlpha || false;
+
+	const tap = ( uv ) => {
+
+		const sample = textureNode.sample( uv );
+
+		return premultipliedAlpha ? premultiplyAlpha( sample ) : sample;
+
+	};
+
+	const sampleUv = vec2( textureNode.uvNode || uv() );
+
+	const base = tap( sampleUv ).toConst();
+	const blur = vec4().toVar();
+	const offset = center.sub( sampleUv ).div( count ).toConst();
+	const w = float( weight ).toVar();
+
+	const noise = interleavedGradientNoise( screenCoordinate );
+	sampleUv.addAssign( offset.mul( noise ) ); // mitigate banding
+
+	Loop( { start: int( 0 ), end: int( count ), type: 'int', condition: '<' }, () => {
+
+		sampleUv.addAssign( offset );
+		const sample = tap( sampleUv );
+
+		blur.addAssign( sample.mul( w ) );
+		w.mulAssign( decay );
+
+	} );
+
+	blur.divAssign( count );
+	blur.mulAssign( exposure );
+
+	const color = mix( blur, base.mul( 2 ), 0.5 );
+
+	return premultipliedAlpha ? unpremultiplyAlpha( color ) : color;
+
+} );

BIN=BIN
examples/screenshots/webgpu_postprocessing_radial_blur.jpg


+ 201 - 0
examples/webgpu_postprocessing_radial_blur.html

@@ -0,0 +1,201 @@
+<!DOCTYPE html>
+<html lang="en">
+	<head>
+		<title>three.js webgpu - radial blur</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="example.css">
+	</head>
+
+	<body>
+
+		<div id="info">
+			<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
+
+			<div class="title-wrapper">
+				<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Radial Blur</span>
+			</div>
+
+			<small>Radial Blur for faked light shafts.</small>
+		</div>
+
+		<script type="importmap">
+			{
+				"imports": {
+					"three": "../build/three.webgpu.js",
+					"three/webgpu": "../build/three.webgpu.js",
+					"three/tsl": "../build/three.tsl.js",
+					"three/addons/": "./jsm/"
+				}
+			}
+		</script>
+
+		<script type="module">
+
+			import * as THREE from 'three/webgpu';
+			import { float, int, pass, uniform } from 'three/tsl';
+			import { radialBlur } from 'three/addons/tsl/display/radialBlur.js';
+
+			import { Inspector } from 'three/addons/inspector/Inspector.js';
+
+			const params = {
+				enabled: true,
+				animated: true
+			};
+
+			let camera, scene, renderer, timer, group;
+			let postProcessing;
+
+			init();
+
+			async function init() {
+
+				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 200 );
+				camera.position.z = 50;
+
+				scene = new THREE.Scene();
+				scene.background = new THREE.Color( 0x000000 );
+
+				timer = new THREE.Timer();
+
+				//
+
+				const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x8d8d8d );
+				hemiLight.position.set( 0, 1000, 0 );
+				scene.add( hemiLight );
+
+				const pointLight = new THREE.PointLight( 0xffffff, 1000 );
+				pointLight.position.set( 0, 0, 0 );
+				scene.add( pointLight );
+
+				//
+
+				group = new THREE.Group();
+
+				const geometry = new THREE.TetrahedronGeometry();
+				const material = new THREE.MeshStandardMaterial( { flatShading: true } );
+
+				const mesh = new THREE.InstancedMesh( geometry, material, 100 );
+				const dummy = new THREE.Object3D();
+				const col = new THREE.Color();
+				const center = new THREE.Vector2();
+
+				for ( let i = 0; i < mesh.count; i ++ ) {
+
+					dummy.position.x = Math.random() * 50 - 25;
+					dummy.position.y = Math.random() * 50 - 25;
+					dummy.position.z = Math.random() * 50 - 25;
+
+					center.set( dummy.position.x, dummy.position.y );
+
+					// make sure tetrahedrons are not positioned at the origin
+
+					if ( center.length() < 6 ) {
+
+						center.normalize().multiplyScalar( 6 );
+						dummy.position.x = center.x;
+						dummy.position.y = center.y;
+
+					}
+
+					dummy.scale.setScalar( Math.random() * 2 + 1 );
+
+					dummy.rotation.x = Math.random() * Math.PI;
+					dummy.rotation.y = Math.random() * Math.PI;
+					dummy.rotation.z = Math.random() * Math.PI;
+
+					dummy.updateMatrix();
+					mesh.setMatrixAt( i, dummy.matrix );
+
+					col.setHSL( 0.55 + ( i / mesh.count ) * 0.15, 1, 0.2 );
+
+					mesh.setColorAt( i, col );
+
+				}
+
+				group.add( mesh );
+				scene.add( group );
+
+				renderer = new THREE.WebGPURenderer();
+				renderer.setPixelRatio( window.devicePixelRatio );
+				renderer.setSize( window.innerWidth, window.innerHeight );
+				renderer.setAnimationLoop( animate );
+				renderer.inspector = new Inspector();
+				renderer.toneMapping = THREE.NeutralToneMapping;
+				document.body.appendChild( renderer.domElement );
+
+				// post processing
+
+				postProcessing = new THREE.PostProcessing( renderer );
+
+				const scenePass = pass( scene, camera );
+
+				const weightUniform = uniform( float( 0.9 ) );
+				const decayUniform = uniform( float( 0.95 ) );
+				const exposureUniform = uniform( int( 5 ) );
+				const countUniform = uniform( int( 32 ) );
+
+				const blurPass = radialBlur( scenePass, { weight: weightUniform, decay: decayUniform, count: countUniform, exposure: exposureUniform } );
+
+				postProcessing.outputNode = blurPass;
+
+				//
+
+				window.addEventListener( 'resize', onWindowResize );
+
+				//
+
+				const gui = renderer.inspector.createParameters( 'Settings' );
+				gui.add( weightUniform, 'value', 0, 1 ).name( 'weight' );
+				gui.add( decayUniform, 'value', 0, 1 ).name( 'decay' );
+				gui.add( countUniform, 'value', 16, 64, 1 ).name( 'sample count' );
+				gui.add( exposureUniform, 'value', 1, 10 ).name( 'exposure' );
+				gui.add( params, 'enabled' ).onChange( ( value ) => {
+			
+					if ( value === true ) {
+
+						postProcessing.outputNode = blurPass;
+
+					} else {
+
+						postProcessing.outputNode = scenePass;
+
+					}
+
+					postProcessing.needsUpdate = true;
+
+				} );
+				gui.add( params, 'animated' );
+
+			}
+
+			function onWindowResize() {
+
+				camera.aspect = window.innerWidth / window.innerHeight;
+				camera.updateProjectionMatrix();
+
+				renderer.setSize( window.innerWidth, window.innerHeight );
+
+			}
+
+			//
+
+			function animate() {
+
+				timer.update();
+				const delta = timer.getDelta();
+
+				if ( params.animated === true ) {
+
+					group.rotation.y += delta * 0.1;
+
+				}
+
+				postProcessing.render();
+
+			}
+
+		</script>
+
+	</body>
+</html>

粤ICP备19079148号