webvr_ballshooter.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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>
  26. var container;
  27. var camera, scene, renderer;
  28. var controller1, controller2;
  29. var room;
  30. var radius = 0.08;
  31. var normal = new THREE.Vector3();
  32. var relativeVelocity = new THREE.Vector3();
  33. init();
  34. animate();
  35. function init() {
  36. container = document.createElement( 'div' );
  37. document.body.appendChild( container );
  38. var info = document.createElement( 'div' );
  39. info.style.position = 'absolute';
  40. info.style.top = '10px';
  41. info.style.width = '100%';
  42. info.style.textAlign = 'center';
  43. info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webvr - ball shooter';
  44. container.appendChild( info );
  45. scene = new THREE.Scene();
  46. scene.background = new THREE.Color( 0x505050 );
  47. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  48. room = new THREE.Mesh(
  49. new THREE.BoxBufferGeometry( 6, 6, 6, 8, 8, 8 ),
  50. new THREE.MeshBasicMaterial( { color: 0x808080, wireframe: true } )
  51. );
  52. room.geometry.translate( 0, 3, 0 );
  53. scene.add( room );
  54. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  55. var light = new THREE.DirectionalLight( 0xffffff );
  56. light.position.set( 1, 1, 1 ).normalize();
  57. scene.add( light );
  58. var geometry = new THREE.IcosahedronBufferGeometry( radius, 2 );
  59. for ( var i = 0; i < 200; i ++ ) {
  60. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  61. object.position.x = Math.random() * 4 - 2;
  62. object.position.y = Math.random() * 4;
  63. object.position.z = Math.random() * 4 - 2;
  64. object.userData.velocity = new THREE.Vector3();
  65. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  66. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  67. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  68. room.add( object );
  69. }
  70. //
  71. renderer = new THREE.WebGLRenderer( { antialias: true } );
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.vr.enabled = true;
  75. container.appendChild( renderer.domElement );
  76. //
  77. document.body.appendChild( WEBVR.createButton( renderer ) );
  78. // controllers
  79. function onSelectStart() {
  80. this.userData.isSelecting = true;
  81. }
  82. function onSelectEnd() {
  83. this.userData.isSelecting = false;
  84. }
  85. controller1 = renderer.vr.getController( 0 );
  86. controller1.addEventListener( 'selectstart', onSelectStart );
  87. controller1.addEventListener( 'selectend', onSelectEnd );
  88. scene.add( controller1 );
  89. controller2 = renderer.vr.getController( 1 );
  90. controller2.addEventListener( 'selectstart', onSelectStart );
  91. controller2.addEventListener( 'selectend', onSelectEnd );
  92. scene.add( controller2 );
  93. // helpers
  94. var geometry = new THREE.BufferGeometry();
  95. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 1 ], 3 ) );
  96. geometry.addAttribute( 'color', new THREE.Float32BufferAttribute( [ 0.5, 0.5, 0.5, 0, 0, 0 ], 3 ) );
  97. var material = new THREE.LineBasicMaterial( { vertexColors: true, linewidth: 2, blending: THREE.AdditiveBlending } );
  98. controller1.add( new THREE.Line( geometry, material ) );
  99. controller2.add( new THREE.Line( geometry, material ) );
  100. //
  101. window.addEventListener( 'resize', onWindowResize, false );
  102. }
  103. function onWindowResize() {
  104. camera.aspect = window.innerWidth / window.innerHeight;
  105. camera.updateProjectionMatrix();
  106. renderer.setSize( window.innerWidth, window.innerHeight );
  107. }
  108. function handleController( controller ) {
  109. if ( controller.userData.isSelecting ) {
  110. var object = room.children[ 0 ];
  111. room.remove( object );
  112. object.position.copy( controller.position );
  113. object.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
  114. object.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
  115. object.userData.velocity.z = ( Math.random() * 0.02 - 0.1 );
  116. object.userData.velocity.applyQuaternion( controller.quaternion );
  117. room.add( object );
  118. }
  119. }
  120. //
  121. function animate() {
  122. renderer.setAnimationLoop( render );
  123. }
  124. function render() {
  125. handleController( controller1 );
  126. handleController( controller2 );
  127. // keep cubes inside room
  128. var range = 3 - radius;
  129. for ( var i = 0; i < room.children.length; i ++ ) {
  130. var cube = room.children[ i ];
  131. cube.position.add( cube.userData.velocity );
  132. if ( cube.position.x < - range || cube.position.x > range ) {
  133. cube.position.x = THREE.Math.clamp( cube.position.x, - range, range );
  134. cube.userData.velocity.x = - cube.userData.velocity.x;
  135. }
  136. if ( cube.position.y < radius || cube.position.y > 6 ) {
  137. cube.position.y = Math.max( cube.position.y, radius );
  138. cube.userData.velocity.x *= 0.98;
  139. cube.userData.velocity.y = - cube.userData.velocity.y * 0.8;
  140. cube.userData.velocity.z *= 0.98;
  141. }
  142. if ( cube.position.z < - range || cube.position.z > range ) {
  143. cube.position.z = THREE.Math.clamp( cube.position.z, - range, range );
  144. cube.userData.velocity.z = - cube.userData.velocity.z;
  145. }
  146. for ( var j = i + 1; j < room.children.length; j ++ ) {
  147. var cube2 = room.children[ j ];
  148. normal.copy( cube.position ).sub( cube2.position );
  149. var distance = normal.length();
  150. if ( distance < 2 * radius ) {
  151. normal.multiplyScalar( 0.5 * distance - radius );
  152. cube.position.sub( normal );
  153. cube2.position.add( normal );
  154. normal.normalize();
  155. relativeVelocity.copy( cube.userData.velocity ).sub( cube2.userData.velocity );
  156. normal = normal.multiplyScalar( relativeVelocity.dot( normal ) );
  157. cube.userData.velocity.sub( normal );
  158. cube2.userData.velocity.add( normal );
  159. }
  160. }
  161. cube.userData.velocity.y -= 0.00098;
  162. }
  163. renderer.render( scene, camera );
  164. }
  165. </script>
  166. </body>
  167. </html>
粤ICP备19079148号