webxr_vr_cubes.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js vr - 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. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> vr - interactive cubes
  12. </div>
  13. <script type="module">
  14. import * as THREE from '../build/three.module.js';
  15. import { BoxLineGeometry } from './jsm/geometries/BoxLineGeometry.js';
  16. import { VRButton } from './jsm/webxr/VRButton.js';
  17. import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
  18. const clock = new THREE.Clock();
  19. let container;
  20. let camera, scene, raycaster, renderer;
  21. let room;
  22. let controller, controllerGrip;
  23. let INTERSECTED;
  24. const tempMatrix = new THREE.Matrix4();
  25. init();
  26. animate();
  27. function init() {
  28. container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. scene = new THREE.Scene();
  31. scene.background = new THREE.Color( 0x505050 );
  32. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  33. camera.position.set( 0, 1.6, 3 );
  34. scene.add( camera );
  35. room = new THREE.LineSegments(
  36. new BoxLineGeometry( 6, 6, 6, 10, 10, 10 ).translate( 0, 3, 0 ),
  37. new THREE.LineBasicMaterial( { color: 0x808080 } )
  38. );
  39. scene.add( room );
  40. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  41. const light = new THREE.DirectionalLight( 0xffffff );
  42. light.position.set( 1, 1, 1 ).normalize();
  43. scene.add( light );
  44. const geometry = new THREE.BoxGeometry( 0.15, 0.15, 0.15 );
  45. for ( let i = 0; i < 200; i ++ ) {
  46. const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  47. object.position.x = Math.random() * 4 - 2;
  48. object.position.y = Math.random() * 4;
  49. object.position.z = Math.random() * 4 - 2;
  50. object.rotation.x = Math.random() * 2 * Math.PI;
  51. object.rotation.y = Math.random() * 2 * Math.PI;
  52. object.rotation.z = Math.random() * 2 * Math.PI;
  53. object.scale.x = Math.random() + 0.5;
  54. object.scale.y = Math.random() + 0.5;
  55. object.scale.z = Math.random() + 0.5;
  56. object.userData.velocity = new THREE.Vector3();
  57. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  58. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  59. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  60. room.add( object );
  61. }
  62. raycaster = new THREE.Raycaster();
  63. renderer = new THREE.WebGLRenderer( { antialias: true } );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. renderer.outputEncoding = THREE.sRGBEncoding;
  67. renderer.xr.enabled = true;
  68. container.appendChild( renderer.domElement );
  69. //
  70. function onSelectStart() {
  71. this.userData.isSelecting = true;
  72. }
  73. function onSelectEnd() {
  74. this.userData.isSelecting = false;
  75. }
  76. controller = renderer.xr.getController( 0 );
  77. controller.addEventListener( 'selectstart', onSelectStart );
  78. controller.addEventListener( 'selectend', onSelectEnd );
  79. controller.addEventListener( 'connected', function ( event ) {
  80. this.add( buildController( event.data ) );
  81. } );
  82. controller.addEventListener( 'disconnected', function () {
  83. this.remove( this.children[ 0 ] );
  84. } );
  85. scene.add( controller );
  86. const controllerModelFactory = new XRControllerModelFactory();
  87. controllerGrip = renderer.xr.getControllerGrip( 0 );
  88. controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
  89. scene.add( controllerGrip );
  90. window.addEventListener( 'resize', onWindowResize );
  91. //
  92. document.body.appendChild( VRButton.createButton( renderer ) );
  93. }
  94. function buildController( data ) {
  95. let geometry, material;
  96. switch ( data.targetRayMode ) {
  97. case 'tracked-pointer':
  98. geometry = new THREE.BufferGeometry();
  99. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  100. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  101. material = new THREE.LineBasicMaterial( { vertexColors: true, blending: THREE.AdditiveBlending } );
  102. return new THREE.Line( geometry, material );
  103. case 'gaze':
  104. geometry = new THREE.RingGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
  105. material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
  106. return new THREE.Mesh( geometry, material );
  107. }
  108. }
  109. function onWindowResize() {
  110. camera.aspect = window.innerWidth / window.innerHeight;
  111. camera.updateProjectionMatrix();
  112. renderer.setSize( window.innerWidth, window.innerHeight );
  113. }
  114. //
  115. function animate() {
  116. renderer.setAnimationLoop( render );
  117. }
  118. function render() {
  119. const delta = clock.getDelta() * 60;
  120. if ( controller.userData.isSelecting === true ) {
  121. const cube = room.children[ 0 ];
  122. room.remove( cube );
  123. cube.position.copy( controller.position );
  124. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
  125. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
  126. cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
  127. cube.userData.velocity.applyQuaternion( controller.quaternion );
  128. room.add( cube );
  129. }
  130. // find intersections
  131. tempMatrix.identity().extractRotation( controller.matrixWorld );
  132. raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  133. raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
  134. const intersects = raycaster.intersectObjects( room.children );
  135. if ( intersects.length > 0 ) {
  136. if ( INTERSECTED != intersects[ 0 ].object ) {
  137. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  138. INTERSECTED = intersects[ 0 ].object;
  139. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  140. INTERSECTED.material.emissive.setHex( 0xff0000 );
  141. }
  142. } else {
  143. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  144. INTERSECTED = undefined;
  145. }
  146. // Keep cubes inside room
  147. for ( let i = 0; i < room.children.length; i ++ ) {
  148. const cube = room.children[ i ];
  149. cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
  150. cube.position.add( cube.userData.velocity );
  151. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  152. cube.position.x = THREE.MathUtils.clamp( cube.position.x, - 3, 3 );
  153. cube.userData.velocity.x = - cube.userData.velocity.x;
  154. }
  155. if ( cube.position.y < 0 || cube.position.y > 6 ) {
  156. cube.position.y = THREE.MathUtils.clamp( cube.position.y, 0, 6 );
  157. cube.userData.velocity.y = - cube.userData.velocity.y;
  158. }
  159. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  160. cube.position.z = THREE.MathUtils.clamp( cube.position.z, - 3, 3 );
  161. cube.userData.velocity.z = - cube.userData.velocity.z;
  162. }
  163. cube.rotation.x += cube.userData.velocity.x * 2 * delta;
  164. cube.rotation.y += cube.userData.velocity.y * 2 * delta;
  165. cube.rotation.z += cube.userData.velocity.z * 2 * delta;
  166. }
  167. renderer.render( scene, camera );
  168. }
  169. </script>
  170. </body>
  171. </html>