123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js vr - ball shooter</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 - ball shooter
- </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 camera, scene, renderer;
- var controller1, controller2;
- var controllerGrip1, controllerGrip2;
- var room;
- var count = 0;
- var radius = 0.08;
- var normal = new THREE.Vector3();
- var relativeVelocity = new THREE.Vector3();
- var clock = new THREE.Clock();
- init();
- animate();
- function init() {
- 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 );
- room = new THREE.LineSegments(
- new BoxLineGeometry( 6, 6, 6, 10, 10, 10 ),
- new THREE.LineBasicMaterial( { color: 0x808080 } )
- );
- room.geometry.translate( 0, 3, 0 );
- 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.IcosahedronBufferGeometry( radius, 2 );
- 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.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 );
- }
- //
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.outputEncoding = THREE.sRGBEncoding;
- renderer.xr.enabled = true;
- document.body.appendChild( renderer.domElement );
- //
- document.body.appendChild( VRButton.createButton( renderer ) );
- // controllers
- function onSelectStart() {
- this.userData.isSelecting = true;
- }
- function onSelectEnd() {
- this.userData.isSelecting = false;
- }
- controller1 = renderer.xr.getController( 0 );
- controller1.addEventListener( 'selectstart', onSelectStart );
- controller1.addEventListener( 'selectend', onSelectEnd );
- controller1.addEventListener( 'connected', function ( event ) {
- this.add( buildController( event.data ) );
- } );
- controller1.addEventListener( 'disconnected', function () {
- this.remove( this.children[ 0 ] );
- } );
- scene.add( controller1 );
- controller2 = renderer.xr.getController( 1 );
- controller2.addEventListener( 'selectstart', onSelectStart );
- controller2.addEventListener( 'selectend', onSelectEnd );
- controller2.addEventListener( 'connected', function ( event ) {
- this.add( buildController( event.data ) );
- } );
- controller2.addEventListener( 'disconnected', function () {
- this.remove( this.children[ 0 ] );
- } );
- scene.add( controller2 );
- // The XRControllerModelFactory will automatically fetch controller models
- // that match what the user is holding as closely as possible. The models
- // should be attached to the object returned from getControllerGrip in
- // order to match the orientation of the held device.
- var controllerModelFactory = new XRControllerModelFactory();
- controllerGrip1 = renderer.xr.getControllerGrip( 0 );
- controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
- scene.add( controllerGrip1 );
- controllerGrip2 = renderer.xr.getControllerGrip( 1 );
- controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
- scene.add( controllerGrip2 );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- 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 handleController( controller ) {
- if ( controller.userData.isSelecting ) {
- var object = room.children[ count ++ ];
- object.position.copy( controller.position );
- object.userData.velocity.x = ( Math.random() - 0.5 ) * 3;
- object.userData.velocity.y = ( Math.random() - 0.5 ) * 3;
- object.userData.velocity.z = ( Math.random() - 9 );
- object.userData.velocity.applyQuaternion( controller.quaternion );
- if ( count === room.children.length ) count = 0;
- }
- }
- //
- function animate() {
- renderer.setAnimationLoop( render );
- }
- function render() {
- handleController( controller1 );
- handleController( controller2 );
- //
- var delta = clock.getDelta() * 0.8; // slow down simulation
- var range = 3 - radius;
- for ( var i = 0; i < room.children.length; i ++ ) {
- var object = room.children[ i ];
- object.position.x += object.userData.velocity.x * delta;
- object.position.y += object.userData.velocity.y * delta;
- object.position.z += object.userData.velocity.z * delta;
- // keep objects inside room
- if ( object.position.x < - range || object.position.x > range ) {
- object.position.x = THREE.MathUtils.clamp( object.position.x, - range, range );
- object.userData.velocity.x = - object.userData.velocity.x;
- }
- if ( object.position.y < radius || object.position.y > 6 ) {
- object.position.y = Math.max( object.position.y, radius );
- object.userData.velocity.x *= 0.98;
- object.userData.velocity.y = - object.userData.velocity.y * 0.8;
- object.userData.velocity.z *= 0.98;
- }
- if ( object.position.z < - range || object.position.z > range ) {
- object.position.z = THREE.MathUtils.clamp( object.position.z, - range, range );
- object.userData.velocity.z = - object.userData.velocity.z;
- }
- for ( var j = i + 1; j < room.children.length; j ++ ) {
- var object2 = room.children[ j ];
- normal.copy( object.position ).sub( object2.position );
- var distance = normal.length();
- if ( distance < 2 * radius ) {
- normal.multiplyScalar( 0.5 * distance - radius );
- object.position.sub( normal );
- object2.position.add( normal );
- normal.normalize();
- relativeVelocity.copy( object.userData.velocity ).sub( object2.userData.velocity );
- normal = normal.multiplyScalar( relativeVelocity.dot( normal ) );
- object.userData.velocity.sub( normal );
- object2.userData.velocity.add( normal );
- }
- }
- object.userData.velocity.y -= 9.8 * delta;
- }
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|