123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js vr - cubes</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info">
- <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - interactive cubes
- </div>
- <script src="js/vr/HelioWebXRPolyfill.js"></script>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { BoxLineGeometry } from './jsm/geometries/BoxLineGeometry.js';
- import { VRButton } from './jsm/webxr/VRButton.js';
- import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
- var clock = new THREE.Clock();
- var container;
- var camera, scene, raycaster, renderer;
- var room;
- var controller, controllerGrip, tempMatrix = new THREE.Matrix4();
- var INTERSECTED;
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- scene = new THREE.Scene();
- scene.background = new THREE.Color( 0x505050 );
- camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
- camera.position.set( 0, 1.6, 3 );
- scene.add( camera );
- room = new THREE.LineSegments(
- new BoxLineGeometry( 6, 6, 6, 10, 10, 10 ).translate( 0, 3, 0 ),
- new THREE.LineBasicMaterial( { color: 0x808080 } )
- );
- scene.add( room );
- scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
- var light = new THREE.DirectionalLight( 0xffffff );
- light.position.set( 1, 1, 1 ).normalize();
- scene.add( light );
- var geometry = new THREE.BoxBufferGeometry( 0.15, 0.15, 0.15 );
- for ( var i = 0; i < 200; i ++ ) {
- var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
- object.position.x = Math.random() * 4 - 2;
- object.position.y = Math.random() * 4;
- object.position.z = Math.random() * 4 - 2;
- object.rotation.x = Math.random() * 2 * Math.PI;
- object.rotation.y = Math.random() * 2 * Math.PI;
- object.rotation.z = Math.random() * 2 * Math.PI;
- object.scale.x = Math.random() + 0.5;
- object.scale.y = Math.random() + 0.5;
- object.scale.z = Math.random() + 0.5;
- object.userData.velocity = new THREE.Vector3();
- object.userData.velocity.x = Math.random() * 0.01 - 0.005;
- object.userData.velocity.y = Math.random() * 0.01 - 0.005;
- object.userData.velocity.z = Math.random() * 0.01 - 0.005;
- room.add( object );
- }
- raycaster = new THREE.Raycaster();
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.outputEncoding = THREE.sRGBEncoding;
- renderer.xr.enabled = true;
- container.appendChild( renderer.domElement );
- //
- function onSelectStart() {
- this.userData.isSelecting = true;
- }
- function onSelectEnd() {
- this.userData.isSelecting = false;
- }
- controller = renderer.xr.getController( 0 );
- controller.addEventListener( 'selectstart', onSelectStart );
- controller.addEventListener( 'selectend', onSelectEnd );
- controller.addEventListener( 'connected', function ( event ) {
- this.add( buildController( event.data ) );
- } );
- controller.addEventListener( 'disconnected', function () {
- this.remove( this.children[ 0 ] );
- } );
- scene.add( controller );
- var controllerModelFactory = new XRControllerModelFactory();
- controllerGrip = renderer.xr.getControllerGrip( 0 );
- controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
- scene.add( controllerGrip );
- window.addEventListener( 'resize', onWindowResize, false );
- //
- document.body.appendChild( VRButton.createButton( renderer ) );
- }
- function buildController( data ) {
- switch ( data.targetRayMode ) {
- case 'tracked-pointer':
- var geometry = new THREE.BufferGeometry();
- geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
- geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
- var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors, blending: THREE.AdditiveBlending } );
- return new THREE.Line( geometry, material );
- case 'gaze':
- var geometry = new THREE.RingBufferGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
- var material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
- return new THREE.Mesh( geometry, material );
- }
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- renderer.setAnimationLoop( render );
- }
- function render() {
- var delta = clock.getDelta() * 60;
- if ( controller.userData.isSelecting === true ) {
- var cube = room.children[ 0 ];
- room.remove( cube );
- cube.position.copy( controller.position );
- cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
- cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
- cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
- cube.userData.velocity.applyQuaternion( controller.quaternion );
- room.add( cube );
- }
- // find intersections
- tempMatrix.identity().extractRotation( controller.matrixWorld );
- raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
- raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
- var intersects = raycaster.intersectObjects( room.children );
- if ( intersects.length > 0 ) {
- if ( INTERSECTED != intersects[ 0 ].object ) {
- if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
- INTERSECTED = intersects[ 0 ].object;
- INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
- INTERSECTED.material.emissive.setHex( 0xff0000 );
- }
- } else {
- if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
- INTERSECTED = undefined;
- }
- // Keep cubes inside room
- for ( var i = 0; i < room.children.length; i ++ ) {
- var cube = room.children[ i ];
- cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
- cube.position.add( cube.userData.velocity );
- if ( cube.position.x < - 3 || cube.position.x > 3 ) {
- cube.position.x = THREE.MathUtils.clamp( cube.position.x, - 3, 3 );
- cube.userData.velocity.x = - cube.userData.velocity.x;
- }
- if ( cube.position.y < 0 || cube.position.y > 6 ) {
- cube.position.y = THREE.MathUtils.clamp( cube.position.y, 0, 6 );
- cube.userData.velocity.y = - cube.userData.velocity.y;
- }
- if ( cube.position.z < - 3 || cube.position.z > 3 ) {
- cube.position.z = THREE.MathUtils.clamp( cube.position.z, - 3, 3 );
- cube.userData.velocity.z = - cube.userData.velocity.z;
- }
- cube.rotation.x += cube.userData.velocity.x * 2 * delta;
- cube.rotation.y += cube.userData.velocity.y * 2 * delta;
- cube.rotation.z += cube.userData.velocity.z * 2 * delta;
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|