webvr_cubes.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.min.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.4 - 0.2;
  85. object.userData.velocity.y = Math.random() * 0.4 - 0.2;
  86. object.userData.velocity.z = Math.random() * 0.4 - 0.2;
  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. //
  103. window.addEventListener( 'resize', onWindowResize, false );
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. effect.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. //
  111. function animate() {
  112. requestAnimationFrame( animate );
  113. render();
  114. }
  115. function render() {
  116. // find intersections
  117. raycaster.setFromCamera( { x: 0, y: 0 }, camera );
  118. var intersects = raycaster.intersectObjects( cubes );
  119. if ( intersects.length > 0 ) {
  120. if ( INTERSECTED != intersects[ 0 ].object ) {
  121. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  122. INTERSECTED = intersects[ 0 ].object;
  123. INTERSECTED.currentHex = INTERSECTED.material.emissive.getHex();
  124. INTERSECTED.material.emissive.setHex( 0xff0000 );
  125. }
  126. } else {
  127. if ( INTERSECTED ) INTERSECTED.material.emissive.setHex( INTERSECTED.currentHex );
  128. INTERSECTED = null;
  129. }
  130. controls.update();
  131. for ( var i = 0; i < cubes.length; i ++ ) {
  132. var cube = cubes[ i ];
  133. cube.position.add( cube.userData.velocity );
  134. if ( cube.position.x < - 100 || cube.position.x > 100 ) {
  135. cube.position.x = THREE.Math.clamp( cube.position.x, - 100, 100 );
  136. cube.userData.velocity.x = - cube.userData.velocity.x;
  137. }
  138. if ( cube.position.y < - 100 || cube.position.y > 100 ) {
  139. cube.position.y = THREE.Math.clamp( cube.position.y, - 100, 100 );
  140. cube.userData.velocity.y = - cube.userData.velocity.y;
  141. }
  142. if ( cube.position.z < - 100 || cube.position.z > 100 ) {
  143. cube.position.z = THREE.Math.clamp( cube.position.z, - 100, 100 );
  144. cube.userData.velocity.z = - cube.userData.velocity.z;
  145. }
  146. cube.rotation.x += 0.01;
  147. }
  148. effect.render( scene, camera );
  149. }
  150. </script>
  151. </body>
  152. </html>