webvr_ballshooter.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - ball shooter</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  7. <!-- Origin Trial Token, feature = WebVR (For Chrome M62+), origin = https://threejs.org, expires = 2018-09-11 -->
  8. <meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-09-11" content="AqhFUYKxq/d+E8CDT0fuYRCg8TvlTP52x0Jv7I9t27sLhR30LmcahBRfSwzP89ukjs2+ia99VrrLoRyaFAwJVA0AAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUzNjYyNDAwMH0=">
  9. <style>
  10. body {
  11. font-family: Monospace;
  12. background-color: #101010;
  13. color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. a {
  18. color: #48f;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <script src="../build/three.js"></script>
  24. <script src="js/vr/WebVR.js"></script>
  25. <script src="js/geometries/BoxLineGeometry.js"></script>
  26. <script>
  27. var container;
  28. var camera, scene, renderer;
  29. var controller1, controller2;
  30. var room;
  31. var radius = 0.08;
  32. var normal = new THREE.Vector3();
  33. var relativeVelocity = new THREE.Vector3();
  34. init();
  35. animate();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. var info = document.createElement( 'div' );
  40. info.style.position = 'absolute';
  41. info.style.top = '10px';
  42. info.style.width = '100%';
  43. info.style.textAlign = 'center';
  44. info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webvr - ball shooter';
  45. container.appendChild( info );
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0x505050 );
  48. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  49. room = new THREE.LineSegments(
  50. new THREE.BoxLineGeometry( 6, 6, 6, 10, 10, 10 ),
  51. new THREE.LineBasicMaterial( { color: 0x808080 } )
  52. );
  53. room.geometry.translate( 0, 3, 0 );
  54. scene.add( room );
  55. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  56. var light = new THREE.DirectionalLight( 0xffffff );
  57. light.position.set( 1, 1, 1 ).normalize();
  58. scene.add( light );
  59. var geometry = new THREE.IcosahedronBufferGeometry( radius, 2 );
  60. for ( var i = 0; i < 200; i ++ ) {
  61. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  62. object.position.x = Math.random() * 4 - 2;
  63. object.position.y = Math.random() * 4;
  64. object.position.z = Math.random() * 4 - 2;
  65. object.userData.velocity = new THREE.Vector3();
  66. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  67. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  68. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  69. room.add( object );
  70. }
  71. //
  72. renderer = new THREE.WebGLRenderer( { antialias: true } );
  73. renderer.setPixelRatio( window.devicePixelRatio );
  74. renderer.setSize( window.innerWidth, window.innerHeight );
  75. renderer.vr.enabled = true;
  76. container.appendChild( renderer.domElement );
  77. //
  78. document.body.appendChild( WEBVR.createButton( renderer ) );
  79. // controllers
  80. function onSelectStart() {
  81. this.userData.isSelecting = true;
  82. }
  83. function onSelectEnd() {
  84. this.userData.isSelecting = false;
  85. }
  86. controller1 = renderer.vr.getController( 0 );
  87. controller1.addEventListener( 'selectstart', onSelectStart );
  88. controller1.addEventListener( 'selectend', onSelectEnd );
  89. scene.add( controller1 );
  90. controller2 = renderer.vr.getController( 1 );
  91. controller2.addEventListener( 'selectstart', onSelectStart );
  92. controller2.addEventListener( 'selectend', onSelectEnd );
  93. scene.add( controller2 );
  94. // helpers
  95. var geometry = new THREE.BufferGeometry();
  96. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  97. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  98. var material = new THREE.LineBasicMaterial( { vertexColors: true, linewidth: 2, blending: THREE.AdditiveBlending } );
  99. controller1.add( new THREE.Line( geometry, material ) );
  100. controller2.add( new THREE.Line( geometry, material ) );
  101. //
  102. window.addEventListener( 'resize', onWindowResize, false );
  103. }
  104. function onWindowResize() {
  105. camera.aspect = window.innerWidth / window.innerHeight;
  106. camera.updateProjectionMatrix();
  107. renderer.setSize( window.innerWidth, window.innerHeight );
  108. }
  109. function handleController( controller ) {
  110. if ( controller.userData.isSelecting ) {
  111. var object = room.children[ 0 ];
  112. room.remove( object );
  113. object.position.copy( controller.position );
  114. object.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
  115. object.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
  116. object.userData.velocity.z = ( Math.random() * 0.02 - 0.1 );
  117. object.userData.velocity.applyQuaternion( controller.quaternion );
  118. room.add( object );
  119. }
  120. }
  121. //
  122. function animate() {
  123. renderer.setAnimationLoop( render );
  124. }
  125. function render() {
  126. handleController( controller1 );
  127. handleController( controller2 );
  128. // keep cubes inside room
  129. var range = 3 - radius;
  130. for ( var i = 0; i < room.children.length; i ++ ) {
  131. var cube = room.children[ i ];
  132. cube.position.add( cube.userData.velocity );
  133. if ( cube.position.x < - range || cube.position.x > range ) {
  134. cube.position.x = THREE.Math.clamp( cube.position.x, - range, range );
  135. cube.userData.velocity.x = - cube.userData.velocity.x;
  136. }
  137. if ( cube.position.y < radius || cube.position.y > 6 ) {
  138. cube.position.y = Math.max( cube.position.y, radius );
  139. cube.userData.velocity.x *= 0.98;
  140. cube.userData.velocity.y = - cube.userData.velocity.y * 0.8;
  141. cube.userData.velocity.z *= 0.98;
  142. }
  143. if ( cube.position.z < - range || cube.position.z > range ) {
  144. cube.position.z = THREE.Math.clamp( cube.position.z, - range, range );
  145. cube.userData.velocity.z = - cube.userData.velocity.z;
  146. }
  147. for ( var j = i + 1; j < room.children.length; j ++ ) {
  148. var cube2 = room.children[ j ];
  149. normal.copy( cube.position ).sub( cube2.position );
  150. var distance = normal.length();
  151. if ( distance < 2 * radius ) {
  152. normal.multiplyScalar( 0.5 * distance - radius );
  153. cube.position.sub( normal );
  154. cube2.position.add( normal );
  155. normal.normalize();
  156. relativeVelocity.copy( cube.userData.velocity ).sub( cube2.userData.velocity );
  157. normal = normal.multiplyScalar( relativeVelocity.dot( normal ) );
  158. cube.userData.velocity.sub( normal );
  159. cube2.userData.velocity.add( normal );
  160. }
  161. }
  162. cube.userData.velocity.y -= 0.00098;
  163. }
  164. renderer.render( scene, camera );
  165. }
  166. </script>
  167. </body>
  168. </html>