webvr_daydream.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - daydream</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-07-25 -->
  8. <meta http-equiv="origin-trial" data-feature="WebVR (For Chrome M62+)" data-expires="2018-07-25" content="AtW06hJFoVWUJTZf5gqymMIlrR60JJi5MsSe44qsHjrCmzDUNmvaTtAVg+K9O9jFpjJtn/W9jvG//hHHaq5HcQoAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUjEuMU02MiIsImV4cGlyeSI6MTUzMjUyNjI5OH0=">
  9. <!-- Origin Trial Token, feature = WebXR Device API, origin = https://threejs.org, expires = 2018-07-21 -->
  10. <meta http-equiv="origin-trial" data-feature="WebXR Device API" data-expires="2018-07-21" content="Anlf1R/bCOUxOEgGI/9TWuzHHNxBMfZSTUMDCN7cLwDj2gpLwgA1K0DPwOzO/O0Jwaur5bsHo7k9KXx+6g+82wIAAABQeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkRldmljZSIsImV4cGlyeSI6MTUzMjE2NjIyNX0=">
  11. <!-- Origin Trial Token, feature = WebXR Gamepad Support, origin = https://threejs.org, expires = 2018-07-21 -->
  12. <meta http-equiv="origin-trial" data-feature="WebXR Gamepad Support" data-expires="2018-07-21" content="ArDsXbwATKHPmvQiPlEIWNCt4DlEjB7bLj9vOgoNmL8r38U+wQNYZyUvjQIqqzgACciUUuAnxluTIL7nNkI89gcAAABYeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJYUkdhbWVwYWRTdXBwb3J0IiwiZXhwaXJ5IjoxNTMyMTY2MjI1fQ==">
  13. <style>
  14. body {
  15. font-family: Monospace;
  16. background-color: #101010;
  17. color: #fff;
  18. margin: 0px;
  19. overflow: hidden;
  20. }
  21. a {
  22. color: #f00;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <script src="../build/three.js"></script>
  28. <script src="js/vr/DaydreamController.js"></script>
  29. <script src="js/vr/WebVR.js"></script>
  30. <script>
  31. var container;
  32. var camera, scene, renderer;
  33. var controller;
  34. var room;
  35. var radius = 0.08;
  36. var normal = new THREE.Vector3();
  37. var relativeVelocity = new THREE.Vector3();
  38. init();
  39. animate();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. var info = document.createElement( 'div' );
  44. info.style.position = 'absolute';
  45. info.style.top = '10px';
  46. info.style.width = '100%';
  47. info.style.textAlign = 'center';
  48. info.innerHTML = '<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webvr - daydream';
  49. container.appendChild( info );
  50. scene = new THREE.Scene();
  51. scene.background = new THREE.Color( 0x505050 );
  52. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  53. room = new THREE.Mesh(
  54. new THREE.BoxBufferGeometry( 6, 6, 6, 8, 8, 8 ),
  55. new THREE.MeshBasicMaterial( { color: 0x808080, wireframe: true } )
  56. );
  57. room.position.y = 3;
  58. scene.add( room );
  59. scene.add( new THREE.HemisphereLight( 0x606060, 0x404040 ) );
  60. var light = new THREE.DirectionalLight( 0xffffff );
  61. light.position.set( 1, 1, 1 ).normalize();
  62. scene.add( light );
  63. var geometry = new THREE.IcosahedronBufferGeometry( radius, 2 );
  64. for ( var i = 0; i < 200; i ++ ) {
  65. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  66. object.position.x = Math.random() * 4 - 2;
  67. object.position.y = Math.random() * 4 - 2;
  68. object.position.z = Math.random() * 4 - 2;
  69. object.userData.velocity = new THREE.Vector3();
  70. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  71. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  72. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  73. room.add( object );
  74. }
  75. //
  76. renderer = new THREE.WebGLRenderer( { antialias: true } );
  77. renderer.setPixelRatio( window.devicePixelRatio );
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. renderer.vr.enabled = true;
  80. container.appendChild( renderer.domElement );
  81. //
  82. document.body.appendChild( WEBVR.createButton( renderer ) );
  83. //
  84. controller = new THREE.DaydreamController();
  85. controller.position.set( 0.3, 0.75, 0 );
  86. scene.add( controller );
  87. //
  88. var controllerHelper = new THREE.Line( new THREE.BufferGeometry(), new THREE.LineBasicMaterial( { linewidth: 2 } ) );
  89. controllerHelper.geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 10 ], 3 ) );
  90. controller.add( controllerHelper );
  91. //
  92. window.addEventListener( 'resize', onWindowResize, false );
  93. }
  94. function onWindowResize() {
  95. camera.aspect = window.innerWidth / window.innerHeight;
  96. camera.updateProjectionMatrix();
  97. renderer.setSize( window.innerWidth, window.innerHeight );
  98. }
  99. //
  100. function animate() {
  101. renderer.setAnimationLoop( render );
  102. }
  103. function render() {
  104. controller.update();
  105. if ( controller.getTouchpadState() === true ) {
  106. var cube = room.children[ 0 ];
  107. room.remove( cube );
  108. cube.position.copy( controller.position ).sub( room.position );
  109. cube.userData.velocity.x = ( Math.random() - 0.5 ) * 0.02;
  110. cube.userData.velocity.y = ( Math.random() - 0.5 ) * 0.02;
  111. cube.userData.velocity.z = ( Math.random() * 0.02 - 0.1 );
  112. cube.userData.velocity.applyQuaternion( controller.quaternion );
  113. room.add( cube );
  114. }
  115. // keep cubes inside room
  116. var range = 3 - radius;
  117. for ( var i = 0; i < room.children.length; i ++ ) {
  118. var cube = room.children[ i ];
  119. cube.position.add( cube.userData.velocity );
  120. if ( cube.position.x < - range || cube.position.x > range ) {
  121. cube.position.x = THREE.Math.clamp( cube.position.x, - range, range );
  122. cube.userData.velocity.x = - cube.userData.velocity.x;
  123. }
  124. if ( cube.position.y < - range || cube.position.y > range ) {
  125. cube.position.y = Math.max( cube.position.y, - range );
  126. cube.userData.velocity.x *= 0.9;
  127. cube.userData.velocity.y = - cube.userData.velocity.y * 0.8;
  128. cube.userData.velocity.z *= 0.9;
  129. }
  130. if ( cube.position.z < - range || cube.position.z > range ) {
  131. cube.position.z = THREE.Math.clamp( cube.position.z, - range, range );
  132. cube.userData.velocity.z = - cube.userData.velocity.z;
  133. }
  134. for ( var j = i + 1; j < room.children.length; j ++ ) {
  135. var cube2 = room.children[ j ];
  136. normal.copy( cube.position ).sub( cube2.position );
  137. if ( normal.length() < 2 * radius ) {
  138. normal.multiplyScalar( 0.5 * normal.length() - radius );
  139. cube.position.sub( normal );
  140. cube2.position.add( normal );
  141. normal.normalize();
  142. relativeVelocity.copy( cube.userData.velocity ).sub( cube2.userData.velocity );
  143. var dot = relativeVelocity.dot( normal );
  144. normal = normal.multiplyScalar( dot );
  145. cube.userData.velocity.sub( normal );
  146. cube2.userData.velocity.add( normal );
  147. }
  148. }
  149. cube.userData.velocity.y -= 0.00098;
  150. }
  151. renderer.render( scene, camera );
  152. }
  153. </script>
  154. </body>
  155. </html>