|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgpu - postprocessing lensflares</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 - postprocessing lensflares<br /> |
| 13 | + <a href="https://skfb.ly/6SqUF" target="_blank" rel="noopener">Space Ship Hallway</a> by |
| 14 | + <a href="https://sketchfab.com/yeeyeeman" target="_blank" rel="noopener">yeeyeeman</a> is licensed under <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank" rel="noopener">Creative Commons Attribution</a>.<br /> |
| 15 | + <a href="https://www.spacespheremaps.com/planetary-spheremaps/" target="_blank" rel="noopener">Ice Planet Close</a> from <a href="https://www.spacespheremaps.com/" target="_blank" rel="noopener">Space Spheremaps</a>. |
| 16 | + </div> |
| 17 | + |
| 18 | + <script type="importmap"> |
| 19 | + { |
| 20 | + "imports": { |
| 21 | + "three": "../build/three.webgpu.js", |
| 22 | + "three/tsl": "../build/three.webgpu.js", |
| 23 | + "three/addons/": "./jsm/" |
| 24 | + } |
| 25 | + } |
| 26 | + </script> |
| 27 | + |
| 28 | + <script type="module"> |
| 29 | + |
| 30 | + import * as THREE from 'three'; |
| 31 | + import { pass, mrt, output, emissive, uniform } from 'three/tsl'; |
| 32 | + import { bloom } from 'three/addons/tsl/display/BloomNode.js'; |
| 33 | + import { lensflare } from 'three/addons/tsl/display/LensflareNode.js'; |
| 34 | + import { gaussianBlur } from 'three/addons/tsl/display/GaussianBlurNode.js'; |
| 35 | + |
| 36 | + import { UltraHDRLoader } from 'three/addons/loaders/UltraHDRLoader.js'; |
| 37 | + |
| 38 | + import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
| 39 | + import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js'; |
| 40 | + |
| 41 | + import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; |
| 42 | + import Stats from 'three/addons/libs/stats.module.js'; |
| 43 | + |
| 44 | + let camera, scene, renderer, controls, stats; |
| 45 | + let postProcessing; |
| 46 | + |
| 47 | + init(); |
| 48 | + |
| 49 | + async function init() { |
| 50 | + |
| 51 | + const container = document.createElement( 'div' ); |
| 52 | + document.body.appendChild( container ); |
| 53 | + |
| 54 | + // |
| 55 | + |
| 56 | + camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 ); |
| 57 | + camera.position.set( 0, 0.5, - 0.5 ); |
| 58 | + |
| 59 | + scene = new THREE.Scene(); |
| 60 | + |
| 61 | + const texture = await new UltraHDRLoader().loadAsync( 'textures/equirectangular/ice_planet_close.jpg' ); |
| 62 | + |
| 63 | + texture.mapping = THREE.EquirectangularReflectionMapping; |
| 64 | + |
| 65 | + scene.background = texture; |
| 66 | + scene.environment = texture; |
| 67 | + |
| 68 | + scene.backgroundIntensity = 2; |
| 69 | + scene.environmentIntensity = 15; |
| 70 | + |
| 71 | + // model |
| 72 | + |
| 73 | + const loader = new GLTFLoader(); |
| 74 | + const gltf = await loader.loadAsync( 'models/gltf/space_ship_hallway.glb' ); |
| 75 | + |
| 76 | + const object = gltf.scene; |
| 77 | + |
| 78 | + const aabb = new THREE.Box3().setFromObject( object ); |
| 79 | + const center = aabb.getCenter( new THREE.Vector3() ); |
| 80 | + |
| 81 | + object.position.x += ( object.position.x - center.x ); |
| 82 | + object.position.y += ( object.position.y - center.y ); |
| 83 | + object.position.z += ( object.position.z - center.z ); |
| 84 | + |
| 85 | + scene.add( object ); |
| 86 | + |
| 87 | + // |
| 88 | + |
| 89 | + renderer = new THREE.WebGPURenderer(); |
| 90 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 91 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 92 | + renderer.setAnimationLoop( render ); |
| 93 | + renderer.toneMapping = THREE.ACESFilmicToneMapping; |
| 94 | + container.appendChild( renderer.domElement ); |
| 95 | + |
| 96 | + // |
| 97 | + |
| 98 | + const scenePass = pass( scene, camera ); |
| 99 | + scenePass.setMRT( mrt( { |
| 100 | + output, |
| 101 | + emissive |
| 102 | + } ) ); |
| 103 | + |
| 104 | + const outputPass = scenePass.getTextureNode(); |
| 105 | + const emissivePass = scenePass.getTextureNode( 'emissive' ); |
| 106 | + |
| 107 | + const bloomPass = bloom( emissivePass, 1, 1 ); |
| 108 | + |
| 109 | + const threshold = uniform( 0.5 ); |
| 110 | + const ghostAttenuationFactor = uniform( 25 ); |
| 111 | + const ghostSpacing = uniform( 0.25 ); |
| 112 | + |
| 113 | + const flarePass = lensflare( bloomPass, { |
| 114 | + threshold, |
| 115 | + ghostAttenuationFactor, |
| 116 | + ghostSpacing |
| 117 | + } ); |
| 118 | + |
| 119 | + const blurPass = gaussianBlur( flarePass, 8 ); // optional (blurring produces better flare quality but also adds some overhead) |
| 120 | + |
| 121 | + postProcessing = new THREE.PostProcessing( renderer ); |
| 122 | + postProcessing.outputNode = outputPass.add( bloomPass ).add( blurPass ); |
| 123 | + |
| 124 | + // |
| 125 | + |
| 126 | + controls = new OrbitControls( camera, renderer.domElement ); |
| 127 | + controls.enableDamping = true; |
| 128 | + controls.enablePan = false; |
| 129 | + controls.enableZoom = false; |
| 130 | + controls.target.copy( camera.position ); |
| 131 | + controls.target.z -= 0.01; |
| 132 | + controls.update(); |
| 133 | + |
| 134 | + window.addEventListener( 'resize', onWindowResize ); |
| 135 | + |
| 136 | + // |
| 137 | + |
| 138 | + stats = new Stats(); |
| 139 | + document.body.appendChild( stats.dom ); |
| 140 | + |
| 141 | + // |
| 142 | + |
| 143 | + const gui = new GUI(); |
| 144 | + |
| 145 | + const bloomFolder = gui.addFolder( 'bloom' ); |
| 146 | + bloomFolder.add( bloomPass.strength, 'value', 0.0, 2.0 ).name( 'strength' ); |
| 147 | + bloomFolder.add( bloomPass.radius, 'value', 0.0, 1.0 ).name( 'radius' ); |
| 148 | + |
| 149 | + const lensflareFolder = gui.addFolder( 'lensflare' ); |
| 150 | + lensflareFolder.add( threshold, 'value', 0.0, 1.0 ).name( 'threshold' ); |
| 151 | + lensflareFolder.add( ghostAttenuationFactor, 'value', 10.0, 50.0 ).name( 'attenuation' ); |
| 152 | + lensflareFolder.add( ghostSpacing, 'value', 0.0, 0.3 ).name( 'spacing' ); |
| 153 | + |
| 154 | + const toneMappingFolder = gui.addFolder( 'tone mapping' ); |
| 155 | + toneMappingFolder.add( renderer, 'toneMappingExposure', 0.1, 2 ).name( 'exposure' ); |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + function onWindowResize() { |
| 160 | + |
| 161 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 162 | + camera.updateProjectionMatrix(); |
| 163 | + |
| 164 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 165 | + |
| 166 | + } |
| 167 | + |
| 168 | + // |
| 169 | + |
| 170 | + function render() { |
| 171 | + |
| 172 | + stats.update(); |
| 173 | + |
| 174 | + controls.update(); |
| 175 | + |
| 176 | + postProcessing.render(); |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + </script> |
| 181 | + |
| 182 | + </body> |
| 183 | +</html> |
0 commit comments