webvr_cubes.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webvr - cubes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #101010;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. a {
  16. color: #f00;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <script src="../build/three.js"></script>
  22. <script src="js/WebVR.js"></script>
  23. <script src="js/effects/VREffect.js"></script>
  24. <script src="js/controls/VRControls.js"></script>
  25. <script>
  26. if ( WEBVR.isLatestAvailable() === false ) {
  27. document.body.appendChild( WEBVR.getMessage() );
  28. }
  29. //
  30. var container;
  31. var camera, scene, raycaster, renderer;
  32. var effect, controls;
  33. var cubes = [];
  34. var INTERSECTED;
  35. var radius = 100, theta = 0;
  36. var crosshair;
  37. init();
  38. animate();
  39. function init() {
  40. container = document.createElement( 'div' );
  41. document.body.appendChild( container );
  42. var info = document.createElement( 'div' );
  43. info.style.position = 'absolute';
  44. info.style.top = '10px';
  45. info.style.width = '100%';
  46. info.style.textAlign = 'center';
  47. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive cubes';
  48. container.appendChild( info );
  49. scene = new THREE.Scene();
  50. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 1000 );
  51. scene.add( camera );
  52. crosshair = new THREE.Mesh(
  53. new THREE.RingGeometry( 0.02, 0.04, 32 ),
  54. new THREE.MeshBasicMaterial( {
  55. color: 0xffffff,
  56. opacity: 0.5,
  57. transparent: true
  58. } )
  59. );
  60. crosshair.position.z = - 2;
  61. camera.add( crosshair );
  62. var mesh = new THREE.Mesh(
  63. new THREE.BoxGeometry( 200, 200, 200, 10, 10, 10 ),
  64. new THREE.MeshBasicMaterial( { color: 0x202020, wireframe: true } )
  65. );
  66. scene.add( mesh );
  67. scene.add( new THREE.HemisphereLight( 0x404020, 0x202040, 0.5 ) );
  68. var light = new THREE.DirectionalLight( 0xffffff );
  69. light.position.set( 1, 1, 1 ).normalize();
  70. scene.add( light );
  71. var geometry = new THREE.BoxGeometry( 5, 5, 5 );
  72. for ( var i = 0; i < 200; i ++ ) {
  73. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  74. object.position.x = Math.random() * 200 - 100;
  75. object.position.y = Math.random() * 200 - 100;
  76. object.position.z = Math.random() * 200 - 100;
  77. object.rotation.x = Math.random() * 2 * Math.PI;
  78. object.rotation.y = Math.random() * 2 * Math.PI;
  79. object.rotation.z = Math.random() * 2 * Math.PI;
  80. object.scale.x = Math.random() + 0.5;
  81. object.scale.y = Math.random() + 0.5;
  82. object.scale.z = Math.random() + 0.5;
  83. object.userData.velocity = new THREE.Vector3();
  84. object.userData.velocity.x = Math.random() - 0.5;
  85. object.userData.velocity.y = Math.random() - 0.5;
  86. object.userData.velocity.z = Math.random() - 0.5;
  87. scene.add( object );
  88. cubes.push( object );
  89. }
  90. raycaster = new THREE.Raycaster();
  91. renderer = new THREE.WebGLRenderer( { antialias: true } );
  92. renderer.setClearColor( 0x101010 );
  93. renderer.setPixelRatio( window.devicePixelRatio );
  94. renderer.setSize( window.innerWidth, window.innerHeight );
  95. renderer.sortObjects = false;
  96. container.appendChild( renderer.domElement );
  97. controls = new THREE.VRControls( camera );
  98. effect = new THREE.VREffect( renderer );
  99. if ( WEBVR.isAvailable() === true ) {
  100. document.body.appendChild( WEBVR.getButton( effect ) );
  101. }
  102. renderer.domElement.addEventListener( 'click', function ( event ) {
  103. if ( INTERSECTED ) {
  104. var object = INTERSECTED;
  105. object.userData.velocity.subVectors( object.position, camera.position ).normalize();
  106. }
  107. } );
  108. //
  109. window.addEventListener( 'resize', onWindowResize, false );
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. effect.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. //
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. render();
  120. }
  121. function render() {
  122. // find intersections
  123. raycaster.setFromCamera( { x: 0, y: 0 }, camera );
  124. var intersects = raycaster.intersectObjects( cubes );
  125. if ( intersects.length > 0 ) {
  126. if ( INTERSECTED != intersects[ 0 ].object ) {
  127. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  128. INTERSECTED = intersects[ 0 ].object;
  129. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  130. INTERSECTED.material.emissive.setHex( 0xff0000 );
  131. }
  132. } else {
  133. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  134. INTERSECTED = undefined;
  135. }
  136. controls.update();
  137. for ( var i = 0; i < cubes.length; i ++ ) {
  138. var cube = cubes[ i ];
  139. cube.userData.velocity.multiplyScalar( 0.999 );
  140. cube.position.add( cube.userData.velocity );
  141. if ( cube.position.x < - 100 || cube.position.x > 100 ) {
  142. cube.position.x = THREE.Math.clamp( cube.position.x, - 100, 100 );
  143. cube.userData.velocity.x = - cube.userData.velocity.x;
  144. }
  145. if ( cube.position.y < - 100 || cube.position.y > 100 ) {
  146. cube.position.y = THREE.Math.clamp( cube.position.y, - 100, 100 );
  147. cube.userData.velocity.y = - cube.userData.velocity.y;
  148. }
  149. if ( cube.position.z < - 100 || cube.position.z > 100 ) {
  150. cube.position.z = THREE.Math.clamp( cube.position.z, - 100, 100 );
  151. cube.userData.velocity.z = - cube.userData.velocity.z;
  152. }
  153. cube.rotation.x += 0.01;
  154. }
  155. effect.render( scene, camera );
  156. }
  157. </script>
  158. </body>
  159. </html>