|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgpu - bloom selective</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0"> |
| 7 | + <link type="text/css" rel="stylesheet" href="main.css"> |
| 8 | + </head> |
| 9 | + <body> |
| 10 | + |
| 11 | + <div id="info"> |
| 12 | + <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - bloom selective |
| 13 | + </div> |
| 14 | + |
| 15 | + <script type="importmap"> |
| 16 | + { |
| 17 | + "imports": { |
| 18 | + "three": "../build/three.webgpu.js", |
| 19 | + "three/tsl": "../build/three.webgpu.js", |
| 20 | + "three/addons/": "./jsm/" |
| 21 | + } |
| 22 | + } |
| 23 | + </script> |
| 24 | + |
| 25 | + <script type="module"> |
| 26 | + |
| 27 | + import * as THREE from 'three'; |
| 28 | + import { pass, mrt, output, float, uniform } from 'three/tsl'; |
| 29 | + |
| 30 | + import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
| 31 | + |
| 32 | + import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; |
| 33 | + |
| 34 | + // scene |
| 35 | + |
| 36 | + const scene = new THREE.Scene(); |
| 37 | + |
| 38 | + const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 200 ); |
| 39 | + camera.position.set( 0, 0, 20 ); |
| 40 | + camera.lookAt( 0, 0, 0 ); |
| 41 | + |
| 42 | + const geometry = new THREE.IcosahedronGeometry( 1, 15 ); |
| 43 | + |
| 44 | + for ( let i = 0; i < 50; i ++ ) { |
| 45 | + |
| 46 | + const color = new THREE.Color(); |
| 47 | + color.setHSL( Math.random(), 0.7, Math.random() * 0.2 + 0.05 ); |
| 48 | + |
| 49 | + const bloomIntensity = Math.random() > 0.5 ? 1 : 0; |
| 50 | + |
| 51 | + const material = new THREE.MeshBasicNodeMaterial( { color: color } ); |
| 52 | + material.mrtNode = mrt( { |
| 53 | + bloomIntensity: uniform( bloomIntensity ) |
| 54 | + } ); |
| 55 | + |
| 56 | + const sphere = new THREE.Mesh( geometry, material ); |
| 57 | + sphere.position.x = Math.random() * 10 - 5; |
| 58 | + sphere.position.y = Math.random() * 10 - 5; |
| 59 | + sphere.position.z = Math.random() * 10 - 5; |
| 60 | + sphere.position.normalize().multiplyScalar( Math.random() * 4.0 + 2.0 ); |
| 61 | + sphere.scale.setScalar( Math.random() * Math.random() + 0.5 ); |
| 62 | + scene.add( sphere ); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + // renderer |
| 67 | + |
| 68 | + const renderer = new THREE.WebGPURenderer(); |
| 69 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 70 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 71 | + renderer.setAnimationLoop( animate ); |
| 72 | + renderer.toneMapping = THREE.NeutralToneMapping; |
| 73 | + document.body.appendChild( renderer.domElement ); |
| 74 | + |
| 75 | + // post processing |
| 76 | + |
| 77 | + const scenePass = pass( scene, camera ); |
| 78 | + scenePass.setMRT( mrt( { |
| 79 | + output, |
| 80 | + bloomIntensity: float( 0 ) // default bloom intensity |
| 81 | + } ) ); |
| 82 | + |
| 83 | + const outputPass = scenePass.getTextureNode(); |
| 84 | + const bloomIntensityPass = scenePass.getTextureNode( 'bloomIntensity' ); |
| 85 | + |
| 86 | + const bloomPass = outputPass.mul( bloomIntensityPass ).bloom(); |
| 87 | + |
| 88 | + const postProcessing = new THREE.PostProcessing( renderer ); |
| 89 | + postProcessing.outputColorTransform = false; |
| 90 | + postProcessing.outputNode = outputPass.add( bloomPass ).renderOutput(); |
| 91 | + |
| 92 | + // controls |
| 93 | + |
| 94 | + const controls = new OrbitControls( camera, renderer.domElement ); |
| 95 | + controls.maxPolarAngle = Math.PI * 0.5; |
| 96 | + controls.minDistance = 1; |
| 97 | + controls.maxDistance = 100; |
| 98 | + |
| 99 | + // raycaster |
| 100 | + |
| 101 | + const raycaster = new THREE.Raycaster(); |
| 102 | + const mouse = new THREE.Vector2(); |
| 103 | + |
| 104 | + window.addEventListener( 'pointerdown', ( event ) => { |
| 105 | + |
| 106 | + mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1; |
| 107 | + mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1; |
| 108 | + |
| 109 | + raycaster.setFromCamera( mouse, camera ); |
| 110 | + |
| 111 | + const intersects = raycaster.intersectObjects( scene.children, false ); |
| 112 | + |
| 113 | + if ( intersects.length > 0 ) { |
| 114 | + |
| 115 | + const material = intersects[ 0 ].object.material; |
| 116 | + |
| 117 | + const bloomIntensity = material.mrtNode.getNode( 'bloomIntensity' ); |
| 118 | + bloomIntensity.value = bloomIntensity.value === 0 ? 1 : 0; |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | + } ); |
| 123 | + |
| 124 | + // gui |
| 125 | + |
| 126 | + const gui = new GUI(); |
| 127 | + |
| 128 | + const bloomFolder = gui.addFolder( 'bloom' ); |
| 129 | + bloomFolder.add( bloomPass.threshold, 'value', 0.0, 1.0 ).name( 'threshold' ); |
| 130 | + bloomFolder.add( bloomPass.strength, 'value', 0.0, 3 ).name( 'strength' ); |
| 131 | + bloomFolder.add( bloomPass.radius, 'value', 0.0, 1.0 ).name( 'radius' ); |
| 132 | + |
| 133 | + const toneMappingFolder = gui.addFolder( 'tone mapping' ); |
| 134 | + toneMappingFolder.add( renderer, 'toneMappingExposure', 0.1, 3 ).name( 'exposure' ); |
| 135 | + |
| 136 | + // events |
| 137 | + |
| 138 | + window.onresize = function () { |
| 139 | + |
| 140 | + const width = window.innerWidth; |
| 141 | + const height = window.innerHeight; |
| 142 | + |
| 143 | + camera.aspect = width / height; |
| 144 | + camera.updateProjectionMatrix(); |
| 145 | + |
| 146 | + renderer.setSize( width, height ); |
| 147 | + |
| 148 | + }; |
| 149 | + |
| 150 | + // animate |
| 151 | + |
| 152 | + function animate() { |
| 153 | + |
| 154 | + postProcessing.render(); |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | + </script> |
| 159 | + |
| 160 | + </body> |
| 161 | + |
| 162 | +</html> |
0 commit comments