webvr_daydream.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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, origin = https://threejs.org, expires = 2017-03-13 -->
  8. <meta http-equiv="origin-trial" data-feature="WebVR" data-expires="2017-03-13" content="Any7HFKv+fkg+mBnJBBFIYyFbQgDyum3bNsVUXBInRDadbhafI6F0hXq/cStNqdi29167TYlkDuH/JE5ZZSI6gcAAABKeyJvcmlnaW4iOiJodHRwczovL3RocmVlanMub3JnOjQ0MyIsImZlYXR1cmUiOiJXZWJWUiIsImV4cGlyeSI6MTQ4OTQxNzU2OX0=">
  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: #f00;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <script src="../build/three.js"></script>
  24. <script src="js/controls/VRControls.js"></script>
  25. <script src="js/effects/VREffect.js"></script>
  26. <script src="js/vr/DaydreamController.js"></script>
  27. <script src="js/vr/WebVR.js"></script>
  28. <script>
  29. if ( WEBVR.isAvailable() === false ) {
  30. document.body.appendChild( WEBVR.getMessage() );
  31. }
  32. //
  33. var clock = new THREE.Clock();
  34. var container;
  35. var camera, scene, ray, raycaster, renderer;
  36. var effect, controls, gamepad;
  37. var room;
  38. var INTERSECTED;
  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">three.js</a> webvr - daydream';
  50. container.appendChild( info );
  51. scene = new THREE.Scene();
  52. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 10 );
  53. scene.add( camera );
  54. room = new THREE.Mesh(
  55. new THREE.BoxGeometry( 6, 6, 6, 8, 8, 8 ),
  56. new THREE.MeshBasicMaterial( { color: 0x404040, wireframe: true } )
  57. );
  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.BoxGeometry( 0.15, 0.15, 0.15 );
  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.rotation.x = Math.random() * 2 * Math.PI;
  70. object.rotation.y = Math.random() * 2 * Math.PI;
  71. object.rotation.z = Math.random() * 2 * Math.PI;
  72. object.scale.x = Math.random() + 0.5;
  73. object.scale.y = Math.random() + 0.5;
  74. object.scale.z = Math.random() + 0.5;
  75. object.userData.velocity = new THREE.Vector3();
  76. object.userData.velocity.x = Math.random() * 0.01 - 0.005;
  77. object.userData.velocity.y = Math.random() * 0.01 - 0.005;
  78. object.userData.velocity.z = Math.random() * 0.01 - 0.005;
  79. room.add( object );
  80. }
  81. //
  82. raycaster = new THREE.Raycaster();
  83. //
  84. renderer = new THREE.WebGLRenderer( { antialias: true } );
  85. renderer.setClearColor( 0x505050 );
  86. renderer.setPixelRatio( window.devicePixelRatio );
  87. renderer.setSize( window.innerWidth, window.innerHeight );
  88. renderer.sortObjects = false;
  89. container.appendChild( renderer.domElement );
  90. //
  91. controls = new THREE.VRControls( camera );
  92. effect = new THREE.VREffect( renderer );
  93. if ( navigator.getVRDisplays ) {
  94. navigator.getVRDisplays()
  95. .then( function ( displays ) {
  96. effect.setVRDisplay( displays[ 0 ] );
  97. controls.setVRDisplay( displays[ 0 ] );
  98. } )
  99. .catch( function () {
  100. // no displays
  101. } );
  102. document.body.appendChild( WEBVR.getButton( effect ) );
  103. }
  104. //
  105. gamepad = new THREE.DaydreamController();
  106. gamepad.position.set( 0.25, - 0.5, 0 );
  107. scene.add( gamepad );
  108. //
  109. var gamepadHelper = new THREE.Line( new THREE.BufferGeometry(), new THREE.LineBasicMaterial( { linewidth: 4 } ) );
  110. gamepadHelper.geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 0, 0, - 10 ], 3 ) );
  111. gamepad.add( gamepadHelper );
  112. //
  113. window.addEventListener( 'resize', onWindowResize, false );
  114. }
  115. function onWindowResize() {
  116. camera.aspect = window.innerWidth / window.innerHeight;
  117. camera.updateProjectionMatrix();
  118. effect.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. //
  121. function animate() {
  122. effect.requestAnimationFrame( animate );
  123. render();
  124. gamepad.update();
  125. }
  126. function render() {
  127. var delta = clock.getDelta() * 60;
  128. // find intersections
  129. raycaster.ray.origin.copy( gamepad.position );
  130. raycaster.ray.direction.set( 0, 0, - 1 ).applyQuaternion( gamepad.quaternion );
  131. var intersects = raycaster.intersectObjects( room.children );
  132. if ( intersects.length > 0 ) {
  133. if ( INTERSECTED != intersects[ 0 ].object ) {
  134. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  135. INTERSECTED = intersects[ 0 ].object;
  136. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  137. INTERSECTED.material.emissive.setHex( 0xff0000 );
  138. }
  139. } else {
  140. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  141. INTERSECTED = undefined;
  142. }
  143. // keep cubes inside room
  144. for ( var i = 0; i < room.children.length; i ++ ) {
  145. var cube = room.children[ i ];
  146. cube.userData.velocity.multiplyScalar( 1 - ( 0.001 * delta ) );
  147. cube.position.add( cube.userData.velocity );
  148. if ( cube.position.x < - 3 || cube.position.x > 3 ) {
  149. cube.position.x = THREE.Math.clamp( cube.position.x, - 3, 3 );
  150. cube.userData.velocity.x = - cube.userData.velocity.x;
  151. }
  152. if ( cube.position.y < - 3 || cube.position.y > 3 ) {
  153. cube.position.y = THREE.Math.clamp( cube.position.y, - 3, 3 );
  154. cube.userData.velocity.y = - cube.userData.velocity.y;
  155. }
  156. if ( cube.position.z < - 3 || cube.position.z > 3 ) {
  157. cube.position.z = THREE.Math.clamp( cube.position.z, - 3, 3 );
  158. cube.userData.velocity.z = - cube.userData.velocity.z;
  159. }
  160. cube.rotation.x += cube.userData.velocity.x * 2 * delta;
  161. cube.rotation.y += cube.userData.velocity.y * 2 * delta;
  162. cube.rotation.z += cube.userData.velocity.z * 2 * delta;
  163. }
  164. controls.update();
  165. effect.render( scene, camera );
  166. }
  167. </script>
  168. </body>
  169. </html>