|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <title>three.js xr - cubes</title> |
| 5 | + <meta charset="utf-8"> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> |
| 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> xr - interactive cubes |
| 13 | + </div> |
| 14 | + |
| 15 | + <script type="importmap"> |
| 16 | + { |
| 17 | + "imports": { |
| 18 | + "three": "../build/three.webgpu.js", |
| 19 | + "three/webgpu": "../build/three.webgpu.js", |
| 20 | + "three/tsl": "../build/three.tsl.js", |
| 21 | + "three/addons/": "./jsm/" |
| 22 | + } |
| 23 | + } |
| 24 | + </script> |
| 25 | + |
| 26 | + <script type="module"> |
| 27 | + |
| 28 | + import * as THREE from 'three'; |
| 29 | + |
| 30 | + import { BoxLineGeometry } from 'three/addons/geometries/BoxLineGeometry.js'; |
| 31 | + import { XRButton } from 'three/addons/webxr/XRButton.js'; |
| 32 | + import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js'; |
| 33 | + |
| 34 | + const clock = new THREE.Clock(); |
| 35 | + |
| 36 | + let container; |
| 37 | + let camera, scene, raycaster, renderer; |
| 38 | + |
| 39 | + let room; |
| 40 | + let masknode; |
| 41 | + |
| 42 | + let controller, controllerGrip; |
| 43 | + let INTERSECTED; |
| 44 | + |
| 45 | + init(); |
| 46 | + |
| 47 | + function init() { |
| 48 | + |
| 49 | + container = document.createElement( 'div' ); |
| 50 | + document.body.appendChild( container ); |
| 51 | + |
| 52 | + scene = new THREE.Scene(); |
| 53 | + scene.background = new THREE.Color( 0x505050 ); |
| 54 | + |
| 55 | + camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 ); |
| 56 | + camera.position.set( 0, 1.6, 3 ); |
| 57 | + scene.add( camera ); |
| 58 | + |
| 59 | + room = new THREE.Object3D(); |
| 60 | + scene.add( room ); |
| 61 | + |
| 62 | + scene.add( new THREE.HemisphereLight( 0xa5a5a5, 0x898989, 3 ) ); |
| 63 | + |
| 64 | + const light = new THREE.DirectionalLight( 0xffffff, 3 ); |
| 65 | + light.position.set( 1, 1, 1 ).normalize(); |
| 66 | + scene.add( light ); |
| 67 | + |
| 68 | + const geometry = new THREE.BoxGeometry( 0.15, 0.15, 0.15 ); |
| 69 | + |
| 70 | + for ( let i = 0; i < 200; i ++ ) { |
| 71 | + |
| 72 | + const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) ); |
| 73 | + |
| 74 | + object.position.x = Math.random() * 4 - 2; |
| 75 | + object.position.y = Math.random() * 4; |
| 76 | + object.position.z = Math.random() * 4 - 2; |
| 77 | + |
| 78 | + object.rotation.x = Math.random() * 2 * Math.PI; |
| 79 | + object.rotation.y = Math.random() * 2 * Math.PI; |
| 80 | + object.rotation.z = Math.random() * 2 * Math.PI; |
| 81 | + |
| 82 | + object.scale.x = Math.random() + 0.5; |
| 83 | + object.scale.y = Math.random() + 0.5; |
| 84 | + object.scale.z = Math.random() + 0.5; |
| 85 | + |
| 86 | + object.userData.velocity = new THREE.Vector3(); |
| 87 | + object.userData.velocity.x = Math.random() * 0.01 - 0.005; |
| 88 | + object.userData.velocity.y = Math.random() * 0.01 - 0.005; |
| 89 | + object.userData.velocity.z = Math.random() * 0.01 - 0.005; |
| 90 | + |
| 91 | + room.add( object ); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + raycaster = new THREE.Raycaster(); |
| 96 | + |
| 97 | + renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: true, colorBufferType: THREE.UnsignedByteType, multiview: true } ); |
| 98 | + renderer.setPixelRatio( window.devicePixelRatio ); |
| 99 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 100 | + renderer.setAnimationLoop( animate ); |
| 101 | + renderer.xr.enabled = true; |
| 102 | + container.appendChild( renderer.domElement ); |
| 103 | + |
| 104 | + // |
| 105 | + |
| 106 | + function onSelectStart() { |
| 107 | + |
| 108 | + this.userData.isSelecting = true; |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + function onSelectEnd() { |
| 113 | + |
| 114 | + this.userData.isSelecting = false; |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + controller = renderer.xr.getController( 0 ); |
| 119 | + controller.addEventListener( 'selectstart', onSelectStart ); |
| 120 | + controller.addEventListener( 'selectend', onSelectEnd ); |
| 121 | + controller.addEventListener( 'connected', function ( event ) { |
| 122 | + |
| 123 | + const targetRayMode = event.data.targetRayMode; |
| 124 | + |
| 125 | + if ( targetRayMode === 'tracked-pointer' || targetRayMode === 'gaze' ) { |
| 126 | + |
| 127 | + this.add( buildController( event.data ) ); |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + } ); |
| 132 | + controller.addEventListener( 'disconnected', function () { |
| 133 | + |
| 134 | + this.remove( this.children[ 0 ] ); |
| 135 | + |
| 136 | + } ); |
| 137 | + scene.add( controller ); |
| 138 | + |
| 139 | + const controllerModelFactory = new XRControllerModelFactory(); |
| 140 | + |
| 141 | + controllerGrip = renderer.xr.getControllerGrip( 0 ); |
| 142 | + controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) ); |
| 143 | + scene.add( controllerGrip ); |
| 144 | + |
| 145 | + window.addEventListener( 'resize', onWindowResize ); |
| 146 | + |
| 147 | + // |
| 148 | + |
| 149 | + document.body.appendChild( XRButton.createButton( renderer , { |
| 150 | + 'optionalFeatures': [ 'depth-sensing' ], |
| 151 | + 'depthSensing': { 'usagePreference': [ 'gpu-optimized' ], 'dataFormatPreference': [] } } ) ); |
| 152 | + |
| 153 | + } |
| 154 | + |
| 155 | + function buildController( data ) { |
| 156 | + |
| 157 | + let geometry, material; |
| 158 | + |
| 159 | + switch ( data.targetRayMode ) { |
| 160 | + |
| 161 | + case 'tracked-pointer': |
| 162 | + |
| 163 | + geometry = new THREE.BufferGeometry(); |
| 164 | + geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) ); |
| 165 | + geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) ); |
| 166 | + |
| 167 | + material = new THREE.LineBasicMaterial( { vertexColors: true, blending: THREE.AdditiveBlending } ); |
| 168 | + |
| 169 | + return new THREE.Line( geometry, material ); |
| 170 | + |
| 171 | + case 'gaze': |
| 172 | + |
| 173 | + geometry = new THREE.RingGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 ); |
| 174 | + material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } ); |
| 175 | + return new THREE.Mesh( geometry, material ); |
| 176 | + |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | + |
| 181 | + function onWindowResize() { |
| 182 | + |
| 183 | + camera.aspect = window.innerWidth / window.innerHeight; |
| 184 | + camera.updateProjectionMatrix(); |
| 185 | + |
| 186 | + renderer.setSize( window.innerWidth, window.innerHeight ); |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | + // |
| 191 | + |
| 192 | + function animate() { |
| 193 | + |
| 194 | + const delta = clock.getDelta() * 60; |
| 195 | + |
| 196 | + renderer.xr.applyOcclusion( scene ); |
| 197 | + |
| 198 | + if ( controller.userData.isSelecting === true ) { |
| 199 | + |
| 200 | + const cube = room.children[ 0 ]; |
| 201 | + room.remove( cube ); |
| 202 | + |
| 203 | + cube.position.copy( controller.position ); |
| 204 | + cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta; |
| 205 | + cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta; |
| 206 | + cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta; |
| 207 | + cube.userData.velocity.applyQuaternion( controller.quaternion ); |
| 208 | + room.add( cube ); |
| 209 | + |
| 210 | + } |
| 211 | + |
| 212 | + // find intersections |
| 213 | + |
| 214 | + raycaster.setFromXRController( controller ); |
| 215 | + |
| 216 | + const intersects = raycaster.intersectObjects( room.children, false ); |
| 217 | + |
| 218 | + if ( intersects.length > 0 ) { |
| 219 | + |
| 220 | + if ( INTERSECTED != intersects[ 0 ].object ) { |
| 221 | + |
| 222 | + if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex ); |
| 223 | + |
| 224 | + INTERSECTED = intersects[ 0 ].object; |
| 225 | + INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex(); |
| 226 | + INTERSECTED.material.emissive.setHex( 0xff0000 ); |
| 227 | + |
| 228 | + } |
| 229 | + |
| 230 | + } else { |
| 231 | + |
| 232 | + if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex ); |
| 233 | + |
| 234 | + INTERSECTED = undefined; |
| 235 | + |
| 236 | + } |
| 237 | + |
| 238 | + // Keep cubes inside room |
| 239 | + |
| 240 | + for ( let i = 0; i < room.children.length; i ++ ) { |
| 241 | + |
| 242 | + const cube = room.children[ i ]; |
| 243 | + |
| 244 | + cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) ); |
| 245 | + |
| 246 | + cube.position.add( cube.userData.velocity ); |
| 247 | + |
| 248 | + if ( cube.position.x < - 3 || cube.position.x > 3 ) { |
| 249 | + |
| 250 | + cube.position.x = THREE.MathUtils.clamp( cube.position.x, - 3, 3 ); |
| 251 | + cube.userData.velocity.x = - cube.userData.velocity.x; |
| 252 | + |
| 253 | + } |
| 254 | + |
| 255 | + if ( cube.position.y < 0 || cube.position.y > 6 ) { |
| 256 | + |
| 257 | + cube.position.y = THREE.MathUtils.clamp( cube.position.y, 0, 6 ); |
| 258 | + cube.userData.velocity.y = - cube.userData.velocity.y; |
| 259 | + |
| 260 | + } |
| 261 | + |
| 262 | + if ( cube.position.z < - 3 || cube.position.z > 3 ) { |
| 263 | + |
| 264 | + cube.position.z = THREE.MathUtils.clamp( cube.position.z, - 3, 3 ); |
| 265 | + cube.userData.velocity.z = - cube.userData.velocity.z; |
| 266 | + |
| 267 | + } |
| 268 | + |
| 269 | + cube.rotation.x += cube.userData.velocity.x * 2 * delta; |
| 270 | + cube.rotation.y += cube.userData.velocity.y * 2 * delta; |
| 271 | + cube.rotation.z += cube.userData.velocity.z * 2 * delta; |
| 272 | + |
| 273 | + } |
| 274 | + |
| 275 | + renderer.render( scene, camera ); |
| 276 | + |
| 277 | + } |
| 278 | + |
| 279 | + </script> |
| 280 | + </body> |
| 281 | +</html> |
0 commit comments