|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgpu - memory test I</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="example.css"> |
| 8 | + </head> |
| 9 | + |
| 10 | + <body> |
| 11 | + <div id="info" class="invert"> |
| 12 | + <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a> |
| 13 | + |
| 14 | + <div class="title-wrapper"> |
| 15 | + <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>WebGPU Memory Test I</span> |
| 16 | + </div> |
| 17 | + |
| 18 | + <small> |
| 19 | + This example tests memory management with WebGPU renderer. |
| 20 | + <br /> Spheres are created, rendered, and disposed in each frame. |
| 21 | + </small> |
| 22 | + </div> |
| 23 | + |
| 24 | + <script type="importmap"> |
| 25 | + { |
| 26 | + "imports": { |
| 27 | + "three": "../build/three.webgpu.js", |
| 28 | + "three/tsl": "../build/three.tsl.js", |
| 29 | + "three/webgpu": "../build/three.webgpu.js", |
| 30 | + "three/addons/": "./jsm/" |
| 31 | + } |
| 32 | + } |
| 33 | + </script> |
| 34 | + |
| 35 | + <script type="module"> |
| 36 | + |
| 37 | + import * as THREE from 'three/webgpu'; |
| 38 | + import { pass, mrt, directionToColor, normalView, screenUV, context, sample, colorToDirection } from 'three/tsl'; |
| 39 | + import { outline } from 'three/addons/tsl/display/OutlineNode.js'; |
| 40 | + import { ao } from 'three/addons/tsl/display/GTAONode.js'; |
| 41 | + |
| 42 | + import { Inspector } from 'three/addons/inspector/Inspector.js'; |
| 43 | + |
| 44 | + let camera, scene, renderer, light; |
| 45 | + let generateMeshes = true; |
| 46 | + let mesh; |
| 47 | + let postProcessing; |
| 48 | + let aoNode, outlineNode, scenePass, prePass; |
| 49 | + const selectedObjects = []; |
| 50 | + |
| 51 | + const params = { |
| 52 | + castShadow: true, |
| 53 | + enablePP: false, |
| 54 | + enableOutline: true, |
| 55 | + enableAO: true, |
| 56 | + recreateLight: () => { |
| 57 | + |
| 58 | + // Remove existing light |
| 59 | + if ( light ) { |
| 60 | + |
| 61 | + scene.remove( light ); |
| 62 | + light.dispose(); |
| 63 | + light = null; |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + // Create new directional light |
| 68 | + light = new THREE.DirectionalLight( 0xffffff, 1 ); |
| 69 | + light.position.set( Math.random() * 200 - 100, 100, Math.random() * 200 - 100 ); |
| 70 | + light.castShadow = params.castShadow; |
| 71 | + scene.add( light ); |
| 72 | + |
| 73 | + }, |
| 74 | + start: () => { |
| 75 | + |
| 76 | + generateMeshes = true; |
| 77 | + |
| 78 | + }, |
| 79 | + stop: () => { |
| 80 | + |
| 81 | + generateMeshes = false; |
| 82 | + |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + init(); |
| 87 | + |
| 88 | + async function init() { |
| 89 | + |
| 90 | + const container = document.createElement( 'div' ); |
| 91 | + document.body.appendChild( container ); |
| 92 | + |
| 93 | + camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 10000 ); |
| 94 | + camera.position.z = 200; |
| 95 | + |
| 96 | + scene = new THREE.Scene(); |
| 97 | + scene.background = new THREE.Color( 0xffffff ); |
| 98 | + |
| 99 | + renderer = new THREE.WebGPURenderer(); |
| 100 | + renderer.shadowMap.enabled = true; |
| 101 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 102 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 103 | + renderer.setAnimationLoop( animate ); |
| 104 | + renderer.inspector = new Inspector(); |
| 105 | + container.appendChild( renderer.domElement ); |
| 106 | + |
| 107 | + await renderer.init(); |
| 108 | + |
| 109 | + light = new THREE.DirectionalLight( 0xffffff, 1 ); |
| 110 | + light.position.set( 0, 100, 0 ); |
| 111 | + light.castShadow = true; |
| 112 | + scene.add( light ); |
| 113 | + |
| 114 | + const planeGeometry = new THREE.PlaneGeometry( 1000, 1000 ); |
| 115 | + const planeMaterial = new THREE.MeshLambertMaterial( { color: 0xcccccc } ); |
| 116 | + const plane = new THREE.Mesh( planeGeometry, planeMaterial ); |
| 117 | + plane.rotation.x = - Math.PI / 2; |
| 118 | + plane.position.y = - 100; |
| 119 | + plane.receiveShadow = true; |
| 120 | + scene.add( plane ); |
| 121 | + |
| 122 | + // Inspector UI |
| 123 | + const gui = renderer.inspector.createParameters( 'Settings' ); |
| 124 | + |
| 125 | + gui.add( params, 'castShadow' ).name( 'Cast Shadow' ).onChange( ( value ) => { |
| 126 | + |
| 127 | + if ( light ) light.castShadow = value; |
| 128 | + |
| 129 | + } ); |
| 130 | + |
| 131 | + const ppFolder = gui.addFolder( 'Post Processing' ); |
| 132 | + ppFolder.add( params, 'enablePP' ).name( 'Enable' ).onChange( updatePostProcessing ); |
| 133 | + ppFolder.add( params, 'enableOutline' ).name( 'Outline' ).onChange( updatePostProcessing ); |
| 134 | + ppFolder.add( params, 'enableAO' ).name( 'AO' ).onChange( updatePostProcessing ); |
| 135 | + |
| 136 | + gui.add( params, 'recreateLight' ).name( 'Recreate Directional Light' ); |
| 137 | + gui.add( params, 'start' ).name( 'Start Creating Meshes' ); |
| 138 | + gui.add( params, 'stop' ).name( 'Stop Creating Meshes' ); |
| 139 | + |
| 140 | + window.addEventListener( 'resize', onWindowResize ); |
| 141 | + |
| 142 | + } |
| 143 | + |
| 144 | + function updatePostProcessing() { |
| 145 | + |
| 146 | + if ( postProcessing ) { |
| 147 | + |
| 148 | + postProcessing.dispose(); |
| 149 | + postProcessing = null; |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | + if ( scenePass ) { |
| 154 | + |
| 155 | + scenePass.dispose(); |
| 156 | + scenePass = null; |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | + if ( prePass ) { |
| 161 | + |
| 162 | + prePass.dispose(); |
| 163 | + prePass = null; |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + if ( aoNode ) { |
| 168 | + |
| 169 | + aoNode.dispose(); |
| 170 | + aoNode = null; |
| 171 | + |
| 172 | + } |
| 173 | + |
| 174 | + if ( outlineNode ) { |
| 175 | + |
| 176 | + outlineNode.dispose(); |
| 177 | + outlineNode = null; |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + if ( params.enablePP ) { |
| 182 | + |
| 183 | + postProcessing = new THREE.PostProcessing( renderer ); |
| 184 | + |
| 185 | + scenePass = pass( scene, camera ); |
| 186 | + let colorNode = scenePass; |
| 187 | + |
| 188 | + if ( params.enableAO ) { |
| 189 | + |
| 190 | + prePass = pass( scene, camera ); |
| 191 | + prePass.setMRT( mrt( { |
| 192 | + output: directionToColor( normalView ) |
| 193 | + } ) ); |
| 194 | + |
| 195 | + const prePassNormal = sample( ( uv ) => { |
| 196 | + |
| 197 | + return colorToDirection( prePass.getTextureNode().sample( uv ) ); |
| 198 | + |
| 199 | + } ); |
| 200 | + const prePassDepth = prePass.getTextureNode( 'depth' ); |
| 201 | + |
| 202 | + aoNode = ao( prePassDepth, prePassNormal, camera ); |
| 203 | + |
| 204 | + scenePass.contextNode = context( { |
| 205 | + ao: aoNode.getTextureNode().sample( screenUV ).r |
| 206 | + } ); |
| 207 | + |
| 208 | + } |
| 209 | + |
| 210 | + if ( params.enableOutline ) { |
| 211 | + |
| 212 | + outlineNode = outline( scene, camera, { |
| 213 | + selectedObjects: selectedObjects |
| 214 | + } ); |
| 215 | + colorNode = colorNode.add( outlineNode ); |
| 216 | + |
| 217 | + } |
| 218 | + |
| 219 | + postProcessing.outputNode = colorNode; |
| 220 | + |
| 221 | + } |
| 222 | + |
| 223 | + } |
| 224 | + |
| 225 | + function onWindowResize() { |
| 226 | + |
| 227 | + const width = window.innerWidth; |
| 228 | + const height = window.innerHeight; |
| 229 | + |
| 230 | + camera.aspect = width / height; |
| 231 | + camera.updateProjectionMatrix(); |
| 232 | + |
| 233 | + renderer.setSize( width, height ); |
| 234 | + |
| 235 | + } |
| 236 | + |
| 237 | + function createImage() { |
| 238 | + |
| 239 | + const canvas = document.createElement( 'canvas' ); |
| 240 | + canvas.width = 256; |
| 241 | + canvas.height = 256; |
| 242 | + |
| 243 | + const canvas2DContext = canvas.getContext( '2d' ); |
| 244 | + canvas2DContext.fillStyle = 'rgb(' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ',' + Math.floor( Math.random() * 256 ) + ')'; |
| 245 | + canvas2DContext.fillRect( 0, 0, 256, 256 ); |
| 246 | + |
| 247 | + return canvas; |
| 248 | + |
| 249 | + } |
| 250 | + |
| 251 | + // |
| 252 | + |
| 253 | + function animate() { |
| 254 | + |
| 255 | + if ( generateMeshes ) { |
| 256 | + |
| 257 | + if ( mesh ) { |
| 258 | + |
| 259 | + scene.remove( mesh ); |
| 260 | + |
| 261 | + mesh.geometry.dispose(); |
| 262 | + mesh.material.map.dispose(); |
| 263 | + mesh.material.dispose(); |
| 264 | + |
| 265 | + } |
| 266 | + |
| 267 | + const geometry = new THREE.SphereGeometry( 50, Math.random() * 64, Math.random() * 32 ); |
| 268 | + |
| 269 | + const texture = new THREE.CanvasTexture( createImage() ); |
| 270 | + |
| 271 | + const material = new THREE.MeshLambertMaterial( { map: texture } ); |
| 272 | + |
| 273 | + mesh = new THREE.Mesh( geometry, material ); |
| 274 | + mesh.castShadow = true; |
| 275 | + |
| 276 | + scene.add( mesh ); |
| 277 | + |
| 278 | + if ( outlineNode ) { |
| 279 | + |
| 280 | + selectedObjects[ 0 ] = mesh; |
| 281 | + |
| 282 | + } |
| 283 | + |
| 284 | + } |
| 285 | + |
| 286 | + if ( postProcessing ) { |
| 287 | + |
| 288 | + postProcessing.render(); |
| 289 | + |
| 290 | + } else { |
| 291 | + |
| 292 | + renderer.render( scene, camera ); |
| 293 | + |
| 294 | + } |
| 295 | + |
| 296 | + } |
| 297 | + |
| 298 | + </script> |
| 299 | + |
| 300 | + </body> |
| 301 | +</html> |
0 commit comments