webvr_daydream.html 7.0 KB

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