webxr_vr_cubes.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 src="js/vr/HelioWebXRPolyfill.js"></script>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { BoxLineGeometry } from './jsm/geometries/BoxLineGeometry.js';
  17. import { VRButton } from './jsm/webxr/VRButton.js';
  18. import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
  19. var clock = new THREE.Clock();
  20. var container;
  21. var camera, scene, raycaster, renderer;
  22. var room;
  23. var controller, controllerGrip, tempMatrix = new THREE.Matrix4();
  24. var INTERSECTED;
  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. var light = new THREE.DirectionalLight( 0xffffff );
  42. light.position.set( 1, 1, 1 ).normalize();
  43. scene.add( light );
  44. var geometry = new THREE.BoxBufferGeometry( 0.15, 0.15, 0.15 );
  45. for ( var i = 0; i < 200; i ++ ) {
  46. var 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. var controllerModelFactory = new XRControllerModelFactory();
  87. controllerGrip = renderer.xr.getControllerGrip( 0 );
  88. controllerGrip.add( controllerModelFactory.createControllerModel( controllerGrip ) );
  89. scene.add( controllerGrip );
  90. window.addEventListener( 'resize', onWindowResize, false );
  91. //
  92. document.body.appendChild( VRButton.createButton( renderer ) );
  93. }
  94. function buildController( data ) {
  95. switch ( data.targetRayMode ) {
  96. case 'tracked-pointer':
  97. var geometry = new THREE.BufferGeometry();
  98. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  99. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  100. var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors, blending: THREE.AdditiveBlending } );
  101. return new THREE.Line( geometry, material );
  102. case 'gaze':
  103. var geometry = new THREE.RingBufferGeometry( 0.02, 0.04, 32 ).translate( 0, 0, - 1 );
  104. var material = new THREE.MeshBasicMaterial( { opacity: 0.5, transparent: true } );
  105. return new THREE.Mesh( geometry, material );
  106. }
  107. }
  108. function onWindowResize() {
  109. camera.aspect = window.innerWidth / window.innerHeight;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. }
  113. //
  114. function animate() {
  115. renderer.setAnimationLoop( render );
  116. }
  117. function render() {
  118. var delta = clock.getDelta() * 60;
  119. if ( controller.userData.isSelecting === true ) {
  120. var cube = room.children[ 0 ];
  121. room.remove( cube );
  122. cube.position.copy( controller.position );
  123. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02 * delta;
  124. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02 * delta;
  125. cube.userData.velocity.z = ( Math.random() * 0.01 - 0.05 ) * delta;
  126. cube.userData.velocity.applyQuaternion( controller.quaternion );
  127. room.add( cube );
  128. }
  129. // find intersections
  130. tempMatrix.identity().extractRotation( controller.matrixWorld );
  131. raycaster.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  132. raycaster.ray.direction.set( 0, 0, - 1 ).applyMatrix4( tempMatrix );
  133. var intersects = raycaster.intersectObjects( room.children );
  134. if ( intersects.length > 0 ) {
  135. if ( INTERSECTED != intersects[ 0 ].object ) {
  136. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  137. INTERSECTED = intersects[ 0 ].object;
  138. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  139. INTERSECTED.material.emissive.setHex( 0xff0000 );
  140. }
  141. } else {
  142. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  143. INTERSECTED = undefined;
  144. }
  145. // Keep cubes inside room
  146. for ( var i = 0; i < room.children.length; i ++ ) {
  147. var cube = room.children[ i ];
  148. cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
  149. cube.position.add( cube.userData.velocity );
  150. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  151. cube.position.x = THREE.MathUtils.clamp( cube.position.x, - 3, 3 );
  152. cube.userData.velocity.x = - cube.userData.velocity.x;
  153. }
  154. if ( cube.position.y < 0 || cube.position.y > 6 ) {
  155. cube.position.y = THREE.MathUtils.clamp( cube.position.y, 0, 6 );
  156. cube.userData.velocity.y = - cube.userData.velocity.y;
  157. }
  158. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  159. cube.position.z = THREE.MathUtils.clamp( cube.position.z, - 3, 3 );
  160. cube.userData.velocity.z = - cube.userData.velocity.z;
  161. }
  162. cube.rotation.x += cube.userData.velocity.x * 2 * delta;
  163. cube.rotation.y += cube.userData.velocity.y * 2 * delta;
  164. cube.rotation.z += cube.userData.velocity.z * 2 * delta;
  165. }
  166. renderer.render( scene, camera );
  167. }
  168. </script>
  169. </body>
  170. </html>