|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js webgpu - materials - alpha hash</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 | + <script type="importmap"> |
| 11 | + { |
| 12 | + "imports": { |
| 13 | + "three": "../build/three.webgpu.js", |
| 14 | + "three/tsl": "../build/three.webgpu.js", |
| 15 | + "three/addons/": "./jsm/" |
| 16 | + } |
| 17 | + } |
| 18 | + </script> |
| 19 | + |
| 20 | + <script type="module"> |
| 21 | + |
| 22 | + import * as THREE from 'three'; |
| 23 | + |
| 24 | + import Stats from 'three/addons/libs/stats.module.js'; |
| 25 | + import { GUI } from 'three/addons/libs/lil-gui.module.min.js'; |
| 26 | + import { OrbitControls } from 'three/addons/controls/OrbitControls.js'; |
| 27 | + import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js'; |
| 28 | + |
| 29 | + import { ssaaPass } from 'three/addons/tsl/display/SSAAPassNode.js'; |
| 30 | + |
| 31 | + let camera, scene, renderer, controls, stats, mesh, material, postProcessing; |
| 32 | + |
| 33 | + const amount = parseInt( window.location.search.slice( 1 ) ) || 3; |
| 34 | + const count = Math.pow( amount, 3 ); |
| 35 | + |
| 36 | + const color = new THREE.Color(); |
| 37 | + |
| 38 | + const params = { |
| 39 | + alpha: 0.5, |
| 40 | + alphaHash: true |
| 41 | + }; |
| 42 | + |
| 43 | + init(); |
| 44 | + |
| 45 | + async function init() { |
| 46 | + |
| 47 | + camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 ); |
| 48 | + camera.position.set( amount, amount, amount ); |
| 49 | + camera.lookAt( 0, 0, 0 ); |
| 50 | + |
| 51 | + scene = new THREE.Scene(); |
| 52 | + |
| 53 | + const geometry = new THREE.IcosahedronGeometry( 0.5, 3 ); |
| 54 | + |
| 55 | + material = new THREE.MeshStandardMaterial( { |
| 56 | + color: 0xffffff, |
| 57 | + alphaHash: params.alphaHash, |
| 58 | + opacity: params.alpha |
| 59 | + } ); |
| 60 | + |
| 61 | + mesh = new THREE.InstancedMesh( geometry, material, count ); |
| 62 | + |
| 63 | + let i = 0; |
| 64 | + const offset = ( amount - 1 ) / 2; |
| 65 | + |
| 66 | + const matrix = new THREE.Matrix4(); |
| 67 | + |
| 68 | + for ( let x = 0; x < amount; x ++ ) { |
| 69 | + |
| 70 | + for ( let y = 0; y < amount; y ++ ) { |
| 71 | + |
| 72 | + for ( let z = 0; z < amount; z ++ ) { |
| 73 | + |
| 74 | + matrix.setPosition( offset - x, offset - y, offset - z ); |
| 75 | + |
| 76 | + mesh.setMatrixAt( i, matrix ); |
| 77 | + mesh.setColorAt( i, color.setHex( Math.random() * 0xffffff ) ); |
| 78 | + |
| 79 | + i ++; |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + scene.add( mesh ); |
| 88 | + |
| 89 | + // |
| 90 | + |
| 91 | + renderer = new THREE.WebGPURenderer(); |
| 92 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 93 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 94 | + renderer.setAnimationLoop( animate ); |
| 95 | + document.body.appendChild( renderer.domElement ); |
| 96 | + |
| 97 | + await renderer.init(); |
| 98 | + |
| 99 | + // |
| 100 | + |
| 101 | + const environment = new RoomEnvironment(); |
| 102 | + const pmremGenerator = new THREE.PMREMGenerator( renderer ); |
| 103 | + |
| 104 | + scene.environment = pmremGenerator.fromScene( environment ).texture; |
| 105 | + environment.dispose(); |
| 106 | + |
| 107 | + // |
| 108 | + |
| 109 | + // postprocessing |
| 110 | + |
| 111 | + postProcessing = new THREE.PostProcessing( renderer ); |
| 112 | + const scenePass = ssaaPass( scene, camera ); |
| 113 | + scenePass.sampleLevel = 3; |
| 114 | + |
| 115 | + postProcessing.outputNode = scenePass; |
| 116 | + |
| 117 | + // |
| 118 | + |
| 119 | + controls = new OrbitControls( camera, renderer.domElement ); |
| 120 | + controls.enableZoom = false; |
| 121 | + controls.enablePan = false; |
| 122 | + |
| 123 | + // |
| 124 | + |
| 125 | + const gui = new GUI(); |
| 126 | + |
| 127 | + gui.add( params, 'alpha', 0, 1 ).onChange( onMaterialUpdate ); |
| 128 | + gui.add( params, 'alphaHash' ).onChange( onMaterialUpdate ); |
| 129 | + |
| 130 | + const ssaaFolder = gui.addFolder( 'SSAA' ); |
| 131 | + ssaaFolder.add( scenePass, 'sampleLevel', 0, 4, 1 ); |
| 132 | + |
| 133 | + // |
| 134 | + |
| 135 | + stats = new Stats(); |
| 136 | + document.body.appendChild( stats.dom ); |
| 137 | + |
| 138 | + window.addEventListener( 'resize', onWindowResize ); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | + function onWindowResize() { |
| 143 | + |
| 144 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 145 | + camera.updateProjectionMatrix(); |
| 146 | + |
| 147 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + function onMaterialUpdate() { |
| 152 | + |
| 153 | + material.opacity = params.alpha; |
| 154 | + material.alphaHash = params.alphaHash; |
| 155 | + material.transparent = ! params.alphaHash; |
| 156 | + material.depthWrite = params.alphaHash; |
| 157 | + |
| 158 | + material.needsUpdate = true; |
| 159 | + |
| 160 | + } |
| 161 | + |
| 162 | + function animate() { |
| 163 | + |
| 164 | + postProcessing.render(); |
| 165 | + |
| 166 | + stats.update(); |
| 167 | + |
| 168 | + } |
| 169 | + |
| 170 | + </script> |
| 171 | + </body> |
| 172 | +</html> |
0 commit comments